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(Painter &, 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 work_area_focus = true;
88 using_xterm_cursor = false;
92 Painter & BufferView::Pimpl::painter()
94 return workarea_->getPainter();
98 void BufferView::Pimpl::buffer(Buffer * b)
100 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
104 buffer_->delUser(bv_);
106 // Put the old text into the TextCache, but
107 // only if the buffer is still loaded.
108 // Also set the owner of the test to 0
109 // bv_->text->owner(0);
110 textcache.add(buffer_, workarea_->workWidth(), bv_->text);
111 if (lyxerr.debugging())
112 textcache.show(lyxerr, "BufferView::buffer");
117 // Set current buffer
120 if (bufferlist.getState() == BufferList::CLOSING) return;
123 // screen is always deleted when the buffer is changed.
127 // If we are closing the buffer, use the first buffer as current
129 buffer_ = bufferlist.first();
133 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
134 buffer_->addUser(bv_);
135 owner_->getMenus()->showMenus();
136 // If we don't have a text object for this, we make one
137 if (bv_->text == 0) {
138 resizeCurrentBuffer();
143 screen_->first = screen_->TopCursorVisible(bv_->text);
145 owner_->getDialogs()->updateBufferDependent();
148 lyxerr[Debug::INFO] << " No Buffer!" << endl;
149 owner_->getMenus()->hideMenus();
153 // Also remove all remaining text's from the testcache.
154 // (there should not be any!) (if there is any it is a
156 if (lyxerr.debugging())
157 textcache.show(lyxerr, "buffer delete all");
160 // should update layoutchoice even if we don't have a buffer.
161 owner_->updateLayoutChoice();
162 owner_->getMiniBuffer()->Init();
163 owner_->updateWindowTitle();
167 void BufferView::Pimpl::resize(int xpos, int ypos, int width, int height)
169 workarea_->resize(xpos, ypos, width, height);
175 void BufferView::Pimpl::resize()
177 // This will resize the buffer. (Asger)
179 resizeCurrentBuffer();
183 void BufferView::Pimpl::redraw()
185 lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
190 bool BufferView::Pimpl::fitCursor()
192 Assert(screen_); // it is a programming error to call fitCursor
193 // without a valid screen.
194 bool ret = screen_->FitCursor(bv_->text);
195 if (ret) updateScrollbar();
200 void BufferView::Pimpl::redoCurrentBuffer()
202 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
203 if (buffer_ && bv_->text) {
205 owner_->updateLayoutChoice();
210 int BufferView::Pimpl::resizeCurrentBuffer()
212 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
214 LyXParagraph * par = 0;
215 LyXParagraph * selstartpar = 0;
216 LyXParagraph * selendpar = 0;
225 owner_->getMiniBuffer()->Set(_("Formatting document..."));
228 par = bv_->text->cursor.par();
229 pos = bv_->text->cursor.pos();
230 selstartpar = bv_->text->sel_start_cursor.par();
231 selstartpos = bv_->text->sel_start_cursor.pos();
232 selendpar = bv_->text->sel_end_cursor.par();
233 selendpos = bv_->text->sel_end_cursor.pos();
234 selection = bv_->text->selection;
235 mark_set = bv_->text->mark_set;
237 bv_->text = new LyXText(bv_);
239 // See if we have a text in TextCache that fits
240 // the new buffer_ with the correct width.
241 bv_->text = textcache.findFit(buffer_, workarea_->workWidth());
243 if (lyxerr.debugging()) {
244 lyxerr << "Found a LyXText that fits:\n";
245 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea_->workWidth(), bv_->text)));
247 // Set the owner of the newly found text
248 // bv_->text->owner(bv_);
249 if (lyxerr.debugging())
250 textcache.show(lyxerr, "resizeCurrentBuffer");
252 bv_->text = new LyXText(bv_);
258 bv_->text->selection = true;
259 /* at this point just to avoid the Delete-Empty-Paragraph
260 * Mechanism when setting the cursor */
261 bv_->text->mark_set = mark_set;
263 bv_->text->SetCursor(bv_, selstartpar, selstartpos);
264 bv_->text->sel_cursor = bv_->text->cursor;
265 bv_->text->SetCursor(bv_, selendpar, selendpos);
266 bv_->text->SetSelection();
267 bv_->text->SetCursor(bv_, par, pos);
269 bv_->text->SetCursor(bv_, par, pos);
270 bv_->text->sel_cursor = bv_->text->cursor;
271 bv_->text->selection = false;
274 screen_->first = screen_->TopCursorVisible(bv_->text);
275 /* this will scroll the
276 * screen such that the
281 owner_->getMiniBuffer()->Init();
285 // Now if the title form still exist kill it
292 void BufferView::Pimpl::gotoError()
297 screen_->HideCursor();
299 update(BufferView::SELECT|BufferView::FITCUR);
302 if (!bv_->text->GotoNextError(bv_)) {
303 if (bv_->text->cursor.pos()
304 || bv_->text->cursor.par() != bv_->text->FirstParagraph()) {
305 tmp = bv_->text->cursor;
306 bv_->text->cursor.par(bv_->text->FirstParagraph());
307 bv_->text->cursor.pos(0);
308 if (!bv_->text->GotoNextError(bv_)) {
309 bv_->text->cursor = tmp;
310 owner_->getMiniBuffer()
311 ->Set(_("No more errors"));
315 owner_->getMiniBuffer()->Set(_("No more errors"));
319 update(BufferView::SELECT|BufferView::FITCUR);
320 bv_->text->sel_cursor = bv_->text->cursor;
324 void BufferView::Pimpl::updateScreen()
326 // Regenerate the screen.
328 screen_ = new LyXScreen(*workarea_);
332 void BufferView::Pimpl::updateScrollbar()
334 /* If the text is smaller than the working area, the scrollbar
335 * maximum must be the working area height. No scrolling will
339 workarea_->setScrollbar(0, 1.0);
343 static unsigned long max2 = 0;
344 static unsigned long height2 = 0;
346 unsigned long cbth = 0;
350 cbth = bv_->text->height;
352 cbsf = screen_->first;
354 // check if anything has changed.
356 height2 == workarea_->height() &&
357 current_scrollbar_value == cbsf)
360 height2 = workarea_->height();
361 current_scrollbar_value = cbsf;
363 if (cbth <= height2) { // text is smaller than screen
364 workarea_->setScrollbar(0, 1.0); // right?
368 long maximum_height = workarea_->height() * 3 / 4 + cbth;
372 double hfloat = workarea_->height();
373 double maxfloat = maximum_height;
375 float slider_size = 0.0;
376 int slider_value = value;
378 workarea_->setScrollbarBounds(0, bv_->text->height - workarea_->height());
379 double lineh = bv_->text->DefaultHeight();
380 workarea_->setScrollbarIncrements(lineh);
381 if (maxfloat > 0.0) {
382 if ((hfloat / maxfloat) * float(height2) < 3)
383 slider_size = 3.0/float(height2);
385 slider_size = hfloat / maxfloat;
387 slider_size = hfloat;
389 workarea_->setScrollbar(slider_value, slider_size / workarea_->height());
393 // Callback for scrollbar slider
394 void BufferView::Pimpl::scrollCB(double value)
396 extern bool cursor_follows_scrollbar;
398 if (buffer_ == 0) return;
400 current_scrollbar_value = long(value);
401 if (current_scrollbar_value < 0)
402 current_scrollbar_value = 0;
407 screen_->Draw(bv_->text, current_scrollbar_value);
409 if (cursor_follows_scrollbar) {
410 LyXText * vbt = bv_->text;
411 unsigned int height = vbt->DefaultHeight();
413 if (vbt->cursor.y() < screen_->first + height) {
414 vbt->SetCursorFromCoordinates(bv_, 0,
417 } else if (vbt->cursor.y() >
418 screen_->first + workarea_->height() - height) {
419 vbt->SetCursorFromCoordinates(bv_, 0,
421 workarea_->height() -
429 int BufferView::Pimpl::scrollUp(long time)
431 if (buffer_ == 0) return 0;
432 if (!screen_) return 0;
434 double value = workarea_->getScrollbarValue();
436 if (value == 0) return 0;
438 float add_value = (bv_->text->DefaultHeight()
439 + float(time) * float(time) * 0.125);
441 if (add_value > workarea_->height())
442 add_value = float(workarea_->height() -
443 bv_->text->DefaultHeight());
450 workarea_->setScrollbarValue(value);
452 bv_->scrollCB(value);
457 int BufferView::Pimpl::scrollDown(long time)
459 if (buffer_ == 0) return 0;
460 if (!screen_) return 0;
462 double value= workarea_->getScrollbarValue();
463 pair<float, float> p = workarea_->getScrollbarBounds();
464 double max = p.second;
466 if (value == max) return 0;
468 float add_value = (bv_->text->DefaultHeight()
469 + float(time) * float(time) * 0.125);
471 if (add_value > workarea_->height())
472 add_value = float(workarea_->height() -
473 bv_->text->DefaultHeight());
480 workarea_->setScrollbarValue(value);
482 bv_->scrollCB(value);
487 void BufferView::Pimpl::workAreaMotionNotify(int x, int y, unsigned int state)
489 // Only use motion with button 1
490 if (!(state & Button1MotionMask))
493 if (buffer_ == 0 || !screen_) return;
495 // Check for inset locking
496 if (bv_->the_locking_inset) {
497 LyXCursor cursor = bv_->text->cursor;
498 bv_->the_locking_inset->
499 InsetMotionNotify(bv_,
501 y - cursor.y() + screen_->first,
506 /* The selection possible is needed, that only motion events are
507 * used, where the bottom press event was on the drawing area too */
508 if (selection_possible) {
509 screen_->HideCursor();
511 bv_->text->SetCursorFromCoordinates(bv_, x, y + screen_->first);
513 if (!bv_->text->selection)
514 update(BufferView::UPDATE); // Maybe an empty line was deleted
516 bv_->text->SetSelection();
517 screen_->ToggleToggle(bv_->text);
519 screen_->ShowCursor(bv_->text);
525 // Single-click on work area
526 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
532 if (buffer_ == 0 || !screen_) return;
534 Inset * inset_hit = checkInsetHit(xpos, ypos, button);
536 // ok ok, this is a hack.
537 if (button == 4 || button == 5) {
540 scrollUp(100); // This number is only temporary
548 if (bv_->the_locking_inset) {
549 // We are in inset locking mode
551 /* Check whether the inset was hit. If not reset mode,
552 otherwise give the event to the inset */
553 if (inset_hit == bv_->the_locking_inset) {
554 bv_->the_locking_inset->
555 InsetButtonPress(bv_,
560 bv_->unlockInset(bv_->the_locking_inset);
565 selection_possible = true;
566 screen_->HideCursor();
569 // Right button mouse click on a table
571 (bv_->text->cursor.par()->table ||
572 bv_->text->MouseHitInTable(bv_, xpos, ypos + screen_->first))) {
573 // Set the cursor to the press-position
574 bv_->text->SetCursorFromCoordinates(bv_, xpos, ypos + screen_->first);
577 // Only show the table popup if the hit is in
579 if (!bv_->text->HitInTable(bv_,
580 bv_->text->cursor.row(), xpos))
583 // Hit above or below the table?
585 if (!bv_->text->selection) {
586 screen_->ToggleSelection(bv_->text);
587 bv_->text->ClearSelection();
588 bv_->text->FullRebreak(bv_);
589 screen_->Update(bv_->text);
592 // Popup table popup when on a table.
593 // This is obviously temporary, since we
594 // should be able to popup various
595 // context-sensitive-menus with the
596 // the right mouse. So this should be done more
597 // general in the future. Matthias.
598 selection_possible = false;
600 ->Dispatch(LFUN_LAYOUT_TABLE,
607 int screen_first = screen_->first;
609 // Middle button press pastes if we have a selection
610 bool paste_internally = false;
612 && bv_->text->selection) {
613 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
614 paste_internally = true;
617 // Clear the selection
618 screen_->ToggleSelection(bv_->text);
619 bv_->text->ClearSelection();
620 bv_->text->FullRebreak(bv_);
621 screen_->Update(bv_->text);
624 // Single left click in math inset?
625 if ((inset_hit != 0) &&
626 (inset_hit->Editable()==Inset::HIGHLY_EDITABLE)) {
627 // Highly editable inset, like math
628 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
629 selection_possible = false;
630 owner_->updateLayoutChoice();
631 owner_->getMiniBuffer()->Set(inset->EditMessage());
632 inset->InsetButtonPress(bv_, xpos, ypos, button);
633 inset->Edit(bv_, xpos, ypos, button);
637 // Right click on a footnote flag opens float menu
639 selection_possible = false;
643 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
644 bv_->text->SetCursorFromCoordinates(bv_, xpos, ypos + screen_first);
645 bv_->text->FinishUndo();
646 bv_->text->sel_cursor = bv_->text->cursor;
647 bv_->text->cursor.x_fix(bv_->text->cursor.x());
649 owner_->updateLayoutChoice();
651 selection_possible = false;
654 // Insert primary selection with middle mouse
655 // if there is a local selection in the current buffer,
658 if (paste_internally)
659 owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
661 owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
663 selection_possible = false;
669 void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, unsigned int button)
672 if (buffer_ && !bv_->the_locking_inset) {
673 if (screen_ && button == 1) {
674 screen_->HideCursor();
675 screen_->ToggleSelection(bv_->text);
676 bv_->text->SelectWord(bv_);
677 screen_->ToggleSelection(bv_->text, false);
678 /* This will fit the cursor on the screen
680 update(BufferView::SELECT|BufferView::FITCUR);
686 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, unsigned int button)
689 if (buffer_ && screen_ && button == 1) {
690 screen_->HideCursor();
691 screen_->ToggleSelection(bv_->text);
692 bv_->text->CursorHome(bv_);
693 bv_->text->sel_cursor = bv_->text->cursor;
694 bv_->text->CursorEnd(bv_);
695 bv_->text->SetSelection();
696 screen_->ToggleSelection(bv_->text, false);
697 /* This will fit the cursor on the screen
699 update(BufferView::SELECT|BufferView::FITCUR);
704 void BufferView::Pimpl::enterView()
706 if (active() && available()) {
707 SetXtermCursor(workarea_->getWin());
708 using_xterm_cursor = true;
713 void BufferView::Pimpl::leaveView()
715 if (using_xterm_cursor) {
716 XUndefineCursor(fl_display, workarea_->getWin());
717 using_xterm_cursor = false;
722 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
725 if (buffer_ == 0 || screen_ == 0) return;
727 // If we hit an inset, we have the inset coordinates in these
728 // and inset_hit points to the inset. If we do not hit an
729 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
730 Inset * inset_hit = checkInsetHit(x, y, button);
732 if (bv_->the_locking_inset) {
733 // We are in inset locking mode.
735 /* LyX does a kind of work-area grabbing for insets.
736 Only a ButtonPress Event outside the inset will
737 force a InsetUnlock. */
738 bv_->the_locking_inset->
739 InsetButtonRelease(bv_, x, y, button);
743 selection_possible = false;
745 if (bv_->text->cursor.par()->table) {
746 int cell = bv_->text->
747 NumberOfCell(bv_->text->cursor.par(),
748 bv_->text->cursor.pos());
749 if (bv_->text->cursor.par()->table->IsContRow(cell) &&
750 bv_->text->cursor.par()->table->
751 CellHasContRow(bv_->text->cursor.par()->table->
752 GetCellAbove(cell))<0) {
753 bv_->text->CursorUp(bv_);
758 if (button >= 2) return;
761 owner_->getMiniBuffer()->Set(CurrentState(bv_));
763 // Did we hit an editable inset?
764 if (inset_hit != 0) {
765 // Inset like error, notes and figures
766 selection_possible = false;
768 // CHECK fix this proper in 0.13
770 // Following a ref shouldn't issue
771 // a push on the undo-stack
772 // anylonger, now that we have
773 // keybindings for following
774 // references and returning from
775 // references. IMHO though, it
776 // should be the inset's own business
777 // to push or not push on the undo
778 // stack. They don't *have* to
779 // alter the document...
781 // ...or maybe the SetCursorParUndo()
782 // below isn't necessary at all anylonger?
783 if (inset_hit->LyxCode() == Inset::REF_CODE) {
784 bv_->text->SetCursorParUndo(bv_->buffer());
787 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
788 if (inset_hit->Editable()==Inset::HIGHLY_EDITABLE) {
789 // Highly editable inset, like math
790 UpdatableInset *inset = (UpdatableInset *)inset_hit;
791 inset->InsetButtonRelease(bv_, x, y, button);
793 inset_hit->InsetButtonRelease(bv_, x, y, button);
794 inset_hit->Edit(bv_, x, y, button);
799 // check whether we want to open a float
803 if (bv_->text->cursor.pos() <
804 bv_->text->cursor.par()->Last()) {
805 c = bv_->text->cursor.par()->
806 GetChar(bv_->text->cursor.pos());
808 if (c == LyXParagraph::META_FOOTNOTE
809 || c == LyXParagraph::META_MARGIN
810 || c == LyXParagraph::META_FIG
811 || c == LyXParagraph::META_TAB
812 || c == LyXParagraph::META_WIDE_FIG
813 || c == LyXParagraph::META_WIDE_TAB
814 || c == LyXParagraph::META_ALGORITHM){
816 } else if (bv_->text->cursor.pos() - 1 >= 0) {
817 c = bv_->text->cursor.par()->
818 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_);
833 selection_possible = false;
838 // Do we want to close a float? (click on the float-label)
839 if (bv_->text->cursor.row()->par()->footnoteflag ==
840 LyXParagraph::OPEN_FOOTNOTE
841 && bv_->text->cursor.row()->previous() &&
842 bv_->text->cursor.row()->previous()->par()->
843 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
844 LyXFont font(LyXFont::ALL_SANE);
845 font.setSize(LyXFont::SIZE_FOOTNOTE);
847 int box_x = 20; // LYX_PAPER_MARGIN;
848 box_x += lyxfont::width(" wide-tab ", font);
850 unsigned int screen_first = screen_->first;
853 && y + screen_first > bv_->text->cursor.y() -
854 bv_->text->cursor.row()->baseline()
855 && y + screen_first < bv_->text->cursor.y() -
856 bv_->text->cursor.row()->baseline()
857 + lyxfont::maxAscent(font) * 1.2 + lyxfont::maxDescent(font) * 1.2) {
859 selection_possible = false;
864 // Maybe we want to edit a bibitem ale970302
865 if (bv_->text->cursor.par()->bibkey && x < 20 +
866 bibitemMaxWidth(bv_->painter(),
869 params.textclass).defaultfont())) {
870 bv_->text->cursor.par()->bibkey->Edit(bv_, 0, 0, 0);
878 * Returns an inset if inset was hit. 0 otherwise.
879 * If hit, the coordinates are changed relative to the inset.
880 * Otherwise coordinates are not changed, and false is returned.
882 Inset * BufferView::Pimpl::checkInsetHit(int & x, int & y,
883 unsigned int /* button */)
888 unsigned int y_tmp = y + screen_->first;
891 bv_->text->SetCursorFromCoordinates(bv_, cursor, x, y_tmp);
892 #if 0 // Are you planning to use this Jürgen? (Lgb)
893 bool move_cursor = ((cursor.par != bv_->text->cursor.par) ||
894 (cursor.pos != bv_->text->cursor.pos()));
896 if (cursor.pos() < cursor.par()->Last()
897 && cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
898 && cursor.par()->GetInset(cursor.pos())
899 && cursor.par()->GetInset(cursor.pos())->Editable()) {
901 // Check whether the inset really was hit
902 Inset * tmpinset = cursor.par()->GetInset(cursor.pos());
903 LyXFont font = bv_->text->GetFont(bv_->buffer(),
904 cursor.par(), cursor.pos());
905 bool is_rtl = font.isVisibleRightToLeft();
909 start_x = cursor.x() - tmpinset->width(bv_->painter(), font);
912 start_x = cursor.x();
913 end_x = cursor.x() + tmpinset->width(bv_->painter(), font);
916 if (x > start_x && x < end_x
917 && y_tmp > cursor.y() - tmpinset->ascent(bv_->painter(), font)
918 && y_tmp < cursor.y() + tmpinset->descent(bv_->painter(), font)) {
920 if (move_cursor && (tmpinset != bv_->the_locking_inset))
922 bv_->text->SetCursor(bv_, cursor.par(),cursor.pos(),true);
924 // The origin of an inset is on the baseline
925 y = y_tmp - (bv_->text->cursor.y());
930 if ((cursor.pos() - 1 >= 0) &&
931 (cursor.par()->GetChar(cursor.pos()-1) == LyXParagraph::META_INSET) &&
932 (cursor.par()->GetInset(cursor.pos() - 1)) &&
933 (cursor.par()->GetInset(cursor.pos() - 1)->Editable())) {
934 Inset * tmpinset = cursor.par()->GetInset(cursor.pos()-1);
935 LyXFont font = bv_->text->GetFont(bv_->buffer(), cursor.par(),
937 bool is_rtl = font.isVisibleRightToLeft();
941 start_x = cursor.x() - tmpinset->width(bv_->painter(), font);
944 start_x = cursor.x();
945 end_x = cursor.x() + tmpinset->width(bv_->painter(), font);
947 if (x > start_x && x < end_x
948 && y_tmp > cursor.y() - tmpinset->ascent(bv_->painter(), font)
949 && y_tmp < cursor.y() + tmpinset->descent(bv_->painter(), font)) {
951 if (move_cursor && (tmpinset != bv_->the_locking_inset))
953 bv_->text->SetCursor(bv_, cursor.par(),cursor.pos()-1,true);
955 // The origin of an inset is on the baseline
956 y = y_tmp - (bv_->text->cursor.y());
964 void BufferView::Pimpl::workAreaExpose()
966 // this is a hack to ensure that we only call this through
967 // BufferView::redraw().
972 static int work_area_width = 0;
973 static unsigned int work_area_height = 0;
975 bool widthChange = workarea_->workWidth() != work_area_width;
976 bool heightChange = workarea_->height() != work_area_height;
978 // update from work area
979 work_area_width = workarea_->workWidth();
980 work_area_height = workarea_->height();
983 // All buffers need a resize
986 // Remove all texts from the textcache
987 // This is not _really_ what we want to do. What
988 // we really want to do is to delete in textcache
989 // that does not have a BufferView with matching
990 // width, but as long as we have only one BufferView
991 // deleting all gives the same result.
992 if (lyxerr.debugging())
993 textcache.show(lyxerr, "Expose delete all");
995 } else if (heightChange) {
996 // Rebuild image of current screen
998 // fitCursor() ensures we don't jump back
999 // to the start of the document on vertical
1003 // The main window size has changed, repaint most stuff
1005 // ...including the minibuffer
1006 owner_->getMiniBuffer()->Init();
1008 } else if (screen_) screen_->Redraw(bv_->text);
1010 // Grey box when we don't have a buffer
1011 workarea_->greyOut();
1014 // always make sure that the scrollbar is sane.
1016 owner_->updateLayoutChoice();
1021 void BufferView::Pimpl::update()
1023 if (screen_) screen_->Update(bv_->text);
1026 // Values used when calling update:
1028 // -2 - update, move sel_cursor if selection, fitcursor
1029 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
1030 // 0 - update, move sel_cursor if selection, fitcursor
1031 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
1032 // 3 - update, move sel_cursor if selection
1035 // a simple redraw of the parts that need refresh
1037 // move sel_cursor if selection -
1038 // the text's sel_cursor is moved if there is selection is progress
1041 // fitCursor() is called and the scrollbar updated
1044 // the buffer is marked dirty.
1053 // UPDATE_ONLY = UPDATE;
1054 // UPDATE_SELECT = UPDATE | SELECT;
1055 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
1056 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1058 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
1059 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
1060 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1061 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1062 // update(3) -> update(1) -> update(1) -> update(SELECT)
1064 //void BufferView::Pimpl::update(signed char f)
1065 void BufferView::Pimpl::update(BufferView::UpdateCodes f)
1067 owner_->updateLayoutChoice();
1069 if (!bv_->text->selection && (f & SELECT)) {
1070 bv_->text->sel_cursor = bv_->text->cursor;
1073 bv_->text->FullRebreak(bv_);
1082 if (buffer_->isLyxClean()) {
1083 buffer_->markDirty();
1084 owner_->getMiniBuffer()->setTimer(4);
1086 buffer_->markDirty();
1092 // Callback for cursor timer
1093 void BufferView::Pimpl::cursorToggle()
1095 // Quite a nice place for asyncron Inset updating, isn't it?
1096 // Actually no! This is run even if no buffer exist... so (Lgb)
1098 goto set_timer_and_return;
1102 // On my quest to solve the gs render hangups I am now
1103 // disabling the SIGHUP completely, and will do a wait
1104 // now and then instead. If the guess that xforms somehow
1105 // destroys something is true, this is likely (hopefully)
1106 // to solve the problem...at least I hope so. Lgb
1108 // ...Ok this seems to work...at least it does not make things
1109 // worse so far. However I still see gs processes that hangs.
1110 // I would really like to know _why_ they are hanging. Anyway
1111 // the solution without the SIGCHLD handler seems to be easier
1114 // When attaching gdb to a a running gs that hangs it shows
1115 // that it is waiting for input(?) Is it possible for us to
1116 // provide that input somehow? Or figure what it is expecing
1119 // One solution is to, after some time, look if there are some
1120 // old gs processes still running and if there are: kill them
1123 // Another solution is to provide the user an option to rerender
1124 // a picture. This would, for the picture in question, check if
1125 // there is a gs running for it, if so kill it, and start a new
1126 // rendering process.
1128 // these comments posted to lyx@via
1131 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1132 if (pid == -1) // error find out what is wrong
1133 ; // ignore it for now.
1135 sigchldhandler(pid, &status);
1138 updatelist.update(bv_);
1141 goto set_timer_and_return;
1144 if (lyx_focus && work_area_focus) {
1145 if (!bv_->the_locking_inset) {
1146 screen_->CursorToggle(bv_->text);
1148 bv_->the_locking_inset->
1149 ToggleInsetCursor(bv_);
1151 goto set_timer_and_return;
1153 // Make sure that the cursor is visible.
1154 if (!bv_->the_locking_inset) {
1155 screen_->ShowCursor(bv_->text);
1157 if (!bv_->the_locking_inset->isCursorVisible())
1158 bv_->the_locking_inset->
1159 ToggleInsetCursor(bv_);
1161 // This is only run when work_area_focus or lyx_focus is false.
1164 XGetInputFocus(fl_display, &tmpwin, &tmp);
1165 // Commenting this out, we have not had problems with this
1166 // for a long time. We will probably work on this code later
1167 // and we can reenable this debug code then. Now it only
1168 // anoying when debugging. (Lgb)
1169 //if (lyxerr.debugging(Debug::INFO)) {
1170 // lyxerr << "tmpwin: " << tmpwin
1171 // << "\nwindow: " << view->owner_->getForm()->window
1172 // << "\nwork_area_focus: " << view->work_area_focus
1173 // << "\nlyx_focus : " << view->lyx_focus
1176 if (tmpwin != owner_->getForm()->window) {
1181 if (!work_area_focus)
1184 goto set_timer_and_return;
1188 set_timer_and_return:
1189 cursor_timeout.restart();
1195 void BufferView::Pimpl::cursorPrevious()
1197 if (!bv_->text->cursor.row()->previous()) return;
1199 long y = screen_->first;
1200 Row * cursorrow = bv_->text->cursor.row();
1201 bv_->text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y);
1202 bv_->text->FinishUndo();
1203 // This is to allow jumping over large insets
1204 if ((cursorrow == bv_->text->cursor.row()))
1205 bv_->text->CursorUp(bv_);
1207 if (bv_->text->cursor.row()->height() < workarea_->height())
1208 screen_->Draw(bv_->text,
1209 bv_->text->cursor.y()
1210 - bv_->text->cursor.row()->baseline()
1211 + bv_->text->cursor.row()->height()
1212 - workarea_->height() + 1 );
1217 void BufferView::Pimpl::cursorNext()
1219 if (!bv_->text->cursor.row()->next()) return;
1221 long y = screen_->first;
1222 bv_->text->GetRowNearY(y);
1223 Row * cursorrow = bv_->text->cursor.row();
1224 bv_->text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y
1225 + workarea_->height());
1226 bv_->text->FinishUndo();
1227 // This is to allow jumping over large insets
1228 if ((cursorrow == bv_->text->cursor.row()))
1229 bv_->text->CursorDown(bv_);
1231 if (bv_->text->cursor.row()->height() < workarea_->height())
1232 screen_->Draw(bv_->text, bv_->text->cursor.y()
1233 - bv_->text->cursor.row()->baseline());
1238 bool BufferView::Pimpl::available() const
1240 if (buffer_ && bv_->text) return true;
1245 void BufferView::Pimpl::beforeChange()
1248 bv_->text->ClearSelection();
1251 //owner_->update_timeout.stop();
1255 void BufferView::Pimpl::savePosition()
1257 backstack.push(buffer_->fileName(),
1258 bv_->text->cursor.x(),
1259 bv_->text->cursor.y());
1263 void BufferView::Pimpl::restorePosition()
1265 if (backstack.empty()) return;
1268 string fname = backstack.pop(&x, &y);
1271 Buffer * b = bufferlist.exists(fname) ?
1272 bufferlist.getBuffer(fname) :
1273 bufferlist.loadLyXFile(fname); // don't ask, just load it
1275 bv_->text->SetCursorFromCoordinates(bv_, x, y);
1276 update(BufferView::SELECT|BufferView::FITCUR);
1280 bool BufferView::Pimpl::NoSavedPositions()
1282 return backstack.empty();
1286 void BufferView::Pimpl::setState()
1288 if (!lyxrc.rtl_support)
1291 if (bv_->text->real_current_font.isRightToLeft() &&
1292 bv_->text->real_current_font.latex() != LyXFont::ON) {
1293 if (owner_->getIntl()->primarykeymap)
1294 owner_->getIntl()->KeyMapSec();
1296 if (!owner_->getIntl()->primarykeymap)
1297 owner_->getIntl()->KeyMapPrim();
1302 void BufferView::Pimpl::insetSleep()
1304 if (bv_->the_locking_inset && !bv_->inset_slept) {
1305 bv_->the_locking_inset->GetCursorPos(bv_->slx, bv_->sly);
1306 bv_->the_locking_inset->InsetUnlock(bv_);
1307 bv_->inset_slept = true;
1312 void BufferView::Pimpl::insetWakeup()
1314 if (bv_->the_locking_inset && bv_->inset_slept) {
1315 bv_->the_locking_inset->Edit(bv_, bv_->slx, bv_->sly, 0);
1316 bv_->inset_slept = false;
1321 void BufferView::Pimpl::insetUnlock()
1323 if (bv_->the_locking_inset) {
1324 if (!bv_->inset_slept) bv_->the_locking_inset->InsetUnlock(bv_);
1325 bv_->the_locking_inset = 0;
1326 bv_->text->FinishUndo();
1327 bv_->inset_slept = false;
1332 bool BufferView::Pimpl::focus() const
1334 return workarea_->hasFocus();
1338 void BufferView::Pimpl::focus(bool f)
1340 if (f) workarea_->setFocus();
1344 bool BufferView::Pimpl::active() const
1346 return workarea_->active();
1350 bool BufferView::Pimpl::belowMouse() const
1352 return workarea_->belowMouse();
1356 void BufferView::Pimpl::showCursor()
1359 screen_->ShowCursor(bv_->text);
1363 void BufferView::Pimpl::hideCursor()
1366 screen_->HideCursor();
1370 void BufferView::Pimpl::toggleSelection(bool b)
1373 screen_->ToggleSelection(bv_->text, b);
1377 void BufferView::Pimpl::toggleToggle()
1380 screen_->ToggleToggle(bv_->text);
1384 void BufferView::Pimpl::center()
1387 if (bv_->text->cursor.y() > workarea_->height() / 2) {
1388 screen_->Draw(bv_->text, bv_->text->cursor.y() - workarea_->height() / 2);
1390 screen_->Draw(bv_->text, 0);
1392 update(BufferView::SELECT|BufferView::FITCUR);
1397 void BufferView::Pimpl::pasteClipboard(bool asPara)
1399 if (buffer_ == 0) return;
1401 screen_->HideCursor();
1402 bv_->beforeChange();
1404 string clip(workarea_->getClipboard());
1406 if (clip.empty()) return;
1409 bv_->text->InsertStringB(bv_, clip);
1411 bv_->text->InsertStringA(bv_, clip);
1413 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1417 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
1419 workarea_->putClipboard(stuff);