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 &);
50 const unsigned int saved_positions_num = 20;
55 XSync(fl_get_display(), 0);
60 void SetXtermCursor(Window win)
63 static bool cursor_undefined = true;
64 if (cursor_undefined){
65 cursor = XCreateFontCursor(fl_get_display(), XC_xterm);
66 XFlush(fl_get_display());
67 cursor_undefined = false;
69 XDefineCursor(fl_get_display(), win, cursor);
70 XFlush(fl_get_display());
74 BufferView::Pimpl::Pimpl(BufferView * b, LyXView * o,
75 int xpos, int ypos, int width, int height)
76 : bv_(b), owner_(o), cursor_timeout(400)
80 workarea_ = new WorkArea(bv_, xpos, ypos, width, height);
82 workarea_ = new WorkArea(xpos, ypos, width, height);
85 workarea_->scrollCB.connect(slot(this, &BufferView::Pimpl::scrollCB));
86 workarea_->workAreaExpose
87 .connect(slot(this, &BufferView::Pimpl::workAreaExpose));
88 workarea_->workAreaEnter
89 .connect(slot(this, &BufferView::Pimpl::enterView));
90 workarea_->workAreaLeave
91 .connect(slot(this, &BufferView::Pimpl::leaveView));
92 workarea_->workAreaButtonPress
93 .connect(slot(this, &BufferView::Pimpl::workAreaButtonPress));
94 workarea_->workAreaButtonRelease
96 &BufferView::Pimpl::workAreaButtonRelease));
97 workarea_->workAreaMotionNotify
98 .connect(slot(this, &BufferView::Pimpl::workAreaMotionNotify));
99 workarea_->workAreaDoubleClick
100 .connect(slot(this, &BufferView::Pimpl::doubleClick));
101 workarea_->workAreaTripleClick
102 .connect(slot(this, &BufferView::Pimpl::tripleClick));
103 workarea_->workAreaKeyPress
104 .connect(slot(this, &BufferView::Pimpl::workAreaKeyPress));
108 cursor_timeout.timeout.connect(slot(this,
109 &BufferView::Pimpl::cursorToggle));
110 current_scrollbar_value = 0;
111 cursor_timeout.start();
112 workarea_->setFocus();
113 using_xterm_cursor = false;
114 saved_positions.resize(saved_positions_num);
118 Painter & BufferView::Pimpl::painter()
120 return workarea_->getPainter();
124 void BufferView::Pimpl::buffer(Buffer * b)
126 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
130 buffer_->delUser(bv_);
132 // Put the old text into the TextCache, but
133 // only if the buffer is still loaded.
134 // Also set the owner of the test to 0
135 // bv_->text->owner(0);
136 textcache.add(buffer_, workarea_->workWidth(), bv_->text);
137 if (lyxerr.debugging())
138 textcache.show(lyxerr, "BufferView::buffer");
143 // Set current buffer
146 if (bufferlist.getState() == BufferList::CLOSING) return;
149 // screen is always deleted when the buffer is changed.
153 // If we are closing the buffer, use the first buffer as current
155 buffer_ = bufferlist.first();
159 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
160 buffer_->addUser(bv_);
161 // If we don't have a text object for this, we make one
162 if (bv_->text == 0) {
163 resizeCurrentBuffer();
168 bv_->text->first = screen_->TopCursorVisible(bv_->text);
169 owner_->updateMenubar();
170 owner_->updateToolbar();
171 // Similarly, buffer-dependent dialogs should be updated or
172 // hidden. This should go here because some dialogs (eg ToC)
173 // require bv_->text.
174 owner_->getDialogs()->updateBufferDependent(true);
178 lyxerr[Debug::INFO] << " No Buffer!" << endl;
179 owner_->updateMenubar();
180 owner_->updateToolbar();
181 owner_->getDialogs()->hideBufferDependent();
185 // Also remove all remaining text's from the testcache.
186 // (there should not be any!) (if there is any it is a
188 if (lyxerr.debugging())
189 textcache.show(lyxerr, "buffer delete all");
192 // should update layoutchoice even if we don't have a buffer.
193 owner_->updateLayoutChoice();
194 owner_->getMiniBuffer()->Init();
195 owner_->updateWindowTitle();
199 void BufferView::Pimpl::resize(int xpos, int ypos, int width, int height)
201 workarea_->resize(xpos, ypos, width, height);
207 void BufferView::Pimpl::resize()
210 resizeCurrentBuffer();
214 void BufferView::Pimpl::redraw()
216 lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
221 bool BufferView::Pimpl::fitCursor(LyXText * text)
225 bool ret = screen_->FitCursor(text, bv_);
232 void BufferView::Pimpl::redoCurrentBuffer()
234 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
235 if (buffer_ && bv_->text) {
237 owner_->updateLayoutChoice();
242 int BufferView::Pimpl::resizeCurrentBuffer()
244 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
246 LyXParagraph * par = 0;
247 LyXParagraph * selstartpar = 0;
248 LyXParagraph * selendpar = 0;
257 owner_->getMiniBuffer()->Set(_("Formatting document..."));
260 par = bv_->text->cursor.par();
261 pos = bv_->text->cursor.pos();
262 selstartpar = bv_->text->sel_start_cursor.par();
263 selstartpos = bv_->text->sel_start_cursor.pos();
264 selendpar = bv_->text->sel_end_cursor.par();
265 selendpos = bv_->text->sel_end_cursor.pos();
266 selection = bv_->text->selection;
267 mark_set = bv_->text->mark_set;
269 bv_->text = new LyXText(bv_);
271 // See if we have a text in TextCache that fits
272 // the new buffer_ with the correct width.
273 bv_->text = textcache.findFit(buffer_, workarea_->workWidth());
275 if (lyxerr.debugging()) {
276 lyxerr << "Found a LyXText that fits:\n";
277 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea_->workWidth(), bv_->text)));
279 // Set the owner of the newly found text
280 // bv_->text->owner(bv_);
281 if (lyxerr.debugging())
282 textcache.show(lyxerr, "resizeCurrentBuffer");
284 bv_->text = new LyXText(bv_);
290 bv_->text->selection = true;
291 /* at this point just to avoid the Delete-Empty-Paragraph
292 * Mechanism when setting the cursor */
293 bv_->text->mark_set = mark_set;
295 bv_->text->SetCursor(bv_, selstartpar, selstartpos);
296 bv_->text->sel_cursor = bv_->text->cursor;
297 bv_->text->SetCursor(bv_, selendpar, selendpos);
298 bv_->text->SetSelection();
299 bv_->text->SetCursor(bv_, par, pos);
301 bv_->text->SetCursor(bv_, par, pos);
302 bv_->text->sel_cursor = bv_->text->cursor;
303 bv_->text->selection = false;
306 bv_->text->first = screen_->TopCursorVisible(bv_->text);
307 buffer_->resizeInsets(bv_);
308 // this will scroll the screen such that the cursor becomes visible
311 owner_->getMiniBuffer()->Init();
315 // Now if the title form still exist kill it
322 void BufferView::Pimpl::updateScreen()
324 // Regenerate the screen.
326 screen_ = new LyXScreen(*workarea_);
330 void BufferView::Pimpl::updateScrollbar()
332 /* If the text is smaller than the working area, the scrollbar
333 * maximum must be the working area height. No scrolling will
337 workarea_->setScrollbar(0, 1.0);
341 static unsigned long max2 = 0;
342 static unsigned long height2 = 0;
344 unsigned long cbth = 0;
348 cbth = bv_->text->height;
349 cbsf = bv_->text->first;
352 // check if anything has changed.
354 height2 == workarea_->height() &&
355 current_scrollbar_value == cbsf)
358 height2 = workarea_->height();
359 current_scrollbar_value = cbsf;
361 if (cbth <= height2) { // text is smaller than screen
362 workarea_->setScrollbar(0, 1.0); // right?
366 long maximum_height = workarea_->height() * 3 / 4 + cbth;
370 double hfloat = workarea_->height();
371 double maxfloat = maximum_height;
373 float slider_size = 0.0;
374 int slider_value = value;
376 workarea_->setScrollbarBounds(0, bv_->text->height - workarea_->height());
377 double lineh = bv_->text->DefaultHeight();
378 workarea_->setScrollbarIncrements(lineh);
379 if (maxfloat > 0.0) {
380 if ((hfloat / maxfloat) * float(height2) < 3)
381 slider_size = 3.0/float(height2);
383 slider_size = hfloat / maxfloat;
385 slider_size = hfloat;
387 workarea_->setScrollbar(slider_value, slider_size / workarea_->height());
391 // Callback for scrollbar slider
392 void BufferView::Pimpl::scrollCB(double value)
394 if (!buffer_) return;
396 current_scrollbar_value = long(value);
397 if (current_scrollbar_value < 0)
398 current_scrollbar_value = 0;
403 screen_->Draw(bv_->text, bv_, current_scrollbar_value);
405 if (!lyxrc.cursor_follows_scrollbar) {
410 LyXText * vbt = bv_->text;
412 int const height = vbt->DefaultHeight();
413 int const first = static_cast<int>((bv_->text->first + height));
414 int const last = static_cast<int>((bv_->text->first + workarea_->height() - height));
416 if (vbt->cursor.y() < first)
417 vbt->SetCursorFromCoordinates(bv_, 0, first);
418 else if (vbt->cursor.y() > last)
419 vbt->SetCursorFromCoordinates(bv_, 0, last);
425 int BufferView::Pimpl::scrollUp(long time)
427 if (!buffer_) 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);
453 int BufferView::Pimpl::scrollDown(long time)
455 if (!buffer_) 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);
483 void BufferView::Pimpl::workAreaKeyPress(KeySym keysym, unsigned int state)
485 bv_->owner()->getLyXFunc()->processKeySym(keysym, state);
489 void BufferView::Pimpl::workAreaMotionNotify(int x, int y, unsigned int state)
491 // Only use motion with button 1
492 if (!(state & Button1MotionMask))
495 if (!buffer_ || !screen_) return;
497 // Check for inset locking
498 if (bv_->theLockingInset()) {
499 LyXCursor cursor = bv_->text->cursor;
500 LyXFont font = bv_->text->GetFont(bv_->buffer(),
501 cursor.par(), cursor.pos());
502 int width = bv_->theLockingInset()->width(bv_, font);
503 int inset_x = font.isVisibleRightToLeft()
504 ? cursor.x() - width : cursor.x();
505 int start_x = inset_x + bv_->theLockingInset()->scroll();
506 bv_->theLockingInset()->
507 InsetMotionNotify(bv_,
509 y - cursor.y() + bv_->text->first,
514 /* The test for not selection possible is needed, that only motion events are
515 * used, where the bottom press event was on the drawing area too */
516 if (!selection_possible)
519 screen_->HideCursor();
521 bv_->text->SetCursorFromCoordinates(bv_, x, y + bv_->text->first);
523 if (!bv_->text->selection)
524 update(BufferView::UPDATE); // Maybe an empty line was deleted
526 bv_->text->SetSelection();
527 screen_->ToggleToggle(bv_->text, bv_);
528 fitCursor(bv_->text);
529 screen_->ShowCursor(bv_->text, bv_);
533 // Single-click on work area
534 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
540 if (!buffer_ || !screen_) return;
542 Inset * inset_hit = checkInsetHit(bv_->text, xpos, ypos, button);
544 // ok ok, this is a hack.
545 if (button == 4 || button == 5) {
548 scrollUp(lyxrc.wheel_jump); // default 100, set in lyxrc
551 scrollDown(lyxrc.wheel_jump);
556 if (bv_->theLockingInset()) {
557 // We are in inset locking mode
559 /* Check whether the inset was hit. If not reset mode,
560 otherwise give the event to the inset */
561 if (inset_hit == bv_->theLockingInset()) {
562 bv_->theLockingInset()->
563 InsetButtonPress(bv_,
568 bv_->unlockInset(bv_->theLockingInset());
573 selection_possible = true;
574 screen_->HideCursor();
576 int const screen_first = bv_->text->first;
578 // Middle button press pastes if we have a selection
579 bool paste_internally = false;
581 && bv_->text->selection) {
582 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
583 paste_internally = true;
586 // Clear the selection
587 screen_->ToggleSelection(bv_->text, bv_);
588 bv_->text->ClearSelection();
589 bv_->text->FullRebreak(bv_);
590 screen_->Update(bv_->text, bv_);
593 // Single left click in math inset?
594 if ((inset_hit != 0) &&
595 (inset_hit->Editable()==Inset::HIGHLY_EDITABLE)) {
596 // Highly editable inset, like math
597 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
598 selection_possible = false;
599 owner_->updateLayoutChoice();
600 owner_->getMiniBuffer()->Set(inset->EditMessage());
601 inset->InsetButtonPress(bv_, xpos, ypos, button);
602 inset->Edit(bv_, xpos, ypos, button);
606 // Right click on a footnote flag opens float menu
608 selection_possible = false;
612 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
613 bv_->text->SetCursorFromCoordinates(bv_, xpos, ypos + screen_first);
614 bv_->text->FinishUndo();
615 bv_->text->sel_cursor = bv_->text->cursor;
616 bv_->text->cursor.x_fix(bv_->text->cursor.x());
618 owner_->updateLayoutChoice();
619 if (fitCursor(bv_->text)) {
620 selection_possible = false;
623 // Insert primary selection with middle mouse
624 // if there is a local selection in the current buffer,
627 if (paste_internally)
628 owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
630 owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
632 selection_possible = false;
638 void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, unsigned int button)
641 if (buffer_ && !bv_->theLockingInset()) {
642 if (screen_ && button == 1) {
643 screen_->HideCursor();
644 screen_->ToggleSelection(bv_->text, bv_);
645 bv_->text->SelectWord(bv_);
646 screen_->ToggleSelection(bv_->text, bv_, false);
647 /* This will fit the cursor on the screen
649 update(BufferView::SELECT|BufferView::FITCUR);
655 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, unsigned int button)
658 if (buffer_ && screen_ && !bv_->theLockingInset() && (button == 1)) {
659 screen_->HideCursor();
660 screen_->ToggleSelection(bv_->text, bv_);
661 bv_->text->CursorHome(bv_);
662 bv_->text->sel_cursor = bv_->text->cursor;
663 bv_->text->CursorEnd(bv_);
664 bv_->text->SetSelection();
665 screen_->ToggleSelection(bv_->text, bv_, false);
666 /* This will fit the cursor on the screen
668 update(BufferView::SELECT|BufferView::FITCUR);
673 void BufferView::Pimpl::enterView()
675 if (active() && available()) {
676 SetXtermCursor(workarea_->getWin());
677 using_xterm_cursor = true;
682 void BufferView::Pimpl::leaveView()
684 if (using_xterm_cursor) {
685 XUndefineCursor(fl_get_display(), workarea_->getWin());
686 using_xterm_cursor = false;
691 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
694 if (!buffer_ || !screen_) return;
696 // If we hit an inset, we have the inset coordinates in these
697 // and inset_hit points to the inset. If we do not hit an
698 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
699 Inset * inset_hit = checkInsetHit(bv_->text, x, y, button);
701 if (bv_->theLockingInset()) {
702 // We are in inset locking mode.
704 /* LyX does a kind of work-area grabbing for insets.
705 Only a ButtonPress Event outside the inset will
706 force a InsetUnlock. */
707 bv_->theLockingInset()->
708 InsetButtonRelease(bv_, x, y, button);
712 selection_possible = false;
714 if (button >= 2) return;
719 // Did we hit an editable inset?
721 // Inset like error, notes and figures
722 selection_possible = false;
724 // CHECK fix this proper in 0.13
726 // Following a ref shouldn't issue
727 // a push on the undo-stack
728 // anylonger, now that we have
729 // keybindings for following
730 // references and returning from
731 // references. IMHO though, it
732 // should be the inset's own business
733 // to push or not push on the undo
734 // stack. They don't *have* to
735 // alter the document...
737 // ...or maybe the SetCursorParUndo()
738 // below isn't necessary at all anylonger?
739 if (inset_hit->LyxCode() == Inset::REF_CODE) {
740 bv_->text->SetCursorParUndo(bv_->buffer());
743 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
744 if (inset_hit->Editable()==Inset::HIGHLY_EDITABLE) {
745 // Highly editable inset, like math
746 UpdatableInset *inset = (UpdatableInset *)inset_hit;
747 inset->InsetButtonRelease(bv_, x, y, button);
749 inset_hit->InsetButtonRelease(bv_, x, y, button);
750 inset_hit->Edit(bv_, x, y, button);
755 // check whether we want to open a float
759 if (bv_->text->cursor.pos() <
760 bv_->text->cursor.par()->Last()) {
761 c = bv_->text->cursor.par()->
762 GetChar(bv_->text->cursor.pos());
765 if(!bv_->text->selection)
766 if (c == LyXParagraph::META_FOOTNOTE
767 || c == LyXParagraph::META_MARGIN
768 || c == LyXParagraph::META_FIG
769 || c == LyXParagraph::META_TAB
770 || c == LyXParagraph::META_WIDE_FIG
771 || c == LyXParagraph::META_WIDE_TAB
772 || c == LyXParagraph::META_ALGORITHM){
776 if (bv_->text->cursor.pos() - 1 >= 0) {
777 c = bv_->text->cursor.par()->
778 GetChar(bv_->text->cursor.pos() - 1);
780 if (c == LyXParagraph::META_FOOTNOTE
781 || c == LyXParagraph::META_MARGIN
782 || c == LyXParagraph::META_FIG
783 || c == LyXParagraph::META_TAB
784 || c == LyXParagraph::META_WIDE_FIG
785 || c == LyXParagraph::META_WIDE_TAB
786 || c == LyXParagraph::META_ALGORITHM){
787 // We are one step too far to the right
788 bv_->text->CursorLeft(bv_);
797 selection_possible = false;
803 // Do we want to close a float? (click on the float-label)
804 if (bv_->text->cursor.row()->par()->footnoteflag ==
805 LyXParagraph::OPEN_FOOTNOTE
806 && bv_->text->cursor.row()->previous() &&
807 bv_->text->cursor.row()->previous()->par()->
808 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
809 LyXFont font(LyXFont::ALL_SANE);
810 font.setSize(LyXFont::SIZE_FOOTNOTE);
812 int box_x = 20; // LYX_PAPER_MARGIN;
813 box_x += lyxfont::width(" wide-tab ", font);
815 unsigned int screen_first = bv_->text->first;
818 && y + screen_first > bv_->text->cursor.y() -
819 bv_->text->cursor.row()->baseline()
820 && y + screen_first < bv_->text->cursor.y() -
821 bv_->text->cursor.row()->baseline()
822 + lyxfont::maxAscent(font) * 1.2 + lyxfont::maxDescent(font) * 1.2) {
824 selection_possible = false;
830 // Maybe we want to edit a bibitem ale970302
831 if (bv_->text->cursor.par()->bibkey && x < 20 +
832 bibitemMaxWidth(bv_, textclasslist.
834 params.textclass).defaultfont())) {
835 bv_->text->cursor.par()->bibkey->Edit(bv_, 0, 0, 0);
843 * Returns an inset if inset was hit. 0 otherwise.
844 * If hit, the coordinates are changed relative to the inset.
845 * Otherwise coordinates are not changed, and false is returned.
847 Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y,
848 unsigned int /* button */)
853 int y_tmp = y + text->first;
856 text->SetCursorFromCoordinates(bv_, cursor, x, y_tmp);
858 if (cursor.pos() < cursor.par()->Last()
859 && cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
860 && cursor.par()->GetInset(cursor.pos())
861 && cursor.par()->GetInset(cursor.pos())->Editable()) {
863 // Check whether the inset really was hit
864 Inset * tmpinset = cursor.par()->GetInset(cursor.pos());
865 LyXFont font = text->GetFont(bv_->buffer(),
866 cursor.par(), cursor.pos());
867 int width = tmpinset->width(bv_, font);
868 int inset_x = font.isVisibleRightToLeft()
869 ? cursor.x() - width : cursor.x();
870 int start_x = inset_x + tmpinset->scroll();
871 int end_x = inset_x + width;
873 if (x > start_x && x < end_x
874 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
875 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
876 text->SetCursor(bv_, cursor.par(),cursor.pos(),true);
878 // The origin of an inset is on the baseline
879 y = y_tmp - (text->cursor.y());
884 if ((cursor.pos() - 1 >= 0) &&
885 (cursor.par()->GetChar(cursor.pos()-1) == LyXParagraph::META_INSET) &&
886 (cursor.par()->GetInset(cursor.pos() - 1)) &&
887 (cursor.par()->GetInset(cursor.pos() - 1)->Editable())) {
888 Inset * tmpinset = cursor.par()->GetInset(cursor.pos()-1);
889 LyXFont font = text->GetFont(bv_->buffer(), cursor.par(),
891 int width = tmpinset->width(bv_, font);
892 int inset_x = font.isVisibleRightToLeft()
893 ? cursor.x() : cursor.x() - width;
894 int start_x = inset_x + tmpinset->scroll();
895 int end_x = inset_x + width;
897 if (x > start_x && x < end_x
898 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
899 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
901 if (move_cursor && (tmpinset != bv_->theLockingInset()))
903 text->SetCursor(bv_, cursor.par(),cursor.pos()-1,true);
905 // The origin of an inset is on the baseline
906 y = y_tmp - (text->cursor.y());
914 void BufferView::Pimpl::workAreaExpose()
916 static int work_area_width = 0;
917 static unsigned int work_area_height = 0;
919 bool widthChange = workarea_->workWidth() != work_area_width;
920 bool heightChange = workarea_->height() != work_area_height;
922 // update from work area
923 work_area_width = workarea_->workWidth();
924 work_area_height = workarea_->height();
927 // All buffers need a resize
930 // Remove all texts from the textcache
931 // This is not _really_ what we want to do. What
932 // we really want to do is to delete in textcache
933 // that does not have a BufferView with matching
934 // width, but as long as we have only one BufferView
935 // deleting all gives the same result.
936 if (lyxerr.debugging())
937 textcache.show(lyxerr, "Expose delete all");
939 } else if (heightChange) {
940 // Rebuild image of current screen
942 // fitCursor() ensures we don't jump back
943 // to the start of the document on vertical
945 fitCursor(bv_->text);
947 // The main window size has changed, repaint most stuff
949 // ...including the minibuffer
950 owner_->getMiniBuffer()->Init();
953 screen_->Redraw(bv_->text, bv_);
955 // Grey box when we don't have a buffer
956 workarea_->greyOut();
959 // always make sure that the scrollbar is sane.
961 owner_->updateLayoutChoice();
966 void BufferView::Pimpl::update()
968 if (screen_) screen_->Update(bv_->text, bv_);
971 // Values used when calling update:
973 // -2 - update, move sel_cursor if selection, fitcursor
974 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
975 // 0 - update, move sel_cursor if selection, fitcursor
976 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
977 // 3 - update, move sel_cursor if selection
980 // a simple redraw of the parts that need refresh
982 // move sel_cursor if selection -
983 // the text's sel_cursor is moved if there is selection is progress
986 // fitCursor() is called and the scrollbar updated
989 // the buffer is marked dirty.
998 // UPDATE_ONLY = UPDATE;
999 // UPDATE_SELECT = UPDATE | SELECT;
1000 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
1001 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1003 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
1004 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
1005 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1006 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1007 // update(3) -> update(1) -> update(1) -> update(SELECT)
1009 void BufferView::Pimpl::update(BufferView::UpdateCodes f)
1011 owner_->updateLayoutChoice();
1013 if (!bv_->text->selection && (f & SELECT)) {
1014 bv_->text->sel_cursor = bv_->text->cursor;
1017 bv_->text->FullRebreak(bv_);
1022 fitCursor(bv_->text);
1026 if (buffer_->isLyxClean()) {
1027 buffer_->markDirty();
1028 owner_->getMiniBuffer()->setTimer(4);
1030 buffer_->markDirty();
1036 // Callback for cursor timer
1037 void BufferView::Pimpl::cursorToggle()
1039 // Quite a nice place for asyncron Inset updating, isn't it?
1040 // Actually no! This is run even if no buffer exist... so (Lgb)
1042 cursor_timeout.restart();
1047 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1048 if (pid == -1) // error find out what is wrong
1049 ; // ignore it for now.
1051 sigchldhandler(pid, &status);
1053 updatelist.update(bv_);
1056 cursor_timeout.restart();
1060 if (!bv_->theLockingInset()) {
1061 screen_->CursorToggle(bv_->text, bv_);
1063 bv_->theLockingInset()->ToggleInsetCursor(bv_);
1066 cursor_timeout.restart();
1070 void BufferView::Pimpl::cursorPrevious(LyXText * text)
1072 if (!text->cursor.row()->previous())
1075 int y = text->first;
1076 if (text->inset_owner)
1077 y += bv_->text->first;
1078 Row * cursorrow = text->cursor.row();
1079 text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y);
1080 bv_->text->FinishUndo();
1081 // This is to allow jumping over large insets
1082 if ((cursorrow == text->cursor.row()))
1083 text->CursorUp(bv_);
1085 if (text->inset_owner ||
1086 text->cursor.row()->height() < workarea_->height())
1087 screen_->Draw(bv_->text, bv_,
1089 - text->cursor.row()->baseline()
1090 + text->cursor.row()->height()
1091 - workarea_->height() + 1 );
1096 void BufferView::Pimpl::cursorNext(LyXText * text)
1098 if (!text->cursor.row()->next())
1101 int y = text->first + workarea_->height();
1102 // if (text->inset_owner)
1103 // y += bv_->text->first;
1104 text->GetRowNearY(y);
1106 Row * cursorrow = text->cursor.row();
1107 text->SetCursorFromCoordinates(bv_, text->cursor.x_fix(), y); // + workarea_->height());
1108 bv_->text->FinishUndo();
1109 // This is to allow jumping over large insets
1110 if ((cursorrow == bv_->text->cursor.row()))
1111 text->CursorDown(bv_);
1113 if (text->inset_owner ||
1114 text->cursor.row()->height() < workarea_->height())
1115 screen_->Draw(bv_->text, bv_, text->cursor.y() -
1116 text->cursor.row()->baseline());
1121 bool BufferView::Pimpl::available() const
1123 if (buffer_ && bv_->text) return true;
1128 void BufferView::Pimpl::beforeChange()
1131 bv_->text->ClearSelection();
1135 void BufferView::Pimpl::savePosition(unsigned int i)
1137 if (i >= saved_positions_num)
1139 saved_positions[i] = Position(buffer_->fileName(),
1140 bv_->text->cursor.par()->id(),
1141 bv_->text->cursor.pos());
1143 owner_->getMiniBuffer()->Set(_("Saved bookmark ") + tostr(i));
1147 void BufferView::Pimpl::restorePosition(unsigned int i)
1149 if (i >= saved_positions_num)
1152 string fname = saved_positions[i].filename;
1156 if (fname != buffer_->fileName()) {
1157 Buffer * b = bufferlist.exists(fname) ?
1158 bufferlist.getBuffer(fname) :
1159 bufferlist.loadLyXFile(fname); // don't ask, just load it
1160 if (b != 0 ) buffer(b);
1163 LyXParagraph * par = bv_->text->GetParFromID(saved_positions[i].par_id);
1167 bv_->text->SetCursor(bv_, par,
1168 min(par->Last(), saved_positions[i].par_pos));
1169 update(BufferView::SELECT|BufferView::FITCUR);
1171 owner_->getMiniBuffer()->Set(_("Moved to bookmark ") + tostr(i));
1175 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
1177 if (i >= saved_positions_num)
1180 return !saved_positions[i].filename.empty();
1184 void BufferView::Pimpl::setState()
1186 if (!lyxrc.rtl_support)
1189 LyXText * text = bv_->getLyXText();
1190 if (text->real_current_font.isRightToLeft() &&
1191 text->real_current_font.latex() != LyXFont::ON) {
1192 if (owner_->getIntl()->primarykeymap)
1193 owner_->getIntl()->KeyMapSec();
1195 if (!owner_->getIntl()->primarykeymap)
1196 owner_->getIntl()->KeyMapPrim();
1201 void BufferView::Pimpl::insetSleep()
1203 if (bv_->theLockingInset() && !bv_->inset_slept) {
1204 bv_->theLockingInset()->GetCursorPos(bv_, bv_->slx, bv_->sly);
1205 bv_->theLockingInset()->InsetUnlock(bv_);
1206 bv_->inset_slept = true;
1211 void BufferView::Pimpl::insetWakeup()
1213 if (bv_->theLockingInset() && bv_->inset_slept) {
1214 bv_->theLockingInset()->Edit(bv_, bv_->slx, bv_->sly, 0);
1215 bv_->inset_slept = false;
1220 void BufferView::Pimpl::insetUnlock()
1222 if (bv_->theLockingInset()) {
1223 if (!bv_->inset_slept)
1224 bv_->theLockingInset()->InsetUnlock(bv_);
1225 bv_->theLockingInset(0);
1226 bv_->text->FinishUndo();
1227 bv_->inset_slept = false;
1232 bool BufferView::Pimpl::focus() const
1234 return workarea_->hasFocus();
1238 void BufferView::Pimpl::focus(bool f)
1240 if (f) workarea_->setFocus();
1244 bool BufferView::Pimpl::active() const
1246 return workarea_->active();
1250 bool BufferView::Pimpl::belowMouse() const
1252 return workarea_->belowMouse();
1256 void BufferView::Pimpl::showCursor()
1259 screen_->ShowCursor(bv_->text, bv_);
1263 void BufferView::Pimpl::hideCursor()
1266 screen_->HideCursor();
1270 void BufferView::Pimpl::toggleSelection(bool b)
1273 screen_->ToggleSelection(bv_->text, bv_, b);
1277 void BufferView::Pimpl::toggleToggle()
1280 screen_->ToggleToggle(bv_->text, bv_);
1284 void BufferView::Pimpl::center()
1287 if (bv_->text->cursor.y() > static_cast<int>((workarea_->height() / 2))) {
1288 screen_->Draw(bv_->text, bv_, bv_->text->cursor.y() - workarea_->height() / 2);
1290 screen_->Draw(bv_->text, bv_, 0);
1292 update(BufferView::SELECT|BufferView::FITCUR);
1297 void BufferView::Pimpl::pasteClipboard(bool asPara)
1299 if (!buffer_) return;
1301 screen_->HideCursor();
1302 bv_->beforeChange();
1304 string const clip(workarea_->getClipboard());
1306 if (clip.empty()) return;
1309 bv_->text->InsertStringB(bv_, clip);
1311 bv_->text->InsertStringA(bv_, clip);
1313 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1317 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
1319 workarea_->putClipboard(stuff);