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();
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 screen_->HideCursor();
651 screen_->ToggleSelection(text, bv_);
652 text->SelectWord(bv_);
653 screen_->ToggleSelection(text, bv_, false);
654 /* This will fit the cursor on the screen
656 update(text, BufferView::SELECT|BufferView::FITCUR);
661 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, unsigned int button)
667 LyXText * text = bv_->getLyXText();
669 if (text->bv_owner && bv_->theLockingInset())
672 if (screen_ && (button == 1)) {
673 screen_->HideCursor();
674 screen_->ToggleSelection(text, bv_);
675 text->CursorHome(bv_);
676 text->sel_cursor = text->cursor;
677 text->CursorEnd(bv_);
678 text->SetSelection(bv_);
679 screen_->ToggleSelection(text, bv_, false);
680 /* This will fit the cursor on the screen
682 update(text, BufferView::SELECT|BufferView::FITCUR);
687 void BufferView::Pimpl::enterView()
689 if (active() && available()) {
690 SetXtermCursor(workarea_->getWin());
691 using_xterm_cursor = true;
696 void BufferView::Pimpl::leaveView()
698 if (using_xterm_cursor) {
699 XUndefineCursor(fl_get_display(), workarea_->getWin());
700 using_xterm_cursor = false;
705 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
708 if (!buffer_ || !screen_) return;
710 // If we hit an inset, we have the inset coordinates in these
711 // and inset_hit points to the inset. If we do not hit an
712 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
713 Inset * inset_hit = checkInsetHit(bv_->text, x, y, button);
715 if (bv_->theLockingInset()) {
716 // We are in inset locking mode.
718 /* LyX does a kind of work-area grabbing for insets.
719 Only a ButtonPress Event outside the inset will
720 force a InsetUnlock. */
721 bv_->theLockingInset()->
722 InsetButtonRelease(bv_, x, y, button);
726 selection_possible = false;
728 if (button >= 2) return;
733 // Did we hit an editable inset?
735 // Inset like error, notes and figures
736 selection_possible = false;
738 // CHECK fix this proper in 0.13
740 // Following a ref shouldn't issue
741 // a push on the undo-stack
742 // anylonger, now that we have
743 // keybindings for following
744 // references and returning from
745 // references. IMHO though, it
746 // should be the inset's own business
747 // to push or not push on the undo
748 // stack. They don't *have* to
749 // alter the document...
751 // ...or maybe the SetCursorParUndo()
752 // below isn't necessary at all anylonger?
753 if (inset_hit->LyxCode() == Inset::REF_CODE) {
754 bv_->text->SetCursorParUndo(bv_->buffer());
757 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
758 if (inset_hit->Editable()==Inset::HIGHLY_EDITABLE) {
759 // Highly editable inset, like math
760 UpdatableInset *inset = (UpdatableInset *)inset_hit;
761 inset->InsetButtonRelease(bv_, x, y, button);
763 inset_hit->InsetButtonRelease(bv_, x, y, button);
764 inset_hit->Edit(bv_, x, y, button);
769 // check whether we want to open a float
773 if (bv_->text->cursor.pos() <
774 bv_->text->cursor.par()->Last()) {
775 c = bv_->text->cursor.par()->
776 GetChar(bv_->text->cursor.pos());
779 if(!bv_->text->selection)
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){
790 if (bv_->text->cursor.pos() - 1 >= 0) {
791 c = bv_->text->cursor.par()->
792 GetChar(bv_->text->cursor.pos() - 1);
794 if (c == LyXParagraph::META_FOOTNOTE
795 || c == LyXParagraph::META_MARGIN
796 || c == LyXParagraph::META_FIG
797 || c == LyXParagraph::META_TAB
798 || c == LyXParagraph::META_WIDE_FIG
799 || c == LyXParagraph::META_WIDE_TAB
800 || c == LyXParagraph::META_ALGORITHM){
801 // We are one step too far to the right
802 bv_->text->CursorLeft(bv_);
811 selection_possible = false;
817 // Do we want to close a float? (click on the float-label)
818 if (bv_->text->cursor.row()->par()->footnoteflag ==
819 LyXParagraph::OPEN_FOOTNOTE
820 && bv_->text->cursor.row()->previous() &&
821 bv_->text->cursor.row()->previous()->par()->
822 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
823 LyXFont font(LyXFont::ALL_SANE);
824 font.setSize(LyXFont::SIZE_FOOTNOTE);
826 int box_x = 20; // LYX_PAPER_MARGIN;
827 box_x += lyxfont::width(" wide-tab ", font);
829 unsigned int screen_first = bv_->text->first;
832 && y + screen_first > bv_->text->cursor.y() -
833 bv_->text->cursor.row()->baseline()
834 && y + screen_first < bv_->text->cursor.y() -
835 bv_->text->cursor.row()->baseline()
836 + lyxfont::maxAscent(font) * 1.2 + lyxfont::maxDescent(font) * 1.2) {
838 selection_possible = false;
844 // Maybe we want to edit a bibitem ale970302
845 if (bv_->text->cursor.par()->bibkey && x < 20 +
846 bibitemMaxWidth(bv_, textclasslist.
848 params.textclass).defaultfont())) {
849 bv_->text->cursor.par()->bibkey->Edit(bv_, 0, 0, 0);
857 * Returns an inset if inset was hit. 0 otherwise.
858 * If hit, the coordinates are changed relative to the inset.
859 * Otherwise coordinates are not changed, and false is returned.
861 Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y,
862 unsigned int /* button */)
867 int y_tmp = y + text->first;
870 text->SetCursorFromCoordinates(bv_, cursor, x, y_tmp);
872 if (cursor.pos() < cursor.par()->Last()
873 && cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
874 && cursor.par()->GetInset(cursor.pos())
875 && cursor.par()->GetInset(cursor.pos())->Editable()) {
877 // Check whether the inset really was hit
878 Inset * tmpinset = cursor.par()->GetInset(cursor.pos());
879 LyXFont font = text->GetFont(bv_->buffer(),
880 cursor.par(), cursor.pos());
881 int width = tmpinset->width(bv_, font);
882 int inset_x = font.isVisibleRightToLeft()
883 ? cursor.x() - width : cursor.x();
884 int start_x = inset_x + tmpinset->scroll();
885 int end_x = inset_x + width;
887 if (x > start_x && x < end_x
888 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
889 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
890 text->SetCursor(bv_, cursor.par(),cursor.pos(),true);
892 // The origin of an inset is on the baseline
893 y = y_tmp - (text->cursor.y());
898 if ((cursor.pos() - 1 >= 0) &&
899 (cursor.par()->GetChar(cursor.pos()-1) == LyXParagraph::META_INSET) &&
900 (cursor.par()->GetInset(cursor.pos() - 1)) &&
901 (cursor.par()->GetInset(cursor.pos() - 1)->Editable())) {
902 Inset * tmpinset = cursor.par()->GetInset(cursor.pos()-1);
903 LyXFont font = text->GetFont(bv_->buffer(), cursor.par(),
905 int width = tmpinset->width(bv_, font);
906 int inset_x = font.isVisibleRightToLeft()
907 ? cursor.x() : cursor.x() - width;
908 int start_x = inset_x + tmpinset->scroll();
909 int end_x = inset_x + width;
911 if (x > start_x && x < end_x
912 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
913 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
915 if (move_cursor && (tmpinset != bv_->theLockingInset()))
917 text->SetCursor(bv_, cursor.par(),cursor.pos()-1,true);
919 // The origin of an inset is on the baseline
920 y = y_tmp - (text->cursor.y());
928 void BufferView::Pimpl::workAreaExpose()
930 static int work_area_width = 0;
931 static unsigned int work_area_height = 0;
933 bool widthChange = workarea_->workWidth() != work_area_width;
934 bool heightChange = workarea_->height() != work_area_height;
936 // update from work area
937 work_area_width = workarea_->workWidth();
938 work_area_height = workarea_->height();
941 // All buffers need a resize
944 // Remove all texts from the textcache
945 // This is not _really_ what we want to do. What
946 // we really want to do is to delete in textcache
947 // that does not have a BufferView with matching
948 // width, but as long as we have only one BufferView
949 // deleting all gives the same result.
950 if (lyxerr.debugging())
951 textcache.show(lyxerr, "Expose delete all");
953 } else if (heightChange) {
954 // Rebuild image of current screen
956 // fitCursor() ensures we don't jump back
957 // to the start of the document on vertical
959 fitCursor(bv_->text);
961 // The main window size has changed, repaint most stuff
963 // ...including the minibuffer
964 owner_->getMiniBuffer()->Init();
967 screen_->Redraw(bv_->text, bv_);
969 // Grey box when we don't have a buffer
970 workarea_->greyOut();
973 // always make sure that the scrollbar is sane.
975 owner_->updateLayoutChoice();
980 void BufferView::Pimpl::update()
982 if (screen_) screen_->Update(bv_->text, bv_);
985 // Values used when calling update:
987 // -2 - update, move sel_cursor if selection, fitcursor
988 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
989 // 0 - update, move sel_cursor if selection, fitcursor
990 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
991 // 3 - update, move sel_cursor if selection
994 // a simple redraw of the parts that need refresh
996 // move sel_cursor if selection -
997 // the text's sel_cursor is moved if there is selection is progress
1000 // fitCursor() is called and the scrollbar updated
1003 // the buffer is marked dirty.
1012 // UPDATE_ONLY = UPDATE;
1013 // UPDATE_SELECT = UPDATE | SELECT;
1014 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
1015 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1017 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
1018 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
1019 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1020 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1021 // update(3) -> update(1) -> update(1) -> update(SELECT)
1023 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
1025 owner_->updateLayoutChoice();
1027 if (!text->selection && (f & SELECT)) {
1028 text->sel_cursor = text->cursor;
1031 text->FullRebreak(bv_);
1033 if (text->inset_owner) {
1034 text->inset_owner->SetUpdateStatus(bv_, InsetText::CURSOR_PAR);
1035 bv_->updateInset(text->inset_owner, true);
1044 if (buffer_->isLyxClean()) {
1045 buffer_->markDirty();
1046 owner_->getMiniBuffer()->setTimer(4);
1048 buffer_->markDirty();
1054 // Callback for cursor timer
1055 void BufferView::Pimpl::cursorToggle()
1057 // Quite a nice place for asyncron Inset updating, isn't it?
1058 // Actually no! This is run even if no buffer exist... so (Lgb)
1060 cursor_timeout.restart();
1065 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1066 if (pid == -1) // error find out what is wrong
1067 ; // ignore it for now.
1069 sigchldhandler(pid, &status);
1071 updatelist.update(bv_);
1074 cursor_timeout.restart();
1078 if (!bv_->theLockingInset()) {
1079 screen_->CursorToggle(bv_->text, bv_);
1081 bv_->theLockingInset()->ToggleInsetCursor(bv_);
1084 cursor_timeout.restart();
1088 void BufferView::Pimpl::cursorPrevious(LyXText * text)
1090 if (!text->cursor.row()->previous())
1093 int y = text->first;
1094 if (text->inset_owner)
1095 y += bv_->text->first;
1096 Row * cursorrow = text->cursor.row();
1097 text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y);
1098 bv_->text->FinishUndo();
1099 // This is to allow jumping over large insets
1100 if ((cursorrow == text->cursor.row()))
1101 text->CursorUp(bv_);
1103 if (text->inset_owner ||
1104 text->cursor.row()->height() < workarea_->height())
1105 screen_->Draw(bv_->text, bv_,
1107 - text->cursor.row()->baseline()
1108 + text->cursor.row()->height()
1109 - workarea_->height() + 1 );
1114 void BufferView::Pimpl::cursorNext(LyXText * text)
1116 if (!text->cursor.row()->next())
1119 int y = text->first + workarea_->height();
1120 // if (text->inset_owner)
1121 // y += bv_->text->first;
1122 text->GetRowNearY(y);
1124 Row * cursorrow = text->cursor.row();
1125 text->SetCursorFromCoordinates(bv_, text->cursor.x_fix(), y); // + workarea_->height());
1126 bv_->text->FinishUndo();
1127 // This is to allow jumping over large insets
1128 if ((cursorrow == bv_->text->cursor.row()))
1129 text->CursorDown(bv_);
1131 if (text->inset_owner ||
1132 text->cursor.row()->height() < workarea_->height())
1133 screen_->Draw(bv_->text, bv_, text->cursor.y() -
1134 text->cursor.row()->baseline());
1139 bool BufferView::Pimpl::available() const
1141 if (buffer_ && bv_->text) return true;
1146 void BufferView::Pimpl::beforeChange(LyXText * text)
1149 text->ClearSelection();
1153 void BufferView::Pimpl::savePosition(unsigned int i)
1155 if (i >= saved_positions_num)
1157 saved_positions[i] = Position(buffer_->fileName(),
1158 bv_->text->cursor.par()->id(),
1159 bv_->text->cursor.pos());
1161 owner_->getMiniBuffer()->Set(_("Saved bookmark ") + tostr(i));
1165 void BufferView::Pimpl::restorePosition(unsigned int i)
1167 if (i >= saved_positions_num)
1170 string fname = saved_positions[i].filename;
1172 beforeChange(bv_->text);
1174 if (fname != buffer_->fileName()) {
1175 Buffer * b = bufferlist.exists(fname) ?
1176 bufferlist.getBuffer(fname) :
1177 bufferlist.loadLyXFile(fname); // don't ask, just load it
1178 if (b != 0 ) buffer(b);
1181 LyXParagraph * par = bv_->text->GetParFromID(saved_positions[i].par_id);
1185 bv_->text->SetCursor(bv_, par,
1186 min(par->Last(), saved_positions[i].par_pos));
1187 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1189 owner_->getMiniBuffer()->Set(_("Moved to bookmark ") + tostr(i));
1193 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
1195 if (i >= saved_positions_num)
1198 return !saved_positions[i].filename.empty();
1202 void BufferView::Pimpl::setState()
1204 if (!lyxrc.rtl_support)
1207 LyXText * text = bv_->getLyXText();
1208 if (text->real_current_font.isRightToLeft() &&
1209 text->real_current_font.latex() != LyXFont::ON) {
1210 if (owner_->getIntl()->primarykeymap)
1211 owner_->getIntl()->KeyMapSec();
1213 if (!owner_->getIntl()->primarykeymap)
1214 owner_->getIntl()->KeyMapPrim();
1219 void BufferView::Pimpl::insetSleep()
1221 if (bv_->theLockingInset() && !bv_->inset_slept) {
1222 bv_->theLockingInset()->GetCursorPos(bv_, bv_->slx, bv_->sly);
1223 bv_->theLockingInset()->InsetUnlock(bv_);
1224 bv_->inset_slept = true;
1229 void BufferView::Pimpl::insetWakeup()
1231 if (bv_->theLockingInset() && bv_->inset_slept) {
1232 bv_->theLockingInset()->Edit(bv_, bv_->slx, bv_->sly, 0);
1233 bv_->inset_slept = false;
1238 void BufferView::Pimpl::insetUnlock()
1240 if (bv_->theLockingInset()) {
1241 if (!bv_->inset_slept)
1242 bv_->theLockingInset()->InsetUnlock(bv_);
1243 bv_->theLockingInset(0);
1244 bv_->text->FinishUndo();
1245 bv_->inset_slept = false;
1250 bool BufferView::Pimpl::focus() const
1252 return workarea_->hasFocus();
1256 void BufferView::Pimpl::focus(bool f)
1258 if (f) workarea_->setFocus();
1262 bool BufferView::Pimpl::active() const
1264 return workarea_->active();
1268 bool BufferView::Pimpl::belowMouse() const
1270 return workarea_->belowMouse();
1274 void BufferView::Pimpl::showCursor()
1277 screen_->ShowCursor(bv_->text, bv_);
1281 void BufferView::Pimpl::hideCursor()
1284 screen_->HideCursor();
1288 void BufferView::Pimpl::toggleSelection(bool b)
1291 screen_->ToggleSelection(bv_->text, bv_, b);
1295 void BufferView::Pimpl::toggleToggle()
1298 screen_->ToggleToggle(bv_->text, bv_);
1302 void BufferView::Pimpl::center()
1304 beforeChange(bv_->text);
1305 if (bv_->text->cursor.y() > static_cast<int>((workarea_->height() / 2))) {
1306 screen_->Draw(bv_->text, bv_, bv_->text->cursor.y() - workarea_->height() / 2);
1308 screen_->Draw(bv_->text, bv_, 0);
1310 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1315 void BufferView::Pimpl::pasteClipboard(bool asPara)
1317 if (!buffer_) return;
1319 screen_->HideCursor();
1320 beforeChange(bv_->text);
1322 string const clip(workarea_->getClipboard());
1324 if (clip.empty()) return;
1327 bv_->text->InsertStringB(bv_, clip);
1329 bv_->text->InsertStringA(bv_, clip);
1331 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1335 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
1337 workarea_->putClipboard(stuff);