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"
31 #ifdef SIGC_CXX_NAMESPACES
40 /* the selection possible is needed, that only motion events are
41 * used, where the bottom press event was on the drawing area too */
42 bool selection_possible = false;
44 extern BufferList bufferlist;
45 extern char ascii_type;
47 extern void sigchldhandler(pid_t pid, int * status);
48 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
54 XSync(fl_get_display(), 0);
59 void SetXtermCursor(Window win)
62 static bool cursor_undefined = true;
63 if (cursor_undefined){
64 cursor = XCreateFontCursor(fl_display, XC_xterm);
66 cursor_undefined = false;
68 XDefineCursor(fl_display, win, cursor);
73 BufferView::Pimpl::Pimpl(BufferView * b, LyXView * o,
74 int xpos, int ypos, int width, int height)
75 : bv_(b), owner_(o), cursor_timeout(400)
78 workarea_ = new WorkArea(bv_, xpos, ypos, width, height);
81 cursor_timeout.timeout.connect(slot(this,
82 &BufferView::Pimpl::cursorToggle));
83 current_scrollbar_value = 0;
84 cursor_timeout.start();
85 workarea_->setFocus();
86 using_xterm_cursor = false;
90 Painter & BufferView::Pimpl::painter()
92 return workarea_->getPainter();
96 void BufferView::Pimpl::buffer(Buffer * b)
98 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
102 buffer_->delUser(bv_);
104 // Put the old text into the TextCache, but
105 // only if the buffer is still loaded.
106 // Also set the owner of the test to 0
107 // bv_->text->owner(0);
108 textcache.add(buffer_, workarea_->workWidth(), bv_->text);
109 if (lyxerr.debugging())
110 textcache.show(lyxerr, "BufferView::buffer");
115 // Set current buffer
118 if (bufferlist.getState() == BufferList::CLOSING) return;
121 // screen is always deleted when the buffer is changed.
125 // If we are closing the buffer, use the first buffer as current
127 buffer_ = bufferlist.first();
131 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
132 buffer_->addUser(bv_);
133 owner_->getMenus()->showMenus();
134 // If we don't have a text object for this, we make one
135 if (bv_->text == 0) {
136 resizeCurrentBuffer();
141 bv_->text->first = screen_->TopCursorVisible(bv_->text);
143 owner_->getDialogs()->updateBufferDependent();
146 lyxerr[Debug::INFO] << " No Buffer!" << endl;
147 owner_->getMenus()->hideMenus();
151 // Also remove all remaining text's from the testcache.
152 // (there should not be any!) (if there is any it is a
154 if (lyxerr.debugging())
155 textcache.show(lyxerr, "buffer delete all");
158 // should update layoutchoice even if we don't have a buffer.
159 owner_->updateLayoutChoice();
160 owner_->getMiniBuffer()->Init();
161 owner_->updateWindowTitle();
165 void BufferView::Pimpl::resize(int xpos, int ypos, int width, int height)
167 workarea_->resize(xpos, ypos, width, height);
173 void BufferView::Pimpl::resize()
175 // This will resize the buffer. (Asger)
177 resizeCurrentBuffer();
181 void BufferView::Pimpl::redraw()
183 lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
188 bool BufferView::Pimpl::fitCursor()
190 Assert(screen_); // it is a programming error to call fitCursor
191 // without a valid screen.
192 bool ret = screen_->FitCursor(bv_->text);
193 if (ret) updateScrollbar();
198 void BufferView::Pimpl::redoCurrentBuffer()
200 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
201 if (buffer_ && bv_->text) {
203 owner_->updateLayoutChoice();
208 int BufferView::Pimpl::resizeCurrentBuffer()
210 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
212 LyXParagraph * par = 0;
213 LyXParagraph * selstartpar = 0;
214 LyXParagraph * selendpar = 0;
223 owner_->getMiniBuffer()->Set(_("Formatting document..."));
226 par = bv_->text->cursor.par();
227 pos = bv_->text->cursor.pos();
228 selstartpar = bv_->text->sel_start_cursor.par();
229 selstartpos = bv_->text->sel_start_cursor.pos();
230 selendpar = bv_->text->sel_end_cursor.par();
231 selendpos = bv_->text->sel_end_cursor.pos();
232 selection = bv_->text->selection;
233 mark_set = bv_->text->mark_set;
235 bv_->text = new LyXText(bv_);
237 // See if we have a text in TextCache that fits
238 // the new buffer_ with the correct width.
239 bv_->text = textcache.findFit(buffer_, workarea_->workWidth());
241 if (lyxerr.debugging()) {
242 lyxerr << "Found a LyXText that fits:\n";
243 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea_->workWidth(), bv_->text)));
245 // Set the owner of the newly found text
246 // bv_->text->owner(bv_);
247 if (lyxerr.debugging())
248 textcache.show(lyxerr, "resizeCurrentBuffer");
250 bv_->text = new LyXText(bv_);
256 bv_->text->selection = true;
257 /* at this point just to avoid the Delete-Empty-Paragraph
258 * Mechanism when setting the cursor */
259 bv_->text->mark_set = mark_set;
261 bv_->text->SetCursor(bv_, selstartpar, selstartpos);
262 bv_->text->sel_cursor = bv_->text->cursor;
263 bv_->text->SetCursor(bv_, selendpar, selendpos);
264 bv_->text->SetSelection();
265 bv_->text->SetCursor(bv_, par, pos);
267 bv_->text->SetCursor(bv_, par, pos);
268 bv_->text->sel_cursor = bv_->text->cursor;
269 bv_->text->selection = false;
272 bv_->text->first = screen_->TopCursorVisible(bv_->text);
273 buffer_->resizeInsets(bv_);
274 // this will scroll the screen such that the cursor becomes visible
277 owner_->getMiniBuffer()->Init();
281 // Now if the title form still exist kill it
288 void BufferView::Pimpl::gotoError()
293 screen_->HideCursor();
295 update(BufferView::SELECT|BufferView::FITCUR);
298 if (!bv_->text->GotoNextError(bv_)) {
299 if (bv_->text->cursor.pos()
300 || bv_->text->cursor.par() != bv_->text->FirstParagraph()) {
301 tmp = bv_->text->cursor;
302 bv_->text->cursor.par(bv_->text->FirstParagraph());
303 bv_->text->cursor.pos(0);
304 if (!bv_->text->GotoNextError(bv_)) {
305 bv_->text->cursor = tmp;
306 owner_->getMiniBuffer()
307 ->Set(_("No more errors"));
311 owner_->getMiniBuffer()->Set(_("No more errors"));
315 update(BufferView::SELECT|BufferView::FITCUR);
316 bv_->text->sel_cursor = bv_->text->cursor;
320 void BufferView::Pimpl::updateScreen()
322 // Regenerate the screen.
324 screen_ = new LyXScreen(*workarea_);
328 void BufferView::Pimpl::updateScrollbar()
330 /* If the text is smaller than the working area, the scrollbar
331 * maximum must be the working area height. No scrolling will
335 workarea_->setScrollbar(0, 1.0);
339 static unsigned long max2 = 0;
340 static unsigned long height2 = 0;
342 unsigned long cbth = 0;
346 cbth = bv_->text->height;
347 cbsf = bv_->text->first;
350 // check if anything has changed.
352 height2 == workarea_->height() &&
353 current_scrollbar_value == cbsf)
356 height2 = workarea_->height();
357 current_scrollbar_value = cbsf;
359 if (cbth <= height2) { // text is smaller than screen
360 workarea_->setScrollbar(0, 1.0); // right?
364 long maximum_height = workarea_->height() * 3 / 4 + cbth;
368 double hfloat = workarea_->height();
369 double maxfloat = maximum_height;
371 float slider_size = 0.0;
372 int slider_value = value;
374 workarea_->setScrollbarBounds(0, bv_->text->height - workarea_->height());
375 double lineh = bv_->text->DefaultHeight();
376 workarea_->setScrollbarIncrements(lineh);
377 if (maxfloat > 0.0) {
378 if ((hfloat / maxfloat) * float(height2) < 3)
379 slider_size = 3.0/float(height2);
381 slider_size = hfloat / maxfloat;
383 slider_size = hfloat;
385 workarea_->setScrollbar(slider_value, slider_size / workarea_->height());
389 // Callback for scrollbar slider
390 void BufferView::Pimpl::scrollCB(double value)
392 extern bool cursor_follows_scrollbar;
394 if (buffer_ == 0) return;
396 current_scrollbar_value = long(value);
397 if (current_scrollbar_value < 0)
398 current_scrollbar_value = 0;
403 screen_->Draw(bv_->text, current_scrollbar_value);
405 if (cursor_follows_scrollbar) {
406 LyXText * vbt = bv_->text;
407 unsigned int height = vbt->DefaultHeight();
409 if (vbt->cursor.y() < bv_->text->first + height) {
410 vbt->SetCursorFromCoordinates(bv_, 0,
413 } else if (vbt->cursor.y() >
414 bv_->text->first + workarea_->height() - height) {
415 vbt->SetCursorFromCoordinates(bv_, 0,
417 workarea_->height() -
425 int BufferView::Pimpl::scrollUp(long time)
427 if (buffer_ == 0) return 0;
428 if (!screen_) return 0;
430 double value = workarea_->getScrollbarValue();
432 if (value == 0) return 0;
434 float add_value = (bv_->text->DefaultHeight()
435 + float(time) * float(time) * 0.125);
437 if (add_value > workarea_->height())
438 add_value = float(workarea_->height() -
439 bv_->text->DefaultHeight());
446 workarea_->setScrollbarValue(value);
448 bv_->scrollCB(value);
453 int BufferView::Pimpl::scrollDown(long time)
455 if (buffer_ == 0) return 0;
456 if (!screen_) return 0;
458 double value= workarea_->getScrollbarValue();
459 pair<float, float> p = workarea_->getScrollbarBounds();
460 double max = p.second;
462 if (value == max) return 0;
464 float add_value = (bv_->text->DefaultHeight()
465 + float(time) * float(time) * 0.125);
467 if (add_value > workarea_->height())
468 add_value = float(workarea_->height() -
469 bv_->text->DefaultHeight());
476 workarea_->setScrollbarValue(value);
478 bv_->scrollCB(value);
483 void BufferView::Pimpl::workAreaMotionNotify(int x, int y, unsigned int state)
485 // Only use motion with button 1
486 if (!(state & Button1MotionMask))
489 if (buffer_ == 0 || !screen_) return;
491 // Check for inset locking
492 if (bv_->the_locking_inset) {
493 LyXCursor cursor = bv_->text->cursor;
494 bv_->the_locking_inset->
495 InsetMotionNotify(bv_,
497 y - cursor.y() + bv_->text->first,
502 /* The selection possible is needed, that only motion events are
503 * used, where the bottom press event was on the drawing area too */
504 if (selection_possible) {
505 screen_->HideCursor();
507 bv_->text->SetCursorFromCoordinates(bv_, x, y + bv_->text->first);
509 if (!bv_->text->selection)
510 update(BufferView::UPDATE); // Maybe an empty line was deleted
512 bv_->text->SetSelection();
513 screen_->ToggleToggle(bv_->text);
515 screen_->ShowCursor(bv_->text);
521 // Single-click on work area
522 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
528 if (buffer_ == 0 || !screen_) return;
530 Inset * inset_hit = checkInsetHit(bv_->text, xpos, ypos, button);
532 // ok ok, this is a hack.
533 if (button == 4 || button == 5) {
536 scrollUp(lyxrc.wheel_jump); // default 100, set in lyxrc
539 scrollDown(lyxrc.wheel_jump);
544 if (bv_->the_locking_inset) {
545 // We are in inset locking mode
547 /* Check whether the inset was hit. If not reset mode,
548 otherwise give the event to the inset */
549 if (inset_hit == bv_->the_locking_inset) {
550 bv_->the_locking_inset->
551 InsetButtonPress(bv_,
556 bv_->unlockInset(bv_->the_locking_inset);
561 selection_possible = true;
562 screen_->HideCursor();
565 // Right button mouse click on a table
567 (bv_->text->cursor.par()->table ||
568 bv_->text->MouseHitInTable(bv_, xpos, ypos + bv_->text->first))) {
569 // Set the cursor to the press-position
570 bv_->text->SetCursorFromCoordinates(bv_, xpos, ypos + bv_->text->first);
573 // Only show the table popup if the hit is in
575 if (!bv_->text->HitInTable(bv_,
576 bv_->text->cursor.row(), xpos))
579 // Hit above or below the table?
581 if (!bv_->text->selection) {
582 screen_->ToggleSelection(bv_->text);
583 bv_->text->ClearSelection();
584 bv_->text->FullRebreak(bv_);
585 screen_->Update(bv_->text);
588 // Popup table popup when on a table.
589 // This is obviously temporary, since we
590 // should be able to popup various
591 // context-sensitive-menus with the
592 // the right mouse. So this should be done more
593 // general in the future. Matthias.
594 selection_possible = false;
596 ->Dispatch(LFUN_LAYOUT_TABLE,
603 int screen_first = bv_->text->first;
605 // Middle button press pastes if we have a selection
606 bool paste_internally = false;
608 && bv_->text->selection) {
609 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
610 paste_internally = true;
613 // Clear the selection
614 screen_->ToggleSelection(bv_->text);
615 bv_->text->ClearSelection();
616 bv_->text->FullRebreak(bv_);
617 screen_->Update(bv_->text);
620 // Single left click in math inset?
621 if ((inset_hit != 0) &&
622 (inset_hit->Editable()==Inset::HIGHLY_EDITABLE)) {
623 // Highly editable inset, like math
624 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
625 selection_possible = false;
626 owner_->updateLayoutChoice();
627 owner_->getMiniBuffer()->Set(inset->EditMessage());
628 inset->InsetButtonPress(bv_, xpos, ypos, button);
629 inset->Edit(bv_, xpos, ypos, button);
633 // Right click on a footnote flag opens float menu
635 selection_possible = false;
639 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
640 bv_->text->SetCursorFromCoordinates(bv_, xpos, ypos + screen_first);
641 bv_->text->FinishUndo();
642 bv_->text->sel_cursor = bv_->text->cursor;
643 bv_->text->cursor.x_fix(bv_->text->cursor.x());
645 owner_->updateLayoutChoice();
647 selection_possible = false;
650 // Insert primary selection with middle mouse
651 // if there is a local selection in the current buffer,
654 if (paste_internally)
655 owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
657 owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
659 selection_possible = false;
665 void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, unsigned int button)
668 if (buffer_ && !bv_->the_locking_inset) {
669 if (screen_ && button == 1) {
670 screen_->HideCursor();
671 screen_->ToggleSelection(bv_->text);
672 bv_->text->SelectWord(bv_);
673 screen_->ToggleSelection(bv_->text, false);
674 /* This will fit the cursor on the screen
676 update(BufferView::SELECT|BufferView::FITCUR);
682 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, unsigned int button)
685 if (buffer_ && screen_ && button == 1) {
686 screen_->HideCursor();
687 screen_->ToggleSelection(bv_->text);
688 bv_->text->CursorHome(bv_);
689 bv_->text->sel_cursor = bv_->text->cursor;
690 bv_->text->CursorEnd(bv_);
691 bv_->text->SetSelection();
692 screen_->ToggleSelection(bv_->text, false);
693 /* This will fit the cursor on the screen
695 update(BufferView::SELECT|BufferView::FITCUR);
700 void BufferView::Pimpl::enterView()
702 if (active() && available()) {
703 SetXtermCursor(workarea_->getWin());
704 using_xterm_cursor = true;
709 void BufferView::Pimpl::leaveView()
711 if (using_xterm_cursor) {
712 XUndefineCursor(fl_display, workarea_->getWin());
713 using_xterm_cursor = false;
718 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
721 if (buffer_ == 0 || screen_ == 0) return;
723 // If we hit an inset, we have the inset coordinates in these
724 // and inset_hit points to the inset. If we do not hit an
725 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
726 Inset * inset_hit = checkInsetHit(bv_->text, x, y, button);
728 if (bv_->the_locking_inset) {
729 // We are in inset locking mode.
731 /* LyX does a kind of work-area grabbing for insets.
732 Only a ButtonPress Event outside the inset will
733 force a InsetUnlock. */
734 bv_->the_locking_inset->
735 InsetButtonRelease(bv_, x, y, button);
739 selection_possible = false;
741 if (bv_->text->cursor.par()->table) {
742 int cell = bv_->text->
743 NumberOfCell(bv_->text->cursor.par(),
744 bv_->text->cursor.pos());
745 if (bv_->text->cursor.par()->table->IsContRow(cell) &&
746 bv_->text->cursor.par()->table->
747 CellHasContRow(bv_->text->cursor.par()->table->
748 GetCellAbove(cell))<0) {
749 bv_->text->CursorUp(bv_);
754 if (button >= 2) return;
757 owner_->getMiniBuffer()->Set(CurrentState(bv_));
759 // Did we hit an editable inset?
760 if (inset_hit != 0) {
761 // Inset like error, notes and figures
762 selection_possible = false;
764 // CHECK fix this proper in 0.13
766 // Following a ref shouldn't issue
767 // a push on the undo-stack
768 // anylonger, now that we have
769 // keybindings for following
770 // references and returning from
771 // references. IMHO though, it
772 // should be the inset's own business
773 // to push or not push on the undo
774 // stack. They don't *have* to
775 // alter the document...
777 // ...or maybe the SetCursorParUndo()
778 // below isn't necessary at all anylonger?
779 if (inset_hit->LyxCode() == Inset::REF_CODE) {
780 bv_->text->SetCursorParUndo(bv_->buffer());
783 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
784 if (inset_hit->Editable()==Inset::HIGHLY_EDITABLE) {
785 // Highly editable inset, like math
786 UpdatableInset *inset = (UpdatableInset *)inset_hit;
787 inset->InsetButtonRelease(bv_, x, y, button);
789 inset_hit->InsetButtonRelease(bv_, x, y, button);
790 inset_hit->Edit(bv_, x, y, button);
795 // check whether we want to open a float
799 if (bv_->text->cursor.pos() <
800 bv_->text->cursor.par()->Last()) {
801 c = bv_->text->cursor.par()->
802 GetChar(bv_->text->cursor.pos());
805 if (c == LyXParagraph::META_FOOTNOTE
806 || c == LyXParagraph::META_MARGIN
807 || c == LyXParagraph::META_FIG
808 || c == LyXParagraph::META_TAB
809 || c == LyXParagraph::META_WIDE_FIG
810 || c == LyXParagraph::META_WIDE_TAB
811 || c == LyXParagraph::META_ALGORITHM){
815 if (bv_->text->cursor.pos() - 1 >= 0) {
816 c = bv_->text->cursor.par()->
817 GetChar(bv_->text->cursor.pos() - 1);
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){
826 // We are one step too far to the right
827 bv_->text->CursorLeft(bv_);
836 selection_possible = false;
842 // Do we want to close a float? (click on the float-label)
843 if (bv_->text->cursor.row()->par()->footnoteflag ==
844 LyXParagraph::OPEN_FOOTNOTE
845 && bv_->text->cursor.row()->previous() &&
846 bv_->text->cursor.row()->previous()->par()->
847 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
848 LyXFont font(LyXFont::ALL_SANE);
849 font.setSize(LyXFont::SIZE_FOOTNOTE);
851 int box_x = 20; // LYX_PAPER_MARGIN;
852 box_x += lyxfont::width(" wide-tab ", font);
854 unsigned int screen_first = bv_->text->first;
857 && y + screen_first > bv_->text->cursor.y() -
858 bv_->text->cursor.row()->baseline()
859 && y + screen_first < bv_->text->cursor.y() -
860 bv_->text->cursor.row()->baseline()
861 + lyxfont::maxAscent(font) * 1.2 + lyxfont::maxDescent(font) * 1.2) {
863 selection_possible = false;
869 // Maybe we want to edit a bibitem ale970302
870 if (bv_->text->cursor.par()->bibkey && x < 20 +
871 bibitemMaxWidth(bv_, textclasslist.
873 params.textclass).defaultfont())) {
874 bv_->text->cursor.par()->bibkey->Edit(bv_, 0, 0, 0);
882 * Returns an inset if inset was hit. 0 otherwise.
883 * If hit, the coordinates are changed relative to the inset.
884 * Otherwise coordinates are not changed, and false is returned.
886 Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y,
887 unsigned int /* button */)
892 unsigned int y_tmp = y + text->first;
895 text->SetCursorFromCoordinates(bv_, cursor, x, y_tmp);
896 #if 0 // Are you planning to use this Jürgen? (Lgb)
897 bool move_cursor = ((cursor.par != text->cursor.par) ||
898 (cursor.pos != text->cursor.pos()));
900 if (cursor.pos() < cursor.par()->Last()
901 && cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
902 && cursor.par()->GetInset(cursor.pos())
903 && cursor.par()->GetInset(cursor.pos())->Editable()) {
905 // Check whether the inset really was hit
906 Inset * tmpinset = cursor.par()->GetInset(cursor.pos());
907 LyXFont font = text->GetFont(bv_->buffer(),
908 cursor.par(), cursor.pos());
909 bool is_rtl = font.isVisibleRightToLeft();
913 start_x = cursor.x() - tmpinset->width(bv_, font);
916 start_x = cursor.x();
917 end_x = cursor.x() + tmpinset->width(bv_, font);
920 if (x > start_x && x < end_x
921 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
922 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
924 if (move_cursor && (tmpinset != bv_->the_locking_inset))
926 text->SetCursor(bv_, cursor.par(),cursor.pos(),true);
928 // The origin of an inset is on the baseline
929 y = y_tmp - (text->cursor.y());
934 if ((cursor.pos() - 1 >= 0) &&
935 (cursor.par()->GetChar(cursor.pos()-1) == LyXParagraph::META_INSET) &&
936 (cursor.par()->GetInset(cursor.pos() - 1)) &&
937 (cursor.par()->GetInset(cursor.pos() - 1)->Editable())) {
938 Inset * tmpinset = cursor.par()->GetInset(cursor.pos()-1);
939 LyXFont font = text->GetFont(bv_->buffer(), cursor.par(),
941 bool is_rtl = font.isVisibleRightToLeft();
945 start_x = cursor.x() - tmpinset->width(bv_, font);
948 start_x = cursor.x();
949 end_x = cursor.x() + tmpinset->width(bv_, font);
951 if (x > start_x && x < end_x
952 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
953 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
955 if (move_cursor && (tmpinset != bv_->the_locking_inset))
957 text->SetCursor(bv_, cursor.par(),cursor.pos()-1,true);
959 // The origin of an inset is on the baseline
960 y = y_tmp - (text->cursor.y());
968 void BufferView::Pimpl::workAreaExpose()
970 // this is a hack to ensure that we only call this through
971 // BufferView::redraw().
976 static int work_area_width = 0;
977 static unsigned int work_area_height = 0;
979 bool widthChange = workarea_->workWidth() != work_area_width;
980 bool heightChange = workarea_->height() != work_area_height;
982 // update from work area
983 work_area_width = workarea_->workWidth();
984 work_area_height = workarea_->height();
987 // All buffers need a resize
990 // Remove all texts from the textcache
991 // This is not _really_ what we want to do. What
992 // we really want to do is to delete in textcache
993 // that does not have a BufferView with matching
994 // width, but as long as we have only one BufferView
995 // deleting all gives the same result.
996 if (lyxerr.debugging())
997 textcache.show(lyxerr, "Expose delete all");
999 } else if (heightChange) {
1000 // Rebuild image of current screen
1002 // fitCursor() ensures we don't jump back
1003 // to the start of the document on vertical
1007 // The main window size has changed, repaint most stuff
1009 // ...including the minibuffer
1010 owner_->getMiniBuffer()->Init();
1012 } else if (screen_) screen_->Redraw(bv_->text);
1014 // Grey box when we don't have a buffer
1015 workarea_->greyOut();
1018 // always make sure that the scrollbar is sane.
1020 owner_->updateLayoutChoice();
1025 void BufferView::Pimpl::update()
1027 if (screen_) screen_->Update(bv_->text);
1030 // Values used when calling update:
1032 // -2 - update, move sel_cursor if selection, fitcursor
1033 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
1034 // 0 - update, move sel_cursor if selection, fitcursor
1035 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
1036 // 3 - update, move sel_cursor if selection
1039 // a simple redraw of the parts that need refresh
1041 // move sel_cursor if selection -
1042 // the text's sel_cursor is moved if there is selection is progress
1045 // fitCursor() is called and the scrollbar updated
1048 // the buffer is marked dirty.
1057 // UPDATE_ONLY = UPDATE;
1058 // UPDATE_SELECT = UPDATE | SELECT;
1059 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
1060 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1062 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
1063 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
1064 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1065 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1066 // update(3) -> update(1) -> update(1) -> update(SELECT)
1068 void BufferView::Pimpl::update(BufferView::UpdateCodes f)
1070 owner_->updateLayoutChoice();
1072 if (!bv_->text->selection && (f & SELECT)) {
1073 bv_->text->sel_cursor = bv_->text->cursor;
1076 bv_->text->FullRebreak(bv_);
1085 if (buffer_->isLyxClean()) {
1086 buffer_->markDirty();
1087 owner_->getMiniBuffer()->setTimer(4);
1089 buffer_->markDirty();
1095 // Callback for cursor timer
1096 void BufferView::Pimpl::cursorToggle()
1098 // Quite a nice place for asyncron Inset updating, isn't it?
1099 // Actually no! This is run even if no buffer exist... so (Lgb)
1101 goto set_timer_and_return;
1105 // On my quest to solve the gs render hangups I am now
1106 // disabling the SIGHUP completely, and will do a wait
1107 // now and then instead. If the guess that xforms somehow
1108 // destroys something is true, this is likely (hopefully)
1109 // to solve the problem...at least I hope so. Lgb
1111 // ...Ok this seems to work...at least it does not make things
1112 // worse so far. However I still see gs processes that hangs.
1113 // I would really like to know _why_ they are hanging. Anyway
1114 // the solution without the SIGCHLD handler seems to be easier
1117 // When attaching gdb to a a running gs that hangs it shows
1118 // that it is waiting for input(?) Is it possible for us to
1119 // provide that input somehow? Or figure what it is expecing
1122 // One solution is to, after some time, look if there are some
1123 // old gs processes still running and if there are: kill them
1126 // Another solution is to provide the user an option to rerender
1127 // a picture. This would, for the picture in question, check if
1128 // there is a gs running for it, if so kill it, and start a new
1129 // rendering process.
1131 // these comments posted to lyx@via
1134 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1135 if (pid == -1) // error find out what is wrong
1136 ; // ignore it for now.
1138 sigchldhandler(pid, &status);
1141 updatelist.update(bv_);
1144 goto set_timer_and_return;
1147 if (!bv_->the_locking_inset) {
1148 screen_->CursorToggle(bv_->text);
1150 bv_->the_locking_inset->
1151 ToggleInsetCursor(bv_);
1154 set_timer_and_return:
1155 cursor_timeout.restart();
1160 void BufferView::Pimpl::cursorPrevious()
1162 if (!bv_->text->cursor.row()->previous()) return;
1164 long y = bv_->text->first;
1165 Row * cursorrow = bv_->text->cursor.row();
1166 bv_->text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y);
1167 bv_->text->FinishUndo();
1168 // This is to allow jumping over large insets
1169 if ((cursorrow == bv_->text->cursor.row()))
1170 bv_->text->CursorUp(bv_);
1172 if (bv_->text->cursor.row()->height() < workarea_->height())
1173 screen_->Draw(bv_->text,
1174 bv_->text->cursor.y()
1175 - bv_->text->cursor.row()->baseline()
1176 + bv_->text->cursor.row()->height()
1177 - workarea_->height() + 1 );
1182 void BufferView::Pimpl::cursorNext()
1184 if (!bv_->text->cursor.row()->next()) return;
1186 long y = bv_->text->first;
1187 bv_->text->GetRowNearY(y);
1188 Row * cursorrow = bv_->text->cursor.row();
1189 bv_->text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y
1190 + workarea_->height());
1191 bv_->text->FinishUndo();
1192 // This is to allow jumping over large insets
1193 if ((cursorrow == bv_->text->cursor.row()))
1194 bv_->text->CursorDown(bv_);
1196 if (bv_->text->cursor.row()->height() < workarea_->height())
1197 screen_->Draw(bv_->text, bv_->text->cursor.y()
1198 - bv_->text->cursor.row()->baseline());
1203 bool BufferView::Pimpl::available() const
1205 if (buffer_ && bv_->text) return true;
1210 void BufferView::Pimpl::beforeChange()
1213 bv_->text->ClearSelection();
1216 //owner_->update_timeout.stop();
1220 void BufferView::Pimpl::savePosition()
1222 backstack.push(buffer_->fileName(),
1223 bv_->text->cursor.x(),
1224 bv_->text->cursor.y());
1228 void BufferView::Pimpl::restorePosition()
1230 if (backstack.empty()) return;
1233 string fname = backstack.pop(&x, &y);
1236 Buffer * b = bufferlist.exists(fname) ?
1237 bufferlist.getBuffer(fname) :
1238 bufferlist.loadLyXFile(fname); // don't ask, just load it
1240 bv_->text->SetCursorFromCoordinates(bv_, x, y);
1241 update(BufferView::SELECT|BufferView::FITCUR);
1245 bool BufferView::Pimpl::NoSavedPositions()
1247 return backstack.empty();
1251 void BufferView::Pimpl::setState()
1253 if (!lyxrc.rtl_support)
1256 if (bv_->text->real_current_font.isRightToLeft() &&
1257 bv_->text->real_current_font.latex() != LyXFont::ON) {
1258 if (owner_->getIntl()->primarykeymap)
1259 owner_->getIntl()->KeyMapSec();
1261 if (!owner_->getIntl()->primarykeymap)
1262 owner_->getIntl()->KeyMapPrim();
1267 void BufferView::Pimpl::insetSleep()
1269 if (bv_->the_locking_inset && !bv_->inset_slept) {
1270 bv_->the_locking_inset->GetCursorPos(bv_, bv_->slx, bv_->sly);
1271 bv_->the_locking_inset->InsetUnlock(bv_);
1272 bv_->inset_slept = true;
1277 void BufferView::Pimpl::insetWakeup()
1279 if (bv_->the_locking_inset && bv_->inset_slept) {
1280 bv_->the_locking_inset->Edit(bv_, bv_->slx, bv_->sly, 0);
1281 bv_->inset_slept = false;
1286 void BufferView::Pimpl::insetUnlock()
1288 if (bv_->the_locking_inset) {
1289 if (!bv_->inset_slept) bv_->the_locking_inset->InsetUnlock(bv_);
1290 bv_->the_locking_inset = 0;
1291 bv_->text->FinishUndo();
1292 bv_->inset_slept = false;
1297 bool BufferView::Pimpl::focus() const
1299 return workarea_->hasFocus();
1303 void BufferView::Pimpl::focus(bool f)
1305 if (f) workarea_->setFocus();
1309 bool BufferView::Pimpl::active() const
1311 return workarea_->active();
1315 bool BufferView::Pimpl::belowMouse() const
1317 return workarea_->belowMouse();
1321 void BufferView::Pimpl::showCursor()
1324 screen_->ShowCursor(bv_->text);
1328 void BufferView::Pimpl::hideCursor()
1331 screen_->HideCursor();
1335 void BufferView::Pimpl::toggleSelection(bool b)
1338 screen_->ToggleSelection(bv_->text, b);
1342 void BufferView::Pimpl::toggleToggle()
1345 screen_->ToggleToggle(bv_->text);
1349 void BufferView::Pimpl::center()
1352 if (bv_->text->cursor.y() > workarea_->height() / 2) {
1353 screen_->Draw(bv_->text, bv_->text->cursor.y() - workarea_->height() / 2);
1355 screen_->Draw(bv_->text, 0);
1357 update(BufferView::SELECT|BufferView::FITCUR);
1362 void BufferView::Pimpl::pasteClipboard(bool asPara)
1364 if (buffer_ == 0) return;
1366 screen_->HideCursor();
1367 bv_->beforeChange();
1369 string clip(workarea_->getClipboard());
1371 if (clip.empty()) return;
1374 bv_->text->InsertStringB(bv_, clip);
1376 bv_->text->InsertStringA(bv_, clip);
1378 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1382 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
1384 workarea_->putClipboard(stuff);