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"
25 #include "lyx_gui_misc.h"
28 #include "support/LAssert.h"
32 /* the selection possible is needed, that only motion events are
33 * used, where the bottom press event was on the drawing area too */
34 bool selection_possible = false;
36 extern BufferList bufferlist;
37 extern char ascii_type;
39 extern void sigchldhandler(pid_t pid, int * status);
40 extern int bibitemMaxWidth(Painter &, LyXFont const &);
41 extern void FreeUpdateTimer();
44 void C_BufferView_CursorToggleCB(FL_OBJECT * ob, long buf);
50 XSync(fl_get_display(), 0);
55 void SetXtermCursor(Window win)
58 static bool cursor_undefined = true;
59 if (cursor_undefined){
60 cursor = XCreateFontCursor(fl_display, XC_xterm);
62 cursor_undefined = false;
64 XDefineCursor(fl_display, win, cursor);
69 BufferView::Pimpl::Pimpl(BufferView * b, LyXView * o,
70 int xpos, int ypos, int width, int height)
74 workarea = new WorkArea(bv_, xpos, ypos, width, height);
78 current_scrollbar_value = 0;
79 fl_set_timer(timer_cursor, 0.4);
81 work_area_focus = true;
83 using_xterm_cursor = false;
87 Painter & BufferView::Pimpl::painter()
89 return workarea->getPainter();
93 void BufferView::Pimpl::buffer(Buffer * b)
95 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
99 buffer_->delUser(bv_);
101 // Put the old text into the TextCache, but
102 // only if the buffer is still loaded.
103 // Also set the owner of the test to 0
105 textcache.add(bv_->text);
106 if (lyxerr.debugging())
107 textcache.show(lyxerr, "BufferView::buffer");
112 // Set current buffer
115 if (bufferlist.getState() == BufferList::CLOSING) return;
118 // screen is always deleted when the buffer is changed.
122 // If we are closing the buffer, use the first buffer as current
124 buffer_ = bufferlist.first();
128 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
129 buffer_->addUser(bv_);
130 owner_->getMenus()->showMenus();
131 // If we don't have a text object for this, we make one
133 resizeCurrentBuffer();
138 screen->first = screen->TopCursorVisible();
140 updateAllVisibleBufferRelatedPopups();
143 lyxerr[Debug::INFO] << " No Buffer!" << endl;
144 owner_->getMenus()->hideMenus();
148 // Also remove all remaining text's from the testcache.
149 // (there should not be any!) (if there is any it is a
151 if (lyxerr.debugging())
152 textcache.show(lyxerr, "buffer delete all");
155 // should update layoutchoice even if we don't have a buffer.
156 owner_->updateLayoutChoice();
157 owner_->getMiniBuffer()->Init();
158 owner_->updateWindowTitle();
161 void BufferView::Pimpl::resize(int xpos, int ypos, int width, int height)
163 workarea->resize(xpos, ypos, width, height);
169 void BufferView::Pimpl::resize()
171 // This will resize the buffer. (Asger)
173 resizeCurrentBuffer();
177 void BufferView::Pimpl::redraw()
179 lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
184 bool BufferView::Pimpl::fitCursor()
186 Assert(screen); // it is a programming error to call fitCursor
187 // without a valid screen.
188 bool ret = screen->FitCursor();
189 if (ret) updateScrollbar();
194 void BufferView::Pimpl::redoCurrentBuffer()
196 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
197 if (buffer_ && bv_->text) {
199 owner_->updateLayoutChoice();
204 int BufferView::Pimpl::resizeCurrentBuffer()
206 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
208 LyXParagraph * par = 0;
209 LyXParagraph * selstartpar = 0;
210 LyXParagraph * selendpar = 0;
219 owner_->getMiniBuffer()->Set(_("Formatting document..."));
222 par = bv_->text->cursor.par;
223 pos = bv_->text->cursor.pos;
224 selstartpar = bv_->text->sel_start_cursor.par;
225 selstartpos = bv_->text->sel_start_cursor.pos;
226 selendpar = bv_->text->sel_end_cursor.par;
227 selendpos = bv_->text->sel_end_cursor.pos;
228 selection = bv_->text->selection;
229 mark_set = bv_->text->mark_set;
231 bv_->text = new LyXText(bv_, workarea->workWidth(), buffer_);
233 // See if we have a text in TextCache that fits
234 // the new buffer_ with the correct width.
235 bv_->text = textcache.findFit(buffer_, workarea->workWidth());
237 if (lyxerr.debugging()) {
238 lyxerr << "Found a LyXText that fits:\n";
239 textcache.show(lyxerr, bv_->text);
241 // Set the owner of the newly found text
242 bv_->text->owner(bv_);
243 if (lyxerr.debugging())
244 textcache.show(lyxerr, "resizeCurrentBuffer");
246 bv_->text = new LyXText(bv_, workarea->workWidth(), buffer_);
252 bv_->text->selection = true;
253 /* at this point just to avoid the Delete-Empty-Paragraph
254 * Mechanism when setting the cursor */
255 bv_->text->mark_set = mark_set;
257 bv_->text->SetCursor(selstartpar, selstartpos);
258 bv_->text->sel_cursor = bv_->text->cursor;
259 bv_->text->SetCursor(selendpar, selendpos);
260 bv_->text->SetSelection();
261 bv_->text->SetCursor(par, pos);
263 bv_->text->SetCursor(par, pos);
264 bv_->text->sel_cursor = bv_->text->cursor;
265 bv_->text->selection = false;
268 screen->first = screen->TopCursorVisible(); /* this will scroll the
269 * screen such that the
274 owner_->getMiniBuffer()->Init();
278 // Now if the title form still exist kill it
285 void BufferView::Pimpl::gotoError()
290 screen->HideCursor();
295 if (!bv_->text->GotoNextError()) {
296 if (bv_->text->cursor.pos
297 || bv_->text->cursor.par != bv_->text->FirstParagraph()) {
298 tmp = bv_->text->cursor;
299 bv_->text->cursor.par = bv_->text->FirstParagraph();
300 bv_->text->cursor.pos = 0;
301 if (!bv_->text->GotoNextError()) {
302 bv_->text->cursor = tmp;
303 owner_->getMiniBuffer()
304 ->Set(_("No more errors"));
308 owner_->getMiniBuffer()->Set(_("No more errors"));
313 bv_->text->sel_cursor = bv_->text->cursor;
317 void BufferView::Pimpl::updateScreen()
319 // Regenerate the screen.
321 screen = new LyXScreen(*workarea, bv_->text);
325 void BufferView::Pimpl::create_view()
334 timer_cursor = obj = fl_add_timer(FL_HIDDEN_TIMER,
335 0, 0, 0, 0, "Timer");
336 fl_set_object_callback(obj, C_BufferView_CursorToggleCB, 0);
341 void BufferView::Pimpl::updateScrollbar()
343 /* If the text is smaller than the working area, the scrollbar
344 * maximum must be the working area height. No scrolling will
348 workarea->setScrollbar(0, 1.0);
352 static unsigned long max2 = 0;
353 static unsigned long height2 = 0;
355 unsigned long cbth = 0;
359 cbth = bv_->text->height;
361 cbsf = screen->first;
363 // check if anything has changed.
365 height2 == workarea->height() &&
366 current_scrollbar_value == cbsf)
369 height2 = workarea->height();
370 current_scrollbar_value = cbsf;
372 if (cbth <= height2) { // text is smaller than screen
373 workarea->setScrollbar(0, 1.0); // right?
377 long maximum_height = workarea->height() * 3 / 4 + cbth;
381 double hfloat = workarea->height();
382 double maxfloat = maximum_height;
384 float slider_size = 0.0;
385 int slider_value = value;
387 workarea->setScrollbarBounds(0, bv_->text->height - workarea->height());
388 double lineh = bv_->text->DefaultHeight();
389 workarea->setScrollbarIncrements(lineh);
390 if (maxfloat > 0.0) {
391 if ((hfloat / maxfloat) * float(height2) < 3)
392 slider_size = 3.0/float(height2);
394 slider_size = hfloat / maxfloat;
396 slider_size = hfloat;
398 workarea->setScrollbar(slider_value, slider_size / workarea->height());
402 // Callback for scrollbar slider
403 void BufferView::Pimpl::scrollCB(double value)
405 extern bool cursor_follows_scrollbar;
407 if (buffer_ == 0) return;
409 current_scrollbar_value = long(value);
410 if (current_scrollbar_value < 0)
411 current_scrollbar_value = 0;
416 screen->Draw(current_scrollbar_value);
418 if (cursor_follows_scrollbar) {
419 LyXText * vbt = bv_->text;
420 unsigned int height = vbt->DefaultHeight();
422 if (vbt->cursor.y < screen->first + height) {
423 vbt->SetCursorFromCoordinates(0,
426 } else if (vbt->cursor.y >
427 screen->first + workarea->height() - height) {
428 vbt->SetCursorFromCoordinates(0,
439 // Callback for scrollbar down button
440 void BufferView::Pimpl::downCB(long time, int button)
442 if (buffer_ == 0) return;
459 int BufferView::Pimpl::scrollUp(long time)
461 if (buffer_ == 0) return 0;
462 if (!screen) return 0;
464 double value = workarea->getScrollbarValue();
466 if (value == 0) return 0;
468 float add_value = (bv_->text->DefaultHeight()
469 + float(time) * float(time) * 0.125);
471 if (add_value > workarea->height())
472 add_value = float(workarea->height() -
473 bv_->text->DefaultHeight());
480 workarea->setScrollbarValue(value);
482 bv_->scrollCB(value);
487 int BufferView::Pimpl::scrollDown(long time)
489 if (buffer_ == 0) return 0;
490 if (!screen) return 0;
492 double value= workarea->getScrollbarValue();
493 pair<double, double> p = workarea->getScrollbarBounds();
494 double max = p.second;
496 if (value == max) return 0;
498 float add_value = (bv_->text->DefaultHeight()
499 + float(time) * float(time) * 0.125);
501 if (add_value > workarea->height())
502 add_value = float(workarea->height() -
503 bv_->text->DefaultHeight());
510 workarea->setScrollbarValue(value);
512 bv_->scrollCB(value);
517 void BufferView::Pimpl::scrollUpOnePage()
519 if (buffer_ == 0) return;
522 long y = screen->first;
526 Row * row = bv_->text->GetRowNearY(y);
528 y = y - workarea->height() + row->height;
530 workarea->setScrollbarValue(y);
537 void BufferView::Pimpl::scrollDownOnePage()
539 if (buffer_ == 0) return;
542 long y = screen->first;
544 if (y > bv_->text->height - workarea->height())
547 y += workarea->height();
548 bv_->text->GetRowNearY(y);
550 workarea->setScrollbarValue(y);
557 void BufferView::Pimpl::workAreaMotionNotify(int x, int y, unsigned int state)
559 if (buffer_ == 0 || !screen) return;
561 // Only use motion with button 1
562 if (!state & Button1MotionMask)
565 // Check for inset locking
566 if (bv_->the_locking_inset) {
567 LyXCursor cursor = bv_->text->cursor;
568 bv_->the_locking_inset->
569 InsetMotionNotify(bv_,
571 y - cursor.y + screen->first,
576 /* The selection possible is needed, that only motion events are
577 * used, where the bottom press event was on the drawing area too */
578 if (selection_possible) {
579 screen->HideCursor();
581 bv_->text->SetCursorFromCoordinates(x, y + screen->first);
583 if (!bv_->text->selection)
584 update(-3); // Maybe an empty line was deleted
586 bv_->text->SetSelection();
587 screen->ToggleToggle();
589 screen->ShowCursor();
595 // Single-click on work area
596 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
602 if (buffer_ == 0 || !screen) return;
604 Inset * inset_hit = checkInsetHit(xpos, ypos, button);
606 // ok ok, this is a hack.
607 if (button == 4 || button == 5) {
610 scrollUp(100); // This number is only temporary
618 if (bv_->the_locking_inset) {
619 // We are in inset locking mode
621 /* Check whether the inset was hit. If not reset mode,
622 otherwise give the event to the inset */
623 if (inset_hit == bv_->the_locking_inset) {
624 bv_->the_locking_inset->
625 InsetButtonPress(bv_,
630 bv_->unlockInset(bv_->the_locking_inset);
635 selection_possible = true;
636 screen->HideCursor();
638 // Right button mouse click on a table
640 (bv_->text->cursor.par->table ||
641 bv_->text->MouseHitInTable(xpos, ypos + screen->first))) {
642 // Set the cursor to the press-position
643 bv_->text->SetCursorFromCoordinates(xpos, ypos + screen->first);
646 // Only show the table popup if the hit is in
648 if (!bv_->text->HitInTable(bv_->text->cursor.row, xpos))
651 // Hit above or below the table?
653 if (!bv_->text->selection) {
654 screen->ToggleSelection();
655 bv_->text->ClearSelection();
656 bv_->text->FullRebreak();
660 // Popup table popup when on a table.
661 // This is obviously temporary, since we
662 // should be able to popup various
663 // context-sensitive-menus with the
664 // the right mouse. So this should be done more
665 // general in the future. Matthias.
666 selection_possible = false;
668 ->Dispatch(LFUN_LAYOUT_TABLE,
674 int screen_first = screen->first;
676 // Middle button press pastes if we have a selection
677 bool paste_internally = false;
679 && bv_->text->selection) {
680 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
681 paste_internally = true;
684 // Clear the selection
685 screen->ToggleSelection();
686 bv_->text->ClearSelection();
687 bv_->text->FullRebreak();
691 // Single left click in math inset?
692 if ((inset_hit != 0) &&
693 (inset_hit->Editable()==Inset::HIGHLY_EDITABLE)) {
694 // Highly editable inset, like math
695 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
696 selection_possible = false;
697 owner_->updateLayoutChoice();
698 owner_->getMiniBuffer()->Set(inset->EditMessage());
699 inset->InsetButtonPress(bv_, xpos, ypos, button);
700 inset->Edit(bv_, xpos, ypos, button);
704 // Right click on a footnote flag opens float menu
706 selection_possible = false;
710 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
711 bv_->text->SetCursorFromCoordinates(xpos, ypos + screen_first);
712 bv_->text->FinishUndo();
713 bv_->text->sel_cursor = bv_->text->cursor;
714 bv_->text->cursor.x_fix = bv_->text->cursor.x;
716 owner_->updateLayoutChoice();
718 selection_possible = false;
721 // Insert primary selection with middle mouse
722 // if there is a local selection in the current buffer,
725 if (paste_internally)
726 owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
728 owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
730 selection_possible = false;
736 void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, unsigned int button)
739 if (buffer_ && !bv_->the_locking_inset) {
740 if (screen && button == 1) {
741 screen->HideCursor();
742 screen->ToggleSelection();
743 bv_->text->SelectWord();
744 screen->ToggleSelection(false);
745 /* This will fit the cursor on the screen
753 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, unsigned int button)
756 if (buffer_ && screen && button == 1) {
757 screen->HideCursor();
758 screen->ToggleSelection();
759 bv_->text->CursorHome();
760 bv_->text->sel_cursor = bv_->text->cursor;
761 bv_->text->CursorEnd();
762 bv_->text->SetSelection();
763 screen->ToggleSelection(false);
764 /* This will fit the cursor on the screen
771 void BufferView::Pimpl::enterView()
773 if (active() && available()) {
774 SetXtermCursor(workarea->getWin());
775 using_xterm_cursor = true;
780 void BufferView::Pimpl::leaveView()
782 if (using_xterm_cursor) {
783 XUndefineCursor(fl_display, workarea->getWin());
784 using_xterm_cursor = false;
789 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
792 if (buffer_ == 0 || screen == 0) return;
794 // If we hit an inset, we have the inset coordinates in these
795 // and inset_hit points to the inset. If we do not hit an
796 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
797 Inset * inset_hit = checkInsetHit(x, y, button);
799 if (bv_->the_locking_inset) {
800 // We are in inset locking mode.
802 /* LyX does a kind of work-area grabbing for insets.
803 Only a ButtonPress Event outside the inset will
804 force a InsetUnlock. */
805 bv_->the_locking_inset->
806 InsetButtonRelease(bv_, x, y, button);
810 selection_possible = false;
811 if (bv_->text->cursor.par->table) {
812 int cell = bv_->text->
813 NumberOfCell(bv_->text->cursor.par,
814 bv_->text->cursor.pos);
815 if (bv_->text->cursor.par->table->IsContRow(cell) &&
816 bv_->text->cursor.par->table->
817 CellHasContRow(bv_->text->cursor.par->table->
818 GetCellAbove(cell))<0) {
819 bv_->text->CursorUp();
823 if (button >= 2) return;
826 owner_->getMiniBuffer()->Set(CurrentState(bv_));
828 // Did we hit an editable inset?
829 if (inset_hit != 0) {
830 // Inset like error, notes and figures
831 selection_possible = false;
833 // CHECK fix this proper in 0.13
835 // Following a ref shouldn't issue
836 // a push on the undo-stack
837 // anylonger, now that we have
838 // keybindings for following
839 // references and returning from
840 // references. IMHO though, it
841 // should be the inset's own business
842 // to push or not push on the undo
843 // stack. They don't *have* to
844 // alter the document...
846 // ...or maybe the SetCursorParUndo()
847 // below isn't necessary at all anylonger?
848 if (inset_hit->LyxCode() == Inset::REF_CODE) {
849 bv_->text->SetCursorParUndo();
852 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
853 if (inset_hit->Editable()==Inset::HIGHLY_EDITABLE) {
854 // Highly editable inset, like math
855 UpdatableInset *inset = (UpdatableInset *)inset_hit;
856 inset->InsetButtonRelease(bv_, x, y, button);
858 inset_hit->InsetButtonRelease(bv_, x, y, button);
859 inset_hit->Edit(bv_, x, y, button);
864 // check whether we want to open a float
868 if (bv_->text->cursor.pos <
869 bv_->text->cursor.par->Last()) {
870 c = bv_->text->cursor.par->
871 GetChar(bv_->text->cursor.pos);
873 if (c == LyXParagraph::META_FOOTNOTE
874 || c == LyXParagraph::META_MARGIN
875 || c == LyXParagraph::META_FIG
876 || c == LyXParagraph::META_TAB
877 || c == LyXParagraph::META_WIDE_FIG
878 || c == LyXParagraph::META_WIDE_TAB
879 || c == LyXParagraph::META_ALGORITHM){
881 } else if (bv_->text->cursor.pos - 1 >= 0) {
882 c = bv_->text->cursor.par->
883 GetChar(bv_->text->cursor.pos - 1);
884 if (c == LyXParagraph::META_FOOTNOTE
885 || c == LyXParagraph::META_MARGIN
886 || c == LyXParagraph::META_FIG
887 || c == LyXParagraph::META_TAB
888 || c == LyXParagraph::META_WIDE_FIG
889 || c == LyXParagraph::META_WIDE_TAB
890 || c == LyXParagraph::META_ALGORITHM){
891 // We are one step too far to the right
892 bv_->text->CursorLeft();
898 selection_possible = false;
903 // Do we want to close a float? (click on the float-label)
904 if (bv_->text->cursor.row->par->footnoteflag ==
905 LyXParagraph::OPEN_FOOTNOTE
906 //&& text->cursor.pos == 0
907 && bv_->text->cursor.row->previous &&
908 bv_->text->cursor.row->previous->par->
909 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
910 LyXFont font(LyXFont::ALL_SANE);
911 font.setSize(LyXFont::SIZE_FOOTNOTE);
913 int box_x = 20; // LYX_PAPER_MARGIN;
914 box_x += lyxfont::width(" wide-tab ", font);
916 unsigned int screen_first = screen->first;
919 && y + screen_first > bv_->text->cursor.y -
920 bv_->text->cursor.row->baseline
921 && y + screen_first < bv_->text->cursor.y -
922 bv_->text->cursor.row->baseline
923 + lyxfont::maxAscent(font) * 1.2 + lyxfont::maxDescent(font) * 1.2) {
925 selection_possible = false;
930 // Maybe we want to edit a bibitem ale970302
931 if (bv_->text->cursor.par->bibkey && x < 20 +
932 bibitemMaxWidth(bv_->painter(),
935 params.textclass).defaultfont())) {
936 bv_->text->cursor.par->bibkey->Edit(bv_, 0, 0, 0);
944 * Returns an inset if inset was hit. 0 otherwise.
945 * If hit, the coordinates are changed relative to the inset.
946 * Otherwise coordinates are not changed, and false is returned.
948 Inset * BufferView::Pimpl::checkInsetHit(int & x, int & y,
949 unsigned int /* button */)
954 unsigned int y_tmp = y + screen->first;
957 bv_->text->SetCursorFromCoordinates(cursor, x, y_tmp);
958 #if 0 // Are you planning to use this Jürgen? (Lgb)
959 bool move_cursor = ((cursor.par != bv_->text->cursor.par) ||
960 (cursor.pos != bv_->text->cursor.pos));
962 if (cursor.pos < cursor.par->Last()
963 && cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET
964 && cursor.par->GetInset(cursor.pos)
965 && cursor.par->GetInset(cursor.pos)->Editable()) {
967 // Check whether the inset really was hit
968 Inset * tmpinset = cursor.par->GetInset(cursor.pos);
969 LyXFont font = bv_->text->GetFont(cursor.par, cursor.pos);
970 bool is_rtl = font.isVisibleRightToLeft();
974 start_x = cursor.x - tmpinset->width(bv_->painter(), font);
978 end_x = cursor.x + tmpinset->width(bv_->painter(), font);
981 if (x > start_x && x < end_x
982 && y_tmp > cursor.y - tmpinset->ascent(bv_->painter(), font)
983 && y_tmp < cursor.y + tmpinset->descent(bv_->painter(), font)) {
985 if (move_cursor && (tmpinset != bv_->the_locking_inset))
987 bv_->text->SetCursor(cursor.par,cursor.pos,true);
989 // The origin of an inset is on the baseline
990 y = y_tmp - (bv_->text->cursor.y);
995 if ((cursor.pos - 1 >= 0) &&
996 (cursor.par->GetChar(cursor.pos-1) == LyXParagraph::META_INSET) &&
997 (cursor.par->GetInset(cursor.pos - 1)) &&
998 (cursor.par->GetInset(cursor.pos - 1)->Editable())) {
999 Inset * tmpinset = cursor.par->GetInset(cursor.pos-1);
1000 LyXFont font = bv_->text->GetFont(cursor.par, cursor.pos-1);
1001 bool is_rtl = font.isVisibleRightToLeft();
1005 start_x = cursor.x - tmpinset->width(bv_->painter(), font);
1009 end_x = cursor.x + tmpinset->width(bv_->painter(), font);
1011 if (x > start_x && x < end_x
1012 && y_tmp > cursor.y - tmpinset->ascent(bv_->painter(), font)
1013 && y_tmp < cursor.y + tmpinset->descent(bv_->painter(), font)) {
1015 if (move_cursor && (tmpinset != bv_->the_locking_inset))
1017 bv_->text->SetCursor(cursor.par,cursor.pos-1,true);
1019 // The origin of an inset is on the baseline
1020 y = y_tmp - (bv_->text->cursor.y);
1028 void BufferView::Pimpl::workAreaExpose()
1030 // this is a hack to ensure that we only call this through
1031 // BufferView::redraw().
1036 static unsigned int work_area_width = 0;
1037 static unsigned int work_area_height = 0;
1039 bool widthChange = workarea->workWidth() != work_area_width;
1040 bool heightChange = workarea->height() != work_area_height;
1042 // update from work area
1043 work_area_width = workarea->workWidth();
1044 work_area_height = workarea->height();
1047 // All buffers need a resize
1048 bufferlist.resize();
1050 // Remove all texts from the textcache
1051 // This is not _really_ what we want to do. What
1052 // we really want to do is to delete in textcache
1053 // that does not have a BufferView with matching
1054 // width, but as long as we have only one BufferView
1055 // deleting all gives the same result.
1056 if (lyxerr.debugging())
1057 textcache.show(lyxerr, "Expose delete all");
1059 } else if (heightChange) {
1060 // Rebuild image of current screen
1062 // fitCursor() ensures we don't jump back
1063 // to the start of the document on vertical
1067 // The main window size has changed, repaint most stuff
1069 // ...including the minibuffer
1070 owner_->getMiniBuffer()->Init();
1072 } else if (screen) screen->Redraw();
1074 // Grey box when we don't have a buffer
1075 workarea->greyOut();
1078 // always make sure that the scrollbar is sane.
1080 owner_->updateLayoutChoice();
1086 string fromClipboard(Window win, XEvent * event)
1089 if (event->xselection.type == XA_STRING
1090 && event->xselection.property) {
1094 unsigned char * uc = 0;
1096 if (XGetWindowProperty(
1097 event->xselection.display, // display
1099 event->xselection.property, // property
1103 XA_STRING, // req_type
1104 &tmpatom, // actual_type_return
1105 &tmpint, // actual_format_return
1116 if (XGetWindowProperty(
1117 event->xselection.display, // display
1119 event->xselection.property, // property
1121 ul2/4+1, // long_length
1123 XA_STRING, // req_type
1124 &tmpatom, // actual_type_return
1125 &tmpint, // actual_format_return
1126 &ul1, // nitems_return
1127 &ul2, // bytes_after_return
1128 &uc // prop_return */
1133 strret = reinterpret_cast<char*>(uc);
1134 free(uc); // yes free!
1142 void BufferView::Pimpl::workAreaSelectionNotify(Window win, XEvent * event)
1144 if (buffer_ == 0) return;
1146 screen->HideCursor();
1147 bv_->beforeChange();
1148 string clb = fromClipboard(win, event);
1151 bv_->text->InsertStringA(clb);
1153 bv_->text->InsertStringB(clb);
1160 void BufferView::Pimpl::update()
1162 if (screen) screen->Update();
1165 // Values used when calling update:
1167 // -2 - update, move sel_cursor if selection, fitcursor
1168 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
1169 // 0 - update, move sel_cursor if selection, fitcursor
1170 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
1171 // 3 - update, move sel_cursor if selection
1174 // a simple redraw of the parts that need refresh
1176 // move sel_cursor if selection -
1177 // the text's sel_cursor is moved if there is selection is progress
1180 // fitCursor() is called and the scrollbar updated
1183 // the buffer is marked dirty.
1192 // UPDATE_ONLY = UPDATE;
1193 // UPDATE_SELECT = UPDATE | SELECT;
1194 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
1195 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1197 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
1198 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
1199 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1200 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1201 // update(3) -> update(1) -> update(1) -> update(SELECT)
1202 void BufferView::Pimpl::update(signed char f)
1205 owner_->updateLayoutChoice();
1207 if (!bv_->text->selection && f > -3)
1208 bv_->text->sel_cursor = bv_->text->cursor;
1211 bv_->text->FullRebreak();
1215 if (f != 3 && f != -3) {
1217 //updateScrollbar();
1220 if (f == 1 || f == -1) {
1221 if (buffer_->isLyxClean()) {
1222 buffer_->markDirty();
1223 owner_->getMiniBuffer()->setTimer(4);
1225 buffer_->markDirty();
1229 owner_->updateLayoutChoice();
1231 if (!bv_->text->selection && (f & SELECT))
1232 bv_->text->sel_cursor = bv_->text->cursor;
1235 bv_->text->FullRebreak();
1241 //updateScrollbar();
1245 if (buffer_->isLyxClean()) {
1246 buffer_->markDirty();
1247 owner_->getMiniBuffer()->setTimer(4);
1249 buffer_->markDirty();
1257 void BufferView::Pimpl::smallUpdate(signed char f)
1262 screen->SmallUpdate();
1263 if (screen->TopCursorVisible()
1270 //updateScrollbar();
1272 if (!bv_->text->selection)
1273 bv_->text->sel_cursor = bv_->text->cursor;
1275 if (f == 1 || f == -1) {
1276 if (buffer_->isLyxClean()) {
1277 buffer_->markDirty();
1278 owner_->getMiniBuffer()->setTimer(4);
1280 buffer_->markDirty();
1288 // Callback for cursor timer
1289 void BufferView::Pimpl::cursorToggle()
1291 // Quite a nice place for asyncron Inset updating, isn't it?
1292 // Actually no! This is run even if no buffer exist... so (Lgb)
1294 goto set_timer_and_return;
1298 // On my quest to solve the gs render hangups I am now
1299 // disabling the SIGHUP completely, and will do a wait
1300 // now and then instead. If the guess that xforms somehow
1301 // destroys something is true, this is likely (hopefully)
1302 // to solve the problem...at least I hope so. Lgb
1304 // ...Ok this seems to work...at least it does not make things
1305 // worse so far. However I still see gs processes that hangs.
1306 // I would really like to know _why_ they are hanging. Anyway
1307 // the solution without the SIGCHLD handler seems to be easier
1310 // When attaching gdb to a a running gs that hangs it shows
1311 // that it is waiting for input(?) Is it possible for us to
1312 // provide that input somehow? Or figure what it is expecing
1315 // One solution is to, after some time, look if there are some
1316 // old gs processes still running and if there are: kill them
1319 // Another solution is to provide the user an option to rerender
1320 // a picture. This would, for the picture in question, check if
1321 // there is a gs running for it, if so kill it, and start a new
1322 // rendering process.
1324 // these comments posted to lyx@via
1327 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1328 if (pid == -1) // error find out what is wrong
1329 ; // ignore it for now.
1331 sigchldhandler(pid, &status);
1334 updatelist.update(bv_);
1337 goto set_timer_and_return;
1340 if (lyx_focus && work_area_focus) {
1341 if (!bv_->the_locking_inset) {
1342 screen->CursorToggle();
1344 bv_->the_locking_inset->
1345 ToggleInsetCursor(bv_);
1347 goto set_timer_and_return;
1349 // Make sure that the cursor is visible.
1350 if (!bv_->the_locking_inset) {
1351 screen->ShowCursor();
1353 if (!bv_->the_locking_inset->isCursorVisible())
1354 bv_->the_locking_inset->
1355 ToggleInsetCursor(bv_);
1357 // This is only run when work_area_focus or lyx_focus is false.
1360 XGetInputFocus(fl_display, &tmpwin, &tmp);
1361 // Commenting this out, we have not had problems with this
1362 // for a long time. We will probably work on this code later
1363 // and we can reenable this debug code then. Now it only
1364 // anoying when debugging. (Lgb)
1365 //if (lyxerr.debugging(Debug::INFO)) {
1366 // lyxerr << "tmpwin: " << tmpwin
1367 // << "\nwindow: " << view->owner_->getForm()->window
1368 // << "\nwork_area_focus: " << view->work_area_focus
1369 // << "\nlyx_focus : " << view->lyx_focus
1372 if (tmpwin != owner_->getForm()->window) {
1377 if (!work_area_focus)
1380 goto set_timer_and_return;
1384 set_timer_and_return:
1385 fl_set_timer(timer_cursor, 0.4);
1391 void BufferView::Pimpl::cursorPrevious()
1393 if (!bv_->text->cursor.row->previous) return;
1395 long y = screen->first;
1396 Row * cursorrow = bv_->text->cursor.row;
1397 bv_->text->SetCursorFromCoordinates(bv_->text->cursor.x_fix, y);
1398 bv_->text->FinishUndo();
1399 // This is to allow jumping over large insets
1400 if ((cursorrow == bv_->text->cursor.row))
1401 bv_->text->CursorUp();
1403 if (bv_->text->cursor.row->height < workarea->height())
1404 screen->Draw(bv_->text->cursor.y
1405 - bv_->text->cursor.row->baseline
1406 + bv_->text->cursor.row->height
1407 - workarea->height() + 1 );
1412 void BufferView::Pimpl::cursorNext()
1414 if (!bv_->text->cursor.row->next) return;
1416 long y = screen->first;
1417 bv_->text->GetRowNearY(y);
1418 Row * cursorrow = bv_->text->cursor.row;
1419 bv_->text->SetCursorFromCoordinates(bv_->text->cursor.x_fix, y
1420 + workarea->height());
1421 bv_->text->FinishUndo();
1422 // This is to allow jumping over large insets
1423 if ((cursorrow == bv_->text->cursor.row))
1424 bv_->text->CursorDown();
1426 if (bv_->text->cursor.row->height < workarea->height())
1427 screen->Draw(bv_->text->cursor.y
1428 - bv_->text->cursor.row->baseline);
1433 bool BufferView::Pimpl::available() const
1435 if (buffer_ && bv_->text) return true;
1440 void BufferView::Pimpl::beforeChange()
1443 bv_->text->ClearSelection();
1448 void BufferView::Pimpl::savePosition()
1450 backstack.push(buffer_->fileName(),
1451 bv_->text->cursor.x,
1452 bv_->text->cursor.y);
1456 void BufferView::Pimpl::restorePosition()
1458 if (backstack.empty()) return;
1461 string fname = backstack.pop(&x, &y);
1464 Buffer * b = bufferlist.exists(fname) ?
1465 bufferlist.getBuffer(fname) :
1466 bufferlist.loadLyXFile(fname); // don't ask, just load it
1468 bv_->text->SetCursorFromCoordinates(x, y);
1473 void BufferView::Pimpl::setState()
1475 if (!lyxrc.rtl_support)
1478 if (bv_->text->real_current_font.isRightToLeft() &&
1479 bv_->text->real_current_font.latex() != LyXFont::ON) {
1480 if (owner_->getIntl()->primarykeymap)
1481 owner_->getIntl()->KeyMapSec();
1483 if (!owner_->getIntl()->primarykeymap)
1484 owner_->getIntl()->KeyMapPrim();
1489 void BufferView::Pimpl::insetSleep()
1491 if (bv_->the_locking_inset && !bv_->inset_slept) {
1492 bv_->the_locking_inset->GetCursorPos(bv_->slx, bv_->sly);
1493 bv_->the_locking_inset->InsetUnlock(bv_);
1494 bv_->inset_slept = true;
1499 void BufferView::Pimpl::insetWakeup()
1501 if (bv_->the_locking_inset && bv_->inset_slept) {
1502 bv_->the_locking_inset->Edit(bv_, bv_->slx, bv_->sly, 0);
1503 bv_->inset_slept = false;
1508 void BufferView::Pimpl::insetUnlock()
1510 if (bv_->the_locking_inset) {
1511 if (!bv_->inset_slept) bv_->the_locking_inset->InsetUnlock(bv_);
1512 bv_->the_locking_inset = 0;
1513 bv_->text->FinishUndo();
1514 bv_->inset_slept = false;
1519 bool BufferView::Pimpl::focus() const
1521 return workarea->hasFocus();
1525 void BufferView::Pimpl::focus(bool f)
1527 if (f) workarea->setFocus();
1531 bool BufferView::Pimpl::active() const
1533 return workarea->active();
1537 bool BufferView::Pimpl::belowMouse() const
1539 return workarea->belowMouse();
1543 void BufferView::Pimpl::showCursor()
1546 screen->ShowCursor();
1550 void BufferView::Pimpl::hideCursor()
1553 screen->HideCursor();
1557 void BufferView::Pimpl::toggleSelection(bool b)
1560 screen->ToggleSelection(b);
1564 void BufferView::Pimpl::toggleToggle()
1567 screen->ToggleToggle();
1571 void BufferView::Pimpl::center()
1574 if (bv_->text->cursor.y > workarea->height() / 2) {
1575 screen->Draw(bv_->text->cursor.y - workarea->height() / 2);