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 bv_->the_locking_inset->scroll(),
511 y - cursor.y() + bv_->text->first,
516 /* The selection possible is needed, that only motion events are
517 * used, where the bottom press event was on the drawing area too */
518 if (selection_possible) {
519 screen_->HideCursor();
521 bv_->text->SetCursorFromCoordinates(bv_, x, y + bv_->text->first);
523 if (!bv_->text->selection)
524 update(BufferView::UPDATE); // Maybe an empty line was deleted
526 bv_->text->SetSelection();
527 screen_->ToggleToggle(bv_->text);
529 screen_->ShowCursor(bv_->text);
535 // Single-click on work area
536 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
542 if (buffer_ == 0 || !screen_) return;
544 Inset * inset_hit = checkInsetHit(bv_->text, xpos, ypos, button);
546 // ok ok, this is a hack.
547 if (button == 4 || button == 5) {
550 scrollUp(lyxrc.wheel_jump); // default 100, set in lyxrc
553 scrollDown(lyxrc.wheel_jump);
558 if (bv_->the_locking_inset) {
559 // We are in inset locking mode
561 /* Check whether the inset was hit. If not reset mode,
562 otherwise give the event to the inset */
563 if (inset_hit == bv_->the_locking_inset) {
564 bv_->the_locking_inset->
565 InsetButtonPress(bv_,
570 bv_->unlockInset(bv_->the_locking_inset);
575 selection_possible = true;
576 screen_->HideCursor();
579 // Right button mouse click on a table
581 (bv_->text->cursor.par()->table ||
582 bv_->text->MouseHitInTable(bv_, xpos, ypos + bv_->text->first))) {
583 // Set the cursor to the press-position
584 bv_->text->SetCursorFromCoordinates(bv_, xpos, ypos + bv_->text->first);
587 // Only show the table popup if the hit is in
589 if (!bv_->text->HitInTable(bv_,
590 bv_->text->cursor.row(), xpos))
593 // Hit above or below the table?
595 if (!bv_->text->selection) {
596 screen_->ToggleSelection(bv_->text);
597 bv_->text->ClearSelection();
598 bv_->text->FullRebreak(bv_);
599 screen_->Update(bv_->text);
602 // Popup table popup when on a table.
603 // This is obviously temporary, since we
604 // should be able to popup various
605 // context-sensitive-menus with the
606 // the right mouse. So this should be done more
607 // general in the future. Matthias.
608 selection_possible = false;
610 ->Dispatch(LFUN_LAYOUT_TABLE,
617 int screen_first = bv_->text->first;
619 // Middle button press pastes if we have a selection
620 bool paste_internally = false;
622 && bv_->text->selection) {
623 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
624 paste_internally = true;
627 // Clear the selection
628 screen_->ToggleSelection(bv_->text);
629 bv_->text->ClearSelection();
630 bv_->text->FullRebreak(bv_);
631 screen_->Update(bv_->text);
634 // Single left click in math inset?
635 if ((inset_hit != 0) &&
636 (inset_hit->Editable()==Inset::HIGHLY_EDITABLE)) {
637 // Highly editable inset, like math
638 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
639 selection_possible = false;
640 owner_->updateLayoutChoice();
641 owner_->getMiniBuffer()->Set(inset->EditMessage());
642 inset->InsetButtonPress(bv_, xpos, ypos, button);
643 inset->Edit(bv_, xpos, ypos, button);
647 // Right click on a footnote flag opens float menu
649 selection_possible = false;
653 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
654 bv_->text->SetCursorFromCoordinates(bv_, xpos, ypos + screen_first);
655 bv_->text->FinishUndo();
656 bv_->text->sel_cursor = bv_->text->cursor;
657 bv_->text->cursor.x_fix(bv_->text->cursor.x());
659 owner_->updateLayoutChoice();
661 selection_possible = false;
664 // Insert primary selection with middle mouse
665 // if there is a local selection in the current buffer,
668 if (paste_internally)
669 owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
671 owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
673 selection_possible = false;
679 void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, unsigned int button)
682 if (buffer_ && !bv_->the_locking_inset) {
683 if (screen_ && button == 1) {
684 screen_->HideCursor();
685 screen_->ToggleSelection(bv_->text);
686 bv_->text->SelectWord(bv_);
687 screen_->ToggleSelection(bv_->text, false);
688 /* This will fit the cursor on the screen
690 update(BufferView::SELECT|BufferView::FITCUR);
696 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, unsigned int button)
699 if (buffer_ && screen_ && button == 1) {
700 screen_->HideCursor();
701 screen_->ToggleSelection(bv_->text);
702 bv_->text->CursorHome(bv_);
703 bv_->text->sel_cursor = bv_->text->cursor;
704 bv_->text->CursorEnd(bv_);
705 bv_->text->SetSelection();
706 screen_->ToggleSelection(bv_->text, false);
707 /* This will fit the cursor on the screen
709 update(BufferView::SELECT|BufferView::FITCUR);
714 void BufferView::Pimpl::enterView()
716 if (active() && available()) {
717 SetXtermCursor(workarea_->getWin());
718 using_xterm_cursor = true;
723 void BufferView::Pimpl::leaveView()
725 if (using_xterm_cursor) {
726 XUndefineCursor(fl_display, workarea_->getWin());
727 using_xterm_cursor = false;
732 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
735 if (buffer_ == 0 || screen_ == 0) return;
737 // If we hit an inset, we have the inset coordinates in these
738 // and inset_hit points to the inset. If we do not hit an
739 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
740 Inset * inset_hit = checkInsetHit(bv_->text, x, y, button);
742 if (bv_->the_locking_inset) {
743 // We are in inset locking mode.
745 /* LyX does a kind of work-area grabbing for insets.
746 Only a ButtonPress Event outside the inset will
747 force a InsetUnlock. */
748 bv_->the_locking_inset->
749 InsetButtonRelease(bv_, x, y, button);
753 selection_possible = false;
755 if (bv_->text->cursor.par()->table) {
756 int cell = bv_->text->
757 NumberOfCell(bv_->text->cursor.par(),
758 bv_->text->cursor.pos());
759 if (bv_->text->cursor.par()->table->IsContRow(cell) &&
760 bv_->text->cursor.par()->table->
761 CellHasContRow(bv_->text->cursor.par()->table->
762 GetCellAbove(cell))<0) {
763 bv_->text->CursorUp(bv_);
768 if (button >= 2) return;
773 // Did we hit an editable inset?
774 if (inset_hit != 0) {
775 // Inset like error, notes and figures
776 selection_possible = false;
778 // CHECK fix this proper in 0.13
780 // Following a ref shouldn't issue
781 // a push on the undo-stack
782 // anylonger, now that we have
783 // keybindings for following
784 // references and returning from
785 // references. IMHO though, it
786 // should be the inset's own business
787 // to push or not push on the undo
788 // stack. They don't *have* to
789 // alter the document...
791 // ...or maybe the SetCursorParUndo()
792 // below isn't necessary at all anylonger?
793 if (inset_hit->LyxCode() == Inset::REF_CODE) {
794 bv_->text->SetCursorParUndo(bv_->buffer());
797 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
798 if (inset_hit->Editable()==Inset::HIGHLY_EDITABLE) {
799 // Highly editable inset, like math
800 UpdatableInset *inset = (UpdatableInset *)inset_hit;
801 inset->InsetButtonRelease(bv_, x, y, button);
803 inset_hit->InsetButtonRelease(bv_, x, y, button);
804 inset_hit->Edit(bv_, x, y, button);
809 // check whether we want to open a float
813 if (bv_->text->cursor.pos() <
814 bv_->text->cursor.par()->Last()) {
815 c = bv_->text->cursor.par()->
816 GetChar(bv_->text->cursor.pos());
819 if (c == LyXParagraph::META_FOOTNOTE
820 || c == LyXParagraph::META_MARGIN
821 || c == LyXParagraph::META_FIG
822 || c == LyXParagraph::META_TAB
823 || c == LyXParagraph::META_WIDE_FIG
824 || c == LyXParagraph::META_WIDE_TAB
825 || c == LyXParagraph::META_ALGORITHM){
829 if (bv_->text->cursor.pos() - 1 >= 0) {
830 c = bv_->text->cursor.par()->
831 GetChar(bv_->text->cursor.pos() - 1);
833 if (c == LyXParagraph::META_FOOTNOTE
834 || c == LyXParagraph::META_MARGIN
835 || c == LyXParagraph::META_FIG
836 || c == LyXParagraph::META_TAB
837 || c == LyXParagraph::META_WIDE_FIG
838 || c == LyXParagraph::META_WIDE_TAB
839 || c == LyXParagraph::META_ALGORITHM){
840 // We are one step too far to the right
841 bv_->text->CursorLeft(bv_);
850 selection_possible = false;
856 // Do we want to close a float? (click on the float-label)
857 if (bv_->text->cursor.row()->par()->footnoteflag ==
858 LyXParagraph::OPEN_FOOTNOTE
859 && bv_->text->cursor.row()->previous() &&
860 bv_->text->cursor.row()->previous()->par()->
861 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
862 LyXFont font(LyXFont::ALL_SANE);
863 font.setSize(LyXFont::SIZE_FOOTNOTE);
865 int box_x = 20; // LYX_PAPER_MARGIN;
866 box_x += lyxfont::width(" wide-tab ", font);
868 unsigned int screen_first = bv_->text->first;
871 && y + screen_first > bv_->text->cursor.y() -
872 bv_->text->cursor.row()->baseline()
873 && y + screen_first < bv_->text->cursor.y() -
874 bv_->text->cursor.row()->baseline()
875 + lyxfont::maxAscent(font) * 1.2 + lyxfont::maxDescent(font) * 1.2) {
877 selection_possible = false;
883 // Maybe we want to edit a bibitem ale970302
884 if (bv_->text->cursor.par()->bibkey && x < 20 +
885 bibitemMaxWidth(bv_, textclasslist.
887 params.textclass).defaultfont())) {
888 bv_->text->cursor.par()->bibkey->Edit(bv_, 0, 0, 0);
896 * Returns an inset if inset was hit. 0 otherwise.
897 * If hit, the coordinates are changed relative to the inset.
898 * Otherwise coordinates are not changed, and false is returned.
900 Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y,
901 unsigned int /* button */)
906 unsigned int y_tmp = y + text->first;
909 text->SetCursorFromCoordinates(bv_, cursor, x, y_tmp);
911 if (cursor.pos() < cursor.par()->Last()
912 && cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
913 && cursor.par()->GetInset(cursor.pos())
914 && cursor.par()->GetInset(cursor.pos())->Editable()) {
916 // Check whether the inset really was hit
917 Inset * tmpinset = cursor.par()->GetInset(cursor.pos());
918 LyXFont font = text->GetFont(bv_->buffer(),
919 cursor.par(), cursor.pos());
920 bool is_rtl = font.isVisibleRightToLeft();
924 start_x = cursor.x() - tmpinset->width(bv_, font) + tmpinset->scroll();
925 end_x = cursor.x() + tmpinset->scroll();
927 start_x = cursor.x() + tmpinset->scroll();
928 end_x = cursor.x() + tmpinset->width(bv_, font) + tmpinset->scroll();
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)) {
934 text->SetCursor(bv_, cursor.par(),cursor.pos(),true);
936 // The origin of an inset is on the baseline
937 y = y_tmp - (text->cursor.y());
942 if ((cursor.pos() - 1 >= 0) &&
943 (cursor.par()->GetChar(cursor.pos()-1) == LyXParagraph::META_INSET) &&
944 (cursor.par()->GetInset(cursor.pos() - 1)) &&
945 (cursor.par()->GetInset(cursor.pos() - 1)->Editable())) {
946 Inset * tmpinset = cursor.par()->GetInset(cursor.pos()-1);
947 LyXFont font = text->GetFont(bv_->buffer(), cursor.par(),
949 bool is_rtl = font.isVisibleRightToLeft();
953 start_x = cursor.x() - tmpinset->width(bv_, font) +
955 end_x = cursor.x() + tmpinset->scroll();
957 start_x = cursor.x() + tmpinset->scroll();
958 end_x = cursor.x() + tmpinset->width(bv_, font) +
961 if (x > start_x && x < end_x
962 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
963 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
965 if (move_cursor && (tmpinset != bv_->the_locking_inset))
967 text->SetCursor(bv_, cursor.par(),cursor.pos()-1,true);
969 // The origin of an inset is on the baseline
970 y = y_tmp - (text->cursor.y());
978 void BufferView::Pimpl::workAreaExpose()
980 // this is a hack to ensure that we only call this through
981 // BufferView::redraw().
986 static int work_area_width = 0;
987 static unsigned int work_area_height = 0;
989 bool widthChange = workarea_->workWidth() != work_area_width;
990 bool heightChange = workarea_->height() != work_area_height;
992 // update from work area
993 work_area_width = workarea_->workWidth();
994 work_area_height = workarea_->height();
997 // All buffers need a resize
1000 // Remove all texts from the textcache
1001 // This is not _really_ what we want to do. What
1002 // we really want to do is to delete in textcache
1003 // that does not have a BufferView with matching
1004 // width, but as long as we have only one BufferView
1005 // deleting all gives the same result.
1006 if (lyxerr.debugging())
1007 textcache.show(lyxerr, "Expose delete all");
1009 } else if (heightChange) {
1010 // Rebuild image of current screen
1012 // fitCursor() ensures we don't jump back
1013 // to the start of the document on vertical
1017 // The main window size has changed, repaint most stuff
1019 // ...including the minibuffer
1020 owner_->getMiniBuffer()->Init();
1022 } else if (screen_) screen_->Redraw(bv_->text);
1024 // Grey box when we don't have a buffer
1025 workarea_->greyOut();
1028 // always make sure that the scrollbar is sane.
1030 owner_->updateLayoutChoice();
1035 void BufferView::Pimpl::update()
1037 if (screen_) screen_->Update(bv_->text);
1040 // Values used when calling update:
1042 // -2 - update, move sel_cursor if selection, fitcursor
1043 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
1044 // 0 - update, move sel_cursor if selection, fitcursor
1045 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
1046 // 3 - update, move sel_cursor if selection
1049 // a simple redraw of the parts that need refresh
1051 // move sel_cursor if selection -
1052 // the text's sel_cursor is moved if there is selection is progress
1055 // fitCursor() is called and the scrollbar updated
1058 // the buffer is marked dirty.
1067 // UPDATE_ONLY = UPDATE;
1068 // UPDATE_SELECT = UPDATE | SELECT;
1069 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
1070 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1072 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
1073 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
1074 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1075 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1076 // update(3) -> update(1) -> update(1) -> update(SELECT)
1078 void BufferView::Pimpl::update(BufferView::UpdateCodes f)
1080 owner_->updateLayoutChoice();
1082 if (!bv_->text->selection && (f & SELECT)) {
1083 bv_->text->sel_cursor = bv_->text->cursor;
1086 bv_->text->FullRebreak(bv_);
1095 if (buffer_->isLyxClean()) {
1096 buffer_->markDirty();
1097 owner_->getMiniBuffer()->setTimer(4);
1099 buffer_->markDirty();
1105 // Callback for cursor timer
1106 void BufferView::Pimpl::cursorToggle()
1108 // Quite a nice place for asyncron Inset updating, isn't it?
1109 // Actually no! This is run even if no buffer exist... so (Lgb)
1111 goto set_timer_and_return;
1115 // On my quest to solve the gs render hangups I am now
1116 // disabling the SIGHUP completely, and will do a wait
1117 // now and then instead. If the guess that xforms somehow
1118 // destroys something is true, this is likely (hopefully)
1119 // to solve the problem...at least I hope so. Lgb
1121 // ...Ok this seems to work...at least it does not make things
1122 // worse so far. However I still see gs processes that hangs.
1123 // I would really like to know _why_ they are hanging. Anyway
1124 // the solution without the SIGCHLD handler seems to be easier
1127 // When attaching gdb to a a running gs that hangs it shows
1128 // that it is waiting for input(?) Is it possible for us to
1129 // provide that input somehow? Or figure what it is expecing
1132 // One solution is to, after some time, look if there are some
1133 // old gs processes still running and if there are: kill them
1136 // Another solution is to provide the user an option to rerender
1137 // a picture. This would, for the picture in question, check if
1138 // there is a gs running for it, if so kill it, and start a new
1139 // rendering process.
1141 // these comments posted to lyx@via
1144 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1145 if (pid == -1) // error find out what is wrong
1146 ; // ignore it for now.
1148 sigchldhandler(pid, &status);
1151 updatelist.update(bv_);
1154 goto set_timer_and_return;
1157 if (!bv_->the_locking_inset) {
1158 screen_->CursorToggle(bv_->text);
1160 bv_->the_locking_inset->
1161 ToggleInsetCursor(bv_);
1164 set_timer_and_return:
1165 cursor_timeout.restart();
1170 void BufferView::Pimpl::cursorPrevious()
1172 if (!bv_->text->cursor.row()->previous()) return;
1174 long y = bv_->text->first;
1175 Row * cursorrow = bv_->text->cursor.row();
1176 bv_->text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y);
1177 bv_->text->FinishUndo();
1178 // This is to allow jumping over large insets
1179 if ((cursorrow == bv_->text->cursor.row()))
1180 bv_->text->CursorUp(bv_);
1182 if (bv_->text->cursor.row()->height() < workarea_->height())
1183 screen_->Draw(bv_->text,
1184 bv_->text->cursor.y()
1185 - bv_->text->cursor.row()->baseline()
1186 + bv_->text->cursor.row()->height()
1187 - workarea_->height() + 1 );
1192 void BufferView::Pimpl::cursorNext()
1194 if (!bv_->text->cursor.row()->next()) return;
1196 long y = bv_->text->first;
1197 bv_->text->GetRowNearY(y);
1198 Row * cursorrow = bv_->text->cursor.row();
1199 bv_->text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y
1200 + workarea_->height());
1201 bv_->text->FinishUndo();
1202 // This is to allow jumping over large insets
1203 if ((cursorrow == bv_->text->cursor.row()))
1204 bv_->text->CursorDown(bv_);
1206 if (bv_->text->cursor.row()->height() < workarea_->height())
1207 screen_->Draw(bv_->text, bv_->text->cursor.y()
1208 - bv_->text->cursor.row()->baseline());
1213 bool BufferView::Pimpl::available() const
1215 if (buffer_ && bv_->text) return true;
1220 void BufferView::Pimpl::beforeChange()
1223 bv_->text->ClearSelection();
1226 //owner_->update_timeout.stop();
1230 void BufferView::Pimpl::savePosition()
1232 backstack.push(buffer_->fileName(),
1233 bv_->text->cursor.x(),
1234 bv_->text->cursor.y());
1238 void BufferView::Pimpl::restorePosition()
1240 if (backstack.empty()) return;
1243 string fname = backstack.pop(&x, &y);
1246 Buffer * b = bufferlist.exists(fname) ?
1247 bufferlist.getBuffer(fname) :
1248 bufferlist.loadLyXFile(fname); // don't ask, just load it
1250 bv_->text->SetCursorFromCoordinates(bv_, x, y);
1251 update(BufferView::SELECT|BufferView::FITCUR);
1255 bool BufferView::Pimpl::NoSavedPositions()
1257 return backstack.empty();
1261 void BufferView::Pimpl::setState()
1263 if (!lyxrc.rtl_support)
1266 if (bv_->text->real_current_font.isRightToLeft() &&
1267 bv_->text->real_current_font.latex() != LyXFont::ON) {
1268 if (owner_->getIntl()->primarykeymap)
1269 owner_->getIntl()->KeyMapSec();
1271 if (!owner_->getIntl()->primarykeymap)
1272 owner_->getIntl()->KeyMapPrim();
1277 void BufferView::Pimpl::insetSleep()
1279 if (bv_->the_locking_inset && !bv_->inset_slept) {
1280 bv_->the_locking_inset->GetCursorPos(bv_, bv_->slx, bv_->sly);
1281 bv_->the_locking_inset->InsetUnlock(bv_);
1282 bv_->inset_slept = true;
1287 void BufferView::Pimpl::insetWakeup()
1289 if (bv_->the_locking_inset && bv_->inset_slept) {
1290 bv_->the_locking_inset->Edit(bv_, bv_->slx, bv_->sly, 0);
1291 bv_->inset_slept = false;
1296 void BufferView::Pimpl::insetUnlock()
1298 if (bv_->the_locking_inset) {
1299 if (!bv_->inset_slept) bv_->the_locking_inset->InsetUnlock(bv_);
1300 bv_->the_locking_inset = 0;
1301 bv_->text->FinishUndo();
1302 bv_->inset_slept = false;
1307 bool BufferView::Pimpl::focus() const
1309 return workarea_->hasFocus();
1313 void BufferView::Pimpl::focus(bool f)
1315 if (f) workarea_->setFocus();
1319 bool BufferView::Pimpl::active() const
1321 return workarea_->active();
1325 bool BufferView::Pimpl::belowMouse() const
1327 return workarea_->belowMouse();
1331 void BufferView::Pimpl::showCursor()
1334 screen_->ShowCursor(bv_->text);
1338 void BufferView::Pimpl::hideCursor()
1341 screen_->HideCursor();
1345 void BufferView::Pimpl::toggleSelection(bool b)
1348 screen_->ToggleSelection(bv_->text, b);
1352 void BufferView::Pimpl::toggleToggle()
1355 screen_->ToggleToggle(bv_->text);
1359 void BufferView::Pimpl::center()
1362 if (bv_->text->cursor.y() > workarea_->height() / 2) {
1363 screen_->Draw(bv_->text, bv_->text->cursor.y() - workarea_->height() / 2);
1365 screen_->Draw(bv_->text, 0);
1367 update(BufferView::SELECT|BufferView::FITCUR);
1372 void BufferView::Pimpl::pasteClipboard(bool asPara)
1374 if (buffer_ == 0) return;
1376 screen_->HideCursor();
1377 bv_->beforeChange();
1379 string clip(workarea_->getClipboard());
1381 if (clip.empty()) return;
1384 bv_->text->InsertStringB(bv_, clip);
1386 bv_->text->InsertStringA(bv_, clip);
1388 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1392 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
1394 workarea_->putClipboard(stuff);