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,
438 int BufferView::Pimpl::scrollUp(long time)
440 if (buffer_ == 0) return 0;
441 if (!screen) return 0;
443 double value = workarea->getScrollbarValue();
445 if (value == 0) return 0;
447 float add_value = (bv_->text->DefaultHeight()
448 + float(time) * float(time) * 0.125);
450 if (add_value > workarea->height())
451 add_value = float(workarea->height() -
452 bv_->text->DefaultHeight());
459 workarea->setScrollbarValue(value);
461 bv_->scrollCB(value);
466 int BufferView::Pimpl::scrollDown(long time)
468 if (buffer_ == 0) return 0;
469 if (!screen) return 0;
471 double value= workarea->getScrollbarValue();
472 pair<float, float> p = workarea->getScrollbarBounds();
473 double max = p.second;
475 if (value == max) return 0;
477 float add_value = (bv_->text->DefaultHeight()
478 + float(time) * float(time) * 0.125);
480 if (add_value > workarea->height())
481 add_value = float(workarea->height() -
482 bv_->text->DefaultHeight());
489 workarea->setScrollbarValue(value);
491 bv_->scrollCB(value);
496 void BufferView::Pimpl::workAreaMotionNotify(int x, int y, unsigned int state)
498 if (buffer_ == 0 || !screen) return;
500 // Only use motion with button 1
501 if (!state & Button1MotionMask)
504 // Check for inset locking
505 if (bv_->the_locking_inset) {
506 LyXCursor cursor = bv_->text->cursor;
507 bv_->the_locking_inset->
508 InsetMotionNotify(bv_,
510 y - cursor.y + screen->first,
515 /* The selection possible is needed, that only motion events are
516 * used, where the bottom press event was on the drawing area too */
517 if (selection_possible) {
518 screen->HideCursor();
520 bv_->text->SetCursorFromCoordinates(x, y + screen->first);
522 if (!bv_->text->selection)
523 update(-3); // Maybe an empty line was deleted
525 bv_->text->SetSelection();
526 screen->ToggleToggle();
528 screen->ShowCursor();
534 // Single-click on work area
535 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
541 if (buffer_ == 0 || !screen) return;
543 Inset * inset_hit = checkInsetHit(xpos, ypos, button);
545 // ok ok, this is a hack.
546 if (button == 4 || button == 5) {
549 scrollUp(100); // This number is only temporary
557 if (bv_->the_locking_inset) {
558 // We are in inset locking mode
560 /* Check whether the inset was hit. If not reset mode,
561 otherwise give the event to the inset */
562 if (inset_hit == bv_->the_locking_inset) {
563 bv_->the_locking_inset->
564 InsetButtonPress(bv_,
569 bv_->unlockInset(bv_->the_locking_inset);
574 selection_possible = true;
575 screen->HideCursor();
577 // Right button mouse click on a table
579 (bv_->text->cursor.par->table ||
580 bv_->text->MouseHitInTable(xpos, ypos + screen->first))) {
581 // Set the cursor to the press-position
582 bv_->text->SetCursorFromCoordinates(xpos, ypos + screen->first);
585 // Only show the table popup if the hit is in
587 if (!bv_->text->HitInTable(bv_->text->cursor.row, xpos))
590 // Hit above or below the table?
592 if (!bv_->text->selection) {
593 screen->ToggleSelection();
594 bv_->text->ClearSelection();
595 bv_->text->FullRebreak();
599 // Popup table popup when on a table.
600 // This is obviously temporary, since we
601 // should be able to popup various
602 // context-sensitive-menus with the
603 // the right mouse. So this should be done more
604 // general in the future. Matthias.
605 selection_possible = false;
607 ->Dispatch(LFUN_LAYOUT_TABLE,
613 int screen_first = screen->first;
615 // Middle button press pastes if we have a selection
616 bool paste_internally = false;
618 && bv_->text->selection) {
619 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
620 paste_internally = true;
623 // Clear the selection
624 screen->ToggleSelection();
625 bv_->text->ClearSelection();
626 bv_->text->FullRebreak();
630 // Single left click in math inset?
631 if ((inset_hit != 0) &&
632 (inset_hit->Editable()==Inset::HIGHLY_EDITABLE)) {
633 // Highly editable inset, like math
634 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
635 selection_possible = false;
636 owner_->updateLayoutChoice();
637 owner_->getMiniBuffer()->Set(inset->EditMessage());
638 inset->InsetButtonPress(bv_, xpos, ypos, button);
639 inset->Edit(bv_, xpos, ypos, button);
643 // Right click on a footnote flag opens float menu
645 selection_possible = false;
649 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
650 bv_->text->SetCursorFromCoordinates(xpos, ypos + screen_first);
651 bv_->text->FinishUndo();
652 bv_->text->sel_cursor = bv_->text->cursor;
653 bv_->text->cursor.x_fix = bv_->text->cursor.x;
655 owner_->updateLayoutChoice();
657 selection_possible = false;
660 // Insert primary selection with middle mouse
661 // if there is a local selection in the current buffer,
664 if (paste_internally)
665 owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
667 owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
669 selection_possible = false;
675 void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, unsigned int button)
678 if (buffer_ && !bv_->the_locking_inset) {
679 if (screen && button == 1) {
680 screen->HideCursor();
681 screen->ToggleSelection();
682 bv_->text->SelectWord();
683 screen->ToggleSelection(false);
684 /* This will fit the cursor on the screen
692 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, unsigned int button)
695 if (buffer_ && screen && button == 1) {
696 screen->HideCursor();
697 screen->ToggleSelection();
698 bv_->text->CursorHome();
699 bv_->text->sel_cursor = bv_->text->cursor;
700 bv_->text->CursorEnd();
701 bv_->text->SetSelection();
702 screen->ToggleSelection(false);
703 /* This will fit the cursor on the screen
710 void BufferView::Pimpl::enterView()
712 if (active() && available()) {
713 SetXtermCursor(workarea->getWin());
714 using_xterm_cursor = true;
719 void BufferView::Pimpl::leaveView()
721 if (using_xterm_cursor) {
722 XUndefineCursor(fl_display, workarea->getWin());
723 using_xterm_cursor = false;
728 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
731 if (buffer_ == 0 || screen == 0) return;
733 // If we hit an inset, we have the inset coordinates in these
734 // and inset_hit points to the inset. If we do not hit an
735 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
736 Inset * inset_hit = checkInsetHit(x, y, button);
738 if (bv_->the_locking_inset) {
739 // We are in inset locking mode.
741 /* LyX does a kind of work-area grabbing for insets.
742 Only a ButtonPress Event outside the inset will
743 force a InsetUnlock. */
744 bv_->the_locking_inset->
745 InsetButtonRelease(bv_, x, y, button);
749 selection_possible = false;
750 if (bv_->text->cursor.par->table) {
751 int cell = bv_->text->
752 NumberOfCell(bv_->text->cursor.par,
753 bv_->text->cursor.pos);
754 if (bv_->text->cursor.par->table->IsContRow(cell) &&
755 bv_->text->cursor.par->table->
756 CellHasContRow(bv_->text->cursor.par->table->
757 GetCellAbove(cell))<0) {
758 bv_->text->CursorUp();
762 if (button >= 2) return;
765 owner_->getMiniBuffer()->Set(CurrentState(bv_));
767 // Did we hit an editable inset?
768 if (inset_hit != 0) {
769 // Inset like error, notes and figures
770 selection_possible = false;
772 // CHECK fix this proper in 0.13
774 // Following a ref shouldn't issue
775 // a push on the undo-stack
776 // anylonger, now that we have
777 // keybindings for following
778 // references and returning from
779 // references. IMHO though, it
780 // should be the inset's own business
781 // to push or not push on the undo
782 // stack. They don't *have* to
783 // alter the document...
785 // ...or maybe the SetCursorParUndo()
786 // below isn't necessary at all anylonger?
787 if (inset_hit->LyxCode() == Inset::REF_CODE) {
788 bv_->text->SetCursorParUndo();
791 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
792 if (inset_hit->Editable()==Inset::HIGHLY_EDITABLE) {
793 // Highly editable inset, like math
794 UpdatableInset *inset = (UpdatableInset *)inset_hit;
795 inset->InsetButtonRelease(bv_, x, y, button);
797 inset_hit->InsetButtonRelease(bv_, x, y, button);
798 inset_hit->Edit(bv_, x, y, button);
803 // check whether we want to open a float
807 if (bv_->text->cursor.pos <
808 bv_->text->cursor.par->Last()) {
809 c = bv_->text->cursor.par->
810 GetChar(bv_->text->cursor.pos);
812 if (c == LyXParagraph::META_FOOTNOTE
813 || c == LyXParagraph::META_MARGIN
814 || c == LyXParagraph::META_FIG
815 || c == LyXParagraph::META_TAB
816 || c == LyXParagraph::META_WIDE_FIG
817 || c == LyXParagraph::META_WIDE_TAB
818 || c == LyXParagraph::META_ALGORITHM){
820 } else if (bv_->text->cursor.pos - 1 >= 0) {
821 c = bv_->text->cursor.par->
822 GetChar(bv_->text->cursor.pos - 1);
823 if (c == LyXParagraph::META_FOOTNOTE
824 || c == LyXParagraph::META_MARGIN
825 || c == LyXParagraph::META_FIG
826 || c == LyXParagraph::META_TAB
827 || c == LyXParagraph::META_WIDE_FIG
828 || c == LyXParagraph::META_WIDE_TAB
829 || c == LyXParagraph::META_ALGORITHM){
830 // We are one step too far to the right
831 bv_->text->CursorLeft();
837 selection_possible = false;
842 // Do we want to close a float? (click on the float-label)
843 if (bv_->text->cursor.row->par->footnoteflag ==
844 LyXParagraph::OPEN_FOOTNOTE
845 //&& text->cursor.pos == 0
846 && bv_->text->cursor.row->previous &&
847 bv_->text->cursor.row->previous->par->
848 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
849 LyXFont font(LyXFont::ALL_SANE);
850 font.setSize(LyXFont::SIZE_FOOTNOTE);
852 int box_x = 20; // LYX_PAPER_MARGIN;
853 box_x += lyxfont::width(" wide-tab ", font);
855 unsigned int screen_first = screen->first;
858 && y + screen_first > bv_->text->cursor.y -
859 bv_->text->cursor.row->baseline
860 && y + screen_first < bv_->text->cursor.y -
861 bv_->text->cursor.row->baseline
862 + lyxfont::maxAscent(font) * 1.2 + lyxfont::maxDescent(font) * 1.2) {
864 selection_possible = false;
869 // Maybe we want to edit a bibitem ale970302
870 if (bv_->text->cursor.par->bibkey && x < 20 +
871 bibitemMaxWidth(bv_->painter(),
874 params.textclass).defaultfont())) {
875 bv_->text->cursor.par->bibkey->Edit(bv_, 0, 0, 0);
883 * Returns an inset if inset was hit. 0 otherwise.
884 * If hit, the coordinates are changed relative to the inset.
885 * Otherwise coordinates are not changed, and false is returned.
887 Inset * BufferView::Pimpl::checkInsetHit(int & x, int & y,
888 unsigned int /* button */)
893 unsigned int y_tmp = y + screen->first;
896 bv_->text->SetCursorFromCoordinates(cursor, x, y_tmp);
897 #if 0 // Are you planning to use this Jürgen? (Lgb)
898 bool move_cursor = ((cursor.par != bv_->text->cursor.par) ||
899 (cursor.pos != bv_->text->cursor.pos));
901 if (cursor.pos < cursor.par->Last()
902 && cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET
903 && cursor.par->GetInset(cursor.pos)
904 && cursor.par->GetInset(cursor.pos)->Editable()) {
906 // Check whether the inset really was hit
907 Inset * tmpinset = cursor.par->GetInset(cursor.pos);
908 LyXFont font = bv_->text->GetFont(cursor.par, cursor.pos);
909 bool is_rtl = font.isVisibleRightToLeft();
913 start_x = cursor.x - tmpinset->width(bv_->painter(), font);
917 end_x = cursor.x + tmpinset->width(bv_->painter(), font);
920 if (x > start_x && x < end_x
921 && y_tmp > cursor.y - tmpinset->ascent(bv_->painter(), font)
922 && y_tmp < cursor.y + tmpinset->descent(bv_->painter(), font)) {
924 if (move_cursor && (tmpinset != bv_->the_locking_inset))
926 bv_->text->SetCursor(cursor.par,cursor.pos,true);
928 // The origin of an inset is on the baseline
929 y = y_tmp - (bv_->text->cursor.y);
934 if ((cursor.pos - 1 >= 0) &&
935 (cursor.par->GetChar(cursor.pos-1) == LyXParagraph::META_INSET) &&
936 (cursor.par->GetInset(cursor.pos - 1)) &&
937 (cursor.par->GetInset(cursor.pos - 1)->Editable())) {
938 Inset * tmpinset = cursor.par->GetInset(cursor.pos-1);
939 LyXFont font = bv_->text->GetFont(cursor.par, cursor.pos-1);
940 bool is_rtl = font.isVisibleRightToLeft();
944 start_x = cursor.x - tmpinset->width(bv_->painter(), font);
948 end_x = cursor.x + tmpinset->width(bv_->painter(), font);
950 if (x > start_x && x < end_x
951 && y_tmp > cursor.y - tmpinset->ascent(bv_->painter(), font)
952 && y_tmp < cursor.y + tmpinset->descent(bv_->painter(), font)) {
954 if (move_cursor && (tmpinset != bv_->the_locking_inset))
956 bv_->text->SetCursor(cursor.par,cursor.pos-1,true);
958 // The origin of an inset is on the baseline
959 y = y_tmp - (bv_->text->cursor.y);
967 void BufferView::Pimpl::workAreaExpose()
969 // this is a hack to ensure that we only call this through
970 // BufferView::redraw().
975 static unsigned int work_area_width = 0;
976 static unsigned int work_area_height = 0;
978 bool widthChange = workarea->workWidth() != work_area_width;
979 bool heightChange = workarea->height() != work_area_height;
981 // update from work area
982 work_area_width = workarea->workWidth();
983 work_area_height = workarea->height();
986 // All buffers need a resize
989 // Remove all texts from the textcache
990 // This is not _really_ what we want to do. What
991 // we really want to do is to delete in textcache
992 // that does not have a BufferView with matching
993 // width, but as long as we have only one BufferView
994 // deleting all gives the same result.
995 if (lyxerr.debugging())
996 textcache.show(lyxerr, "Expose delete all");
998 } else if (heightChange) {
999 // Rebuild image of current screen
1001 // fitCursor() ensures we don't jump back
1002 // to the start of the document on vertical
1006 // The main window size has changed, repaint most stuff
1008 // ...including the minibuffer
1009 owner_->getMiniBuffer()->Init();
1011 } else if (screen) screen->Redraw();
1013 // Grey box when we don't have a buffer
1014 workarea->greyOut();
1017 // always make sure that the scrollbar is sane.
1019 owner_->updateLayoutChoice();
1025 string fromClipboard(Window win, XEvent * event)
1028 if (event->xselection.type == XA_STRING
1029 && event->xselection.property) {
1033 unsigned char * uc = 0;
1035 if (XGetWindowProperty(
1036 event->xselection.display, // display
1038 event->xselection.property, // property
1042 XA_STRING, // req_type
1043 &tmpatom, // actual_type_return
1044 &tmpint, // actual_format_return
1055 if (XGetWindowProperty(
1056 event->xselection.display, // display
1058 event->xselection.property, // property
1060 ul2/4+1, // long_length
1062 XA_STRING, // req_type
1063 &tmpatom, // actual_type_return
1064 &tmpint, // actual_format_return
1065 &ul1, // nitems_return
1066 &ul2, // bytes_after_return
1067 &uc // prop_return */
1072 strret = reinterpret_cast<char*>(uc);
1073 free(uc); // yes free!
1081 void BufferView::Pimpl::workAreaSelectionNotify(Window win, XEvent * event)
1083 if (buffer_ == 0) return;
1085 screen->HideCursor();
1086 bv_->beforeChange();
1087 string clb = fromClipboard(win, event);
1090 bv_->text->InsertStringA(clb);
1092 bv_->text->InsertStringB(clb);
1099 void BufferView::Pimpl::update()
1101 if (screen) screen->Update();
1104 // Values used when calling update:
1106 // -2 - update, move sel_cursor if selection, fitcursor
1107 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
1108 // 0 - update, move sel_cursor if selection, fitcursor
1109 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
1110 // 3 - update, move sel_cursor if selection
1113 // a simple redraw of the parts that need refresh
1115 // move sel_cursor if selection -
1116 // the text's sel_cursor is moved if there is selection is progress
1119 // fitCursor() is called and the scrollbar updated
1122 // the buffer is marked dirty.
1131 // UPDATE_ONLY = UPDATE;
1132 // UPDATE_SELECT = UPDATE | SELECT;
1133 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
1134 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1136 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
1137 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
1138 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1139 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1140 // update(3) -> update(1) -> update(1) -> update(SELECT)
1141 void BufferView::Pimpl::update(signed char f)
1144 owner_->updateLayoutChoice();
1146 if (!bv_->text->selection && f > -3)
1147 bv_->text->sel_cursor = bv_->text->cursor;
1150 bv_->text->FullRebreak();
1154 if (f != 3 && f != -3) {
1156 //updateScrollbar();
1159 if (f == 1 || f == -1) {
1160 if (buffer_->isLyxClean()) {
1161 buffer_->markDirty();
1162 owner_->getMiniBuffer()->setTimer(4);
1164 buffer_->markDirty();
1168 owner_->updateLayoutChoice();
1170 if (!bv_->text->selection && (f & SELECT))
1171 bv_->text->sel_cursor = bv_->text->cursor;
1174 bv_->text->FullRebreak();
1180 //updateScrollbar();
1184 if (buffer_->isLyxClean()) {
1185 buffer_->markDirty();
1186 owner_->getMiniBuffer()->setTimer(4);
1188 buffer_->markDirty();
1195 // Callback for cursor timer
1196 void BufferView::Pimpl::cursorToggle()
1198 // Quite a nice place for asyncron Inset updating, isn't it?
1199 // Actually no! This is run even if no buffer exist... so (Lgb)
1201 goto set_timer_and_return;
1205 // On my quest to solve the gs render hangups I am now
1206 // disabling the SIGHUP completely, and will do a wait
1207 // now and then instead. If the guess that xforms somehow
1208 // destroys something is true, this is likely (hopefully)
1209 // to solve the problem...at least I hope so. Lgb
1211 // ...Ok this seems to work...at least it does not make things
1212 // worse so far. However I still see gs processes that hangs.
1213 // I would really like to know _why_ they are hanging. Anyway
1214 // the solution without the SIGCHLD handler seems to be easier
1217 // When attaching gdb to a a running gs that hangs it shows
1218 // that it is waiting for input(?) Is it possible for us to
1219 // provide that input somehow? Or figure what it is expecing
1222 // One solution is to, after some time, look if there are some
1223 // old gs processes still running and if there are: kill them
1226 // Another solution is to provide the user an option to rerender
1227 // a picture. This would, for the picture in question, check if
1228 // there is a gs running for it, if so kill it, and start a new
1229 // rendering process.
1231 // these comments posted to lyx@via
1234 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1235 if (pid == -1) // error find out what is wrong
1236 ; // ignore it for now.
1238 sigchldhandler(pid, &status);
1241 updatelist.update(bv_);
1244 goto set_timer_and_return;
1247 if (lyx_focus && work_area_focus) {
1248 if (!bv_->the_locking_inset) {
1249 screen->CursorToggle();
1251 bv_->the_locking_inset->
1252 ToggleInsetCursor(bv_);
1254 goto set_timer_and_return;
1256 // Make sure that the cursor is visible.
1257 if (!bv_->the_locking_inset) {
1258 screen->ShowCursor();
1260 if (!bv_->the_locking_inset->isCursorVisible())
1261 bv_->the_locking_inset->
1262 ToggleInsetCursor(bv_);
1264 // This is only run when work_area_focus or lyx_focus is false.
1267 XGetInputFocus(fl_display, &tmpwin, &tmp);
1268 // Commenting this out, we have not had problems with this
1269 // for a long time. We will probably work on this code later
1270 // and we can reenable this debug code then. Now it only
1271 // anoying when debugging. (Lgb)
1272 //if (lyxerr.debugging(Debug::INFO)) {
1273 // lyxerr << "tmpwin: " << tmpwin
1274 // << "\nwindow: " << view->owner_->getForm()->window
1275 // << "\nwork_area_focus: " << view->work_area_focus
1276 // << "\nlyx_focus : " << view->lyx_focus
1279 if (tmpwin != owner_->getForm()->window) {
1284 if (!work_area_focus)
1287 goto set_timer_and_return;
1291 set_timer_and_return:
1292 fl_set_timer(timer_cursor, 0.4);
1298 void BufferView::Pimpl::cursorPrevious()
1300 if (!bv_->text->cursor.row->previous) return;
1302 long y = screen->first;
1303 Row * cursorrow = bv_->text->cursor.row;
1304 bv_->text->SetCursorFromCoordinates(bv_->text->cursor.x_fix, y);
1305 bv_->text->FinishUndo();
1306 // This is to allow jumping over large insets
1307 if ((cursorrow == bv_->text->cursor.row))
1308 bv_->text->CursorUp();
1310 if (bv_->text->cursor.row->height < workarea->height())
1311 screen->Draw(bv_->text->cursor.y
1312 - bv_->text->cursor.row->baseline
1313 + bv_->text->cursor.row->height
1314 - workarea->height() + 1 );
1319 void BufferView::Pimpl::cursorNext()
1321 if (!bv_->text->cursor.row->next) return;
1323 long y = screen->first;
1324 bv_->text->GetRowNearY(y);
1325 Row * cursorrow = bv_->text->cursor.row;
1326 bv_->text->SetCursorFromCoordinates(bv_->text->cursor.x_fix, y
1327 + workarea->height());
1328 bv_->text->FinishUndo();
1329 // This is to allow jumping over large insets
1330 if ((cursorrow == bv_->text->cursor.row))
1331 bv_->text->CursorDown();
1333 if (bv_->text->cursor.row->height < workarea->height())
1334 screen->Draw(bv_->text->cursor.y
1335 - bv_->text->cursor.row->baseline);
1340 bool BufferView::Pimpl::available() const
1342 if (buffer_ && bv_->text) return true;
1347 void BufferView::Pimpl::beforeChange()
1350 bv_->text->ClearSelection();
1355 void BufferView::Pimpl::savePosition()
1357 backstack.push(buffer_->fileName(),
1358 bv_->text->cursor.x,
1359 bv_->text->cursor.y);
1363 void BufferView::Pimpl::restorePosition()
1365 if (backstack.empty()) return;
1368 string fname = backstack.pop(&x, &y);
1371 Buffer * b = bufferlist.exists(fname) ?
1372 bufferlist.getBuffer(fname) :
1373 bufferlist.loadLyXFile(fname); // don't ask, just load it
1375 bv_->text->SetCursorFromCoordinates(x, y);
1380 void BufferView::Pimpl::setState()
1382 if (!lyxrc.rtl_support)
1385 if (bv_->text->real_current_font.isRightToLeft() &&
1386 bv_->text->real_current_font.latex() != LyXFont::ON) {
1387 if (owner_->getIntl()->primarykeymap)
1388 owner_->getIntl()->KeyMapSec();
1390 if (!owner_->getIntl()->primarykeymap)
1391 owner_->getIntl()->KeyMapPrim();
1396 void BufferView::Pimpl::insetSleep()
1398 if (bv_->the_locking_inset && !bv_->inset_slept) {
1399 bv_->the_locking_inset->GetCursorPos(bv_->slx, bv_->sly);
1400 bv_->the_locking_inset->InsetUnlock(bv_);
1401 bv_->inset_slept = true;
1406 void BufferView::Pimpl::insetWakeup()
1408 if (bv_->the_locking_inset && bv_->inset_slept) {
1409 bv_->the_locking_inset->Edit(bv_, bv_->slx, bv_->sly, 0);
1410 bv_->inset_slept = false;
1415 void BufferView::Pimpl::insetUnlock()
1417 if (bv_->the_locking_inset) {
1418 if (!bv_->inset_slept) bv_->the_locking_inset->InsetUnlock(bv_);
1419 bv_->the_locking_inset = 0;
1420 bv_->text->FinishUndo();
1421 bv_->inset_slept = false;
1426 bool BufferView::Pimpl::focus() const
1428 return workarea->hasFocus();
1432 void BufferView::Pimpl::focus(bool f)
1434 if (f) workarea->setFocus();
1438 bool BufferView::Pimpl::active() const
1440 return workarea->active();
1444 bool BufferView::Pimpl::belowMouse() const
1446 return workarea->belowMouse();
1450 void BufferView::Pimpl::showCursor()
1453 screen->ShowCursor();
1457 void BufferView::Pimpl::hideCursor()
1460 screen->HideCursor();
1464 void BufferView::Pimpl::toggleSelection(bool b)
1467 screen->ToggleSelection(b);
1471 void BufferView::Pimpl::toggleToggle()
1474 screen->ToggleToggle();
1478 void BufferView::Pimpl::center()
1481 if (bv_->text->cursor.y > workarea->height() / 2) {
1482 screen->Draw(bv_->text->cursor.y - workarea->height() / 2);