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"
33 #ifdef SIGC_CXX_NAMESPACES
42 /* the selection possible is needed, that only motion events are
43 * used, where the bottom press event was on the drawing area too */
44 bool selection_possible = false;
46 extern BufferList bufferlist;
47 extern char ascii_type;
49 extern "C" void TimerCB(FL_OBJECT *, long);
50 extern void sigchldhandler(pid_t pid, int * status);
51 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
57 XSync(fl_get_display(), 0);
62 void SetXtermCursor(Window win)
65 static bool cursor_undefined = true;
66 if (cursor_undefined){
67 cursor = XCreateFontCursor(fl_display, XC_xterm);
69 cursor_undefined = false;
71 XDefineCursor(fl_display, win, cursor);
76 BufferView::Pimpl::Pimpl(BufferView * b, LyXView * o,
77 int xpos, int ypos, int width, int height)
78 : bv_(b), owner_(o), cursor_timeout(400)
81 workarea_ = new WorkArea(bv_, xpos, ypos, width, height);
84 cursor_timeout.timeout.connect(slot(this,
85 &BufferView::Pimpl::cursorToggle));
86 current_scrollbar_value = 0;
87 cursor_timeout.start();
88 workarea_->setFocus();
89 using_xterm_cursor = false;
93 Painter & BufferView::Pimpl::painter()
95 return workarea_->getPainter();
99 void BufferView::Pimpl::buffer(Buffer * b)
101 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
105 buffer_->delUser(bv_);
107 // Put the old text into the TextCache, but
108 // only if the buffer is still loaded.
109 // Also set the owner of the test to 0
110 // bv_->text->owner(0);
111 textcache.add(buffer_, workarea_->workWidth(), bv_->text);
112 if (lyxerr.debugging())
113 textcache.show(lyxerr, "BufferView::buffer");
118 // Set current buffer
121 if (bufferlist.getState() == BufferList::CLOSING) return;
124 // screen is always deleted when the buffer is changed.
128 // If we are closing the buffer, use the first buffer as current
130 buffer_ = bufferlist.first();
134 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
135 buffer_->addUser(bv_);
136 // If we don't have a text object for this, we make one
137 if (bv_->text == 0) {
138 resizeCurrentBuffer();
143 bv_->text->first = screen_->TopCursorVisible(bv_->text);
145 owner_->updateMenubar();
147 owner_->getMenus()->showMenus();
149 owner_->updateToolbar();
151 owner_->getDialogs()->updateBufferDependent();
154 lyxerr[Debug::INFO] << " No Buffer!" << endl;
156 owner_->updateMenubar();
158 owner_->getMenus()->hideMenus();
160 owner_->updateToolbar();
164 // Also remove all remaining text's from the testcache.
165 // (there should not be any!) (if there is any it is a
167 if (lyxerr.debugging())
168 textcache.show(lyxerr, "buffer delete all");
171 // should update layoutchoice even if we don't have a buffer.
172 owner_->updateLayoutChoice();
173 owner_->getMiniBuffer()->Init();
174 owner_->updateWindowTitle();
178 void BufferView::Pimpl::resize(int xpos, int ypos, int width, int height)
180 workarea_->resize(xpos, ypos, width, height);
186 void BufferView::Pimpl::resize()
188 // This will resize the buffer. (Asger)
190 resizeCurrentBuffer();
194 void BufferView::Pimpl::redraw()
196 lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
201 bool BufferView::Pimpl::fitCursor()
203 Assert(screen_); // it is a programming error to call fitCursor
204 // without a valid screen.
205 bool ret = screen_->FitCursor(bv_->text);
206 if (ret) updateScrollbar();
211 void BufferView::Pimpl::redoCurrentBuffer()
213 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
214 if (buffer_ && bv_->text) {
216 owner_->updateLayoutChoice();
221 int BufferView::Pimpl::resizeCurrentBuffer()
223 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
225 LyXParagraph * par = 0;
226 LyXParagraph * selstartpar = 0;
227 LyXParagraph * selendpar = 0;
236 owner_->getMiniBuffer()->Set(_("Formatting document..."));
239 par = bv_->text->cursor.par();
240 pos = bv_->text->cursor.pos();
241 selstartpar = bv_->text->sel_start_cursor.par();
242 selstartpos = bv_->text->sel_start_cursor.pos();
243 selendpar = bv_->text->sel_end_cursor.par();
244 selendpos = bv_->text->sel_end_cursor.pos();
245 selection = bv_->text->selection;
246 mark_set = bv_->text->mark_set;
248 bv_->text = new LyXText(bv_);
250 // See if we have a text in TextCache that fits
251 // the new buffer_ with the correct width.
252 bv_->text = textcache.findFit(buffer_, workarea_->workWidth());
254 if (lyxerr.debugging()) {
255 lyxerr << "Found a LyXText that fits:\n";
256 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea_->workWidth(), bv_->text)));
258 // Set the owner of the newly found text
259 // bv_->text->owner(bv_);
260 if (lyxerr.debugging())
261 textcache.show(lyxerr, "resizeCurrentBuffer");
263 bv_->text = new LyXText(bv_);
269 bv_->text->selection = true;
270 /* at this point just to avoid the Delete-Empty-Paragraph
271 * Mechanism when setting the cursor */
272 bv_->text->mark_set = mark_set;
274 bv_->text->SetCursor(bv_, selstartpar, selstartpos);
275 bv_->text->sel_cursor = bv_->text->cursor;
276 bv_->text->SetCursor(bv_, selendpar, selendpos);
277 bv_->text->SetSelection();
278 bv_->text->SetCursor(bv_, par, pos);
280 bv_->text->SetCursor(bv_, par, pos);
281 bv_->text->sel_cursor = bv_->text->cursor;
282 bv_->text->selection = false;
285 bv_->text->first = screen_->TopCursorVisible(bv_->text);
286 buffer_->resizeInsets(bv_);
287 // this will scroll the screen such that the cursor becomes visible
290 owner_->getMiniBuffer()->Init();
294 // Now if the title form still exist kill it
301 void BufferView::Pimpl::gotoError()
306 screen_->HideCursor();
308 update(BufferView::SELECT|BufferView::FITCUR);
311 if (!bv_->text->GotoNextError(bv_)) {
312 if (bv_->text->cursor.pos()
313 || bv_->text->cursor.par() != bv_->text->FirstParagraph()) {
314 tmp = bv_->text->cursor;
315 bv_->text->cursor.par(bv_->text->FirstParagraph());
316 bv_->text->cursor.pos(0);
317 if (!bv_->text->GotoNextError(bv_)) {
318 bv_->text->cursor = tmp;
319 owner_->getMiniBuffer()
320 ->Set(_("No more errors"));
324 owner_->getMiniBuffer()->Set(_("No more errors"));
328 update(BufferView::SELECT|BufferView::FITCUR);
329 bv_->text->sel_cursor = bv_->text->cursor;
333 void BufferView::Pimpl::updateScreen()
335 // Regenerate the screen.
337 screen_ = new LyXScreen(*workarea_);
341 void BufferView::Pimpl::updateScrollbar()
343 /* If the text is smaller than the working area, the scrollbar
344 * maximum must be the working area height. No scrolling will
348 workarea_->setScrollbar(0, 1.0);
352 static unsigned long max2 = 0;
353 static unsigned long height2 = 0;
355 unsigned long cbth = 0;
359 cbth = bv_->text->height;
360 cbsf = bv_->text->first;
363 // check if anything has changed.
365 height2 == workarea_->height() &&
366 current_scrollbar_value == cbsf)
369 height2 = workarea_->height();
370 current_scrollbar_value = cbsf;
372 if (cbth <= height2) { // text is smaller than screen
373 workarea_->setScrollbar(0, 1.0); // right?
377 long maximum_height = workarea_->height() * 3 / 4 + cbth;
381 double hfloat = workarea_->height();
382 double maxfloat = maximum_height;
384 float slider_size = 0.0;
385 int slider_value = value;
387 workarea_->setScrollbarBounds(0, bv_->text->height - workarea_->height());
388 double lineh = bv_->text->DefaultHeight();
389 workarea_->setScrollbarIncrements(lineh);
390 if (maxfloat > 0.0) {
391 if ((hfloat / maxfloat) * float(height2) < 3)
392 slider_size = 3.0/float(height2);
394 slider_size = hfloat / maxfloat;
396 slider_size = hfloat;
398 workarea_->setScrollbar(slider_value, slider_size / workarea_->height());
402 // Callback for scrollbar slider
403 void BufferView::Pimpl::scrollCB(double value)
405 extern bool cursor_follows_scrollbar;
407 if (buffer_ == 0) return;
409 current_scrollbar_value = long(value);
410 if (current_scrollbar_value < 0)
411 current_scrollbar_value = 0;
416 screen_->Draw(bv_->text, current_scrollbar_value);
418 if (cursor_follows_scrollbar) {
419 LyXText * vbt = bv_->text;
420 unsigned int height = vbt->DefaultHeight();
422 if (vbt->cursor.y() < bv_->text->first + height) {
423 vbt->SetCursorFromCoordinates(bv_, 0,
426 } else if (vbt->cursor.y() >
427 bv_->text->first + workarea_->height() - height) {
428 vbt->SetCursorFromCoordinates(bv_, 0,
430 workarea_->height() -
438 int BufferView::Pimpl::scrollUp(long time)
440 if (buffer_ == 0) return 0;
441 if (!screen_) return 0;
443 double value = workarea_->getScrollbarValue();
445 if (value == 0) return 0;
447 float add_value = (bv_->text->DefaultHeight()
448 + float(time) * float(time) * 0.125);
450 if (add_value > workarea_->height())
451 add_value = float(workarea_->height() -
452 bv_->text->DefaultHeight());
459 workarea_->setScrollbarValue(value);
461 bv_->scrollCB(value);
466 int BufferView::Pimpl::scrollDown(long time)
468 if (buffer_ == 0) return 0;
469 if (!screen_) return 0;
471 double value= workarea_->getScrollbarValue();
472 pair<float, float> p = workarea_->getScrollbarBounds();
473 double max = p.second;
475 if (value == max) return 0;
477 float add_value = (bv_->text->DefaultHeight()
478 + float(time) * float(time) * 0.125);
480 if (add_value > workarea_->height())
481 add_value = float(workarea_->height() -
482 bv_->text->DefaultHeight());
489 workarea_->setScrollbarValue(value);
491 bv_->scrollCB(value);
496 void BufferView::Pimpl::workAreaMotionNotify(int x, int y, unsigned int state)
498 // Only use motion with button 1
499 if (!(state & Button1MotionMask))
502 if (buffer_ == 0 || !screen_) return;
504 // Check for inset locking
505 if (bv_->the_locking_inset) {
506 LyXCursor cursor = bv_->text->cursor;
507 bv_->the_locking_inset->
508 InsetMotionNotify(bv_,
510 y - cursor.y() + bv_->text->first,
515 /* The selection possible is needed, that only motion events are
516 * used, where the bottom press event was on the drawing area too */
517 if (selection_possible) {
518 screen_->HideCursor();
520 bv_->text->SetCursorFromCoordinates(bv_, x, y + bv_->text->first);
522 if (!bv_->text->selection)
523 update(BufferView::UPDATE); // Maybe an empty line was deleted
525 bv_->text->SetSelection();
526 screen_->ToggleToggle(bv_->text);
528 screen_->ShowCursor(bv_->text);
534 // Single-click on work area
535 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
541 if (buffer_ == 0 || !screen_) return;
543 Inset * inset_hit = checkInsetHit(bv_->text, xpos, ypos, button);
545 // ok ok, this is a hack.
546 if (button == 4 || button == 5) {
549 scrollUp(lyxrc.wheel_jump); // default 100, set in lyxrc
552 scrollDown(lyxrc.wheel_jump);
557 if (bv_->the_locking_inset) {
558 // We are in inset locking mode
560 /* Check whether the inset was hit. If not reset mode,
561 otherwise give the event to the inset */
562 if (inset_hit == bv_->the_locking_inset) {
563 bv_->the_locking_inset->
564 InsetButtonPress(bv_,
569 bv_->unlockInset(bv_->the_locking_inset);
574 selection_possible = true;
575 screen_->HideCursor();
578 // Right button mouse click on a table
580 (bv_->text->cursor.par()->table ||
581 bv_->text->MouseHitInTable(bv_, xpos, ypos + bv_->text->first))) {
582 // Set the cursor to the press-position
583 bv_->text->SetCursorFromCoordinates(bv_, xpos, ypos + bv_->text->first);
586 // Only show the table popup if the hit is in
588 if (!bv_->text->HitInTable(bv_,
589 bv_->text->cursor.row(), xpos))
592 // Hit above or below the table?
594 if (!bv_->text->selection) {
595 screen_->ToggleSelection(bv_->text);
596 bv_->text->ClearSelection();
597 bv_->text->FullRebreak(bv_);
598 screen_->Update(bv_->text);
601 // Popup table popup when on a table.
602 // This is obviously temporary, since we
603 // should be able to popup various
604 // context-sensitive-menus with the
605 // the right mouse. So this should be done more
606 // general in the future. Matthias.
607 selection_possible = false;
609 ->Dispatch(LFUN_LAYOUT_TABLE,
616 int screen_first = bv_->text->first;
618 // Middle button press pastes if we have a selection
619 bool paste_internally = false;
621 && bv_->text->selection) {
622 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
623 paste_internally = true;
626 // Clear the selection
627 screen_->ToggleSelection(bv_->text);
628 bv_->text->ClearSelection();
629 bv_->text->FullRebreak(bv_);
630 screen_->Update(bv_->text);
633 // Single left click in math inset?
634 if ((inset_hit != 0) &&
635 (inset_hit->Editable()==Inset::HIGHLY_EDITABLE)) {
636 // Highly editable inset, like math
637 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
638 selection_possible = false;
639 owner_->updateLayoutChoice();
640 owner_->getMiniBuffer()->Set(inset->EditMessage());
641 inset->InsetButtonPress(bv_, xpos, ypos, button);
642 inset->Edit(bv_, xpos, ypos, button);
646 // Right click on a footnote flag opens float menu
648 selection_possible = false;
652 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
653 bv_->text->SetCursorFromCoordinates(bv_, xpos, ypos + screen_first);
654 bv_->text->FinishUndo();
655 bv_->text->sel_cursor = bv_->text->cursor;
656 bv_->text->cursor.x_fix(bv_->text->cursor.x());
658 owner_->updateLayoutChoice();
660 selection_possible = false;
663 // Insert primary selection with middle mouse
664 // if there is a local selection in the current buffer,
667 if (paste_internally)
668 owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
670 owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
672 selection_possible = false;
678 void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, unsigned int button)
681 if (buffer_ && !bv_->the_locking_inset) {
682 if (screen_ && button == 1) {
683 screen_->HideCursor();
684 screen_->ToggleSelection(bv_->text);
685 bv_->text->SelectWord(bv_);
686 screen_->ToggleSelection(bv_->text, false);
687 /* This will fit the cursor on the screen
689 update(BufferView::SELECT|BufferView::FITCUR);
695 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, unsigned int button)
698 if (buffer_ && screen_ && button == 1) {
699 screen_->HideCursor();
700 screen_->ToggleSelection(bv_->text);
701 bv_->text->CursorHome(bv_);
702 bv_->text->sel_cursor = bv_->text->cursor;
703 bv_->text->CursorEnd(bv_);
704 bv_->text->SetSelection();
705 screen_->ToggleSelection(bv_->text, false);
706 /* This will fit the cursor on the screen
708 update(BufferView::SELECT|BufferView::FITCUR);
713 void BufferView::Pimpl::enterView()
715 if (active() && available()) {
716 SetXtermCursor(workarea_->getWin());
717 using_xterm_cursor = true;
722 void BufferView::Pimpl::leaveView()
724 if (using_xterm_cursor) {
725 XUndefineCursor(fl_display, workarea_->getWin());
726 using_xterm_cursor = false;
731 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
734 if (buffer_ == 0 || screen_ == 0) return;
736 // If we hit an inset, we have the inset coordinates in these
737 // and inset_hit points to the inset. If we do not hit an
738 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
739 Inset * inset_hit = checkInsetHit(bv_->text, x, y, button);
741 if (bv_->the_locking_inset) {
742 // We are in inset locking mode.
744 /* LyX does a kind of work-area grabbing for insets.
745 Only a ButtonPress Event outside the inset will
746 force a InsetUnlock. */
747 bv_->the_locking_inset->
748 InsetButtonRelease(bv_, x, y, button);
752 selection_possible = false;
754 if (bv_->text->cursor.par()->table) {
755 int cell = bv_->text->
756 NumberOfCell(bv_->text->cursor.par(),
757 bv_->text->cursor.pos());
758 if (bv_->text->cursor.par()->table->IsContRow(cell) &&
759 bv_->text->cursor.par()->table->
760 CellHasContRow(bv_->text->cursor.par()->table->
761 GetCellAbove(cell))<0) {
762 bv_->text->CursorUp(bv_);
767 if (button >= 2) return;
772 // Did we hit an editable inset?
773 if (inset_hit != 0) {
774 // Inset like error, notes and figures
775 selection_possible = false;
777 // CHECK fix this proper in 0.13
779 // Following a ref shouldn't issue
780 // a push on the undo-stack
781 // anylonger, now that we have
782 // keybindings for following
783 // references and returning from
784 // references. IMHO though, it
785 // should be the inset's own business
786 // to push or not push on the undo
787 // stack. They don't *have* to
788 // alter the document...
790 // ...or maybe the SetCursorParUndo()
791 // below isn't necessary at all anylonger?
792 if (inset_hit->LyxCode() == Inset::REF_CODE) {
793 bv_->text->SetCursorParUndo(bv_->buffer());
796 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
797 if (inset_hit->Editable()==Inset::HIGHLY_EDITABLE) {
798 // Highly editable inset, like math
799 UpdatableInset *inset = (UpdatableInset *)inset_hit;
800 inset->InsetButtonRelease(bv_, x, y, button);
802 inset_hit->InsetButtonRelease(bv_, x, y, button);
803 inset_hit->Edit(bv_, x, y, button);
808 // check whether we want to open a float
812 if (bv_->text->cursor.pos() <
813 bv_->text->cursor.par()->Last()) {
814 c = bv_->text->cursor.par()->
815 GetChar(bv_->text->cursor.pos());
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){
828 if (bv_->text->cursor.pos() - 1 >= 0) {
829 c = bv_->text->cursor.par()->
830 GetChar(bv_->text->cursor.pos() - 1);
832 if (c == LyXParagraph::META_FOOTNOTE
833 || c == LyXParagraph::META_MARGIN
834 || c == LyXParagraph::META_FIG
835 || c == LyXParagraph::META_TAB
836 || c == LyXParagraph::META_WIDE_FIG
837 || c == LyXParagraph::META_WIDE_TAB
838 || c == LyXParagraph::META_ALGORITHM){
839 // We are one step too far to the right
840 bv_->text->CursorLeft(bv_);
849 selection_possible = false;
855 // Do we want to close a float? (click on the float-label)
856 if (bv_->text->cursor.row()->par()->footnoteflag ==
857 LyXParagraph::OPEN_FOOTNOTE
858 && bv_->text->cursor.row()->previous() &&
859 bv_->text->cursor.row()->previous()->par()->
860 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
861 LyXFont font(LyXFont::ALL_SANE);
862 font.setSize(LyXFont::SIZE_FOOTNOTE);
864 int box_x = 20; // LYX_PAPER_MARGIN;
865 box_x += lyxfont::width(" wide-tab ", font);
867 unsigned int screen_first = bv_->text->first;
870 && y + screen_first > bv_->text->cursor.y() -
871 bv_->text->cursor.row()->baseline()
872 && y + screen_first < bv_->text->cursor.y() -
873 bv_->text->cursor.row()->baseline()
874 + lyxfont::maxAscent(font) * 1.2 + lyxfont::maxDescent(font) * 1.2) {
876 selection_possible = false;
882 // Maybe we want to edit a bibitem ale970302
883 if (bv_->text->cursor.par()->bibkey && x < 20 +
884 bibitemMaxWidth(bv_, textclasslist.
886 params.textclass).defaultfont())) {
887 bv_->text->cursor.par()->bibkey->Edit(bv_, 0, 0, 0);
895 * Returns an inset if inset was hit. 0 otherwise.
896 * If hit, the coordinates are changed relative to the inset.
897 * Otherwise coordinates are not changed, and false is returned.
899 Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y,
900 unsigned int /* button */)
905 unsigned int y_tmp = y + text->first;
908 text->SetCursorFromCoordinates(bv_, cursor, x, y_tmp);
909 #if 0 // Are you planning to use this Jürgen? (Lgb)
910 bool move_cursor = ((cursor.par != text->cursor.par) ||
911 (cursor.pos != text->cursor.pos()));
913 if (cursor.pos() < cursor.par()->Last()
914 && cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
915 && cursor.par()->GetInset(cursor.pos())
916 && cursor.par()->GetInset(cursor.pos())->Editable()) {
918 // Check whether the inset really was hit
919 Inset * tmpinset = cursor.par()->GetInset(cursor.pos());
920 LyXFont font = text->GetFont(bv_->buffer(),
921 cursor.par(), cursor.pos());
922 bool is_rtl = font.isVisibleRightToLeft();
926 start_x = cursor.x() - tmpinset->width(bv_, font);
929 start_x = cursor.x();
930 end_x = cursor.x() + tmpinset->width(bv_, font);
933 if (x > start_x && x < end_x
934 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
935 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
937 if (move_cursor && (tmpinset != bv_->the_locking_inset))
939 text->SetCursor(bv_, cursor.par(),cursor.pos(),true);
941 // The origin of an inset is on the baseline
942 y = y_tmp - (text->cursor.y());
947 if ((cursor.pos() - 1 >= 0) &&
948 (cursor.par()->GetChar(cursor.pos()-1) == LyXParagraph::META_INSET) &&
949 (cursor.par()->GetInset(cursor.pos() - 1)) &&
950 (cursor.par()->GetInset(cursor.pos() - 1)->Editable())) {
951 Inset * tmpinset = cursor.par()->GetInset(cursor.pos()-1);
952 LyXFont font = text->GetFont(bv_->buffer(), cursor.par(),
954 bool is_rtl = font.isVisibleRightToLeft();
958 start_x = cursor.x() - tmpinset->width(bv_, font);
961 start_x = cursor.x();
962 end_x = cursor.x() + tmpinset->width(bv_, font);
964 if (x > start_x && x < end_x
965 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
966 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
968 if (move_cursor && (tmpinset != bv_->the_locking_inset))
970 text->SetCursor(bv_, cursor.par(),cursor.pos()-1,true);
972 // The origin of an inset is on the baseline
973 y = y_tmp - (text->cursor.y());
981 void BufferView::Pimpl::workAreaExpose()
983 // this is a hack to ensure that we only call this through
984 // BufferView::redraw().
989 static int work_area_width = 0;
990 static unsigned int work_area_height = 0;
992 bool widthChange = workarea_->workWidth() != work_area_width;
993 bool heightChange = workarea_->height() != work_area_height;
995 // update from work area
996 work_area_width = workarea_->workWidth();
997 work_area_height = workarea_->height();
1000 // All buffers need a resize
1001 bufferlist.resize();
1003 // Remove all texts from the textcache
1004 // This is not _really_ what we want to do. What
1005 // we really want to do is to delete in textcache
1006 // that does not have a BufferView with matching
1007 // width, but as long as we have only one BufferView
1008 // deleting all gives the same result.
1009 if (lyxerr.debugging())
1010 textcache.show(lyxerr, "Expose delete all");
1012 } else if (heightChange) {
1013 // Rebuild image of current screen
1015 // fitCursor() ensures we don't jump back
1016 // to the start of the document on vertical
1020 // The main window size has changed, repaint most stuff
1022 // ...including the minibuffer
1023 owner_->getMiniBuffer()->Init();
1025 } else if (screen_) screen_->Redraw(bv_->text);
1027 // Grey box when we don't have a buffer
1028 workarea_->greyOut();
1031 // always make sure that the scrollbar is sane.
1033 owner_->updateLayoutChoice();
1038 void BufferView::Pimpl::update()
1040 if (screen_) screen_->Update(bv_->text);
1043 // Values used when calling update:
1045 // -2 - update, move sel_cursor if selection, fitcursor
1046 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
1047 // 0 - update, move sel_cursor if selection, fitcursor
1048 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
1049 // 3 - update, move sel_cursor if selection
1052 // a simple redraw of the parts that need refresh
1054 // move sel_cursor if selection -
1055 // the text's sel_cursor is moved if there is selection is progress
1058 // fitCursor() is called and the scrollbar updated
1061 // the buffer is marked dirty.
1070 // UPDATE_ONLY = UPDATE;
1071 // UPDATE_SELECT = UPDATE | SELECT;
1072 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
1073 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1075 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
1076 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
1077 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1078 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1079 // update(3) -> update(1) -> update(1) -> update(SELECT)
1081 void BufferView::Pimpl::update(BufferView::UpdateCodes f)
1083 owner_->updateLayoutChoice();
1085 if (!bv_->text->selection && (f & SELECT)) {
1086 bv_->text->sel_cursor = bv_->text->cursor;
1089 bv_->text->FullRebreak(bv_);
1098 if (buffer_->isLyxClean()) {
1099 buffer_->markDirty();
1100 owner_->getMiniBuffer()->setTimer(4);
1102 buffer_->markDirty();
1108 // Callback for cursor timer
1109 void BufferView::Pimpl::cursorToggle()
1111 // Quite a nice place for asyncron Inset updating, isn't it?
1112 // Actually no! This is run even if no buffer exist... so (Lgb)
1114 goto set_timer_and_return;
1118 // On my quest to solve the gs render hangups I am now
1119 // disabling the SIGHUP completely, and will do a wait
1120 // now and then instead. If the guess that xforms somehow
1121 // destroys something is true, this is likely (hopefully)
1122 // to solve the problem...at least I hope so. Lgb
1124 // ...Ok this seems to work...at least it does not make things
1125 // worse so far. However I still see gs processes that hangs.
1126 // I would really like to know _why_ they are hanging. Anyway
1127 // the solution without the SIGCHLD handler seems to be easier
1130 // When attaching gdb to a a running gs that hangs it shows
1131 // that it is waiting for input(?) Is it possible for us to
1132 // provide that input somehow? Or figure what it is expecing
1135 // One solution is to, after some time, look if there are some
1136 // old gs processes still running and if there are: kill them
1139 // Another solution is to provide the user an option to rerender
1140 // a picture. This would, for the picture in question, check if
1141 // there is a gs running for it, if so kill it, and start a new
1142 // rendering process.
1144 // these comments posted to lyx@via
1147 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1148 if (pid == -1) // error find out what is wrong
1149 ; // ignore it for now.
1151 sigchldhandler(pid, &status);
1154 updatelist.update(bv_);
1157 goto set_timer_and_return;
1160 if (!bv_->the_locking_inset) {
1161 screen_->CursorToggle(bv_->text);
1163 bv_->the_locking_inset->
1164 ToggleInsetCursor(bv_);
1167 set_timer_and_return:
1168 cursor_timeout.restart();
1173 void BufferView::Pimpl::cursorPrevious()
1175 if (!bv_->text->cursor.row()->previous()) return;
1177 long y = bv_->text->first;
1178 Row * cursorrow = bv_->text->cursor.row();
1179 bv_->text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y);
1180 bv_->text->FinishUndo();
1181 // This is to allow jumping over large insets
1182 if ((cursorrow == bv_->text->cursor.row()))
1183 bv_->text->CursorUp(bv_);
1185 if (bv_->text->cursor.row()->height() < workarea_->height())
1186 screen_->Draw(bv_->text,
1187 bv_->text->cursor.y()
1188 - bv_->text->cursor.row()->baseline()
1189 + bv_->text->cursor.row()->height()
1190 - workarea_->height() + 1 );
1195 void BufferView::Pimpl::cursorNext()
1197 if (!bv_->text->cursor.row()->next()) return;
1199 long y = bv_->text->first;
1200 bv_->text->GetRowNearY(y);
1201 Row * cursorrow = bv_->text->cursor.row();
1202 bv_->text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y
1203 + workarea_->height());
1204 bv_->text->FinishUndo();
1205 // This is to allow jumping over large insets
1206 if ((cursorrow == bv_->text->cursor.row()))
1207 bv_->text->CursorDown(bv_);
1209 if (bv_->text->cursor.row()->height() < workarea_->height())
1210 screen_->Draw(bv_->text, bv_->text->cursor.y()
1211 - bv_->text->cursor.row()->baseline());
1216 bool BufferView::Pimpl::available() const
1218 if (buffer_ && bv_->text) return true;
1223 void BufferView::Pimpl::beforeChange()
1226 bv_->text->ClearSelection();
1229 //owner_->update_timeout.stop();
1233 void BufferView::Pimpl::savePosition()
1235 backstack.push(buffer_->fileName(),
1236 bv_->text->cursor.x(),
1237 bv_->text->cursor.y());
1241 void BufferView::Pimpl::restorePosition()
1243 if (backstack.empty()) return;
1246 string fname = backstack.pop(&x, &y);
1249 Buffer * b = bufferlist.exists(fname) ?
1250 bufferlist.getBuffer(fname) :
1251 bufferlist.loadLyXFile(fname); // don't ask, just load it
1253 bv_->text->SetCursorFromCoordinates(bv_, x, y);
1254 update(BufferView::SELECT|BufferView::FITCUR);
1258 bool BufferView::Pimpl::NoSavedPositions()
1260 return backstack.empty();
1264 void BufferView::Pimpl::setState()
1266 if (!lyxrc.rtl_support)
1269 if (bv_->text->real_current_font.isRightToLeft() &&
1270 bv_->text->real_current_font.latex() != LyXFont::ON) {
1271 if (owner_->getIntl()->primarykeymap)
1272 owner_->getIntl()->KeyMapSec();
1274 if (!owner_->getIntl()->primarykeymap)
1275 owner_->getIntl()->KeyMapPrim();
1280 void BufferView::Pimpl::insetSleep()
1282 if (bv_->the_locking_inset && !bv_->inset_slept) {
1283 bv_->the_locking_inset->GetCursorPos(bv_, bv_->slx, bv_->sly);
1284 bv_->the_locking_inset->InsetUnlock(bv_);
1285 bv_->inset_slept = true;
1290 void BufferView::Pimpl::insetWakeup()
1292 if (bv_->the_locking_inset && bv_->inset_slept) {
1293 bv_->the_locking_inset->Edit(bv_, bv_->slx, bv_->sly, 0);
1294 bv_->inset_slept = false;
1299 void BufferView::Pimpl::insetUnlock()
1301 if (bv_->the_locking_inset) {
1302 if (!bv_->inset_slept) bv_->the_locking_inset->InsetUnlock(bv_);
1303 bv_->the_locking_inset = 0;
1304 bv_->text->FinishUndo();
1305 bv_->inset_slept = false;
1310 bool BufferView::Pimpl::focus() const
1312 return workarea_->hasFocus();
1316 void BufferView::Pimpl::focus(bool f)
1318 if (f) workarea_->setFocus();
1322 bool BufferView::Pimpl::active() const
1324 return workarea_->active();
1328 bool BufferView::Pimpl::belowMouse() const
1330 return workarea_->belowMouse();
1334 void BufferView::Pimpl::showCursor()
1337 screen_->ShowCursor(bv_->text);
1341 void BufferView::Pimpl::hideCursor()
1344 screen_->HideCursor();
1348 void BufferView::Pimpl::toggleSelection(bool b)
1351 screen_->ToggleSelection(bv_->text, b);
1355 void BufferView::Pimpl::toggleToggle()
1358 screen_->ToggleToggle(bv_->text);
1362 void BufferView::Pimpl::center()
1365 if (bv_->text->cursor.y() > workarea_->height() / 2) {
1366 screen_->Draw(bv_->text, bv_->text->cursor.y() - workarea_->height() / 2);
1368 screen_->Draw(bv_->text, 0);
1370 update(BufferView::SELECT|BufferView::FITCUR);
1375 void BufferView::Pimpl::pasteClipboard(bool asPara)
1377 if (buffer_ == 0) return;
1379 screen_->HideCursor();
1380 bv_->beforeChange();
1382 string clip(workarea_->getClipboard());
1384 if (clip.empty()) return;
1387 bv_->text->InsertStringB(bv_, clip);
1389 bv_->text->InsertStringA(bv_, clip);
1391 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1395 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
1397 workarea_->putClipboard(stuff);