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 "insets/insettext.h"
25 #include "lyx_gui_misc.h"
28 #include "support/LAssert.h"
29 #include "frontends/Dialogs.h"
31 #ifdef SIGC_CXX_NAMESPACES
40 /* the selection possible is needed, that only motion events are
41 * used, where the bottom press event was on the drawing area too */
42 bool selection_possible = false;
44 extern BufferList bufferlist;
45 extern char ascii_type;
47 extern void sigchldhandler(pid_t pid, int * status);
48 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
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)
79 workarea_ = new WorkArea(xpos, ypos, width, height);
81 workarea_->scrollCB.connect(slot(this, &BufferView::Pimpl::scrollCB));
82 workarea_->workAreaExpose
83 .connect(slot(this, &BufferView::Pimpl::workAreaExpose));
84 workarea_->workAreaEnter
85 .connect(slot(this, &BufferView::Pimpl::enterView));
86 workarea_->workAreaLeave
87 .connect(slot(this, &BufferView::Pimpl::leaveView));
88 workarea_->workAreaButtonPress
89 .connect(slot(this, &BufferView::Pimpl::workAreaButtonPress));
90 workarea_->workAreaButtonRelease
92 &BufferView::Pimpl::workAreaButtonRelease));
93 workarea_->workAreaMotionNotify
94 .connect(slot(this, &BufferView::Pimpl::workAreaMotionNotify));
95 workarea_->workAreaDoubleClick
96 .connect(slot(this, &BufferView::Pimpl::doubleClick));
97 workarea_->workAreaTripleClick
98 .connect(slot(this, &BufferView::Pimpl::tripleClick));
99 workarea_->workAreaKeyPress
100 .connect(slot(this, &BufferView::Pimpl::workAreaKeyPress));
104 cursor_timeout.timeout.connect(slot(this,
105 &BufferView::Pimpl::cursorToggle));
106 current_scrollbar_value = 0;
107 cursor_timeout.start();
108 workarea_->setFocus();
109 using_xterm_cursor = false;
110 saved_positions.resize(saved_positions_num);
114 Painter & BufferView::Pimpl::painter()
116 return workarea_->getPainter();
120 void BufferView::Pimpl::buffer(Buffer * b)
122 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
126 buffer_->delUser(bv_);
128 // Put the old text into the TextCache, but
129 // only if the buffer is still loaded.
130 // Also set the owner of the test to 0
131 // bv_->text->owner(0);
132 textcache.add(buffer_, workarea_->workWidth(), bv_->text);
133 if (lyxerr.debugging())
134 textcache.show(lyxerr, "BufferView::buffer");
139 // Set current buffer
142 if (bufferlist.getState() == BufferList::CLOSING) return;
145 // screen is always deleted when the buffer is changed.
149 // If we are closing the buffer, use the first buffer as current
151 buffer_ = bufferlist.first();
155 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
156 buffer_->addUser(bv_);
157 // If we don't have a text object for this, we make one
158 if (bv_->text == 0) {
159 resizeCurrentBuffer();
164 bv_->text->first = screen_->TopCursorVisible(bv_->text);
165 owner_->updateMenubar();
166 owner_->updateToolbar();
167 // Similarly, buffer-dependent dialogs should be updated or
168 // hidden. This should go here because some dialogs (eg ToC)
169 // require bv_->text.
170 owner_->getDialogs()->updateBufferDependent(true);
174 lyxerr[Debug::INFO] << " No Buffer!" << endl;
175 owner_->updateMenubar();
176 owner_->updateToolbar();
177 owner_->getDialogs()->hideBufferDependent();
181 // Also remove all remaining text's from the testcache.
182 // (there should not be any!) (if there is any it is a
184 if (lyxerr.debugging())
185 textcache.show(lyxerr, "buffer delete all");
188 // should update layoutchoice even if we don't have a buffer.
189 owner_->updateLayoutChoice();
190 owner_->getMiniBuffer()->Init();
191 owner_->updateWindowTitle();
195 void BufferView::Pimpl::resize(int xpos, int ypos, int width, int height)
197 workarea_->resize(xpos, ypos, width, height);
198 update(bv_->text, SELECT);
203 void BufferView::Pimpl::resize()
206 resizeCurrentBuffer();
210 void BufferView::Pimpl::redraw()
212 lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
217 bool BufferView::Pimpl::fitCursor(LyXText * text)
221 bool ret = screen_->FitCursor(text, bv_);
228 void BufferView::Pimpl::redoCurrentBuffer()
230 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
231 if (buffer_ && bv_->text) {
233 owner_->updateLayoutChoice();
238 int BufferView::Pimpl::resizeCurrentBuffer()
240 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
242 LyXParagraph * par = 0;
243 LyXParagraph * selstartpar = 0;
244 LyXParagraph * selendpar = 0;
245 UpdatableInset * the_locking_inset;
255 owner_->getMiniBuffer()->Set(_("Formatting document..."));
258 par = bv_->text->cursor.par();
259 pos = bv_->text->cursor.pos();
260 selstartpar = bv_->text->sel_start_cursor.par();
261 selstartpos = bv_->text->sel_start_cursor.pos();
262 selendpar = bv_->text->sel_end_cursor.par();
263 selendpos = bv_->text->sel_end_cursor.pos();
264 selection = bv_->text->selection;
265 mark_set = bv_->text->mark_set;
266 the_locking_inset = bv_->text->the_locking_inset;
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(bv_);
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;
304 // remake the inset locking
305 bv_->text->the_locking_inset = the_locking_inset;
307 bv_->text->first = screen_->TopCursorVisible(bv_->text);
308 buffer_->resizeInsets(bv_);
309 // this will scroll the screen such that the cursor becomes visible
312 owner_->getMiniBuffer()->Init();
316 owner_->getDialogs()->hideSplash();
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(bv_->text, BufferView::UPDATE); // Maybe an empty line was deleted
526 bv_->text->SetSelection(bv_);
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(bv_);
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)
644 LyXText * text = bv_->getLyXText();
646 if (text->bv_owner && bv_->theLockingInset())
649 if (screen_ && button == 1) {
650 if (text->bv_owner) {
651 screen_->HideCursor();
652 screen_->ToggleSelection(text, bv_);
653 text->SelectWord(bv_);
654 screen_->ToggleSelection(text, bv_, false);
656 text->SelectWord(bv_);
658 /* This will fit the cursor on the screen
660 update(text, BufferView::SELECT|BufferView::FITCUR);
665 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, unsigned int button)
671 LyXText * text = bv_->getLyXText();
673 if (text->bv_owner && bv_->theLockingInset())
676 if (screen_ && (button == 1)) {
677 screen_->HideCursor();
678 screen_->ToggleSelection(text, bv_);
679 text->CursorHome(bv_);
680 text->sel_cursor = text->cursor;
681 text->CursorEnd(bv_);
682 text->SetSelection(bv_);
683 screen_->ToggleSelection(text, bv_, false);
684 /* This will fit the cursor on the screen
686 update(text, BufferView::SELECT|BufferView::FITCUR);
691 void BufferView::Pimpl::enterView()
693 if (active() && available()) {
694 SetXtermCursor(workarea_->getWin());
695 using_xterm_cursor = true;
700 void BufferView::Pimpl::leaveView()
702 if (using_xterm_cursor) {
703 XUndefineCursor(fl_get_display(), workarea_->getWin());
704 using_xterm_cursor = false;
709 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
712 if (!buffer_ || !screen_) return;
714 // If we hit an inset, we have the inset coordinates in these
715 // and inset_hit points to the inset. If we do not hit an
716 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
717 Inset * inset_hit = checkInsetHit(bv_->text, x, y, button);
719 if (bv_->theLockingInset()) {
720 // We are in inset locking mode.
722 /* LyX does a kind of work-area grabbing for insets.
723 Only a ButtonPress Event outside the inset will
724 force a InsetUnlock. */
725 bv_->theLockingInset()->
726 InsetButtonRelease(bv_, x, y, button);
730 selection_possible = false;
732 if (button >= 2) return;
737 // Did we hit an editable inset?
739 // Inset like error, notes and figures
740 selection_possible = false;
742 // CHECK fix this proper in 0.13
744 // Following a ref shouldn't issue
745 // a push on the undo-stack
746 // anylonger, now that we have
747 // keybindings for following
748 // references and returning from
749 // references. IMHO though, it
750 // should be the inset's own business
751 // to push or not push on the undo
752 // stack. They don't *have* to
753 // alter the document...
755 // ...or maybe the SetCursorParUndo()
756 // below isn't necessary at all anylonger?
757 if (inset_hit->LyxCode() == Inset::REF_CODE) {
758 bv_->text->SetCursorParUndo(bv_->buffer());
761 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
762 if (inset_hit->Editable()==Inset::HIGHLY_EDITABLE) {
763 // Highly editable inset, like math
764 UpdatableInset *inset = (UpdatableInset *)inset_hit;
765 inset->InsetButtonRelease(bv_, x, y, button);
767 inset_hit->InsetButtonRelease(bv_, x, y, button);
768 inset_hit->Edit(bv_, x, y, button);
773 // check whether we want to open a float
777 if (bv_->text->cursor.pos() <
778 bv_->text->cursor.par()->Last()) {
779 c = bv_->text->cursor.par()->
780 GetChar(bv_->text->cursor.pos());
783 if(!bv_->text->selection)
784 if (c == LyXParagraph::META_FOOTNOTE
785 || c == LyXParagraph::META_MARGIN
786 || c == LyXParagraph::META_FIG
787 || c == LyXParagraph::META_TAB
788 || c == LyXParagraph::META_WIDE_FIG
789 || c == LyXParagraph::META_WIDE_TAB
790 || c == LyXParagraph::META_ALGORITHM){
794 if (bv_->text->cursor.pos() - 1 >= 0) {
795 c = bv_->text->cursor.par()->
796 GetChar(bv_->text->cursor.pos() - 1);
798 if (c == LyXParagraph::META_FOOTNOTE
799 || c == LyXParagraph::META_MARGIN
800 || c == LyXParagraph::META_FIG
801 || c == LyXParagraph::META_TAB
802 || c == LyXParagraph::META_WIDE_FIG
803 || c == LyXParagraph::META_WIDE_TAB
804 || c == LyXParagraph::META_ALGORITHM){
805 // We are one step too far to the right
806 bv_->text->CursorLeft(bv_);
815 selection_possible = false;
821 // Do we want to close a float? (click on the float-label)
822 if (bv_->text->cursor.row()->par()->footnoteflag ==
823 LyXParagraph::OPEN_FOOTNOTE
824 && bv_->text->cursor.row()->previous() &&
825 bv_->text->cursor.row()->previous()->par()->
826 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
827 LyXFont font(LyXFont::ALL_SANE);
828 font.setSize(LyXFont::SIZE_FOOTNOTE);
830 int box_x = 20; // LYX_PAPER_MARGIN;
831 box_x += lyxfont::width(" wide-tab ", font);
833 unsigned int screen_first = bv_->text->first;
836 && y + screen_first > bv_->text->cursor.y() -
837 bv_->text->cursor.row()->baseline()
838 && y + screen_first < bv_->text->cursor.y() -
839 bv_->text->cursor.row()->baseline()
840 + lyxfont::maxAscent(font) * 1.2 + lyxfont::maxDescent(font) * 1.2) {
842 selection_possible = false;
848 // Maybe we want to edit a bibitem ale970302
849 if (bv_->text->cursor.par()->bibkey && x < 20 +
850 bibitemMaxWidth(bv_, textclasslist.
852 params.textclass).defaultfont())) {
853 bv_->text->cursor.par()->bibkey->Edit(bv_, 0, 0, 0);
861 * Returns an inset if inset was hit. 0 otherwise.
862 * If hit, the coordinates are changed relative to the inset.
863 * Otherwise coordinates are not changed, and false is returned.
865 Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y,
866 unsigned int /* button */)
871 int y_tmp = y + text->first;
874 text->SetCursorFromCoordinates(bv_, cursor, x, y_tmp);
876 if (cursor.pos() < cursor.par()->Last()
877 && cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
878 && cursor.par()->GetInset(cursor.pos())
879 && cursor.par()->GetInset(cursor.pos())->Editable()) {
881 // Check whether the inset really was hit
882 Inset * tmpinset = cursor.par()->GetInset(cursor.pos());
883 LyXFont font = text->GetFont(bv_->buffer(),
884 cursor.par(), cursor.pos());
885 int width = tmpinset->width(bv_, font);
886 int inset_x = font.isVisibleRightToLeft()
887 ? cursor.x() - width : cursor.x();
888 int start_x = inset_x + tmpinset->scroll();
889 int end_x = inset_x + width;
891 if (x > start_x && x < end_x
892 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
893 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
894 text->SetCursor(bv_, cursor.par(),cursor.pos(),true);
896 // The origin of an inset is on the baseline
897 y = y_tmp - (text->cursor.y());
902 if ((cursor.pos() - 1 >= 0) &&
903 (cursor.par()->GetChar(cursor.pos()-1) == LyXParagraph::META_INSET) &&
904 (cursor.par()->GetInset(cursor.pos() - 1)) &&
905 (cursor.par()->GetInset(cursor.pos() - 1)->Editable())) {
906 Inset * tmpinset = cursor.par()->GetInset(cursor.pos()-1);
907 LyXFont font = text->GetFont(bv_->buffer(), cursor.par(),
909 int width = tmpinset->width(bv_, font);
910 int inset_x = font.isVisibleRightToLeft()
911 ? cursor.x() : cursor.x() - width;
912 int start_x = inset_x + tmpinset->scroll();
913 int end_x = inset_x + width;
915 if (x > start_x && x < end_x
916 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
917 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
919 if (move_cursor && (tmpinset != bv_->theLockingInset()))
921 text->SetCursor(bv_, cursor.par(),cursor.pos()-1,true);
923 // The origin of an inset is on the baseline
924 y = y_tmp - (text->cursor.y());
932 void BufferView::Pimpl::workAreaExpose()
934 static int work_area_width = 0;
935 static unsigned int work_area_height = 0;
937 bool widthChange = workarea_->workWidth() != work_area_width;
938 bool heightChange = workarea_->height() != work_area_height;
940 // update from work area
941 work_area_width = workarea_->workWidth();
942 work_area_height = workarea_->height();
945 // All buffers need a resize
948 // Remove all texts from the textcache
949 // This is not _really_ what we want to do. What
950 // we really want to do is to delete in textcache
951 // that does not have a BufferView with matching
952 // width, but as long as we have only one BufferView
953 // deleting all gives the same result.
954 if (lyxerr.debugging())
955 textcache.show(lyxerr, "Expose delete all");
957 } else if (heightChange) {
958 // Rebuild image of current screen
960 // fitCursor() ensures we don't jump back
961 // to the start of the document on vertical
963 fitCursor(bv_->text);
965 // The main window size has changed, repaint most stuff
967 // ...including the minibuffer
968 owner_->getMiniBuffer()->Init();
971 screen_->Redraw(bv_->text, bv_);
973 // Grey box when we don't have a buffer
974 workarea_->greyOut();
977 // always make sure that the scrollbar is sane.
979 owner_->updateLayoutChoice();
984 void BufferView::Pimpl::update()
986 if (screen_) screen_->Update(bv_->text, bv_);
989 // Values used when calling update:
991 // -2 - update, move sel_cursor if selection, fitcursor
992 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
993 // 0 - update, move sel_cursor if selection, fitcursor
994 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
995 // 3 - update, move sel_cursor if selection
998 // a simple redraw of the parts that need refresh
1000 // move sel_cursor if selection -
1001 // the text's sel_cursor is moved if there is selection is progress
1004 // fitCursor() is called and the scrollbar updated
1007 // the buffer is marked dirty.
1016 // UPDATE_ONLY = UPDATE;
1017 // UPDATE_SELECT = UPDATE | SELECT;
1018 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
1019 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1021 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
1022 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
1023 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1024 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1025 // update(3) -> update(1) -> update(1) -> update(SELECT)
1027 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
1029 owner_->updateLayoutChoice();
1031 if (!text->selection && (f & SELECT)) {
1032 text->sel_cursor = text->cursor;
1035 text->FullRebreak(bv_);
1037 if (text->inset_owner) {
1038 text->inset_owner->SetUpdateStatus(bv_, InsetText::NONE);
1039 bv_->updateInset(text->inset_owner, true);
1048 if (buffer_->isLyxClean()) {
1049 buffer_->markDirty();
1050 owner_->getMiniBuffer()->setTimer(4);
1052 buffer_->markDirty();
1058 // Callback for cursor timer
1059 void BufferView::Pimpl::cursorToggle()
1061 // Quite a nice place for asyncron Inset updating, isn't it?
1062 // Actually no! This is run even if no buffer exist... so (Lgb)
1064 cursor_timeout.restart();
1069 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1070 if (pid == -1) // error find out what is wrong
1071 ; // ignore it for now.
1073 sigchldhandler(pid, &status);
1075 updatelist.update(bv_);
1078 cursor_timeout.restart();
1082 if (!bv_->theLockingInset()) {
1083 screen_->CursorToggle(bv_->text, bv_);
1085 bv_->theLockingInset()->ToggleInsetCursor(bv_);
1088 cursor_timeout.restart();
1092 void BufferView::Pimpl::cursorPrevious(LyXText * text)
1094 if (!text->cursor.row()->previous())
1097 int y = text->first;
1098 if (text->inset_owner)
1099 y += bv_->text->first;
1100 Row * cursorrow = text->cursor.row();
1101 text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y);
1102 bv_->text->FinishUndo();
1103 // This is to allow jumping over large insets
1104 if ((cursorrow == text->cursor.row()))
1105 text->CursorUp(bv_);
1107 if (text->inset_owner ||
1108 text->cursor.row()->height() < workarea_->height())
1109 screen_->Draw(bv_->text, bv_,
1111 - text->cursor.row()->baseline()
1112 + text->cursor.row()->height()
1113 - workarea_->height() + 1 );
1118 void BufferView::Pimpl::cursorNext(LyXText * text)
1120 if (!text->cursor.row()->next())
1123 int y = text->first + workarea_->height();
1124 // if (text->inset_owner)
1125 // y += bv_->text->first;
1126 text->GetRowNearY(y);
1128 Row * cursorrow = text->cursor.row();
1129 text->SetCursorFromCoordinates(bv_, text->cursor.x_fix(), y); // + workarea_->height());
1130 bv_->text->FinishUndo();
1131 // This is to allow jumping over large insets
1132 if ((cursorrow == bv_->text->cursor.row()))
1133 text->CursorDown(bv_);
1135 if (text->inset_owner ||
1136 text->cursor.row()->height() < workarea_->height())
1137 screen_->Draw(bv_->text, bv_, text->cursor.y() -
1138 text->cursor.row()->baseline());
1143 bool BufferView::Pimpl::available() const
1145 if (buffer_ && bv_->text) return true;
1150 void BufferView::Pimpl::beforeChange(LyXText * text)
1153 text->ClearSelection(bv_);
1157 void BufferView::Pimpl::savePosition(unsigned int i)
1159 if (i >= saved_positions_num)
1161 saved_positions[i] = Position(buffer_->fileName(),
1162 bv_->text->cursor.par()->id(),
1163 bv_->text->cursor.pos());
1165 owner_->getMiniBuffer()->Set(_("Saved bookmark ") + tostr(i));
1169 void BufferView::Pimpl::restorePosition(unsigned int i)
1171 if (i >= saved_positions_num)
1174 string fname = saved_positions[i].filename;
1176 beforeChange(bv_->text);
1178 if (fname != buffer_->fileName()) {
1179 Buffer * b = bufferlist.exists(fname) ?
1180 bufferlist.getBuffer(fname) :
1181 bufferlist.loadLyXFile(fname); // don't ask, just load it
1182 if (b != 0 ) buffer(b);
1185 LyXParagraph * par = bv_->text->GetParFromID(saved_positions[i].par_id);
1189 bv_->text->SetCursor(bv_, par,
1190 min(par->Last(), saved_positions[i].par_pos));
1191 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1193 owner_->getMiniBuffer()->Set(_("Moved to bookmark ") + tostr(i));
1197 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
1199 if (i >= saved_positions_num)
1202 return !saved_positions[i].filename.empty();
1206 void BufferView::Pimpl::setState()
1208 if (!lyxrc.rtl_support)
1211 LyXText * text = bv_->getLyXText();
1212 if (text->real_current_font.isRightToLeft() &&
1213 text->real_current_font.latex() != LyXFont::ON) {
1214 if (owner_->getIntl()->primarykeymap)
1215 owner_->getIntl()->KeyMapSec();
1217 if (!owner_->getIntl()->primarykeymap)
1218 owner_->getIntl()->KeyMapPrim();
1223 void BufferView::Pimpl::insetSleep()
1225 if (bv_->theLockingInset() && !bv_->inset_slept) {
1226 bv_->theLockingInset()->GetCursorPos(bv_, bv_->slx, bv_->sly);
1227 bv_->theLockingInset()->InsetUnlock(bv_);
1228 bv_->inset_slept = true;
1233 void BufferView::Pimpl::insetWakeup()
1235 if (bv_->theLockingInset() && bv_->inset_slept) {
1236 bv_->theLockingInset()->Edit(bv_, bv_->slx, bv_->sly, 0);
1237 bv_->inset_slept = false;
1242 void BufferView::Pimpl::insetUnlock()
1244 if (bv_->theLockingInset()) {
1245 if (!bv_->inset_slept)
1246 bv_->theLockingInset()->InsetUnlock(bv_);
1247 bv_->theLockingInset(0);
1248 bv_->text->FinishUndo();
1249 bv_->inset_slept = false;
1254 bool BufferView::Pimpl::focus() const
1256 return workarea_->hasFocus();
1260 void BufferView::Pimpl::focus(bool f)
1262 if (f) workarea_->setFocus();
1266 bool BufferView::Pimpl::active() const
1268 return workarea_->active();
1272 bool BufferView::Pimpl::belowMouse() const
1274 return workarea_->belowMouse();
1278 void BufferView::Pimpl::showCursor()
1281 screen_->ShowCursor(bv_->text, bv_);
1285 void BufferView::Pimpl::hideCursor()
1288 screen_->HideCursor();
1292 void BufferView::Pimpl::toggleSelection(bool b)
1295 screen_->ToggleSelection(bv_->text, bv_, b);
1299 void BufferView::Pimpl::toggleToggle()
1302 screen_->ToggleToggle(bv_->text, bv_);
1306 void BufferView::Pimpl::center()
1308 beforeChange(bv_->text);
1309 if (bv_->text->cursor.y() > static_cast<int>((workarea_->height() / 2))) {
1310 screen_->Draw(bv_->text, bv_, bv_->text->cursor.y() - workarea_->height() / 2);
1312 screen_->Draw(bv_->text, bv_, 0);
1314 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1319 void BufferView::Pimpl::pasteClipboard(bool asPara)
1321 if (!buffer_) return;
1323 screen_->HideCursor();
1324 beforeChange(bv_->text);
1326 string const clip(workarea_->getClipboard());
1328 if (clip.empty()) return;
1331 bv_->text->InsertStringB(bv_, clip);
1333 bv_->text->InsertStringA(bv_, clip);
1335 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1339 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
1341 workarea_->putClipboard(stuff);