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 void sigchldhandler(pid_t pid, int * status);
47 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
49 const unsigned int saved_positions_num = 20;
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_get_display(), XC_xterm);
65 XFlush(fl_get_display());
66 cursor_undefined = false;
68 XDefineCursor(fl_get_display(), win, cursor);
69 XFlush(fl_get_display());
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)
79 workarea_ = new WorkArea(bv_, xpos, ypos, width, height);
81 workarea_ = new WorkArea(xpos, ypos, width, height);
84 workarea_->scrollCB.connect(slot(this, &BufferView::Pimpl::scrollCB));
85 workarea_->workAreaExpose
86 .connect(slot(this, &BufferView::Pimpl::workAreaExpose));
87 workarea_->workAreaEnter
88 .connect(slot(this, &BufferView::Pimpl::enterView));
89 workarea_->workAreaLeave
90 .connect(slot(this, &BufferView::Pimpl::leaveView));
91 workarea_->workAreaButtonPress
92 .connect(slot(this, &BufferView::Pimpl::workAreaButtonPress));
93 workarea_->workAreaButtonRelease
95 &BufferView::Pimpl::workAreaButtonRelease));
96 workarea_->workAreaMotionNotify
97 .connect(slot(this, &BufferView::Pimpl::workAreaMotionNotify));
98 workarea_->workAreaDoubleClick
99 .connect(slot(this, &BufferView::Pimpl::doubleClick));
100 workarea_->workAreaTripleClick
101 .connect(slot(this, &BufferView::Pimpl::tripleClick));
102 workarea_->workAreaKeyPress
103 .connect(slot(this, &BufferView::Pimpl::workAreaKeyPress));
107 cursor_timeout.timeout.connect(slot(this,
108 &BufferView::Pimpl::cursorToggle));
109 current_scrollbar_value = 0;
110 cursor_timeout.start();
111 workarea_->setFocus();
112 using_xterm_cursor = false;
113 saved_positions.resize(saved_positions_num);
117 Painter & BufferView::Pimpl::painter()
119 return workarea_->getPainter();
123 void BufferView::Pimpl::buffer(Buffer * b)
125 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
129 buffer_->delUser(bv_);
131 // Put the old text into the TextCache, but
132 // only if the buffer is still loaded.
133 // Also set the owner of the test to 0
134 // bv_->text->owner(0);
135 textcache.add(buffer_, workarea_->workWidth(), bv_->text);
136 if (lyxerr.debugging())
137 textcache.show(lyxerr, "BufferView::buffer");
142 // Set current buffer
145 if (bufferlist.getState() == BufferList::CLOSING) return;
148 // screen is always deleted when the buffer is changed.
152 // If we are closing the buffer, use the first buffer as current
154 buffer_ = bufferlist.first();
158 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
159 buffer_->addUser(bv_);
160 // If we don't have a text object for this, we make one
161 if (bv_->text == 0) {
162 resizeCurrentBuffer();
167 bv_->text->first = screen_->TopCursorVisible(bv_->text);
168 owner_->updateMenubar();
169 owner_->updateToolbar();
170 // Similarly, buffer-dependent dialogs should be updated or
171 // hidden. This should go here because some dialogs (eg ToC)
172 // require bv_->text.
173 owner_->getDialogs()->updateBufferDependent(true);
177 lyxerr[Debug::INFO] << " No Buffer!" << endl;
178 owner_->updateMenubar();
179 owner_->updateToolbar();
180 owner_->getDialogs()->hideBufferDependent();
184 // Also remove all remaining text's from the testcache.
185 // (there should not be any!) (if there is any it is a
187 if (lyxerr.debugging())
188 textcache.show(lyxerr, "buffer delete all");
191 // should update layoutchoice even if we don't have a buffer.
192 owner_->updateLayoutChoice();
193 owner_->getMiniBuffer()->Init();
194 owner_->updateWindowTitle();
198 void BufferView::Pimpl::resize(int xpos, int ypos, int width, int height)
200 workarea_->resize(xpos, ypos, width, height);
206 void BufferView::Pimpl::resize()
209 resizeCurrentBuffer();
213 void BufferView::Pimpl::redraw()
215 lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
220 bool BufferView::Pimpl::fitCursor(LyXText * text)
224 bool ret = screen_->FitCursor(text, bv_);
231 void BufferView::Pimpl::redoCurrentBuffer()
233 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
234 if (buffer_ && bv_->text) {
236 owner_->updateLayoutChoice();
241 int BufferView::Pimpl::resizeCurrentBuffer()
243 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
245 LyXParagraph * par = 0;
246 LyXParagraph * selstartpar = 0;
247 LyXParagraph * selendpar = 0;
256 owner_->getMiniBuffer()->Set(_("Formatting document..."));
259 par = bv_->text->cursor.par();
260 pos = bv_->text->cursor.pos();
261 selstartpar = bv_->text->sel_start_cursor.par();
262 selstartpos = bv_->text->sel_start_cursor.pos();
263 selendpar = bv_->text->sel_end_cursor.par();
264 selendpos = bv_->text->sel_end_cursor.pos();
265 selection = bv_->text->selection;
266 mark_set = bv_->text->mark_set;
268 bv_->text = new LyXText(bv_);
270 // See if we have a text in TextCache that fits
271 // the new buffer_ with the correct width.
272 bv_->text = textcache.findFit(buffer_, workarea_->workWidth());
274 if (lyxerr.debugging()) {
275 lyxerr << "Found a LyXText that fits:\n";
276 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea_->workWidth(), bv_->text)));
278 // Set the owner of the newly found text
279 // bv_->text->owner(bv_);
280 if (lyxerr.debugging())
281 textcache.show(lyxerr, "resizeCurrentBuffer");
283 bv_->text = new LyXText(bv_);
289 bv_->text->selection = true;
290 /* at this point just to avoid the Delete-Empty-Paragraph
291 * Mechanism when setting the cursor */
292 bv_->text->mark_set = mark_set;
294 bv_->text->SetCursor(bv_, selstartpar, selstartpos);
295 bv_->text->sel_cursor = bv_->text->cursor;
296 bv_->text->SetCursor(bv_, selendpar, selendpos);
297 bv_->text->SetSelection();
298 bv_->text->SetCursor(bv_, par, pos);
300 bv_->text->SetCursor(bv_, par, pos);
301 bv_->text->sel_cursor = bv_->text->cursor;
302 bv_->text->selection = false;
305 bv_->text->first = screen_->TopCursorVisible(bv_->text);
306 buffer_->resizeInsets(bv_);
307 // this will scroll the screen such that the cursor becomes visible
310 owner_->getMiniBuffer()->Init();
314 owner_->getDialogs()->hideSplash();
320 void BufferView::Pimpl::updateScreen()
322 // Regenerate the screen.
324 screen_ = new LyXScreen(*workarea_);
328 void BufferView::Pimpl::updateScrollbar()
330 /* If the text is smaller than the working area, the scrollbar
331 * maximum must be the working area height. No scrolling will
335 workarea_->setScrollbar(0, 1.0);
339 static unsigned long max2 = 0;
340 static unsigned long height2 = 0;
342 unsigned long cbth = 0;
346 cbth = bv_->text->height;
347 cbsf = bv_->text->first;
350 // check if anything has changed.
352 height2 == workarea_->height() &&
353 current_scrollbar_value == cbsf)
356 height2 = workarea_->height();
357 current_scrollbar_value = cbsf;
359 if (cbth <= height2) { // text is smaller than screen
360 workarea_->setScrollbar(0, 1.0); // right?
364 long maximum_height = workarea_->height() * 3 / 4 + cbth;
368 double hfloat = workarea_->height();
369 double maxfloat = maximum_height;
371 float slider_size = 0.0;
372 int slider_value = value;
374 workarea_->setScrollbarBounds(0, bv_->text->height - workarea_->height());
375 double lineh = bv_->text->DefaultHeight();
376 workarea_->setScrollbarIncrements(lineh);
377 if (maxfloat > 0.0) {
378 if ((hfloat / maxfloat) * float(height2) < 3)
379 slider_size = 3.0/float(height2);
381 slider_size = hfloat / maxfloat;
383 slider_size = hfloat;
385 workarea_->setScrollbar(slider_value, slider_size / workarea_->height());
389 // Callback for scrollbar slider
390 void BufferView::Pimpl::scrollCB(double value)
392 if (!buffer_) return;
394 current_scrollbar_value = long(value);
395 if (current_scrollbar_value < 0)
396 current_scrollbar_value = 0;
401 screen_->Draw(bv_->text, bv_, current_scrollbar_value);
403 if (!lyxrc.cursor_follows_scrollbar) {
408 LyXText * vbt = bv_->text;
410 int const height = vbt->DefaultHeight();
411 int const first = static_cast<int>((bv_->text->first + height));
412 int const last = static_cast<int>((bv_->text->first + workarea_->height() - height));
414 if (vbt->cursor.y() < first)
415 vbt->SetCursorFromCoordinates(bv_, 0, first);
416 else if (vbt->cursor.y() > last)
417 vbt->SetCursorFromCoordinates(bv_, 0, last);
423 int BufferView::Pimpl::scrollUp(long time)
425 if (!buffer_) return 0;
426 if (!screen_) return 0;
428 double value = workarea_->getScrollbarValue();
430 if (value == 0) return 0;
432 float add_value = (bv_->text->DefaultHeight()
433 + float(time) * float(time) * 0.125);
435 if (add_value > workarea_->height())
436 add_value = float(workarea_->height() -
437 bv_->text->DefaultHeight());
444 workarea_->setScrollbarValue(value);
451 int BufferView::Pimpl::scrollDown(long time)
453 if (!buffer_) return 0;
454 if (!screen_) return 0;
456 double value= workarea_->getScrollbarValue();
457 pair<float, float> p = workarea_->getScrollbarBounds();
458 double max = p.second;
460 if (value == max) return 0;
462 float add_value = (bv_->text->DefaultHeight()
463 + float(time) * float(time) * 0.125);
465 if (add_value > workarea_->height())
466 add_value = float(workarea_->height() -
467 bv_->text->DefaultHeight());
474 workarea_->setScrollbarValue(value);
481 void BufferView::Pimpl::workAreaKeyPress(KeySym keysym, unsigned int state)
483 bv_->owner()->getLyXFunc()->processKeySym(keysym, state);
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_ || !screen_) return;
495 // Check for inset locking
496 if (bv_->theLockingInset()) {
497 LyXCursor cursor = bv_->text->cursor;
498 LyXFont font = bv_->text->GetFont(bv_->buffer(),
499 cursor.par(), cursor.pos());
500 int width = bv_->theLockingInset()->width(bv_, font);
501 int inset_x = font.isVisibleRightToLeft()
502 ? cursor.x() - width : cursor.x();
503 int start_x = inset_x + bv_->theLockingInset()->scroll();
504 bv_->theLockingInset()->
505 InsetMotionNotify(bv_,
507 y - cursor.y() + bv_->text->first,
512 /* The test for not selection possible is needed, that only motion events are
513 * used, where the bottom press event was on the drawing area too */
514 if (!selection_possible)
517 screen_->HideCursor();
519 bv_->text->SetCursorFromCoordinates(bv_, x, y + bv_->text->first);
521 if (!bv_->text->selection)
522 update(BufferView::UPDATE); // Maybe an empty line was deleted
524 bv_->text->SetSelection();
525 screen_->ToggleToggle(bv_->text, bv_);
526 fitCursor(bv_->text);
527 screen_->ShowCursor(bv_->text, bv_);
531 // Single-click on work area
532 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
538 if (!buffer_ || !screen_) return;
540 Inset * inset_hit = checkInsetHit(bv_->text, xpos, ypos, button);
542 // ok ok, this is a hack.
543 if (button == 4 || button == 5) {
546 scrollUp(lyxrc.wheel_jump); // default 100, set in lyxrc
549 scrollDown(lyxrc.wheel_jump);
554 if (bv_->theLockingInset()) {
555 // We are in inset locking mode
557 /* Check whether the inset was hit. If not reset mode,
558 otherwise give the event to the inset */
559 if (inset_hit == bv_->theLockingInset()) {
560 bv_->theLockingInset()->
561 InsetButtonPress(bv_,
566 bv_->unlockInset(bv_->theLockingInset());
571 selection_possible = true;
572 screen_->HideCursor();
574 int const screen_first = bv_->text->first;
576 // Middle button press pastes if we have a selection
577 bool paste_internally = false;
579 && bv_->text->selection) {
580 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
581 paste_internally = true;
584 // Clear the selection
585 screen_->ToggleSelection(bv_->text, bv_);
586 bv_->text->ClearSelection();
587 bv_->text->FullRebreak(bv_);
588 screen_->Update(bv_->text, bv_);
591 // Single left click in math inset?
592 if ((inset_hit != 0) &&
593 (inset_hit->Editable()==Inset::HIGHLY_EDITABLE)) {
594 // Highly editable inset, like math
595 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
596 selection_possible = false;
597 owner_->updateLayoutChoice();
598 owner_->getMiniBuffer()->Set(inset->EditMessage());
599 inset->InsetButtonPress(bv_, xpos, ypos, button);
600 inset->Edit(bv_, xpos, ypos, button);
604 // Right click on a footnote flag opens float menu
606 selection_possible = false;
610 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
611 bv_->text->SetCursorFromCoordinates(bv_, xpos, ypos + screen_first);
612 bv_->text->FinishUndo();
613 bv_->text->sel_cursor = bv_->text->cursor;
614 bv_->text->cursor.x_fix(bv_->text->cursor.x());
616 owner_->updateLayoutChoice();
617 if (fitCursor(bv_->text)) {
618 selection_possible = false;
621 // Insert primary selection with middle mouse
622 // if there is a local selection in the current buffer,
625 if (paste_internally)
626 owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
628 owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
630 selection_possible = false;
636 void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, unsigned int button)
639 if (buffer_ && !bv_->theLockingInset()) {
640 if (screen_ && button == 1) {
641 screen_->HideCursor();
642 screen_->ToggleSelection(bv_->text, bv_);
643 bv_->text->SelectWord(bv_);
644 screen_->ToggleSelection(bv_->text, bv_, false);
645 /* This will fit the cursor on the screen
647 update(BufferView::SELECT|BufferView::FITCUR);
653 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, unsigned int button)
656 if (buffer_ && screen_ && !bv_->theLockingInset() && (button == 1)) {
657 screen_->HideCursor();
658 screen_->ToggleSelection(bv_->text, bv_);
659 bv_->text->CursorHome(bv_);
660 bv_->text->sel_cursor = bv_->text->cursor;
661 bv_->text->CursorEnd(bv_);
662 bv_->text->SetSelection();
663 screen_->ToggleSelection(bv_->text, bv_, false);
664 /* This will fit the cursor on the screen
666 update(BufferView::SELECT|BufferView::FITCUR);
671 void BufferView::Pimpl::enterView()
673 if (active() && available()) {
674 SetXtermCursor(workarea_->getWin());
675 using_xterm_cursor = true;
680 void BufferView::Pimpl::leaveView()
682 if (using_xterm_cursor) {
683 XUndefineCursor(fl_get_display(), workarea_->getWin());
684 using_xterm_cursor = false;
689 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
692 if (!buffer_ || !screen_) return;
694 // If we hit an inset, we have the inset coordinates in these
695 // and inset_hit points to the inset. If we do not hit an
696 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
697 Inset * inset_hit = checkInsetHit(bv_->text, x, y, button);
699 if (bv_->theLockingInset()) {
700 // We are in inset locking mode.
702 /* LyX does a kind of work-area grabbing for insets.
703 Only a ButtonPress Event outside the inset will
704 force a InsetUnlock. */
705 bv_->theLockingInset()->
706 InsetButtonRelease(bv_, x, y, button);
710 selection_possible = false;
712 if (button >= 2) return;
717 // Did we hit an editable inset?
719 // Inset like error, notes and figures
720 selection_possible = false;
722 // CHECK fix this proper in 0.13
724 // Following a ref shouldn't issue
725 // a push on the undo-stack
726 // anylonger, now that we have
727 // keybindings for following
728 // references and returning from
729 // references. IMHO though, it
730 // should be the inset's own business
731 // to push or not push on the undo
732 // stack. They don't *have* to
733 // alter the document...
735 // ...or maybe the SetCursorParUndo()
736 // below isn't necessary at all anylonger?
737 if (inset_hit->LyxCode() == Inset::REF_CODE) {
738 bv_->text->SetCursorParUndo(bv_->buffer());
741 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
742 if (inset_hit->Editable()==Inset::HIGHLY_EDITABLE) {
743 // Highly editable inset, like math
744 UpdatableInset *inset = (UpdatableInset *)inset_hit;
745 inset->InsetButtonRelease(bv_, x, y, button);
747 inset_hit->InsetButtonRelease(bv_, x, y, button);
748 inset_hit->Edit(bv_, x, y, button);
753 // check whether we want to open a float
757 if (bv_->text->cursor.pos() <
758 bv_->text->cursor.par()->Last()) {
759 c = bv_->text->cursor.par()->
760 GetChar(bv_->text->cursor.pos());
763 if(!bv_->text->selection)
764 if (c == LyXParagraph::META_FOOTNOTE
765 || c == LyXParagraph::META_MARGIN
766 || c == LyXParagraph::META_FIG
767 || c == LyXParagraph::META_TAB
768 || c == LyXParagraph::META_WIDE_FIG
769 || c == LyXParagraph::META_WIDE_TAB
770 || c == LyXParagraph::META_ALGORITHM){
774 if (bv_->text->cursor.pos() - 1 >= 0) {
775 c = bv_->text->cursor.par()->
776 GetChar(bv_->text->cursor.pos() - 1);
778 if (c == LyXParagraph::META_FOOTNOTE
779 || c == LyXParagraph::META_MARGIN
780 || c == LyXParagraph::META_FIG
781 || c == LyXParagraph::META_TAB
782 || c == LyXParagraph::META_WIDE_FIG
783 || c == LyXParagraph::META_WIDE_TAB
784 || c == LyXParagraph::META_ALGORITHM){
785 // We are one step too far to the right
786 bv_->text->CursorLeft(bv_);
795 selection_possible = false;
801 // Do we want to close a float? (click on the float-label)
802 if (bv_->text->cursor.row()->par()->footnoteflag ==
803 LyXParagraph::OPEN_FOOTNOTE
804 && bv_->text->cursor.row()->previous() &&
805 bv_->text->cursor.row()->previous()->par()->
806 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
807 LyXFont font(LyXFont::ALL_SANE);
808 font.setSize(LyXFont::SIZE_FOOTNOTE);
810 int box_x = 20; // LYX_PAPER_MARGIN;
811 box_x += lyxfont::width(" wide-tab ", font);
813 unsigned int screen_first = bv_->text->first;
816 && y + screen_first > bv_->text->cursor.y() -
817 bv_->text->cursor.row()->baseline()
818 && y + screen_first < bv_->text->cursor.y() -
819 bv_->text->cursor.row()->baseline()
820 + lyxfont::maxAscent(font) * 1.2 + lyxfont::maxDescent(font) * 1.2) {
822 selection_possible = false;
828 // Maybe we want to edit a bibitem ale970302
829 if (bv_->text->cursor.par()->bibkey && x < 20 +
830 bibitemMaxWidth(bv_, textclasslist.
832 params.textclass).defaultfont())) {
833 bv_->text->cursor.par()->bibkey->Edit(bv_, 0, 0, 0);
841 * Returns an inset if inset was hit. 0 otherwise.
842 * If hit, the coordinates are changed relative to the inset.
843 * Otherwise coordinates are not changed, and false is returned.
845 Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y,
846 unsigned int /* button */)
851 int y_tmp = y + text->first;
854 text->SetCursorFromCoordinates(bv_, cursor, x, y_tmp);
856 if (cursor.pos() < cursor.par()->Last()
857 && cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
858 && cursor.par()->GetInset(cursor.pos())
859 && cursor.par()->GetInset(cursor.pos())->Editable()) {
861 // Check whether the inset really was hit
862 Inset * tmpinset = cursor.par()->GetInset(cursor.pos());
863 LyXFont font = text->GetFont(bv_->buffer(),
864 cursor.par(), cursor.pos());
865 int width = tmpinset->width(bv_, font);
866 int inset_x = font.isVisibleRightToLeft()
867 ? cursor.x() - width : cursor.x();
868 int start_x = inset_x + tmpinset->scroll();
869 int end_x = inset_x + width;
871 if (x > start_x && x < end_x
872 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
873 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
874 text->SetCursor(bv_, cursor.par(),cursor.pos(),true);
876 // The origin of an inset is on the baseline
877 y = y_tmp - (text->cursor.y());
882 if ((cursor.pos() - 1 >= 0) &&
883 (cursor.par()->GetChar(cursor.pos()-1) == LyXParagraph::META_INSET) &&
884 (cursor.par()->GetInset(cursor.pos() - 1)) &&
885 (cursor.par()->GetInset(cursor.pos() - 1)->Editable())) {
886 Inset * tmpinset = cursor.par()->GetInset(cursor.pos()-1);
887 LyXFont font = text->GetFont(bv_->buffer(), cursor.par(),
889 int width = tmpinset->width(bv_, font);
890 int inset_x = font.isVisibleRightToLeft()
891 ? cursor.x() : cursor.x() - width;
892 int start_x = inset_x + tmpinset->scroll();
893 int end_x = inset_x + width;
895 if (x > start_x && x < end_x
896 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
897 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
899 if (move_cursor && (tmpinset != bv_->theLockingInset()))
901 text->SetCursor(bv_, cursor.par(),cursor.pos()-1,true);
903 // The origin of an inset is on the baseline
904 y = y_tmp - (text->cursor.y());
912 void BufferView::Pimpl::workAreaExpose()
914 static int work_area_width = 0;
915 static unsigned int work_area_height = 0;
917 bool widthChange = workarea_->workWidth() != work_area_width;
918 bool heightChange = workarea_->height() != work_area_height;
920 // update from work area
921 work_area_width = workarea_->workWidth();
922 work_area_height = workarea_->height();
925 // All buffers need a resize
928 // Remove all texts from the textcache
929 // This is not _really_ what we want to do. What
930 // we really want to do is to delete in textcache
931 // that does not have a BufferView with matching
932 // width, but as long as we have only one BufferView
933 // deleting all gives the same result.
934 if (lyxerr.debugging())
935 textcache.show(lyxerr, "Expose delete all");
937 } else if (heightChange) {
938 // Rebuild image of current screen
940 // fitCursor() ensures we don't jump back
941 // to the start of the document on vertical
943 fitCursor(bv_->text);
945 // The main window size has changed, repaint most stuff
947 // ...including the minibuffer
948 owner_->getMiniBuffer()->Init();
951 screen_->Redraw(bv_->text, bv_);
953 // Grey box when we don't have a buffer
954 workarea_->greyOut();
957 // always make sure that the scrollbar is sane.
959 owner_->updateLayoutChoice();
964 void BufferView::Pimpl::update()
966 if (screen_) screen_->Update(bv_->text, bv_);
969 // Values used when calling update:
971 // -2 - update, move sel_cursor if selection, fitcursor
972 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
973 // 0 - update, move sel_cursor if selection, fitcursor
974 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
975 // 3 - update, move sel_cursor if selection
978 // a simple redraw of the parts that need refresh
980 // move sel_cursor if selection -
981 // the text's sel_cursor is moved if there is selection is progress
984 // fitCursor() is called and the scrollbar updated
987 // the buffer is marked dirty.
996 // UPDATE_ONLY = UPDATE;
997 // UPDATE_SELECT = UPDATE | SELECT;
998 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
999 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1001 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
1002 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
1003 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1004 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1005 // update(3) -> update(1) -> update(1) -> update(SELECT)
1007 void BufferView::Pimpl::update(BufferView::UpdateCodes f)
1009 owner_->updateLayoutChoice();
1011 if (!bv_->text->selection && (f & SELECT)) {
1012 bv_->text->sel_cursor = bv_->text->cursor;
1015 bv_->text->FullRebreak(bv_);
1020 fitCursor(bv_->text);
1024 if (buffer_->isLyxClean()) {
1025 buffer_->markDirty();
1026 owner_->getMiniBuffer()->setTimer(4);
1028 buffer_->markDirty();
1034 // Callback for cursor timer
1035 void BufferView::Pimpl::cursorToggle()
1037 // Quite a nice place for asyncron Inset updating, isn't it?
1038 // Actually no! This is run even if no buffer exist... so (Lgb)
1040 cursor_timeout.restart();
1045 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1046 if (pid == -1) // error find out what is wrong
1047 ; // ignore it for now.
1049 sigchldhandler(pid, &status);
1051 updatelist.update(bv_);
1054 cursor_timeout.restart();
1058 if (!bv_->theLockingInset()) {
1059 screen_->CursorToggle(bv_->text, bv_);
1061 bv_->theLockingInset()->ToggleInsetCursor(bv_);
1064 cursor_timeout.restart();
1068 void BufferView::Pimpl::cursorPrevious(LyXText * text)
1070 if (!text->cursor.row()->previous())
1073 int y = text->first;
1074 if (text->inset_owner)
1075 y += bv_->text->first;
1076 Row * cursorrow = text->cursor.row();
1077 text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y);
1078 bv_->text->FinishUndo();
1079 // This is to allow jumping over large insets
1080 if ((cursorrow == text->cursor.row()))
1081 text->CursorUp(bv_);
1083 if (text->inset_owner ||
1084 text->cursor.row()->height() < workarea_->height())
1085 screen_->Draw(bv_->text, bv_,
1087 - text->cursor.row()->baseline()
1088 + text->cursor.row()->height()
1089 - workarea_->height() + 1 );
1094 void BufferView::Pimpl::cursorNext(LyXText * text)
1096 if (!text->cursor.row()->next())
1099 int y = text->first + workarea_->height();
1100 // if (text->inset_owner)
1101 // y += bv_->text->first;
1102 text->GetRowNearY(y);
1104 Row * cursorrow = text->cursor.row();
1105 text->SetCursorFromCoordinates(bv_, text->cursor.x_fix(), y); // + workarea_->height());
1106 bv_->text->FinishUndo();
1107 // This is to allow jumping over large insets
1108 if ((cursorrow == bv_->text->cursor.row()))
1109 text->CursorDown(bv_);
1111 if (text->inset_owner ||
1112 text->cursor.row()->height() < workarea_->height())
1113 screen_->Draw(bv_->text, bv_, text->cursor.y() -
1114 text->cursor.row()->baseline());
1119 bool BufferView::Pimpl::available() const
1121 if (buffer_ && bv_->text) return true;
1126 void BufferView::Pimpl::beforeChange()
1129 bv_->text->ClearSelection();
1133 void BufferView::Pimpl::savePosition(unsigned int i)
1135 if (i >= saved_positions_num)
1137 saved_positions[i] = Position(buffer_->fileName(),
1138 bv_->text->cursor.par()->id(),
1139 bv_->text->cursor.pos());
1141 owner_->getMiniBuffer()->Set(_("Saved bookmark ") + tostr(i));
1145 void BufferView::Pimpl::restorePosition(unsigned int i)
1147 if (i >= saved_positions_num)
1150 string fname = saved_positions[i].filename;
1154 if (fname != buffer_->fileName()) {
1155 Buffer * b = bufferlist.exists(fname) ?
1156 bufferlist.getBuffer(fname) :
1157 bufferlist.loadLyXFile(fname); // don't ask, just load it
1158 if (b != 0 ) buffer(b);
1161 LyXParagraph * par = bv_->text->GetParFromID(saved_positions[i].par_id);
1165 bv_->text->SetCursor(bv_, par,
1166 min(par->Last(), saved_positions[i].par_pos));
1167 update(BufferView::SELECT|BufferView::FITCUR);
1169 owner_->getMiniBuffer()->Set(_("Moved to bookmark ") + tostr(i));
1173 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
1175 if (i >= saved_positions_num)
1178 return !saved_positions[i].filename.empty();
1182 void BufferView::Pimpl::setState()
1184 if (!lyxrc.rtl_support)
1187 LyXText * text = bv_->getLyXText();
1188 if (text->real_current_font.isRightToLeft() &&
1189 text->real_current_font.latex() != LyXFont::ON) {
1190 if (owner_->getIntl()->primarykeymap)
1191 owner_->getIntl()->KeyMapSec();
1193 if (!owner_->getIntl()->primarykeymap)
1194 owner_->getIntl()->KeyMapPrim();
1199 void BufferView::Pimpl::insetSleep()
1201 if (bv_->theLockingInset() && !bv_->inset_slept) {
1202 bv_->theLockingInset()->GetCursorPos(bv_, bv_->slx, bv_->sly);
1203 bv_->theLockingInset()->InsetUnlock(bv_);
1204 bv_->inset_slept = true;
1209 void BufferView::Pimpl::insetWakeup()
1211 if (bv_->theLockingInset() && bv_->inset_slept) {
1212 bv_->theLockingInset()->Edit(bv_, bv_->slx, bv_->sly, 0);
1213 bv_->inset_slept = false;
1218 void BufferView::Pimpl::insetUnlock()
1220 if (bv_->theLockingInset()) {
1221 if (!bv_->inset_slept)
1222 bv_->theLockingInset()->InsetUnlock(bv_);
1223 bv_->theLockingInset(0);
1224 bv_->text->FinishUndo();
1225 bv_->inset_slept = false;
1230 bool BufferView::Pimpl::focus() const
1232 return workarea_->hasFocus();
1236 void BufferView::Pimpl::focus(bool f)
1238 if (f) workarea_->setFocus();
1242 bool BufferView::Pimpl::active() const
1244 return workarea_->active();
1248 bool BufferView::Pimpl::belowMouse() const
1250 return workarea_->belowMouse();
1254 void BufferView::Pimpl::showCursor()
1257 screen_->ShowCursor(bv_->text, bv_);
1261 void BufferView::Pimpl::hideCursor()
1264 screen_->HideCursor();
1268 void BufferView::Pimpl::toggleSelection(bool b)
1271 screen_->ToggleSelection(bv_->text, bv_, b);
1275 void BufferView::Pimpl::toggleToggle()
1278 screen_->ToggleToggle(bv_->text, bv_);
1282 void BufferView::Pimpl::center()
1285 if (bv_->text->cursor.y() > static_cast<int>((workarea_->height() / 2))) {
1286 screen_->Draw(bv_->text, bv_, bv_->text->cursor.y() - workarea_->height() / 2);
1288 screen_->Draw(bv_->text, bv_, 0);
1290 update(BufferView::SELECT|BufferView::FITCUR);
1295 void BufferView::Pimpl::pasteClipboard(bool asPara)
1297 if (!buffer_) return;
1299 screen_->HideCursor();
1300 bv_->beforeChange();
1302 string const clip(workarea_->getClipboard());
1304 if (clip.empty()) return;
1307 bv_->text->InsertStringB(bv_, clip);
1309 bv_->text->InsertStringA(bv_, clip);
1311 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1315 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
1317 workarea_->putClipboard(stuff);