10 #include "BufferView_pimpl.h"
12 #include "lyxscreen.h"
16 #include "commandtags.h"
18 #include "minibuffer.h"
20 #include "bufferview_funcs.h"
21 #include "TextCache.h"
22 #include "bufferlist.h"
23 #include "insets/insetbib.h"
24 #include "lyx_gui_misc.h"
27 #include "support/LAssert.h"
28 #include "frontends/Dialogs.h"
30 #ifdef SIGC_CXX_NAMESPACES
39 /* the selection possible is needed, that only motion events are
40 * used, where the bottom press event was on the drawing area too */
41 bool selection_possible = false;
43 extern BufferList bufferlist;
44 extern char ascii_type;
46 extern "C" void TimerCB(FL_OBJECT *, long);
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);
80 workarea_->workAreaExpose
81 .connect(slot(this, &BufferView::Pimpl::workAreaExpose));
82 workarea_->workAreaEnter
83 .connect(slot(this, &BufferView::Pimpl::enterView));
84 workarea_->workAreaLeave
85 .connect(slot(this, &BufferView::Pimpl::leaveView));
86 workarea_->workAreaButtonPress
87 .connect(slot(this, &BufferView::Pimpl::workAreaButtonPress));
88 workarea_->workAreaButtonRelease
90 &BufferView::Pimpl::workAreaButtonRelease));
91 workarea_->workAreaMotionNotify
92 .connect(slot(this, &BufferView::Pimpl::workAreaMotionNotify));
93 workarea_->workAreaDoubleClick
94 .connect(slot(this, &BufferView::Pimpl::doubleClick));
95 workarea_->workAreaTripleClick
96 .connect(slot(this, &BufferView::Pimpl::tripleClick));
97 workarea_->workAreaKeyPress
98 .connect(slot(this, &BufferView::Pimpl::workAreaKeyPress));
102 cursor_timeout.timeout.connect(slot(this,
103 &BufferView::Pimpl::cursorToggle));
104 current_scrollbar_value = 0;
105 cursor_timeout.start();
106 workarea_->setFocus();
107 using_xterm_cursor = false;
111 Painter & BufferView::Pimpl::painter()
113 return workarea_->getPainter();
117 void BufferView::Pimpl::buffer(Buffer * b)
119 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
123 buffer_->delUser(bv_);
125 // Put the old text into the TextCache, but
126 // only if the buffer is still loaded.
127 // Also set the owner of the test to 0
128 // bv_->text->owner(0);
129 textcache.add(buffer_, workarea_->workWidth(), bv_->text);
130 if (lyxerr.debugging())
131 textcache.show(lyxerr, "BufferView::buffer");
136 // Set current buffer
139 if (bufferlist.getState() == BufferList::CLOSING) return;
142 // screen is always deleted when the buffer is changed.
146 // If we are closing the buffer, use the first buffer as current
148 buffer_ = bufferlist.first();
152 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
153 buffer_->addUser(bv_);
154 // If we don't have a text object for this, we make one
155 if (bv_->text == 0) {
156 resizeCurrentBuffer();
161 bv_->text->first = screen_->TopCursorVisible(bv_->text);
162 owner_->updateMenubar();
163 owner_->updateToolbar();
165 owner_->getDialogs()->updateBufferDependent();
168 lyxerr[Debug::INFO] << " No Buffer!" << endl;
169 owner_->updateMenubar();
170 owner_->updateToolbar();
174 // Also remove all remaining text's from the testcache.
175 // (there should not be any!) (if there is any it is a
177 if (lyxerr.debugging())
178 textcache.show(lyxerr, "buffer delete all");
181 // should update layoutchoice even if we don't have a buffer.
182 owner_->updateLayoutChoice();
183 owner_->getMiniBuffer()->Init();
184 owner_->updateWindowTitle();
188 void BufferView::Pimpl::resize(int xpos, int ypos, int width, int height)
190 workarea_->resize(xpos, ypos, width, height);
196 void BufferView::Pimpl::resize()
198 // This will resize the buffer. (Asger)
200 resizeCurrentBuffer();
204 void BufferView::Pimpl::redraw()
206 lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
211 bool BufferView::Pimpl::fitCursor()
213 Assert(screen_); // it is a programming error to call fitCursor
214 // without a valid screen.
215 bool ret = screen_->FitCursor(bv_->text);
216 if (ret) updateScrollbar();
221 void BufferView::Pimpl::redoCurrentBuffer()
223 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
224 if (buffer_ && bv_->text) {
226 owner_->updateLayoutChoice();
231 int BufferView::Pimpl::resizeCurrentBuffer()
233 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
235 LyXParagraph * par = 0;
236 LyXParagraph * selstartpar = 0;
237 LyXParagraph * selendpar = 0;
246 owner_->getMiniBuffer()->Set(_("Formatting document..."));
249 par = bv_->text->cursor.par();
250 pos = bv_->text->cursor.pos();
251 selstartpar = bv_->text->sel_start_cursor.par();
252 selstartpos = bv_->text->sel_start_cursor.pos();
253 selendpar = bv_->text->sel_end_cursor.par();
254 selendpos = bv_->text->sel_end_cursor.pos();
255 selection = bv_->text->selection;
256 mark_set = bv_->text->mark_set;
258 bv_->text = new LyXText(bv_);
260 // See if we have a text in TextCache that fits
261 // the new buffer_ with the correct width.
262 bv_->text = textcache.findFit(buffer_, workarea_->workWidth());
264 if (lyxerr.debugging()) {
265 lyxerr << "Found a LyXText that fits:\n";
266 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea_->workWidth(), bv_->text)));
268 // Set the owner of the newly found text
269 // bv_->text->owner(bv_);
270 if (lyxerr.debugging())
271 textcache.show(lyxerr, "resizeCurrentBuffer");
273 bv_->text = new LyXText(bv_);
279 bv_->text->selection = true;
280 /* at this point just to avoid the Delete-Empty-Paragraph
281 * Mechanism when setting the cursor */
282 bv_->text->mark_set = mark_set;
284 bv_->text->SetCursor(bv_, selstartpar, selstartpos);
285 bv_->text->sel_cursor = bv_->text->cursor;
286 bv_->text->SetCursor(bv_, selendpar, selendpos);
287 bv_->text->SetSelection();
288 bv_->text->SetCursor(bv_, par, pos);
290 bv_->text->SetCursor(bv_, par, pos);
291 bv_->text->sel_cursor = bv_->text->cursor;
292 bv_->text->selection = false;
295 bv_->text->first = screen_->TopCursorVisible(bv_->text);
296 buffer_->resizeInsets(bv_);
297 // this will scroll the screen such that the cursor becomes visible
300 owner_->getMiniBuffer()->Init();
304 // Now if the title form still exist kill it
311 void BufferView::Pimpl::gotoError()
316 screen_->HideCursor();
318 update(BufferView::SELECT|BufferView::FITCUR);
321 if (!bv_->text->GotoNextError(bv_)) {
322 if (bv_->text->cursor.pos()
323 || bv_->text->cursor.par() != bv_->text->FirstParagraph()) {
324 tmp = bv_->text->cursor;
325 bv_->text->cursor.par(bv_->text->FirstParagraph());
326 bv_->text->cursor.pos(0);
327 if (!bv_->text->GotoNextError(bv_)) {
328 bv_->text->cursor = tmp;
329 owner_->getMiniBuffer()
330 ->Set(_("No more errors"));
334 owner_->getMiniBuffer()->Set(_("No more errors"));
338 update(BufferView::SELECT|BufferView::FITCUR);
339 bv_->text->sel_cursor = bv_->text->cursor;
343 void BufferView::Pimpl::updateScreen()
345 // Regenerate the screen.
347 screen_ = new LyXScreen(*workarea_);
351 void BufferView::Pimpl::updateScrollbar()
353 /* If the text is smaller than the working area, the scrollbar
354 * maximum must be the working area height. No scrolling will
358 workarea_->setScrollbar(0, 1.0);
362 static unsigned long max2 = 0;
363 static unsigned long height2 = 0;
365 unsigned long cbth = 0;
369 cbth = bv_->text->height;
370 cbsf = bv_->text->first;
373 // check if anything has changed.
375 height2 == workarea_->height() &&
376 current_scrollbar_value == cbsf)
379 height2 = workarea_->height();
380 current_scrollbar_value = cbsf;
382 if (cbth <= height2) { // text is smaller than screen
383 workarea_->setScrollbar(0, 1.0); // right?
387 long maximum_height = workarea_->height() * 3 / 4 + cbth;
391 double hfloat = workarea_->height();
392 double maxfloat = maximum_height;
394 float slider_size = 0.0;
395 int slider_value = value;
397 workarea_->setScrollbarBounds(0, bv_->text->height - workarea_->height());
398 double lineh = bv_->text->DefaultHeight();
399 workarea_->setScrollbarIncrements(lineh);
400 if (maxfloat > 0.0) {
401 if ((hfloat / maxfloat) * float(height2) < 3)
402 slider_size = 3.0/float(height2);
404 slider_size = hfloat / maxfloat;
406 slider_size = hfloat;
408 workarea_->setScrollbar(slider_value, slider_size / workarea_->height());
412 // Callback for scrollbar slider
413 void BufferView::Pimpl::scrollCB(double value)
415 extern bool cursor_follows_scrollbar;
417 if (buffer_ == 0) return;
419 current_scrollbar_value = long(value);
420 if (current_scrollbar_value < 0)
421 current_scrollbar_value = 0;
426 screen_->Draw(bv_->text, current_scrollbar_value);
428 if (cursor_follows_scrollbar) {
429 LyXText * vbt = bv_->text;
430 unsigned int height = vbt->DefaultHeight();
432 if (vbt->cursor.y() < bv_->text->first + height) {
433 vbt->SetCursorFromCoordinates(bv_, 0,
436 } else if (vbt->cursor.y() >
437 bv_->text->first + workarea_->height() - height) {
438 vbt->SetCursorFromCoordinates(bv_, 0,
440 workarea_->height() -
448 int BufferView::Pimpl::scrollUp(long time)
450 if (buffer_ == 0) return 0;
451 if (!screen_) return 0;
453 double value = workarea_->getScrollbarValue();
455 if (value == 0) return 0;
457 float add_value = (bv_->text->DefaultHeight()
458 + float(time) * float(time) * 0.125);
460 if (add_value > workarea_->height())
461 add_value = float(workarea_->height() -
462 bv_->text->DefaultHeight());
469 workarea_->setScrollbarValue(value);
471 bv_->scrollCB(value);
476 int BufferView::Pimpl::scrollDown(long time)
478 if (buffer_ == 0) return 0;
479 if (!screen_) return 0;
481 double value= workarea_->getScrollbarValue();
482 pair<float, float> p = workarea_->getScrollbarBounds();
483 double max = p.second;
485 if (value == max) return 0;
487 float add_value = (bv_->text->DefaultHeight()
488 + float(time) * float(time) * 0.125);
490 if (add_value > workarea_->height())
491 add_value = float(workarea_->height() -
492 bv_->text->DefaultHeight());
499 workarea_->setScrollbarValue(value);
501 bv_->scrollCB(value);
506 void BufferView::Pimpl::workAreaKeyPress(KeySym keysym, unsigned int state)
508 bv_->owner()->getLyXFunc()->processKeySym(keysym, state);
512 void BufferView::Pimpl::workAreaMotionNotify(int x, int y, unsigned int state)
514 // Only use motion with button 1
515 if (!(state & Button1MotionMask))
518 if (buffer_ == 0 || !screen_) return;
520 // Check for inset locking
521 if (bv_->the_locking_inset) {
522 LyXCursor cursor = bv_->text->cursor;
523 bv_->the_locking_inset->
524 InsetMotionNotify(bv_,
526 bv_->the_locking_inset->scroll(),
527 y - cursor.y() + bv_->text->first,
532 /* The selection possible is needed, that only motion events are
533 * used, where the bottom press event was on the drawing area too */
534 if (selection_possible) {
535 screen_->HideCursor();
537 bv_->text->SetCursorFromCoordinates(bv_, x, y + bv_->text->first);
539 if (!bv_->text->selection)
540 update(BufferView::UPDATE); // Maybe an empty line was deleted
542 bv_->text->SetSelection();
543 screen_->ToggleToggle(bv_->text);
545 screen_->ShowCursor(bv_->text);
551 // Single-click on work area
552 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
558 if (buffer_ == 0 || !screen_) return;
560 Inset * inset_hit = checkInsetHit(bv_->text, xpos, ypos, button);
562 // ok ok, this is a hack.
563 if (button == 4 || button == 5) {
566 scrollUp(lyxrc.wheel_jump); // default 100, set in lyxrc
569 scrollDown(lyxrc.wheel_jump);
574 if (bv_->the_locking_inset) {
575 // We are in inset locking mode
577 /* Check whether the inset was hit. If not reset mode,
578 otherwise give the event to the inset */
579 if (inset_hit == bv_->the_locking_inset) {
580 bv_->the_locking_inset->
581 InsetButtonPress(bv_,
586 bv_->unlockInset(bv_->the_locking_inset);
591 selection_possible = true;
592 screen_->HideCursor();
595 // Right button mouse click on a table
597 (bv_->text->cursor.par()->table ||
598 bv_->text->MouseHitInTable(bv_, xpos, ypos + bv_->text->first))) {
599 // Set the cursor to the press-position
600 bv_->text->SetCursorFromCoordinates(bv_, xpos, ypos + bv_->text->first);
603 // Only show the table popup if the hit is in
605 if (!bv_->text->HitInTable(bv_,
606 bv_->text->cursor.row(), xpos))
609 // Hit above or below the table?
611 if (!bv_->text->selection) {
612 screen_->ToggleSelection(bv_->text);
613 bv_->text->ClearSelection();
614 bv_->text->FullRebreak(bv_);
615 screen_->Update(bv_->text);
618 // Popup table popup when on a table.
619 // This is obviously temporary, since we
620 // should be able to popup various
621 // context-sensitive-menus with the
622 // the right mouse. So this should be done more
623 // general in the future. Matthias.
624 selection_possible = false;
626 ->Dispatch(LFUN_LAYOUT_TABLE,
633 int screen_first = bv_->text->first;
635 // Middle button press pastes if we have a selection
636 bool paste_internally = false;
638 && bv_->text->selection) {
639 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
640 paste_internally = true;
643 // Clear the selection
644 screen_->ToggleSelection(bv_->text);
645 bv_->text->ClearSelection();
646 bv_->text->FullRebreak(bv_);
647 screen_->Update(bv_->text);
650 // Single left click in math inset?
651 if ((inset_hit != 0) &&
652 (inset_hit->Editable()==Inset::HIGHLY_EDITABLE)) {
653 // Highly editable inset, like math
654 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
655 selection_possible = false;
656 owner_->updateLayoutChoice();
657 owner_->getMiniBuffer()->Set(inset->EditMessage());
658 inset->InsetButtonPress(bv_, xpos, ypos, button);
659 inset->Edit(bv_, xpos, ypos, button);
663 // Right click on a footnote flag opens float menu
665 selection_possible = false;
669 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
670 bv_->text->SetCursorFromCoordinates(bv_, xpos, ypos + screen_first);
671 bv_->text->FinishUndo();
672 bv_->text->sel_cursor = bv_->text->cursor;
673 bv_->text->cursor.x_fix(bv_->text->cursor.x());
675 owner_->updateLayoutChoice();
677 selection_possible = false;
680 // Insert primary selection with middle mouse
681 // if there is a local selection in the current buffer,
684 if (paste_internally)
685 owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
687 owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
689 selection_possible = false;
695 void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, unsigned int button)
698 if (buffer_ && !bv_->the_locking_inset) {
699 if (screen_ && button == 1) {
700 screen_->HideCursor();
701 screen_->ToggleSelection(bv_->text);
702 bv_->text->SelectWord(bv_);
703 screen_->ToggleSelection(bv_->text, false);
704 /* This will fit the cursor on the screen
706 update(BufferView::SELECT|BufferView::FITCUR);
712 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, unsigned int button)
715 if (buffer_ && screen_ && !bv_->the_locking_inset && (button == 1)) {
716 screen_->HideCursor();
717 screen_->ToggleSelection(bv_->text);
718 bv_->text->CursorHome(bv_);
719 bv_->text->sel_cursor = bv_->text->cursor;
720 bv_->text->CursorEnd(bv_);
721 bv_->text->SetSelection();
722 screen_->ToggleSelection(bv_->text, false);
723 /* This will fit the cursor on the screen
725 update(BufferView::SELECT|BufferView::FITCUR);
730 void BufferView::Pimpl::enterView()
732 if (active() && available()) {
733 SetXtermCursor(workarea_->getWin());
734 using_xterm_cursor = true;
739 void BufferView::Pimpl::leaveView()
741 if (using_xterm_cursor) {
742 XUndefineCursor(fl_display, workarea_->getWin());
743 using_xterm_cursor = false;
748 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
751 if (buffer_ == 0 || screen_ == 0) return;
753 // If we hit an inset, we have the inset coordinates in these
754 // and inset_hit points to the inset. If we do not hit an
755 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
756 Inset * inset_hit = checkInsetHit(bv_->text, x, y, button);
758 if (bv_->the_locking_inset) {
759 // We are in inset locking mode.
761 /* LyX does a kind of work-area grabbing for insets.
762 Only a ButtonPress Event outside the inset will
763 force a InsetUnlock. */
764 bv_->the_locking_inset->
765 InsetButtonRelease(bv_, x, y, button);
769 selection_possible = false;
771 if (bv_->text->cursor.par()->table) {
772 int cell = bv_->text->
773 NumberOfCell(bv_->text->cursor.par(),
774 bv_->text->cursor.pos());
775 if (bv_->text->cursor.par()->table->IsContRow(cell) &&
776 bv_->text->cursor.par()->table->
777 CellHasContRow(bv_->text->cursor.par()->table->
778 GetCellAbove(cell))<0) {
779 bv_->text->CursorUp(bv_);
784 if (button >= 2) return;
789 // Did we hit an editable inset?
790 if (inset_hit != 0) {
791 // Inset like error, notes and figures
792 selection_possible = false;
794 // CHECK fix this proper in 0.13
796 // Following a ref shouldn't issue
797 // a push on the undo-stack
798 // anylonger, now that we have
799 // keybindings for following
800 // references and returning from
801 // references. IMHO though, it
802 // should be the inset's own business
803 // to push or not push on the undo
804 // stack. They don't *have* to
805 // alter the document...
807 // ...or maybe the SetCursorParUndo()
808 // below isn't necessary at all anylonger?
809 if (inset_hit->LyxCode() == Inset::REF_CODE) {
810 bv_->text->SetCursorParUndo(bv_->buffer());
813 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
814 if (inset_hit->Editable()==Inset::HIGHLY_EDITABLE) {
815 // Highly editable inset, like math
816 UpdatableInset *inset = (UpdatableInset *)inset_hit;
817 inset->InsetButtonRelease(bv_, x, y, button);
819 inset_hit->InsetButtonRelease(bv_, x, y, button);
820 inset_hit->Edit(bv_, x, y, button);
825 // check whether we want to open a float
829 if (bv_->text->cursor.pos() <
830 bv_->text->cursor.par()->Last()) {
831 c = bv_->text->cursor.par()->
832 GetChar(bv_->text->cursor.pos());
835 if (c == LyXParagraph::META_FOOTNOTE
836 || c == LyXParagraph::META_MARGIN
837 || c == LyXParagraph::META_FIG
838 || c == LyXParagraph::META_TAB
839 || c == LyXParagraph::META_WIDE_FIG
840 || c == LyXParagraph::META_WIDE_TAB
841 || c == LyXParagraph::META_ALGORITHM){
845 if (bv_->text->cursor.pos() - 1 >= 0) {
846 c = bv_->text->cursor.par()->
847 GetChar(bv_->text->cursor.pos() - 1);
849 if (c == LyXParagraph::META_FOOTNOTE
850 || c == LyXParagraph::META_MARGIN
851 || c == LyXParagraph::META_FIG
852 || c == LyXParagraph::META_TAB
853 || c == LyXParagraph::META_WIDE_FIG
854 || c == LyXParagraph::META_WIDE_TAB
855 || c == LyXParagraph::META_ALGORITHM){
856 // We are one step too far to the right
857 bv_->text->CursorLeft(bv_);
866 selection_possible = false;
872 // Do we want to close a float? (click on the float-label)
873 if (bv_->text->cursor.row()->par()->footnoteflag ==
874 LyXParagraph::OPEN_FOOTNOTE
875 && bv_->text->cursor.row()->previous() &&
876 bv_->text->cursor.row()->previous()->par()->
877 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
878 LyXFont font(LyXFont::ALL_SANE);
879 font.setSize(LyXFont::SIZE_FOOTNOTE);
881 int box_x = 20; // LYX_PAPER_MARGIN;
882 box_x += lyxfont::width(" wide-tab ", font);
884 unsigned int screen_first = bv_->text->first;
887 && y + screen_first > bv_->text->cursor.y() -
888 bv_->text->cursor.row()->baseline()
889 && y + screen_first < bv_->text->cursor.y() -
890 bv_->text->cursor.row()->baseline()
891 + lyxfont::maxAscent(font) * 1.2 + lyxfont::maxDescent(font) * 1.2) {
893 selection_possible = false;
899 // Maybe we want to edit a bibitem ale970302
900 if (bv_->text->cursor.par()->bibkey && x < 20 +
901 bibitemMaxWidth(bv_, textclasslist.
903 params.textclass).defaultfont())) {
904 bv_->text->cursor.par()->bibkey->Edit(bv_, 0, 0, 0);
912 * Returns an inset if inset was hit. 0 otherwise.
913 * If hit, the coordinates are changed relative to the inset.
914 * Otherwise coordinates are not changed, and false is returned.
916 Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y,
917 unsigned int /* button */)
922 unsigned int y_tmp = y + text->first;
925 text->SetCursorFromCoordinates(bv_, cursor, x, y_tmp);
927 if (cursor.pos() < cursor.par()->Last()
928 && cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
929 && cursor.par()->GetInset(cursor.pos())
930 && cursor.par()->GetInset(cursor.pos())->Editable()) {
932 // Check whether the inset really was hit
933 Inset * tmpinset = cursor.par()->GetInset(cursor.pos());
934 LyXFont font = text->GetFont(bv_->buffer(),
935 cursor.par(), cursor.pos());
936 bool is_rtl = font.isVisibleRightToLeft();
940 start_x = cursor.x() - tmpinset->width(bv_, font) + tmpinset->scroll();
941 end_x = cursor.x() + tmpinset->scroll();
943 start_x = cursor.x() + tmpinset->scroll();
944 end_x = cursor.x() + tmpinset->width(bv_, font) + tmpinset->scroll();
947 if (x > start_x && x < end_x
948 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
949 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
950 text->SetCursor(bv_, cursor.par(),cursor.pos(),true);
952 // The origin of an inset is on the baseline
953 y = y_tmp - (text->cursor.y());
958 if ((cursor.pos() - 1 >= 0) &&
959 (cursor.par()->GetChar(cursor.pos()-1) == LyXParagraph::META_INSET) &&
960 (cursor.par()->GetInset(cursor.pos() - 1)) &&
961 (cursor.par()->GetInset(cursor.pos() - 1)->Editable())) {
962 Inset * tmpinset = cursor.par()->GetInset(cursor.pos()-1);
963 LyXFont font = text->GetFont(bv_->buffer(), cursor.par(),
965 bool is_rtl = font.isVisibleRightToLeft();
969 start_x = cursor.x() - tmpinset->width(bv_, font) +
971 end_x = cursor.x() + tmpinset->scroll();
973 start_x = cursor.x() + tmpinset->scroll();
974 end_x = cursor.x() + tmpinset->width(bv_, font) +
977 if (x > start_x && x < end_x
978 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
979 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
981 if (move_cursor && (tmpinset != bv_->the_locking_inset))
983 text->SetCursor(bv_, cursor.par(),cursor.pos()-1,true);
985 // The origin of an inset is on the baseline
986 y = y_tmp - (text->cursor.y());
994 void BufferView::Pimpl::workAreaExpose()
996 // this is a hack to ensure that we only call this through
997 // BufferView::redraw().
1002 static int work_area_width = 0;
1003 static unsigned int work_area_height = 0;
1005 bool widthChange = workarea_->workWidth() != work_area_width;
1006 bool heightChange = workarea_->height() != work_area_height;
1008 // update from work area
1009 work_area_width = workarea_->workWidth();
1010 work_area_height = workarea_->height();
1013 // All buffers need a resize
1014 bufferlist.resize();
1016 // Remove all texts from the textcache
1017 // This is not _really_ what we want to do. What
1018 // we really want to do is to delete in textcache
1019 // that does not have a BufferView with matching
1020 // width, but as long as we have only one BufferView
1021 // deleting all gives the same result.
1022 if (lyxerr.debugging())
1023 textcache.show(lyxerr, "Expose delete all");
1025 } else if (heightChange) {
1026 // Rebuild image of current screen
1028 // fitCursor() ensures we don't jump back
1029 // to the start of the document on vertical
1033 // The main window size has changed, repaint most stuff
1035 // ...including the minibuffer
1036 owner_->getMiniBuffer()->Init();
1038 } else if (screen_) screen_->Redraw(bv_->text);
1040 // Grey box when we don't have a buffer
1041 workarea_->greyOut();
1044 // always make sure that the scrollbar is sane.
1046 owner_->updateLayoutChoice();
1051 void BufferView::Pimpl::update()
1053 if (screen_) screen_->Update(bv_->text);
1056 // Values used when calling update:
1058 // -2 - update, move sel_cursor if selection, fitcursor
1059 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
1060 // 0 - update, move sel_cursor if selection, fitcursor
1061 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
1062 // 3 - update, move sel_cursor if selection
1065 // a simple redraw of the parts that need refresh
1067 // move sel_cursor if selection -
1068 // the text's sel_cursor is moved if there is selection is progress
1071 // fitCursor() is called and the scrollbar updated
1074 // the buffer is marked dirty.
1083 // UPDATE_ONLY = UPDATE;
1084 // UPDATE_SELECT = UPDATE | SELECT;
1085 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
1086 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1088 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
1089 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
1090 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1091 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1092 // update(3) -> update(1) -> update(1) -> update(SELECT)
1094 void BufferView::Pimpl::update(BufferView::UpdateCodes f)
1096 owner_->updateLayoutChoice();
1098 if (!bv_->text->selection && (f & SELECT)) {
1099 bv_->text->sel_cursor = bv_->text->cursor;
1102 bv_->text->FullRebreak(bv_);
1111 if (buffer_->isLyxClean()) {
1112 buffer_->markDirty();
1113 owner_->getMiniBuffer()->setTimer(4);
1115 buffer_->markDirty();
1121 // Callback for cursor timer
1122 void BufferView::Pimpl::cursorToggle()
1124 // Quite a nice place for asyncron Inset updating, isn't it?
1125 // Actually no! This is run even if no buffer exist... so (Lgb)
1127 goto set_timer_and_return;
1131 // On my quest to solve the gs render hangups I am now
1132 // disabling the SIGHUP completely, and will do a wait
1133 // now and then instead. If the guess that xforms somehow
1134 // destroys something is true, this is likely (hopefully)
1135 // to solve the problem...at least I hope so. Lgb
1137 // ...Ok this seems to work...at least it does not make things
1138 // worse so far. However I still see gs processes that hangs.
1139 // I would really like to know _why_ they are hanging. Anyway
1140 // the solution without the SIGCHLD handler seems to be easier
1143 // When attaching gdb to a a running gs that hangs it shows
1144 // that it is waiting for input(?) Is it possible for us to
1145 // provide that input somehow? Or figure what it is expecing
1148 // One solution is to, after some time, look if there are some
1149 // old gs processes still running and if there are: kill them
1152 // Another solution is to provide the user an option to rerender
1153 // a picture. This would, for the picture in question, check if
1154 // there is a gs running for it, if so kill it, and start a new
1155 // rendering process.
1157 // these comments posted to lyx@via
1160 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1161 if (pid == -1) // error find out what is wrong
1162 ; // ignore it for now.
1164 sigchldhandler(pid, &status);
1167 updatelist.update(bv_);
1170 goto set_timer_and_return;
1173 if (!bv_->the_locking_inset) {
1174 screen_->CursorToggle(bv_->text);
1176 bv_->the_locking_inset->
1177 ToggleInsetCursor(bv_);
1180 set_timer_and_return:
1181 cursor_timeout.restart();
1186 void BufferView::Pimpl::cursorPrevious()
1188 if (!bv_->text->cursor.row()->previous()) return;
1190 long y = bv_->text->first;
1191 Row * cursorrow = bv_->text->cursor.row();
1192 bv_->text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y);
1193 bv_->text->FinishUndo();
1194 // This is to allow jumping over large insets
1195 if ((cursorrow == bv_->text->cursor.row()))
1196 bv_->text->CursorUp(bv_);
1198 if (bv_->text->cursor.row()->height() < workarea_->height())
1199 screen_->Draw(bv_->text,
1200 bv_->text->cursor.y()
1201 - bv_->text->cursor.row()->baseline()
1202 + bv_->text->cursor.row()->height()
1203 - workarea_->height() + 1 );
1208 void BufferView::Pimpl::cursorNext()
1210 if (!bv_->text->cursor.row()->next()) return;
1212 long y = bv_->text->first;
1213 bv_->text->GetRowNearY(y);
1214 Row * cursorrow = bv_->text->cursor.row();
1215 bv_->text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y
1216 + workarea_->height());
1217 bv_->text->FinishUndo();
1218 // This is to allow jumping over large insets
1219 if ((cursorrow == bv_->text->cursor.row()))
1220 bv_->text->CursorDown(bv_);
1222 if (bv_->text->cursor.row()->height() < workarea_->height())
1223 screen_->Draw(bv_->text, bv_->text->cursor.y()
1224 - bv_->text->cursor.row()->baseline());
1229 bool BufferView::Pimpl::available() const
1231 if (buffer_ && bv_->text) return true;
1236 void BufferView::Pimpl::beforeChange()
1239 bv_->text->ClearSelection();
1242 //owner_->update_timeout.stop();
1246 void BufferView::Pimpl::savePosition()
1248 backstack.push(buffer_->fileName(),
1249 bv_->text->cursor.x(),
1250 bv_->text->cursor.y());
1254 void BufferView::Pimpl::restorePosition()
1256 if (backstack.empty()) return;
1259 string fname = backstack.pop(&x, &y);
1263 if( fname != buffer_->fileName() ) {
1264 Buffer * b = bufferlist.exists(fname) ?
1265 bufferlist.getBuffer(fname) :
1266 bufferlist.loadLyXFile(fname); // don't ask, just load it
1267 if( b != 0 ) buffer(b);
1270 bv_->text->SetCursorFromCoordinates(bv_, x, y);
1271 update(BufferView::SELECT|BufferView::FITCUR);
1275 bool BufferView::Pimpl::NoSavedPositions()
1277 return backstack.empty();
1281 void BufferView::Pimpl::setState()
1283 if (!lyxrc.rtl_support)
1286 if (bv_->text->real_current_font.isRightToLeft() &&
1287 bv_->text->real_current_font.latex() != LyXFont::ON) {
1288 if (owner_->getIntl()->primarykeymap)
1289 owner_->getIntl()->KeyMapSec();
1291 if (!owner_->getIntl()->primarykeymap)
1292 owner_->getIntl()->KeyMapPrim();
1297 void BufferView::Pimpl::insetSleep()
1299 if (bv_->the_locking_inset && !bv_->inset_slept) {
1300 bv_->the_locking_inset->GetCursorPos(bv_, bv_->slx, bv_->sly);
1301 bv_->the_locking_inset->InsetUnlock(bv_);
1302 bv_->inset_slept = true;
1307 void BufferView::Pimpl::insetWakeup()
1309 if (bv_->the_locking_inset && bv_->inset_slept) {
1310 bv_->the_locking_inset->Edit(bv_, bv_->slx, bv_->sly, 0);
1311 bv_->inset_slept = false;
1316 void BufferView::Pimpl::insetUnlock()
1318 if (bv_->the_locking_inset) {
1319 if (!bv_->inset_slept) bv_->the_locking_inset->InsetUnlock(bv_);
1320 bv_->the_locking_inset = 0;
1321 bv_->text->FinishUndo();
1322 bv_->inset_slept = false;
1327 bool BufferView::Pimpl::focus() const
1329 return workarea_->hasFocus();
1333 void BufferView::Pimpl::focus(bool f)
1335 if (f) workarea_->setFocus();
1339 bool BufferView::Pimpl::active() const
1341 return workarea_->active();
1345 bool BufferView::Pimpl::belowMouse() const
1347 return workarea_->belowMouse();
1351 void BufferView::Pimpl::showCursor()
1354 screen_->ShowCursor(bv_->text);
1358 void BufferView::Pimpl::hideCursor()
1361 screen_->HideCursor();
1365 void BufferView::Pimpl::toggleSelection(bool b)
1368 screen_->ToggleSelection(bv_->text, b);
1372 void BufferView::Pimpl::toggleToggle()
1375 screen_->ToggleToggle(bv_->text);
1379 void BufferView::Pimpl::center()
1382 if (bv_->text->cursor.y() > workarea_->height() / 2) {
1383 screen_->Draw(bv_->text, bv_->text->cursor.y() - workarea_->height() / 2);
1385 screen_->Draw(bv_->text, 0);
1387 update(BufferView::SELECT|BufferView::FITCUR);
1392 void BufferView::Pimpl::pasteClipboard(bool asPara)
1394 if (buffer_ == 0) return;
1396 screen_->HideCursor();
1397 bv_->beforeChange();
1399 string clip(workarea_->getClipboard());
1401 if (clip.empty()) return;
1404 bv_->text->InsertStringB(bv_, clip);
1406 bv_->text->InsertStringA(bv_, clip);
1408 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1412 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
1414 workarea_->putClipboard(stuff);