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"
25 #include "lyx_gui_misc.h"
28 #include "support/LAssert.h"
29 #include "frontends/Dialogs.h"
30 #include "insets/insetcite.h"
32 #ifdef SIGC_CXX_NAMESPACES
41 /* the selection possible is needed, that only motion events are
42 * used, where the bottom press event was on the drawing area too */
43 bool selection_possible = false;
45 extern BufferList bufferlist;
46 extern char ascii_type;
48 extern void sigchldhandler(pid_t pid, int * status);
49 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
55 XSync(fl_get_display(), 0);
60 void SetXtermCursor(Window win)
63 static bool cursor_undefined = true;
64 if (cursor_undefined){
65 cursor = XCreateFontCursor(fl_display, XC_xterm);
67 cursor_undefined = false;
69 XDefineCursor(fl_display, win, cursor);
74 BufferView::Pimpl::Pimpl(BufferView * b, LyXView * o,
75 int xpos, int ypos, int width, int height)
76 : bv_(b), owner_(o), cursor_timeout(400)
79 workarea_ = new WorkArea(bv_, xpos, ypos, width, height);
82 cursor_timeout.timeout.connect(slot(this,
83 &BufferView::Pimpl::cursorToggle));
84 current_scrollbar_value = 0;
85 cursor_timeout.start();
86 workarea_->setFocus();
87 using_xterm_cursor = false;
91 Painter & BufferView::Pimpl::painter()
93 return workarea_->getPainter();
97 void BufferView::Pimpl::buffer(Buffer * b)
99 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
103 buffer_->delUser(bv_);
105 // Put the old text into the TextCache, but
106 // only if the buffer is still loaded.
107 // Also set the owner of the test to 0
108 // bv_->text->owner(0);
109 textcache.add(buffer_, workarea_->workWidth(), bv_->text);
110 if (lyxerr.debugging())
111 textcache.show(lyxerr, "BufferView::buffer");
116 // Set current buffer
119 if (bufferlist.getState() == BufferList::CLOSING) return;
122 // screen is always deleted when the buffer is changed.
126 // If we are closing the buffer, use the first buffer as current
128 buffer_ = bufferlist.first();
132 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
133 buffer_->addUser(bv_);
134 owner_->getMenus()->showMenus();
135 // If we don't have a text object for this, we make one
136 if (bv_->text == 0) {
137 resizeCurrentBuffer();
142 bv_->text->first = screen_->TopCursorVisible(bv_->text);
144 owner_->getDialogs()->updateBufferDependent();
147 lyxerr[Debug::INFO] << " No Buffer!" << endl;
148 owner_->getMenus()->hideMenus();
152 // Also remove all remaining text's from the testcache.
153 // (there should not be any!) (if there is any it is a
155 if (lyxerr.debugging())
156 textcache.show(lyxerr, "buffer delete all");
159 // should update layoutchoice even if we don't have a buffer.
160 owner_->updateLayoutChoice();
161 owner_->getMiniBuffer()->Init();
162 owner_->updateWindowTitle();
166 void BufferView::Pimpl::resize(int xpos, int ypos, int width, int height)
168 workarea_->resize(xpos, ypos, width, height);
174 void BufferView::Pimpl::resize()
176 // This will resize the buffer. (Asger)
178 resizeCurrentBuffer();
182 void BufferView::Pimpl::redraw()
184 lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
189 bool BufferView::Pimpl::fitCursor()
191 Assert(screen_); // it is a programming error to call fitCursor
192 // without a valid screen.
193 bool ret = screen_->FitCursor(bv_->text);
194 if (ret) updateScrollbar();
199 void BufferView::Pimpl::redoCurrentBuffer()
201 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
202 if (buffer_ && bv_->text) {
204 owner_->updateLayoutChoice();
209 int BufferView::Pimpl::resizeCurrentBuffer()
211 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
213 LyXParagraph * par = 0;
214 LyXParagraph * selstartpar = 0;
215 LyXParagraph * selendpar = 0;
224 owner_->getMiniBuffer()->Set(_("Formatting document..."));
227 par = bv_->text->cursor.par();
228 pos = bv_->text->cursor.pos();
229 selstartpar = bv_->text->sel_start_cursor.par();
230 selstartpos = bv_->text->sel_start_cursor.pos();
231 selendpar = bv_->text->sel_end_cursor.par();
232 selendpos = bv_->text->sel_end_cursor.pos();
233 selection = bv_->text->selection;
234 mark_set = bv_->text->mark_set;
236 bv_->text = new LyXText(bv_);
238 // See if we have a text in TextCache that fits
239 // the new buffer_ with the correct width.
240 bv_->text = textcache.findFit(buffer_, workarea_->workWidth());
242 if (lyxerr.debugging()) {
243 lyxerr << "Found a LyXText that fits:\n";
244 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea_->workWidth(), bv_->text)));
246 // Set the owner of the newly found text
247 // bv_->text->owner(bv_);
248 if (lyxerr.debugging())
249 textcache.show(lyxerr, "resizeCurrentBuffer");
251 bv_->text = new LyXText(bv_);
257 bv_->text->selection = true;
258 /* at this point just to avoid the Delete-Empty-Paragraph
259 * Mechanism when setting the cursor */
260 bv_->text->mark_set = mark_set;
262 bv_->text->SetCursor(bv_, selstartpar, selstartpos);
263 bv_->text->sel_cursor = bv_->text->cursor;
264 bv_->text->SetCursor(bv_, selendpar, selendpos);
265 bv_->text->SetSelection();
266 bv_->text->SetCursor(bv_, par, pos);
268 bv_->text->SetCursor(bv_, par, pos);
269 bv_->text->sel_cursor = bv_->text->cursor;
270 bv_->text->selection = false;
273 bv_->text->first = screen_->TopCursorVisible(bv_->text);
274 /* this will scroll the
275 * screen such that the
280 owner_->getMiniBuffer()->Init();
284 // Now if the title form still exist kill it
291 void BufferView::Pimpl::gotoError()
296 screen_->HideCursor();
298 update(BufferView::SELECT|BufferView::FITCUR);
301 if (!bv_->text->GotoNextError(bv_)) {
302 if (bv_->text->cursor.pos()
303 || bv_->text->cursor.par() != bv_->text->FirstParagraph()) {
304 tmp = bv_->text->cursor;
305 bv_->text->cursor.par(bv_->text->FirstParagraph());
306 bv_->text->cursor.pos(0);
307 if (!bv_->text->GotoNextError(bv_)) {
308 bv_->text->cursor = tmp;
309 owner_->getMiniBuffer()
310 ->Set(_("No more errors"));
314 owner_->getMiniBuffer()->Set(_("No more errors"));
318 update(BufferView::SELECT|BufferView::FITCUR);
319 bv_->text->sel_cursor = bv_->text->cursor;
323 void BufferView::Pimpl::updateScreen()
325 // Regenerate the screen.
327 screen_ = new LyXScreen(*workarea_);
331 void BufferView::Pimpl::updateScrollbar()
333 /* If the text is smaller than the working area, the scrollbar
334 * maximum must be the working area height. No scrolling will
338 workarea_->setScrollbar(0, 1.0);
342 static unsigned long max2 = 0;
343 static unsigned long height2 = 0;
345 unsigned long cbth = 0;
349 cbth = bv_->text->height;
350 cbsf = bv_->text->first;
353 // check if anything has changed.
355 height2 == workarea_->height() &&
356 current_scrollbar_value == cbsf)
359 height2 = workarea_->height();
360 current_scrollbar_value = cbsf;
362 if (cbth <= height2) { // text is smaller than screen
363 workarea_->setScrollbar(0, 1.0); // right?
367 long maximum_height = workarea_->height() * 3 / 4 + cbth;
371 double hfloat = workarea_->height();
372 double maxfloat = maximum_height;
374 float slider_size = 0.0;
375 int slider_value = value;
377 workarea_->setScrollbarBounds(0, bv_->text->height - workarea_->height());
378 double lineh = bv_->text->DefaultHeight();
379 workarea_->setScrollbarIncrements(lineh);
380 if (maxfloat > 0.0) {
381 if ((hfloat / maxfloat) * float(height2) < 3)
382 slider_size = 3.0/float(height2);
384 slider_size = hfloat / maxfloat;
386 slider_size = hfloat;
388 workarea_->setScrollbar(slider_value, slider_size / workarea_->height());
392 // Callback for scrollbar slider
393 void BufferView::Pimpl::scrollCB(double value)
395 extern bool cursor_follows_scrollbar;
397 if (buffer_ == 0) return;
399 current_scrollbar_value = long(value);
400 if (current_scrollbar_value < 0)
401 current_scrollbar_value = 0;
406 screen_->Draw(bv_->text, current_scrollbar_value);
408 if (cursor_follows_scrollbar) {
409 LyXText * vbt = bv_->text;
410 unsigned int height = vbt->DefaultHeight();
412 if (vbt->cursor.y() < bv_->text->first + height) {
413 vbt->SetCursorFromCoordinates(bv_, 0,
416 } else if (vbt->cursor.y() >
417 bv_->text->first + workarea_->height() - height) {
418 vbt->SetCursorFromCoordinates(bv_, 0,
420 workarea_->height() -
428 int BufferView::Pimpl::scrollUp(long time)
430 if (buffer_ == 0) return 0;
431 if (!screen_) return 0;
433 double value = workarea_->getScrollbarValue();
435 if (value == 0) return 0;
437 float add_value = (bv_->text->DefaultHeight()
438 + float(time) * float(time) * 0.125);
440 if (add_value > workarea_->height())
441 add_value = float(workarea_->height() -
442 bv_->text->DefaultHeight());
449 workarea_->setScrollbarValue(value);
451 bv_->scrollCB(value);
456 int BufferView::Pimpl::scrollDown(long time)
458 if (buffer_ == 0) return 0;
459 if (!screen_) return 0;
461 double value= workarea_->getScrollbarValue();
462 pair<float, float> p = workarea_->getScrollbarBounds();
463 double max = p.second;
465 if (value == max) return 0;
467 float add_value = (bv_->text->DefaultHeight()
468 + float(time) * float(time) * 0.125);
470 if (add_value > workarea_->height())
471 add_value = float(workarea_->height() -
472 bv_->text->DefaultHeight());
479 workarea_->setScrollbarValue(value);
481 bv_->scrollCB(value);
486 void BufferView::Pimpl::workAreaMotionNotify(int x, int y, unsigned int state)
488 // Only use motion with button 1
489 if (!(state & Button1MotionMask))
492 if (buffer_ == 0 || !screen_) return;
494 // Check for inset locking
495 if (bv_->the_locking_inset) {
496 LyXCursor cursor = bv_->text->cursor;
497 bv_->the_locking_inset->
498 InsetMotionNotify(bv_,
500 y - cursor.y() + bv_->text->first,
505 /* The selection possible is needed, that only motion events are
506 * used, where the bottom press event was on the drawing area too */
507 if (selection_possible) {
508 screen_->HideCursor();
510 bv_->text->SetCursorFromCoordinates(bv_, x, y + bv_->text->first);
512 if (!bv_->text->selection)
513 update(BufferView::UPDATE); // Maybe an empty line was deleted
515 bv_->text->SetSelection();
516 screen_->ToggleToggle(bv_->text);
518 screen_->ShowCursor(bv_->text);
524 // Single-click on work area
525 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
531 if (buffer_ == 0 || !screen_) return;
533 Inset * inset_hit = checkInsetHit(bv_->text, xpos, ypos, button);
535 // ok ok, this is a hack.
536 if (button == 4 || button == 5) {
539 scrollUp(lyxrc.wheel_jump); // default 100, set in lyxrc
542 scrollDown(lyxrc.wheel_jump);
547 if (bv_->the_locking_inset) {
548 // We are in inset locking mode
550 /* Check whether the inset was hit. If not reset mode,
551 otherwise give the event to the inset */
552 if (inset_hit == bv_->the_locking_inset) {
553 bv_->the_locking_inset->
554 InsetButtonPress(bv_,
559 bv_->unlockInset(bv_->the_locking_inset);
564 selection_possible = true;
565 screen_->HideCursor();
568 // Right button mouse click on a table
570 (bv_->text->cursor.par()->table ||
571 bv_->text->MouseHitInTable(bv_, xpos, ypos + bv_->text->first))) {
572 // Set the cursor to the press-position
573 bv_->text->SetCursorFromCoordinates(bv_, xpos, ypos + bv_->text->first);
576 // Only show the table popup if the hit is in
578 if (!bv_->text->HitInTable(bv_,
579 bv_->text->cursor.row(), xpos))
582 // Hit above or below the table?
584 if (!bv_->text->selection) {
585 screen_->ToggleSelection(bv_->text);
586 bv_->text->ClearSelection();
587 bv_->text->FullRebreak(bv_);
588 screen_->Update(bv_->text);
591 // Popup table popup when on a table.
592 // This is obviously temporary, since we
593 // should be able to popup various
594 // context-sensitive-menus with the
595 // the right mouse. So this should be done more
596 // general in the future. Matthias.
597 selection_possible = false;
599 ->Dispatch(LFUN_LAYOUT_TABLE,
606 int screen_first = bv_->text->first;
608 // Middle button press pastes if we have a selection
609 bool paste_internally = false;
611 && bv_->text->selection) {
612 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
613 paste_internally = true;
616 // Clear the selection
617 screen_->ToggleSelection(bv_->text);
618 bv_->text->ClearSelection();
619 bv_->text->FullRebreak(bv_);
620 screen_->Update(bv_->text);
623 // Single left click in math inset?
624 if ((inset_hit != 0) &&
625 (inset_hit->Editable()==Inset::HIGHLY_EDITABLE)) {
626 // Highly editable inset, like math
627 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
628 selection_possible = false;
629 owner_->updateLayoutChoice();
630 owner_->getMiniBuffer()->Set(inset->EditMessage());
631 inset->InsetButtonPress(bv_, xpos, ypos, button);
632 inset->Edit(bv_, xpos, ypos, button);
636 // Right click on a footnote flag opens float menu
638 selection_possible = false;
642 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
643 bv_->text->SetCursorFromCoordinates(bv_, xpos, ypos + screen_first);
644 bv_->text->FinishUndo();
645 bv_->text->sel_cursor = bv_->text->cursor;
646 bv_->text->cursor.x_fix(bv_->text->cursor.x());
648 owner_->updateLayoutChoice();
650 selection_possible = false;
653 // Insert primary selection with middle mouse
654 // if there is a local selection in the current buffer,
657 if (paste_internally)
658 owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
660 owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
662 selection_possible = false;
668 void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, unsigned int button)
671 if (buffer_ && !bv_->the_locking_inset) {
672 if (screen_ && button == 1) {
673 screen_->HideCursor();
674 screen_->ToggleSelection(bv_->text);
675 bv_->text->SelectWord(bv_);
676 screen_->ToggleSelection(bv_->text, false);
677 /* This will fit the cursor on the screen
679 update(BufferView::SELECT|BufferView::FITCUR);
685 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, unsigned int button)
688 if (buffer_ && screen_ && button == 1) {
689 screen_->HideCursor();
690 screen_->ToggleSelection(bv_->text);
691 bv_->text->CursorHome(bv_);
692 bv_->text->sel_cursor = bv_->text->cursor;
693 bv_->text->CursorEnd(bv_);
694 bv_->text->SetSelection();
695 screen_->ToggleSelection(bv_->text, false);
696 /* This will fit the cursor on the screen
698 update(BufferView::SELECT|BufferView::FITCUR);
703 void BufferView::Pimpl::enterView()
705 if (active() && available()) {
706 SetXtermCursor(workarea_->getWin());
707 using_xterm_cursor = true;
712 void BufferView::Pimpl::leaveView()
714 if (using_xterm_cursor) {
715 XUndefineCursor(fl_display, workarea_->getWin());
716 using_xterm_cursor = false;
721 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
724 if (buffer_ == 0 || screen_ == 0) return;
726 // If we hit an inset, we have the inset coordinates in these
727 // and inset_hit points to the inset. If we do not hit an
728 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
729 Inset * inset_hit = checkInsetHit(bv_->text, x, y, button);
731 if (bv_->the_locking_inset) {
732 // We are in inset locking mode.
734 /* LyX does a kind of work-area grabbing for insets.
735 Only a ButtonPress Event outside the inset will
736 force a InsetUnlock. */
737 bv_->the_locking_inset->
738 InsetButtonRelease(bv_, x, y, button);
742 selection_possible = false;
744 if (bv_->text->cursor.par()->table) {
745 int cell = bv_->text->
746 NumberOfCell(bv_->text->cursor.par(),
747 bv_->text->cursor.pos());
748 if (bv_->text->cursor.par()->table->IsContRow(cell) &&
749 bv_->text->cursor.par()->table->
750 CellHasContRow(bv_->text->cursor.par()->table->
751 GetCellAbove(cell))<0) {
752 bv_->text->CursorUp(bv_);
757 if (button >= 2) return;
760 owner_->getMiniBuffer()->Set(CurrentState(bv_));
762 // Did we hit an editable inset?
763 if (inset_hit != 0) {
764 // Inset like error, notes and figures
765 selection_possible = false;
767 // CHECK fix this proper in 0.13
769 // Following a ref shouldn't issue
770 // a push on the undo-stack
771 // anylonger, now that we have
772 // keybindings for following
773 // references and returning from
774 // references. IMHO though, it
775 // should be the inset's own business
776 // to push or not push on the undo
777 // stack. They don't *have* to
778 // alter the document...
780 // ...or maybe the SetCursorParUndo()
781 // below isn't necessary at all anylonger?
782 if (inset_hit->LyxCode() == Inset::REF_CODE) {
783 bv_->text->SetCursorParUndo(bv_->buffer());
786 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
787 if (inset_hit->Editable()==Inset::HIGHLY_EDITABLE) {
788 // Highly editable inset, like math
789 UpdatableInset *inset = (UpdatableInset *)inset_hit;
790 inset->InsetButtonRelease(bv_, x, y, button);
792 inset_hit->InsetButtonRelease(bv_, x, y, button);
794 switch( inset_hit->LyxCode() ) {
795 case Inset::CITATION_CODE:
797 owner_->getDialogs()->
798 showCitation( (InsetCitation *)inset_hit );
802 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());
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){
826 } else if (bv_->text->cursor.pos() - 1 >= 0) {
827 c = bv_->text->cursor.par()->
828 GetChar(bv_->text->cursor.pos() - 1);
829 if (c == LyXParagraph::META_FOOTNOTE
830 || c == LyXParagraph::META_MARGIN
831 || c == LyXParagraph::META_FIG
832 || c == LyXParagraph::META_TAB
833 || c == LyXParagraph::META_WIDE_FIG
834 || c == LyXParagraph::META_WIDE_TAB
835 || c == LyXParagraph::META_ALGORITHM){
836 // We are one step too far to the right
837 bv_->text->CursorLeft(bv_);
843 selection_possible = false;
848 // Do we want to close a float? (click on the float-label)
849 if (bv_->text->cursor.row()->par()->footnoteflag ==
850 LyXParagraph::OPEN_FOOTNOTE
851 && bv_->text->cursor.row()->previous() &&
852 bv_->text->cursor.row()->previous()->par()->
853 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
854 LyXFont font(LyXFont::ALL_SANE);
855 font.setSize(LyXFont::SIZE_FOOTNOTE);
857 int box_x = 20; // LYX_PAPER_MARGIN;
858 box_x += lyxfont::width(" wide-tab ", font);
860 unsigned int screen_first = bv_->text->first;
863 && y + screen_first > bv_->text->cursor.y() -
864 bv_->text->cursor.row()->baseline()
865 && y + screen_first < bv_->text->cursor.y() -
866 bv_->text->cursor.row()->baseline()
867 + lyxfont::maxAscent(font) * 1.2 + lyxfont::maxDescent(font) * 1.2) {
869 selection_possible = false;
874 // Maybe we want to edit a bibitem ale970302
875 if (bv_->text->cursor.par()->bibkey && x < 20 +
876 bibitemMaxWidth(bv_, textclasslist.
878 params.textclass).defaultfont())) {
879 bv_->text->cursor.par()->bibkey->Edit(bv_, 0, 0, 0);
887 * Returns an inset if inset was hit. 0 otherwise.
888 * If hit, the coordinates are changed relative to the inset.
889 * Otherwise coordinates are not changed, and false is returned.
891 Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y,
892 unsigned int /* button */)
897 unsigned int y_tmp = y + text->first;
900 text->SetCursorFromCoordinates(bv_, cursor, x, y_tmp);
901 #if 0 // Are you planning to use this Jürgen? (Lgb)
902 bool move_cursor = ((cursor.par != text->cursor.par) ||
903 (cursor.pos != text->cursor.pos()));
905 if (cursor.pos() < cursor.par()->Last()
906 && cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
907 && cursor.par()->GetInset(cursor.pos())
908 && cursor.par()->GetInset(cursor.pos())->Editable()) {
910 // Check whether the inset really was hit
911 Inset * tmpinset = cursor.par()->GetInset(cursor.pos());
912 LyXFont font = text->GetFont(bv_->buffer(),
913 cursor.par(), cursor.pos());
914 bool is_rtl = font.isVisibleRightToLeft();
918 start_x = cursor.x() - tmpinset->width(bv_, font);
921 start_x = cursor.x();
922 end_x = cursor.x() + tmpinset->width(bv_, font);
925 if (x > start_x && x < end_x
926 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
927 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
929 if (move_cursor && (tmpinset != bv_->the_locking_inset))
931 text->SetCursor(bv_, cursor.par(),cursor.pos(),true);
933 // The origin of an inset is on the baseline
934 y = y_tmp - (text->cursor.y());
939 if ((cursor.pos() - 1 >= 0) &&
940 (cursor.par()->GetChar(cursor.pos()-1) == LyXParagraph::META_INSET) &&
941 (cursor.par()->GetInset(cursor.pos() - 1)) &&
942 (cursor.par()->GetInset(cursor.pos() - 1)->Editable())) {
943 Inset * tmpinset = cursor.par()->GetInset(cursor.pos()-1);
944 LyXFont font = text->GetFont(bv_->buffer(), cursor.par(),
946 bool is_rtl = font.isVisibleRightToLeft();
950 start_x = cursor.x() - tmpinset->width(bv_, font);
953 start_x = cursor.x();
954 end_x = cursor.x() + tmpinset->width(bv_, font);
956 if (x > start_x && x < end_x
957 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
958 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
960 if (move_cursor && (tmpinset != bv_->the_locking_inset))
962 text->SetCursor(bv_, cursor.par(),cursor.pos()-1,true);
964 // The origin of an inset is on the baseline
965 y = y_tmp - (text->cursor.y());
973 void BufferView::Pimpl::workAreaExpose()
975 // this is a hack to ensure that we only call this through
976 // BufferView::redraw().
981 static int work_area_width = 0;
982 static unsigned int work_area_height = 0;
984 bool widthChange = workarea_->workWidth() != work_area_width;
985 bool heightChange = workarea_->height() != work_area_height;
987 // update from work area
988 work_area_width = workarea_->workWidth();
989 work_area_height = workarea_->height();
992 // All buffers need a resize
995 // Remove all texts from the textcache
996 // This is not _really_ what we want to do. What
997 // we really want to do is to delete in textcache
998 // that does not have a BufferView with matching
999 // width, but as long as we have only one BufferView
1000 // deleting all gives the same result.
1001 if (lyxerr.debugging())
1002 textcache.show(lyxerr, "Expose delete all");
1004 } else if (heightChange) {
1005 // Rebuild image of current screen
1007 // fitCursor() ensures we don't jump back
1008 // to the start of the document on vertical
1012 // The main window size has changed, repaint most stuff
1014 // ...including the minibuffer
1015 owner_->getMiniBuffer()->Init();
1017 } else if (screen_) screen_->Redraw(bv_->text);
1019 // Grey box when we don't have a buffer
1020 workarea_->greyOut();
1023 // always make sure that the scrollbar is sane.
1025 owner_->updateLayoutChoice();
1030 void BufferView::Pimpl::update()
1032 if (screen_) screen_->Update(bv_->text);
1035 // Values used when calling update:
1037 // -2 - update, move sel_cursor if selection, fitcursor
1038 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
1039 // 0 - update, move sel_cursor if selection, fitcursor
1040 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
1041 // 3 - update, move sel_cursor if selection
1044 // a simple redraw of the parts that need refresh
1046 // move sel_cursor if selection -
1047 // the text's sel_cursor is moved if there is selection is progress
1050 // fitCursor() is called and the scrollbar updated
1053 // the buffer is marked dirty.
1062 // UPDATE_ONLY = UPDATE;
1063 // UPDATE_SELECT = UPDATE | SELECT;
1064 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
1065 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1067 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
1068 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
1069 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1070 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1071 // update(3) -> update(1) -> update(1) -> update(SELECT)
1073 void BufferView::Pimpl::update(BufferView::UpdateCodes f)
1075 owner_->updateLayoutChoice();
1077 if (!bv_->text->selection && (f & SELECT)) {
1078 bv_->text->sel_cursor = bv_->text->cursor;
1081 bv_->text->FullRebreak(bv_);
1090 if (buffer_->isLyxClean()) {
1091 buffer_->markDirty();
1092 owner_->getMiniBuffer()->setTimer(4);
1094 buffer_->markDirty();
1100 // Callback for cursor timer
1101 void BufferView::Pimpl::cursorToggle()
1103 // Quite a nice place for asyncron Inset updating, isn't it?
1104 // Actually no! This is run even if no buffer exist... so (Lgb)
1106 goto set_timer_and_return;
1110 // On my quest to solve the gs render hangups I am now
1111 // disabling the SIGHUP completely, and will do a wait
1112 // now and then instead. If the guess that xforms somehow
1113 // destroys something is true, this is likely (hopefully)
1114 // to solve the problem...at least I hope so. Lgb
1116 // ...Ok this seems to work...at least it does not make things
1117 // worse so far. However I still see gs processes that hangs.
1118 // I would really like to know _why_ they are hanging. Anyway
1119 // the solution without the SIGCHLD handler seems to be easier
1122 // When attaching gdb to a a running gs that hangs it shows
1123 // that it is waiting for input(?) Is it possible for us to
1124 // provide that input somehow? Or figure what it is expecing
1127 // One solution is to, after some time, look if there are some
1128 // old gs processes still running and if there are: kill them
1131 // Another solution is to provide the user an option to rerender
1132 // a picture. This would, for the picture in question, check if
1133 // there is a gs running for it, if so kill it, and start a new
1134 // rendering process.
1136 // these comments posted to lyx@via
1139 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1140 if (pid == -1) // error find out what is wrong
1141 ; // ignore it for now.
1143 sigchldhandler(pid, &status);
1146 updatelist.update(bv_);
1149 goto set_timer_and_return;
1152 if (!bv_->the_locking_inset) {
1153 screen_->CursorToggle(bv_->text);
1155 bv_->the_locking_inset->
1156 ToggleInsetCursor(bv_);
1159 set_timer_and_return:
1160 cursor_timeout.restart();
1165 void BufferView::Pimpl::cursorPrevious()
1167 if (!bv_->text->cursor.row()->previous()) return;
1169 long y = bv_->text->first;
1170 Row * cursorrow = bv_->text->cursor.row();
1171 bv_->text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y);
1172 bv_->text->FinishUndo();
1173 // This is to allow jumping over large insets
1174 if ((cursorrow == bv_->text->cursor.row()))
1175 bv_->text->CursorUp(bv_);
1177 if (bv_->text->cursor.row()->height() < workarea_->height())
1178 screen_->Draw(bv_->text,
1179 bv_->text->cursor.y()
1180 - bv_->text->cursor.row()->baseline()
1181 + bv_->text->cursor.row()->height()
1182 - workarea_->height() + 1 );
1187 void BufferView::Pimpl::cursorNext()
1189 if (!bv_->text->cursor.row()->next()) return;
1191 long y = bv_->text->first;
1192 bv_->text->GetRowNearY(y);
1193 Row * cursorrow = bv_->text->cursor.row();
1194 bv_->text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y
1195 + workarea_->height());
1196 bv_->text->FinishUndo();
1197 // This is to allow jumping over large insets
1198 if ((cursorrow == bv_->text->cursor.row()))
1199 bv_->text->CursorDown(bv_);
1201 if (bv_->text->cursor.row()->height() < workarea_->height())
1202 screen_->Draw(bv_->text, bv_->text->cursor.y()
1203 - bv_->text->cursor.row()->baseline());
1208 bool BufferView::Pimpl::available() const
1210 if (buffer_ && bv_->text) return true;
1215 void BufferView::Pimpl::beforeChange()
1218 bv_->text->ClearSelection();
1221 //owner_->update_timeout.stop();
1225 void BufferView::Pimpl::savePosition()
1227 backstack.push(buffer_->fileName(),
1228 bv_->text->cursor.x(),
1229 bv_->text->cursor.y());
1233 void BufferView::Pimpl::restorePosition()
1235 if (backstack.empty()) return;
1238 string fname = backstack.pop(&x, &y);
1241 Buffer * b = bufferlist.exists(fname) ?
1242 bufferlist.getBuffer(fname) :
1243 bufferlist.loadLyXFile(fname); // don't ask, just load it
1245 bv_->text->SetCursorFromCoordinates(bv_, x, y);
1246 update(BufferView::SELECT|BufferView::FITCUR);
1250 bool BufferView::Pimpl::NoSavedPositions()
1252 return backstack.empty();
1256 void BufferView::Pimpl::setState()
1258 if (!lyxrc.rtl_support)
1261 if (bv_->text->real_current_font.isRightToLeft() &&
1262 bv_->text->real_current_font.latex() != LyXFont::ON) {
1263 if (owner_->getIntl()->primarykeymap)
1264 owner_->getIntl()->KeyMapSec();
1266 if (!owner_->getIntl()->primarykeymap)
1267 owner_->getIntl()->KeyMapPrim();
1272 void BufferView::Pimpl::insetSleep()
1274 if (bv_->the_locking_inset && !bv_->inset_slept) {
1275 bv_->the_locking_inset->GetCursorPos(bv_, bv_->slx, bv_->sly);
1276 bv_->the_locking_inset->InsetUnlock(bv_);
1277 bv_->inset_slept = true;
1282 void BufferView::Pimpl::insetWakeup()
1284 if (bv_->the_locking_inset && bv_->inset_slept) {
1285 bv_->the_locking_inset->Edit(bv_, bv_->slx, bv_->sly, 0);
1286 bv_->inset_slept = false;
1291 void BufferView::Pimpl::insetUnlock()
1293 if (bv_->the_locking_inset) {
1294 if (!bv_->inset_slept) bv_->the_locking_inset->InsetUnlock(bv_);
1295 bv_->the_locking_inset = 0;
1296 bv_->text->FinishUndo();
1297 bv_->inset_slept = false;
1302 bool BufferView::Pimpl::focus() const
1304 return workarea_->hasFocus();
1308 void BufferView::Pimpl::focus(bool f)
1310 if (f) workarea_->setFocus();
1314 bool BufferView::Pimpl::active() const
1316 return workarea_->active();
1320 bool BufferView::Pimpl::belowMouse() const
1322 return workarea_->belowMouse();
1326 void BufferView::Pimpl::showCursor()
1329 screen_->ShowCursor(bv_->text);
1333 void BufferView::Pimpl::hideCursor()
1336 screen_->HideCursor();
1340 void BufferView::Pimpl::toggleSelection(bool b)
1343 screen_->ToggleSelection(bv_->text, b);
1347 void BufferView::Pimpl::toggleToggle()
1350 screen_->ToggleToggle(bv_->text);
1354 void BufferView::Pimpl::center()
1357 if (bv_->text->cursor.y() > workarea_->height() / 2) {
1358 screen_->Draw(bv_->text, bv_->text->cursor.y() - workarea_->height() / 2);
1360 screen_->Draw(bv_->text, 0);
1362 update(BufferView::SELECT|BufferView::FITCUR);
1367 void BufferView::Pimpl::pasteClipboard(bool asPara)
1369 if (buffer_ == 0) return;
1371 screen_->HideCursor();
1372 bv_->beforeChange();
1374 string clip(workarea_->getClipboard());
1376 if (clip.empty()) return;
1379 bv_->text->InsertStringB(bv_, clip);
1381 bv_->text->InsertStringA(bv_, clip);
1383 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1387 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
1389 workarea_->putClipboard(stuff);