2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-1999 The LyX Team.
10 * ====================================================== */
23 #include "support/lstrings.h"
26 #pragma implementation
29 #include "commandtags.h"
30 #include "BufferView.h"
31 #include "bufferlist.h"
34 #include "insets/lyxinset.h"
35 #include "minibuffer.h"
36 #include "lyxscreen.h"
41 #include "lyx_gui_misc.h"
42 #include "BackStack.h"
50 extern BufferList bufferlist;
51 void sigchldhandler(pid_t pid, int * status);
53 extern void SetXtermCursor(Window win);
54 extern bool input_prohibited;
55 extern bool selection_possible;
56 extern char ascii_type;
57 //extern int UnlockInset(UpdatableInset * inset);
58 extern void MenuPasteSelection(char at);
59 extern InsetUpdateStruct * InsetUpdateList;
60 extern void UpdateInsetUpdateList();
61 extern void FreeUpdateTimer();
63 // This is _very_ temporary
64 FL_OBJECT * figinset_canvas;
66 BufferView::BufferView(LyXView * o, int xpos, int ypos,
67 int width, int height)
79 current_scrollbar_value = 0;
80 create_view(xpos, ypos, width, height);
81 // Activate the timer for the cursor
82 fl_set_timer(timer_cursor, 0.4);
83 fl_set_focus_object(owner_->getForm(), work_area);
84 work_area_focus = true;
86 the_locking_inset = 0;
91 BufferView::~BufferView()
96 // This is only the very first implemetation and use of the TextCache,
97 // operations on it needs to be put into a class or a namespace, that part
98 // is _NOT_ finished so don't bother to come with too many comments on it
99 // (unless you have some nice ideas on where/how to do it)
101 // I think we need a TextCache that is common for all BufferViews,
102 // please tell if you don't agree.
104 // Q. What are we caching?
105 // A. We are caching the screen representations (LyXText) of the
106 // documents (Buffer,LyXParagraph) for specific BufferView widths.
108 // A. It is not really needed, but it speeds things up a lot
109 // when you have more than one document loaded at once since a total
110 // rebreak (reformatting) need not be done when switching between
111 // documents. When the cache is in function a document only needs to be
112 // formatted upon loading and when the with of the BufferView changes.
113 // Later it will also be unneccessary to reformat when having two
114 // BufferViews of equal width with the same document, a simple copy
115 // of the LyXText structure will do.
116 // Invariant for the TextCache:
117 // - The buffer of the text in the TextCache _must_ exists
118 // in the bufferlist.
119 // - For a text in the TextCache there _must not_ be an equivalent
120 // text in any BufferView. (same buffer and width).
121 // Among others this mean:
122 // - When a document is closed all trace of it must be removed from
125 // I believe there are only three possible scenarios where the two first
126 // are also covered by the third.
127 // - The simplest scenario is what we have now, a single
130 // Nothing to do with the TextCache is done when opening a file.
132 // We switch from buffer A to buffer B.
133 // * A's text is cached in TextCache.
134 // * We make a search for a text in TextCache that fits B
135 // (same buffer and same width).
136 // o Horizontal resize
137 // If the BufferView's width (LyXView) is horizontally changed all
138 // the entries in the TextCache are deleted. (This causes
139 // reformat of all loaded documents when next viewed)
141 // When a buffer is closed we don't have to do anything, because
142 // to close a single buffer it is required to only exist in the
143 // BufferView and not in the TextCache. Upon LFUN_QUIT we
144 // don't really care since everything is deleted anyway.
145 // - The next scenario is when we have several BufferViews (in one or
146 // more LyXViews) of equal width.
148 // Nothing to do with the TextCache is done when opening a file.
150 // We switch from buffer A to buffer B.
151 // * If A is in another Bufferview we do not put it into TextCache.
152 // else we put A into TextCache.
153 // * If B is viewed in another BufferView we make a copy of its
154 // text and use that, else we search in TextCache for a match.
155 // (same buffer same width)
156 // o Horizontal resize
157 // If the BufferView's width (LyXView) is horisontaly changed all
158 // the entries in the TextCache is deleted. (This causes
159 // reformat of all loaded documents when next viewed)
161 // - The last scenario should cover both the previous ones, this time
162 // we have several BufferViews (in one or more LyXViews) with no
163 // limitations on width. (And if you wonder why the two other
164 // senarios are needed... I used them to get to this one.)
166 // Nothing to do with the TextCache is done when opening a file.
168 // We switch from buffer A to buffer B.
169 // o Horisontal rezize
172 typedef vector<LyXText*> TextCache;
177 text_fits(Buffer * b, unsigned short p)
179 bool operator()(TextCache::value_type & vt) {
180 if (vt->params == buf && vt->paperWidth() == pw) return true;
191 show_text(ostream & o) : os(o) {}
192 void operator()(TextCache::value_type & vt) {
193 os << "Buffer: " << vt->params
194 << "\nWidth: " << vt->paperWidth() << endl;
200 void showTextCache(string const & str)
202 lyxerr << "TextCache: " << str << endl;
203 for_each(textcache.begin(), textcache.end(), show_text(lyxerr));
207 void BufferView::buffer(Buffer * b)
209 lyxerr[Debug::INFO] << "Setting buffer in BufferView" << endl;
212 buffer_->delUser(this);
213 // Put the old text into the TextCache.
214 textcache.push_back(text);
215 showTextCache("buffer");
220 // Set current buffer
223 if (bufferlist.getState() == BufferList::CLOSING) return;
226 // screen is always deleted when the buffer is changed.
231 // If we are closing the buffer, use the first buffer as current
233 buffer_ = bufferlist.first();
237 lyxerr[Debug::INFO] << " Buffer addr: " << buffer_ << endl;
238 buffer_->addUser(this);
239 owner_->getMenus()->showMenus();
240 // If we don't have a text object for this, we make one
242 resizeCurrentBuffer();
247 screen->first = screen->TopCursorVisible();
249 updateAllVisibleBufferRelatedPopups();
252 lyxerr[Debug::INFO] << " No Buffer!" << endl;
253 owner_->getMenus()->hideMenus();
255 fl_redraw_object(work_area);
256 // Also remove all remaining text's from the testcache.
257 showTextCache("buffer delete all");
258 while (!textcache.empty()) {
259 LyXText * tt = textcache.front();
260 textcache.erase(textcache.begin());
264 // should update layoutchoice even if we don't have a buffer.
265 owner_->updateLayoutChoice();
266 owner_->getMiniBuffer()->Init();
267 owner_->updateWindowTitle();
271 void BufferView::updateScreen()
273 // Regenerate the screen.
276 screen = new LyXScreen(FL_ObjWin(work_area),
285 void BufferView::resize()
287 // This will resize the buffer. (Asger)
289 resizeCurrentBuffer();
293 static bool lgb_hack = false;
295 void BufferView::redraw()
297 lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
299 fl_redraw_object(work_area);
300 fl_redraw_object(scrollbar);
301 fl_redraw_object(button_down);
302 fl_redraw_object(button_up);
307 void BufferView::fitCursor()
309 if (screen) screen->FitCursor();
313 void BufferView::update()
315 if (screen) screen->Update();
319 void BufferView::updateScrollbar()
321 /* If the text is smaller than the working area, the scrollbar
322 * maximum must be the working area height. No scrolling will
326 fl_set_slider_value(scrollbar, 0);
327 fl_set_slider_size(scrollbar, scrollbar->h);
331 static long max2 = 0;
332 static long height2 = 0;
340 cbsf = screen->first;
342 // check if anything has changed.
344 height2 == work_area->h &&
345 current_scrollbar_value == cbsf)
349 height2 = work_area->h;
350 current_scrollbar_value = cbsf;
352 if (cbth <= height2) { // text is smaller than screen
353 fl_set_slider_size(scrollbar, scrollbar->h);
357 long maximum_height = work_area->h * 3 / 4 + cbth;
361 double hfloat = work_area->h;
362 double maxfloat = maximum_height;
364 fl_set_slider_value(scrollbar, value);
365 fl_set_slider_bounds(scrollbar, 0,
366 maximum_height - work_area->h);
368 double lineh = text->DefaultHeight();
369 fl_set_slider_increment(scrollbar, work_area->h-lineh, lineh);
372 if ((hfloat / maxfloat) * float(height2) < 3)
373 fl_set_slider_size(scrollbar,
376 fl_set_slider_size(scrollbar,
379 fl_set_slider_size(scrollbar, hfloat);
380 fl_set_slider_precision(scrollbar, 0);
384 void BufferView::redoCurrentBuffer()
386 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
387 if (buffer_ && text) {
389 owner_->updateLayoutChoice();
394 int BufferView::resizeCurrentBuffer()
396 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
398 LyXParagraph * par = 0;
399 LyXParagraph * selstartpar = 0;
400 LyXParagraph * selendpar = 0;
409 owner_->getMiniBuffer()->Set(_("Formatting document..."));
412 par = text->cursor.par;
413 pos = text->cursor.pos;
414 selstartpar = text->sel_start_cursor.par;
415 selstartpos = text->sel_start_cursor.pos;
416 selendpar = text->sel_end_cursor.par;
417 selendpos = text->sel_end_cursor.pos;
418 selection = text->selection;
419 mark_set = text->mark_set;
421 text = new LyXText(work_area->w, buffer_);
423 // See if we have a text in TextCache that fits
424 // the new buffer_ with the correct width.
425 TextCache::iterator it =
426 find_if(textcache.begin(),
430 if (it != textcache.end()) {
432 // take it out of textcache.
434 showTextCache("resizeCurrentBuffer");
436 text = new LyXText(work_area->w, buffer_);
442 text->selection = true;
443 /* at this point just to avoid the Delete-Empty-Paragraph
444 * Mechanism when setting the cursor */
445 text->mark_set = mark_set;
447 text->SetCursor(selstartpar, selstartpos);
448 text->sel_cursor = text->cursor;
449 text->SetCursor(selendpar, selendpos);
450 text->SetSelection();
451 text->SetCursor(par, pos);
453 text->SetCursor(par, pos);
454 text->sel_cursor = text->cursor;
455 text->selection = false;
458 screen->first = screen->TopCursorVisible(); /* this will scroll the
459 * screen such that the
464 owner_->getMiniBuffer()->Init();
467 // Now if the title form still exist kill it
474 void BufferView::gotoError()
479 screen->HideCursor();
484 if (!text->GotoNextError()) {
486 || text->cursor.par != text->FirstParagraph()) {
488 text->cursor.par = text->FirstParagraph();
489 text->cursor.pos = 0;
490 if (!text->GotoNextError()) {
492 owner_->getMiniBuffer()
493 ->Set(_("No more errors"));
497 owner_->getMiniBuffer()->Set(_("No more errors"));
502 text->sel_cursor = text->cursor;
507 // Just a bunch of C wrappers around static members of BufferView
508 void C_BufferView_UpCB(FL_OBJECT * ob, long buf)
510 BufferView::UpCB(ob, buf);
514 void C_BufferView_DownCB(FL_OBJECT * ob, long buf)
516 BufferView::DownCB(ob, buf);
520 void C_BufferView_ScrollCB(FL_OBJECT * ob, long buf)
522 BufferView::ScrollCB(ob, buf);
526 void C_BufferView_CursorToggleCB(FL_OBJECT * ob, long buf)
528 BufferView::CursorToggleCB(ob, buf);
532 int C_BufferView_work_area_handler(FL_OBJECT * ob, int event,
536 return BufferView::work_area_handler(ob, event,
542 void BufferView::create_view(int xpos, int ypos, int width, int height)
545 int const bw = abs(fl_get_border_width());
547 // a hack for the figinsets (Matthias)
548 // This one first, then it will probably be invisible. (Lgb)
549 ::figinset_canvas = figinset_canvas = obj =
550 fl_add_canvas(FL_NORMAL_CANVAS,
553 fl_set_object_boxtype(obj, FL_NO_BOX);
554 fl_set_object_resize(obj, FL_RESIZE_ALL);
555 fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
558 obj = fl_add_box(FL_BORDER_BOX, xpos, ypos,
561 fl_set_object_resize(obj, FL_RESIZE_ALL);
562 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
565 work_area = obj = fl_add_free(FL_INPUT_FREE,
566 xpos + bw, ypos + bw,
567 width - 15 - 2 * bw /* scrollbarwidth */,
569 C_BufferView_work_area_handler);
570 obj->wantkey = FL_KEY_TAB;
571 obj->u_vdata = this; /* This is how we pass the BufferView
572 to the work_area_handler. */
573 fl_set_object_boxtype(obj, FL_DOWN_BOX);
574 fl_set_object_resize(obj, FL_RESIZE_ALL);
575 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
581 // up - scrollbar button
583 fl_set_border_width(-1);
585 fl_set_border_width(-2); // to get visible feedback
587 button_up = obj = fl_add_pixmapbutton(FL_TOUCH_BUTTON,
591 fl_set_object_boxtype(obj, FL_UP_BOX);
592 fl_set_object_color(obj, FL_MCOL, FL_BLUE);
593 fl_set_object_resize(obj, FL_RESIZE_ALL);
594 fl_set_object_gravity(obj, NorthEastGravity, NorthEastGravity);
595 fl_set_object_callback(obj, C_BufferView_UpCB, 0);
597 fl_set_pixmapbutton_data(obj, const_cast<char**>(up_xpm));
600 // Remove the blue feedback rectangle
601 fl_set_pixmapbutton_focus_outline(obj, 0);
604 // the scrollbar slider
605 fl_set_border_width(-bw);
606 scrollbar = obj = fl_add_slider(FL_VERT_SLIDER,
609 15, height - 30, "");
610 fl_set_object_color(obj, FL_COL1, FL_MCOL);
611 fl_set_object_boxtype(obj, FL_UP_BOX);
612 fl_set_object_resize(obj, FL_RESIZE_ALL);
613 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
614 fl_set_object_callback(obj, C_BufferView_ScrollCB, 0);
617 // down - scrollbar button
619 fl_set_border_width(-1);
621 fl_set_border_width(-2); // to get visible feedback
623 button_down = obj = fl_add_pixmapbutton(FL_TOUCH_BUTTON,
627 fl_set_object_boxtype(obj, FL_UP_BOX);
628 fl_set_object_color(obj, FL_MCOL, FL_BLUE);
629 fl_set_object_resize(obj, FL_RESIZE_ALL);
630 fl_set_object_gravity(obj, SouthEastGravity, SouthEastGravity);
631 fl_set_object_callback(obj, C_BufferView_DownCB, 0);
633 fl_set_pixmapbutton_data(obj, const_cast<char**>(down_xpm));
634 fl_set_border_width(-bw);
637 // Remove the blue feedback rectangle
638 fl_set_pixmapbutton_focus_outline(obj, 0);
646 timer_cursor = obj = fl_add_timer(FL_HIDDEN_TIMER,
647 0, 0, 0, 0, "Timer");
648 fl_set_object_callback(obj, C_BufferView_CursorToggleCB, 0);
653 // Callback for scrollbar up button
654 void BufferView::UpCB(FL_OBJECT * ob, long)
656 BufferView * view = static_cast<BufferView*>(ob->u_vdata);
658 if (view->buffer_ == 0) return;
660 static long time = 0;
661 XEvent const * ev2 = fl_last_event();
662 if (ev2->type == ButtonPress || ev2->type == ButtonRelease)
664 int button = fl_get_button_numb(ob);
667 view->ScrollUpOnePage(time++); break;
669 view->ScrollDownOnePage(time++); break;
671 view->ScrollUp(time++); break;
683 a = XInternAtom(fl_display, "WAIT_FOR_X", False);
686 XSetWindowAttributes attr;
687 mask = CWOverrideRedirect;
688 attr.override_redirect = 1;
689 w = XCreateWindow(fl_display, fl_root,
690 0, 0, 1, 1, 0, CopyFromParent,
691 InputOnly, CopyFromParent, mask, &attr);
692 XSelectInput(fl_display, w, PropertyChangeMask);
693 XMapWindow(fl_display, w);
696 XChangeProperty(fl_display, w, a, a, 8,
698 reinterpret_cast<unsigned char*>(""), 0);
699 XWindowEvent(fl_display, w, PropertyChangeMask, &ev);
701 XSync(fl_get_display(), 0);
705 // Callback for scrollbar slider
706 void BufferView::ScrollCB(FL_OBJECT * ob, long)
708 BufferView * view = static_cast<BufferView*>(ob->u_vdata);
709 extern bool cursor_follows_scrollbar;
711 if (view->buffer_ == 0) return;
713 view->current_scrollbar_value = long(fl_get_slider_value(ob));
714 if (view->current_scrollbar_value < 0)
715 view->current_scrollbar_value = 0;
720 view->screen->Draw(view->current_scrollbar_value);
722 if (cursor_follows_scrollbar) {
723 LyXText * vbt = view->text;
724 int height = vbt->DefaultHeight();
726 if (vbt->cursor.y < view->screen->first + height) {
727 vbt->SetCursorFromCoordinates(0,
728 view->screen->first +
731 else if (vbt->cursor.y >
732 view->screen->first + view->work_area->h - height) {
733 vbt->SetCursorFromCoordinates(0,
734 view->screen->first +
743 // Callback for scrollbar down button
744 void BufferView::DownCB(FL_OBJECT * ob, long)
746 BufferView * view = static_cast<BufferView*>(ob->u_vdata);
748 if (view->buffer_ == 0) return;
751 static long time = 0;
752 ev2 = fl_last_event();
753 if (ev2->type == ButtonPress || ev2->type == ButtonRelease)
755 int button = fl_get_button_numb(ob);
758 view->ScrollUpOnePage(time++); break;
760 view->ScrollDownOnePage(time++); break;
762 view->ScrollDown(time++); break;
767 int BufferView::ScrollUp(long time)
769 if (buffer_ == 0) return 0;
770 if (!screen) return 0;
772 double value = fl_get_slider_value(scrollbar);
774 if (value == 0) return 0;
776 float add_value = (text->DefaultHeight()
777 + float(time) * float(time) * 0.125);
779 if (add_value > work_area->h)
780 add_value = float(work_area->h -
781 text->DefaultHeight());
788 fl_set_slider_value(scrollbar, value);
790 ScrollCB(scrollbar, 0);
795 int BufferView::ScrollDown(long time)
797 if (buffer_ == 0) return 0;
798 if (!screen) return 0;
800 double value= fl_get_slider_value(scrollbar);
802 fl_get_slider_bounds(scrollbar, &min, &max);
804 if (value == max) return 0;
806 float add_value = (text->DefaultHeight()
807 + float(time) * float(time) * 0.125);
809 if (add_value > work_area->h)
810 add_value = float(work_area->h -
811 text->DefaultHeight());
818 fl_set_slider_value(scrollbar, value);
820 ScrollCB(scrollbar, 0);
825 void BufferView::ScrollUpOnePage(long /*time*/)
827 if (buffer_ == 0) return;
830 long y = screen->first;
834 Row * row = text->GetRowNearY(y);
836 y = y - work_area->h + row->height;
838 fl_set_slider_value(scrollbar, y);
840 ScrollCB(scrollbar, 0);
844 void BufferView::ScrollDownOnePage(long /*time*/)
846 if (buffer_ == 0) return;
850 fl_get_slider_bounds(scrollbar, &min, &max);
851 long y = screen->first;
853 if (y > text->height - work_area->h)
857 text->GetRowNearY(y);
859 fl_set_slider_value(scrollbar, y);
861 ScrollCB(scrollbar, 0);
865 int BufferView::work_area_handler(FL_OBJECT * ob, int event,
867 int /*key*/, void * xev)
869 static int x_old = -1;
870 static int y_old = -1;
871 static long scrollbar_value_old = -1;
873 XEvent * ev = static_cast<XEvent*>(xev);
874 BufferView * view = static_cast<BufferView*>(ob->u_vdata);
876 // If we don't have a view yet; return
877 if (!view || quitting) return 0;
881 view->workAreaExpose();
884 view->WorkAreaButtonPress(ob, 0, 0, 0, ev, 0);
887 view->WorkAreaButtonRelease(ob, 0, 0, 0, ev, 0);
890 if (ev->xmotion.x != x_old ||
891 ev->xmotion.y != y_old ||
892 view->current_scrollbar_value != scrollbar_value_old) {
893 x_old = ev->xmotion.x;
894 y_old = ev->xmotion.y;
895 scrollbar_value_old = view->current_scrollbar_value;
896 view->WorkAreaMotionNotify(ob, 0, 0, 0, ev, 0);
899 // Done by the raw callback:
901 // WorkAreaKeyPress(ob, 0, 0, 0, ev, 0); break;
903 if (!view->owner_->getMiniBuffer()->shows_no_match)
904 view->owner_->getMiniBuffer()->Init();
905 view->owner_->getMiniBuffer()->shows_no_match = false;
906 view->work_area_focus = true;
907 fl_set_timer(view->timer_cursor, 0.4);
910 view->owner_->getMiniBuffer()->ExecCommand();
911 view->work_area_focus = false;
914 SetXtermCursor(view->owner_->getForm()->window);
916 view->lyx_focus = true;
917 fl_set_timer(view->timer_cursor, 0.4);
920 if (!input_prohibited)
921 XUndefineCursor(fl_display,
922 view->owner_->getForm()->window);
923 view->lyx_focus = false; // This is not an absolute truth
924 // but if it is not true, it will be changed within a blink
925 // of an eye. ... Not good enough... use regulare timeperiod
926 //fl_set_timer(view->timer_cursor, 0.01); // 0.1 sec blink
927 fl_set_timer(view->timer_cursor, 0.4); // 0.4 sec blink
931 if (!view->the_locking_inset) {
932 if (view->screen && ev->xbutton.button == 1) {
933 view->screen->HideCursor();
934 view->screen->ToggleSelection();
935 view->text->SelectWord();
936 view->screen->ToggleSelection(false);
937 /* This will fit the cursor on the screen
945 if (view->buffer_ && view->screen && ev->xbutton.button == 1) {
946 view->screen->HideCursor();
947 view->screen->ToggleSelection();
948 view->text->CursorHome();
949 view->text->sel_cursor = view->text->cursor;
950 view->text->CursorEnd();
951 view->text->SetSelection();
952 view->screen->ToggleSelection(false);
953 /* This will fit the cursor on the screen
959 view->WorkAreaSelectionNotify(ob,
960 view->owner_->getForm()->window,
967 int BufferView::WorkAreaMotionNotify(FL_OBJECT * ob, Window,
968 int /*w*/, int /*h*/,
969 XEvent * ev, void * /*d*/)
971 if (buffer_ == 0) return 0;
972 if (!screen) return 0;
974 // Check for inset locking
975 if (the_locking_inset) {
976 LyXCursor cursor = text->cursor;
978 InsetMotionNotify(ev->xbutton.x - ob->x - cursor.x,
979 ev->xbutton.y - ob->y -
985 // Only use motion with button 1
986 if (!ev->xmotion.state & Button1MotionMask)
989 /* The selection possible is needed, that only motion events are
990 * used, where the bottom press event was on the drawing area too */
991 if (selection_possible) {
992 screen->HideCursor();
994 text->SetCursorFromCoordinates(ev->xbutton.x - ob->x,
995 ev->xbutton.y - ob->y +
998 if (!text->selection)
999 update(-3); // Maybe an empty line was deleted
1001 text->SetSelection();
1002 screen->ToggleToggle();
1003 if (screen->FitCursor())
1005 screen->ShowCursor();
1011 extern int bibitemMaxWidth(LyXFont const &);
1013 // Single-click on work area
1014 int BufferView::WorkAreaButtonPress(FL_OBJECT * ob, Window,
1015 int /*w*/, int /*h*/,
1016 XEvent * ev, void */*d*/)
1021 if (buffer_ == 0) return 0;
1022 if (!screen) return 0;
1024 int const x = ev->xbutton.x - ob->x;
1025 int const y = ev->xbutton.y - ob->y;
1026 // If we hit an inset, we have the inset coordinates in these
1027 // and inset_hit points to the inset. If we do not hit an
1028 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1031 Inset * inset_hit = checkInsetHit(inset_x, inset_y);
1033 // ok ok, this is a hack.
1034 int button = ev->xbutton.button;
1035 if (button == 4 || button == 5) goto wheel;
1038 if (the_locking_inset) {
1039 // We are in inset locking mode
1041 /* Check whether the inset was hit. If not reset mode,
1042 otherwise give the event to the inset */
1043 if (inset_hit != 0) {
1045 InsetButtonPress(inset_x, inset_y,
1049 unlockInset(the_locking_inset);
1053 selection_possible = true;
1054 screen->HideCursor();
1056 // Right button mouse click on a table
1058 (text->cursor.par->table ||
1059 text->MouseHitInTable(x, y + screen->first))) {
1060 // Set the cursor to the press-position
1061 text->SetCursorFromCoordinates(x, y + screen->first);
1064 // Only show the table popup if the hit is in
1066 if (!text->HitInTable(text->cursor.row, x))
1069 // Hit above or below the table?
1071 if (!text->selection) {
1072 screen->ToggleSelection();
1073 text->ClearSelection();
1074 text->FullRebreak();
1078 // Popup table popup when on a table.
1079 // This is obviously temporary, since we
1080 // should be able to popup various
1081 // context-sensitive-menus with the
1082 // the right mouse. So this should be done more
1083 // general in the future. Matthias.
1084 selection_possible = false;
1085 owner_->getLyXFunc()
1086 ->Dispatch(LFUN_LAYOUT_TABLE,
1092 int screen_first = screen->first;
1094 // Middle button press pastes if we have a selection
1095 bool paste_internally = false;
1096 if (button == 2 // && !buffer_->the_locking_inset
1097 && text->selection) {
1098 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
1099 paste_internally = true;
1102 // Clear the selection
1103 screen->ToggleSelection();
1104 text->ClearSelection();
1105 text->FullRebreak();
1109 // Single left click in math inset?
1110 if (inset_hit != 0 && inset_hit->Editable() == 2) {
1111 // Highly editable inset, like math
1112 selection_possible = false;
1113 owner_->updateLayoutChoice();
1114 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
1115 inset_hit->Edit(inset_x, inset_y);
1119 // Right click on a footnote flag opens float menu
1121 selection_possible = false;
1125 text->SetCursorFromCoordinates(x, y + screen_first);
1127 text->sel_cursor = text->cursor;
1128 text->cursor.x_fix = text->cursor.x;
1130 owner_->updateLayoutChoice();
1131 if (screen->FitCursor()){
1133 selection_possible = false;
1136 // Insert primary selection with middle mouse
1137 // if there is a local selection in the current buffer,
1139 if (button == 2) { // && !buffer_->the_locking_inset){
1140 if (paste_internally)
1141 owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
1143 owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
1145 selection_possible = false;
1152 // I am not quite sure if this is the correct place to put
1153 // this, but it will not cause any harm.
1154 // Patch from Mark Huang (markman@mit.edu) to make LyX
1155 // recognise button 4 and 5. This enables LyX use use
1156 // the scrollwhell on certain mice for something useful. (Lgb)
1157 // Added wheel acceleration detection code. (Rvdk)
1158 static Time lastTime = 0;
1159 int diff = ev->xbutton.time - lastTime;
1160 int scroll = int(1.0 + (4.0 / (abs(diff) + 1.0)) * 200.0);
1169 lastTime = ev->xbutton.time;
1180 int BufferView::WorkAreaButtonRelease(FL_OBJECT * ob, Window ,
1181 int /*w*/, int /*h*/,
1182 XEvent * ev, void * /*d*/)
1184 if (buffer_ == 0 || screen == 0) return 0;
1186 int const x = ev->xbutton.x - ob->x;
1187 int const y = ev->xbutton.y - ob->y;
1189 // If we hit an inset, we have the inset coordinates in these
1190 // and inset_hit points to the inset. If we do not hit an
1191 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1194 Inset * inset_hit = checkInsetHit(inset_x, inset_y);
1196 if (the_locking_inset) {
1197 // We are in inset locking mode.
1199 /* LyX does a kind of work-area grabbing for insets.
1200 Only a ButtonPress Event outside the inset will
1201 force a InsetUnlock. */
1203 InsetButtonRelease(inset_x, inset_y,
1204 ev->xbutton.button);
1208 selection_possible = false;
1209 if (text->cursor.par->table) {
1211 NumberOfCell(text->cursor.par,
1213 if (text->cursor.par->table->IsContRow(cell) &&
1214 text->cursor.par->table->
1215 CellHasContRow(text->cursor.par->table->
1216 GetCellAbove(cell))<0) {
1221 if (ev->xbutton.button >= 2)
1224 // Make sure that the press was not far from the release
1225 if ((abs(last_click_x - x) >= 5) ||
1226 (abs(last_click_y - y) >= 5)) {
1230 // Did we hit an editable inset?
1231 if (inset_hit != 0) {
1232 // Inset like error, notes and figures
1233 selection_possible = false;
1234 #ifdef WITH_WARNINGS
1235 #warning fix this proper in 0.13
1237 // Following a ref shouldn't issue
1238 // a push on the undo-stack
1239 // anylonger, now that we have
1240 // keybindings for following
1241 // references and returning from
1242 // references. IMHO though, it
1243 // should be the inset's own business
1244 // to push or not push on the undo
1245 // stack. They don't *have* to
1246 // alter the document...
1248 // ...or maybe the SetCursorParUndo()
1249 // below isn't necessary at all anylonger?
1250 if (inset_hit->LyxCode() == Inset::REF_CODE) {
1251 text->SetCursorParUndo();
1254 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
1255 inset_hit->Edit(inset_x, inset_y);
1259 // check whether we want to open a float
1263 if (text->cursor.pos <
1264 text->cursor.par->Last()) {
1265 c = text->cursor.par->
1266 GetChar(text->cursor.pos);
1268 if (c == LyXParagraph::META_FOOTNOTE
1269 || c == LyXParagraph::META_MARGIN
1270 || c == LyXParagraph::META_FIG
1271 || c == LyXParagraph::META_TAB
1272 || c == LyXParagraph::META_WIDE_FIG
1273 || c == LyXParagraph::META_WIDE_TAB
1274 || c == LyXParagraph::META_ALGORITHM){
1276 } else if (text->cursor.pos - 1 >= 0) {
1277 c = text->cursor.par->
1278 GetChar(text->cursor.pos - 1);
1279 if (c == LyXParagraph::META_FOOTNOTE
1280 || c == LyXParagraph::META_MARGIN
1281 || c == LyXParagraph::META_FIG
1282 || c == LyXParagraph::META_TAB
1283 || c == LyXParagraph::META_WIDE_FIG
1284 || c == LyXParagraph::META_WIDE_TAB
1285 || c == LyXParagraph::META_ALGORITHM){
1286 // We are one step too far to the right
1293 selection_possible = false;
1298 // Do we want to close a float? (click on the float-label)
1299 if (text->cursor.row->par->footnoteflag ==
1300 LyXParagraph::OPEN_FOOTNOTE
1301 && text->cursor.pos == 0
1302 && text->cursor.row->previous &&
1303 text->cursor.row->previous->par->
1304 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
1305 LyXFont font (LyXFont::ALL_SANE);
1306 font.setSize(LyXFont::SIZE_SMALL);
1308 int box_x = 20; // LYX_PAPER_MARGIN;
1309 box_x += font.textWidth("Mwide-figM", 10);
1311 int screen_first = screen->first;
1314 && y + screen_first > text->cursor.y -
1315 text->cursor.row->baseline
1316 && y + screen_first < text->cursor.y -
1317 text->cursor.row->baseline
1318 + font.maxAscent() * 1.2 + font.maxDescent() * 1.2) {
1320 selection_possible = false;
1325 // Maybe we want to edit a bibitem ale970302
1326 if (text->cursor.par->bibkey && x < 20 +
1327 bibitemMaxWidth(textclasslist
1328 .TextClass(buffer_->
1329 params.textclass).defaultfont())) {
1330 text->cursor.par->bibkey->Edit(0, 0);
1338 * Returns an inset if inset was hit. 0 otherwise.
1339 * If hit, the coordinates are changed relative to the inset.
1340 * Otherwise coordinates are not changed, and false is returned.
1342 Inset * BufferView::checkInsetHit(int & x, int & y)
1347 int y_tmp = y + getScreen()->first;
1349 LyXCursor cursor = text->cursor;
1350 if (cursor.pos < cursor.par->Last()
1351 && cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET
1352 && cursor.par->GetInset(cursor.pos)
1353 && cursor.par->GetInset(cursor.pos)->Editable()) {
1355 // Check whether the inset really was hit
1356 Inset * tmpinset = cursor.par->GetInset(cursor.pos);
1357 LyXFont font = text->GetFont(cursor.par, cursor.pos);
1359 && x < cursor.x + tmpinset->Width(font)
1360 && y_tmp > cursor.y - tmpinset->Ascent(font)
1361 && y_tmp < cursor.y + tmpinset->Descent(font)) {
1363 // The origin of an inset is on the baseline
1364 y = y_tmp - (cursor.y);
1367 } else if (cursor.pos - 1 >= 0
1368 && cursor.par->GetChar(cursor.pos - 1) == LyXParagraph::META_INSET
1369 && cursor.par->GetInset(cursor.pos - 1)
1370 && cursor.par->GetInset(cursor.pos - 1)->Editable()) {
1372 Inset * result = checkInsetHit(x, y);
1374 text->CursorRight();
1384 int BufferView::workAreaExpose()
1386 if (!work_area || !work_area->form->visible)
1389 // this is a hack to ensure that we only call this through
1390 // BufferView::redraw().
1395 static int work_area_width = work_area->w;
1396 static int work_area_height = work_area->h;
1398 bool widthChange = work_area->w != work_area_width;
1399 bool heightChange = work_area->h != work_area_height;
1401 // update from work area
1402 work_area_width = work_area->w;
1403 work_area_height = work_area->h;
1406 // All buffers need a resize
1407 bufferlist.resize();
1409 // Remove all texts from the textcache
1410 showTextCache("Expose delete all");
1411 while(!textcache.empty()) {
1412 LyXText * tt = textcache.front();
1413 textcache.erase(textcache.begin());
1416 } else if (heightChange) {
1417 // Rebuild image of current screen
1419 // fitCursor() ensures we don't jump back
1420 // to the start of the document on vertical
1424 // The main window size has changed, repaint most stuff
1426 // ...including the minibuffer
1427 owner_->getMiniBuffer()->Init();
1429 } else if (screen) screen->Redraw();
1431 // Grey box when we don't have a buffer
1432 fl_winset(FL_ObjWin(work_area));
1433 fl_rectangle(1, work_area->x, work_area->y,
1434 work_area->w, work_area->h, FL_GRAY63);
1437 // always make sure that the scrollbar is sane.
1439 owner_->updateLayoutChoice();
1444 // Callback for cursor timer
1445 void BufferView::CursorToggleCB(FL_OBJECT * ob, long)
1447 BufferView * view = static_cast<BufferView*>(ob->u_vdata);
1449 // Quite a nice place for asyncron Inset updating, isn't it?
1450 // Actually no! This is run even if no buffer exist... so (Lgb)
1451 if (view && !view->buffer_) {
1452 goto set_timer_and_return;
1456 // On my quest to solve the gs render hangups I am now
1457 // disabling the SIGHUP completely, and will do a wait
1458 // now and then instead. If the guess that xforms somehow
1459 // destroys something is true, this is likely (hopefully)
1460 // to solve the problem...at least I hope so. Lgb
1462 // ...Ok this seems to work...at least it does not make things
1463 // worse so far. However I still see gs processes that hangs.
1464 // I would really like to know _why_ they are hanging. Anyway
1465 // the solution without the SIGCHLD handler seems to be easier
1468 // When attaching gdb to a a running gs that hangs it shows
1469 // that it is waiting for input(?) Is it possible for us to
1470 // provide that input somehow? Or figure what it is expecing
1473 // One solution is to, after some time, look if there are some
1474 // old gs processes still running and if there are: kill them
1477 // Another solution is to provide the user an option to rerender
1478 // a picture. This would, for the picture in question, check if
1479 // there is a gs running for it, if so kill it, and start a new
1480 // rendering process.
1482 // these comments posted to lyx@via
1485 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1486 if (pid == -1) // error find out what is wrong
1487 ; // ignore it for now.
1489 sigchldhandler(pid, &status);
1491 if (InsetUpdateList)
1492 UpdateInsetUpdateList();
1494 if (view && !view->screen){
1495 goto set_timer_and_return;
1498 if (view->lyx_focus && view->work_area_focus) {
1499 if (!view->the_locking_inset) {
1500 view->screen->CursorToggle();
1502 view->the_locking_inset->
1503 ToggleInsetCursor();
1505 goto set_timer_and_return;
1507 // Make sure that the cursor is visible.
1508 if (!view->the_locking_inset) {
1509 view->screen->ShowCursor();
1511 if (!view->the_locking_inset->isCursorVisible())
1512 view->the_locking_inset->
1513 ToggleInsetCursor();
1515 // This is only run when work_area_focus or lyx_focus is false.
1518 XGetInputFocus(fl_display, &tmpwin, &tmp);
1519 if (lyxerr.debugging(Debug::INFO)) {
1520 lyxerr << "tmpwin: " << tmpwin
1521 << "\nwindow: " << view->owner_->getForm()->window
1522 << "\nwork_area_focus: " << view->work_area_focus
1523 << "\nlyx_focus : " << view->lyx_focus
1526 if (tmpwin != view->owner_->getForm()->window) {
1527 view->lyx_focus = false;
1530 view->lyx_focus = true;
1531 if (!view->work_area_focus)
1534 goto set_timer_and_return;
1538 set_timer_and_return:
1539 fl_set_timer(ob, 0.4);
1545 int BufferView::WorkAreaSelectionNotify(FL_OBJECT *, Window win,
1546 int /*w*/, int /*h*/,
1547 XEvent * event, void */*d*/)
1549 if (buffer_ == 0) return 0;
1550 if (event->type != SelectionNotify)
1556 unsigned char * uc = 0;
1558 screen->HideCursor();
1560 if (event->xselection.type == XA_STRING
1561 && event->xselection.property) {
1563 if (XGetWindowProperty(
1564 fl_display /* display */,
1566 event->xselection.property /* property */,
1567 0 /* long_offset */,
1568 0 /* long_length */,
1570 XA_STRING /* req_type */,
1571 &tmpatom /* actual_type_return */,
1572 &tmpint /* actual_format_return */,
1573 &ul1 /* nitems_return */,
1574 &ul2 /* bytes_after_return */,
1575 &uc /* prop_return */
1586 if (XGetWindowProperty(
1587 fl_display /* display */,
1589 event->xselection.property /* property */,
1590 0 /* long_offset */,
1591 ul2/4+1 /* long_length */,
1593 XA_STRING /* req_type */,
1594 &tmpatom /* actual_type_return */,
1595 &tmpint /* actual_format_return */,
1596 &ul1 /* nitems_return */,
1597 &ul2 /* bytes_after_return */,
1598 &uc /* prop_return */
1606 text->InsertStringA(reinterpret_cast<char*>(uc));
1608 text->InsertStringB(reinterpret_cast<char*>(uc));
1620 void BufferView::cursorPrevious()
1622 if (!text->cursor.row->previous) return;
1624 long y = getScreen()->first;
1625 Row * cursorrow = text->cursor.row;
1626 text->SetCursorFromCoordinates(text->cursor.x_fix, y);
1628 // This is to allow jumping over large insets
1629 if ((cursorrow == text->cursor.row))
1632 if (text->cursor.row->height < work_area->h)
1633 getScreen()->Draw(text->cursor.y
1634 - text->cursor.row->baseline
1635 + text->cursor.row->height
1636 - work_area->h +1 );
1640 void BufferView::cursorNext()
1642 if (!text->cursor.row->next) return;
1644 long y = getScreen()->first;
1645 text->GetRowNearY(y);
1646 Row * cursorrow = text->cursor.row;
1647 text->SetCursorFromCoordinates(text->cursor.x_fix, y + work_area->h);
1649 // This is to allow jumping over large insets
1650 if ((cursorrow == text->cursor.row))
1653 if (text->cursor.row->height < work_area->h)
1654 getScreen()->Draw(text->cursor.y
1655 - text->cursor.row->baseline);
1659 bool BufferView::available() const
1661 if (buffer_ && text) return true;
1666 void BufferView::beforeChange()
1668 getScreen()->ToggleSelection();
1669 text->ClearSelection();
1674 void BufferView::savePosition()
1676 backstack.push(buffer()->fileName(),
1682 void BufferView::restorePosition()
1684 if (backstack.empty()) return;
1687 string fname = backstack.pop(&x, &y);
1690 Buffer * b = bufferlist.exists(fname) ?
1691 bufferlist.getBuffer(fname) :
1692 bufferlist.loadLyXFile(fname); // don't ask, just load it
1694 text->SetCursorFromCoordinates(x, y);
1699 void BufferView::update(signed char f)
1701 owner()->updateLayoutChoice();
1703 if (!text->selection && f > -3)
1704 text->sel_cursor = text->cursor;
1707 text->FullRebreak();
1711 if (f != 3 && f != -3) {
1716 if (f == 1 || f == -1) {
1717 if (buffer()->isLyxClean()) {
1718 buffer()->markDirty();
1719 owner()->getMiniBuffer()->setTimer(4);
1721 buffer()->markDirty();
1727 void BufferView::smallUpdate(signed char f)
1729 getScreen()->SmallUpdate();
1730 if (getScreen()->TopCursorVisible()
1731 != getScreen()->first) {
1739 if (!text->selection)
1740 text->sel_cursor = text->cursor;
1742 if (f == 1 || f == -1) {
1743 if (buffer()->isLyxClean()) {
1744 buffer()->markDirty();
1745 owner()->getMiniBuffer()->setTimer(4);
1747 buffer()->markDirty();
1753 void BufferView::insetSleep()
1755 if (the_locking_inset && !inset_slept) {
1756 the_locking_inset->GetCursorPos(slx, sly);
1757 the_locking_inset->InsetUnlock();
1763 void BufferView::insetWakeup()
1765 if (the_locking_inset && inset_slept) {
1766 the_locking_inset->Edit(slx, sly);
1767 inset_slept = false;
1772 void BufferView::insetUnlock()
1774 if (the_locking_inset) {
1775 if (!inset_slept) the_locking_inset->InsetUnlock();
1776 the_locking_inset = 0;
1778 inset_slept = false;