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 if (lyxerr.debugging())
216 showTextCache("buffer");
221 // Set current buffer
224 if (bufferlist.getState() == BufferList::CLOSING) return;
227 // screen is always deleted when the buffer is changed.
228 if (screen) // DEL LINE
232 // If we are closing the buffer, use the first buffer as current
234 buffer_ = bufferlist.first();
238 lyxerr[Debug::INFO] << " Buffer addr: " << buffer_ << endl;
239 buffer_->addUser(this);
240 owner_->getMenus()->showMenus();
241 // If we don't have a text object for this, we make one
243 resizeCurrentBuffer();
248 screen->first = screen->TopCursorVisible();
250 updateAllVisibleBufferRelatedPopups();
253 lyxerr[Debug::INFO] << " No Buffer!" << endl;
254 owner_->getMenus()->hideMenus();
256 fl_redraw_object(work_area);
257 // Also remove all remaining text's from the testcache.
258 if (lyxerr.debugging())
259 showTextCache("buffer delete all");
260 while (!textcache.empty()) {
261 LyXText * tt = textcache.front();
262 textcache.erase(textcache.begin());
266 // should update layoutchoice even if we don't have a buffer.
267 owner_->updateLayoutChoice();
268 owner_->getMiniBuffer()->Init();
269 owner_->updateWindowTitle();
273 void BufferView::updateScreen()
275 // Regenerate the screen.
276 if (screen) // DEL LINE
278 screen = new LyXScreen(FL_ObjWin(work_area),
287 void BufferView::resize()
289 // This will resize the buffer. (Asger)
291 resizeCurrentBuffer();
295 static bool lgb_hack = false;
297 void BufferView::redraw()
299 lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
301 fl_redraw_object(work_area);
302 fl_redraw_object(scrollbar);
303 fl_redraw_object(button_down);
304 fl_redraw_object(button_up);
309 void BufferView::fitCursor()
311 if (screen) screen->FitCursor();
315 void BufferView::update()
317 if (screen) screen->Update();
321 void BufferView::updateScrollbar()
323 /* If the text is smaller than the working area, the scrollbar
324 * maximum must be the working area height. No scrolling will
328 fl_set_slider_value(scrollbar, 0);
329 fl_set_slider_size(scrollbar, scrollbar->h);
333 static long max2 = 0;
334 static long height2 = 0;
342 cbsf = screen->first;
344 // check if anything has changed.
346 height2 == work_area->h &&
347 current_scrollbar_value == cbsf)
351 height2 = work_area->h;
352 current_scrollbar_value = cbsf;
354 if (cbth <= height2) { // text is smaller than screen
355 fl_set_slider_size(scrollbar, scrollbar->h);
359 long maximum_height = work_area->h * 3 / 4 + cbth;
363 double hfloat = work_area->h;
364 double maxfloat = maximum_height;
366 fl_set_slider_value(scrollbar, value);
367 fl_set_slider_bounds(scrollbar, 0,
368 maximum_height - work_area->h);
370 double lineh = text->DefaultHeight();
371 fl_set_slider_increment(scrollbar, work_area->h-lineh, lineh);
374 if ((hfloat / maxfloat) * float(height2) < 3)
375 fl_set_slider_size(scrollbar,
378 fl_set_slider_size(scrollbar,
381 fl_set_slider_size(scrollbar, hfloat);
382 fl_set_slider_precision(scrollbar, 0);
386 void BufferView::redoCurrentBuffer()
388 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
389 if (buffer_ && text) {
391 owner_->updateLayoutChoice();
396 int BufferView::resizeCurrentBuffer()
398 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
400 LyXParagraph * par = 0;
401 LyXParagraph * selstartpar = 0;
402 LyXParagraph * selendpar = 0;
411 owner_->getMiniBuffer()->Set(_("Formatting document..."));
414 par = text->cursor.par;
415 pos = text->cursor.pos;
416 selstartpar = text->sel_start_cursor.par;
417 selstartpos = text->sel_start_cursor.pos;
418 selendpar = text->sel_end_cursor.par;
419 selendpos = text->sel_end_cursor.pos;
420 selection = text->selection;
421 mark_set = text->mark_set;
423 text = new LyXText(work_area->w, buffer_);
425 // See if we have a text in TextCache that fits
426 // the new buffer_ with the correct width.
427 TextCache::iterator it =
428 find_if(textcache.begin(),
432 if (it != textcache.end()) {
434 // take it out of textcache.
436 if (lyxerr.debugging())
437 showTextCache("resizeCurrentBuffer");
439 text = new LyXText(work_area->w, buffer_);
445 text->selection = true;
446 /* at this point just to avoid the Delete-Empty-Paragraph
447 * Mechanism when setting the cursor */
448 text->mark_set = mark_set;
450 text->SetCursor(selstartpar, selstartpos);
451 text->sel_cursor = text->cursor;
452 text->SetCursor(selendpar, selendpos);
453 text->SetSelection();
454 text->SetCursor(par, pos);
456 text->SetCursor(par, pos);
457 text->sel_cursor = text->cursor;
458 text->selection = false;
461 screen->first = screen->TopCursorVisible(); /* this will scroll the
462 * screen such that the
467 owner_->getMiniBuffer()->Init();
470 // Now if the title form still exist kill it
477 void BufferView::gotoError()
482 screen->HideCursor();
487 if (!text->GotoNextError()) {
489 || text->cursor.par != text->FirstParagraph()) {
491 text->cursor.par = text->FirstParagraph();
492 text->cursor.pos = 0;
493 if (!text->GotoNextError()) {
495 owner_->getMiniBuffer()
496 ->Set(_("No more errors"));
500 owner_->getMiniBuffer()->Set(_("No more errors"));
505 text->sel_cursor = text->cursor;
510 // Just a bunch of C wrappers around static members of BufferView
511 void C_BufferView_UpCB(FL_OBJECT * ob, long buf)
513 BufferView::UpCB(ob, buf);
517 void C_BufferView_DownCB(FL_OBJECT * ob, long buf)
519 BufferView::DownCB(ob, buf);
523 void C_BufferView_ScrollCB(FL_OBJECT * ob, long buf)
525 BufferView::ScrollCB(ob, buf);
529 void C_BufferView_CursorToggleCB(FL_OBJECT * ob, long buf)
531 BufferView::CursorToggleCB(ob, buf);
535 int C_BufferView_work_area_handler(FL_OBJECT * ob, int event,
539 return BufferView::work_area_handler(ob, event,
545 void BufferView::create_view(int xpos, int ypos, int width, int height)
548 int const bw = abs(fl_get_border_width());
550 // a hack for the figinsets (Matthias)
551 // This one first, then it will probably be invisible. (Lgb)
552 ::figinset_canvas = figinset_canvas = obj =
553 fl_add_canvas(FL_NORMAL_CANVAS,
556 fl_set_object_boxtype(obj, FL_NO_BOX);
557 fl_set_object_resize(obj, FL_RESIZE_ALL);
558 fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
561 obj = fl_add_box(FL_BORDER_BOX, xpos, ypos,
564 fl_set_object_resize(obj, FL_RESIZE_ALL);
565 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
568 work_area = obj = fl_add_free(FL_INPUT_FREE,
569 xpos + bw, ypos + bw,
570 width - 15 - 2 * bw /* scrollbarwidth */,
572 C_BufferView_work_area_handler);
573 obj->wantkey = FL_KEY_TAB;
574 obj->u_vdata = this; /* This is how we pass the BufferView
575 to the work_area_handler. */
576 fl_set_object_boxtype(obj, FL_DOWN_BOX);
577 fl_set_object_resize(obj, FL_RESIZE_ALL);
578 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
584 // up - scrollbar button
586 fl_set_border_width(-1);
588 fl_set_border_width(-2); // to get visible feedback
590 button_up = obj = fl_add_pixmapbutton(FL_TOUCH_BUTTON,
594 fl_set_object_boxtype(obj, FL_UP_BOX);
595 fl_set_object_color(obj, FL_MCOL, FL_BLUE);
596 fl_set_object_resize(obj, FL_RESIZE_ALL);
597 fl_set_object_gravity(obj, NorthEastGravity, NorthEastGravity);
598 fl_set_object_callback(obj, C_BufferView_UpCB, 0);
600 fl_set_pixmapbutton_data(obj, const_cast<char**>(up_xpm));
603 // Remove the blue feedback rectangle
604 fl_set_pixmapbutton_focus_outline(obj, 0);
607 // the scrollbar slider
608 fl_set_border_width(-bw);
609 scrollbar = obj = fl_add_slider(FL_VERT_SLIDER,
612 15, height - 30, "");
613 fl_set_object_color(obj, FL_COL1, FL_MCOL);
614 fl_set_object_boxtype(obj, FL_UP_BOX);
615 fl_set_object_resize(obj, FL_RESIZE_ALL);
616 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
617 fl_set_object_callback(obj, C_BufferView_ScrollCB, 0);
620 // down - scrollbar button
622 fl_set_border_width(-1);
624 fl_set_border_width(-2); // to get visible feedback
626 button_down = obj = fl_add_pixmapbutton(FL_TOUCH_BUTTON,
630 fl_set_object_boxtype(obj, FL_UP_BOX);
631 fl_set_object_color(obj, FL_MCOL, FL_BLUE);
632 fl_set_object_resize(obj, FL_RESIZE_ALL);
633 fl_set_object_gravity(obj, SouthEastGravity, SouthEastGravity);
634 fl_set_object_callback(obj, C_BufferView_DownCB, 0);
636 fl_set_pixmapbutton_data(obj, const_cast<char**>(down_xpm));
637 fl_set_border_width(-bw);
640 // Remove the blue feedback rectangle
641 fl_set_pixmapbutton_focus_outline(obj, 0);
649 timer_cursor = obj = fl_add_timer(FL_HIDDEN_TIMER,
650 0, 0, 0, 0, "Timer");
651 fl_set_object_callback(obj, C_BufferView_CursorToggleCB, 0);
656 // Callback for scrollbar up button
657 void BufferView::UpCB(FL_OBJECT * ob, long)
659 BufferView * view = static_cast<BufferView*>(ob->u_vdata);
661 if (view->buffer_ == 0) return;
663 static long time = 0;
664 XEvent const * ev2 = fl_last_event();
665 if (ev2->type == ButtonPress || ev2->type == ButtonRelease)
667 int button = fl_get_button_numb(ob);
670 view->ScrollUpOnePage(time++); break;
672 view->ScrollDownOnePage(time++); break;
674 view->ScrollUp(time++); break;
686 a = XInternAtom(fl_display, "WAIT_FOR_X", False);
689 XSetWindowAttributes attr;
690 mask = CWOverrideRedirect;
691 attr.override_redirect = 1;
692 w = XCreateWindow(fl_display, fl_root,
693 0, 0, 1, 1, 0, CopyFromParent,
694 InputOnly, CopyFromParent, mask, &attr);
695 XSelectInput(fl_display, w, PropertyChangeMask);
696 XMapWindow(fl_display, w);
699 XChangeProperty(fl_display, w, a, a, 8,
701 reinterpret_cast<unsigned char*>(""), 0);
702 XWindowEvent(fl_display, w, PropertyChangeMask, &ev);
704 XSync(fl_get_display(), 0);
708 // Callback for scrollbar slider
709 void BufferView::ScrollCB(FL_OBJECT * ob, long)
711 BufferView * view = static_cast<BufferView*>(ob->u_vdata);
712 extern bool cursor_follows_scrollbar;
714 if (view->buffer_ == 0) return;
716 view->current_scrollbar_value = long(fl_get_slider_value(ob));
717 if (view->current_scrollbar_value < 0)
718 view->current_scrollbar_value = 0;
723 view->screen->Draw(view->current_scrollbar_value);
725 if (cursor_follows_scrollbar) {
726 LyXText * vbt = view->text;
727 int height = vbt->DefaultHeight();
729 if (vbt->cursor.y < view->screen->first + height) {
730 vbt->SetCursorFromCoordinates(0,
731 view->screen->first +
734 else if (vbt->cursor.y >
735 view->screen->first + view->work_area->h - height) {
736 vbt->SetCursorFromCoordinates(0,
737 view->screen->first +
746 // Callback for scrollbar down button
747 void BufferView::DownCB(FL_OBJECT * ob, long)
749 BufferView * view = static_cast<BufferView*>(ob->u_vdata);
751 if (view->buffer_ == 0) return;
754 static long time = 0;
755 ev2 = fl_last_event();
756 if (ev2->type == ButtonPress || ev2->type == ButtonRelease)
758 int button = fl_get_button_numb(ob);
761 view->ScrollUpOnePage(time++); break;
763 view->ScrollDownOnePage(time++); break;
765 view->ScrollDown(time++); break;
770 int BufferView::ScrollUp(long time)
772 if (buffer_ == 0) return 0;
773 if (!screen) return 0;
775 double value = fl_get_slider_value(scrollbar);
777 if (value == 0) return 0;
779 float add_value = (text->DefaultHeight()
780 + float(time) * float(time) * 0.125);
782 if (add_value > work_area->h)
783 add_value = float(work_area->h -
784 text->DefaultHeight());
791 fl_set_slider_value(scrollbar, value);
793 ScrollCB(scrollbar, 0);
798 int BufferView::ScrollDown(long time)
800 if (buffer_ == 0) return 0;
801 if (!screen) return 0;
803 double value= fl_get_slider_value(scrollbar);
805 fl_get_slider_bounds(scrollbar, &min, &max);
807 if (value == max) return 0;
809 float add_value = (text->DefaultHeight()
810 + float(time) * float(time) * 0.125);
812 if (add_value > work_area->h)
813 add_value = float(work_area->h -
814 text->DefaultHeight());
821 fl_set_slider_value(scrollbar, value);
823 ScrollCB(scrollbar, 0);
828 void BufferView::ScrollUpOnePage(long /*time*/)
830 if (buffer_ == 0) return;
833 long y = screen->first;
837 Row * row = text->GetRowNearY(y);
839 y = y - work_area->h + row->height;
841 fl_set_slider_value(scrollbar, y);
843 ScrollCB(scrollbar, 0);
847 void BufferView::ScrollDownOnePage(long /*time*/)
849 if (buffer_ == 0) return;
853 fl_get_slider_bounds(scrollbar, &min, &max);
854 long y = screen->first;
856 if (y > text->height - work_area->h)
860 text->GetRowNearY(y);
862 fl_set_slider_value(scrollbar, y);
864 ScrollCB(scrollbar, 0);
868 int BufferView::work_area_handler(FL_OBJECT * ob, int event,
870 int /*key*/, void * xev)
872 static int x_old = -1;
873 static int y_old = -1;
874 static long scrollbar_value_old = -1;
876 XEvent * ev = static_cast<XEvent*>(xev);
877 BufferView * view = static_cast<BufferView*>(ob->u_vdata);
879 // If we don't have a view yet; return
880 if (!view || quitting) return 0;
884 view->workAreaExpose();
887 view->WorkAreaButtonPress(ob, 0, 0, 0, ev, 0);
890 view->WorkAreaButtonRelease(ob, 0, 0, 0, ev, 0);
893 if (ev->xmotion.x != x_old ||
894 ev->xmotion.y != y_old ||
895 view->current_scrollbar_value != scrollbar_value_old) {
896 x_old = ev->xmotion.x;
897 y_old = ev->xmotion.y;
898 scrollbar_value_old = view->current_scrollbar_value;
899 view->WorkAreaMotionNotify(ob, 0, 0, 0, ev, 0);
902 // Done by the raw callback:
904 // WorkAreaKeyPress(ob, 0, 0, 0, ev, 0); break;
906 if (!view->owner_->getMiniBuffer()->shows_no_match)
907 view->owner_->getMiniBuffer()->Init();
908 view->owner_->getMiniBuffer()->shows_no_match = false;
909 view->work_area_focus = true;
910 fl_set_timer(view->timer_cursor, 0.4);
913 view->owner_->getMiniBuffer()->ExecCommand();
914 view->work_area_focus = false;
917 SetXtermCursor(view->owner_->getForm()->window);
919 view->lyx_focus = true;
920 fl_set_timer(view->timer_cursor, 0.4);
923 if (!input_prohibited)
924 XUndefineCursor(fl_display,
925 view->owner_->getForm()->window);
926 view->lyx_focus = false; // This is not an absolute truth
927 // but if it is not true, it will be changed within a blink
928 // of an eye. ... Not good enough... use regulare timeperiod
929 //fl_set_timer(view->timer_cursor, 0.01); // 0.1 sec blink
930 fl_set_timer(view->timer_cursor, 0.4); // 0.4 sec blink
934 if (!view->the_locking_inset) {
935 if (view->screen && ev->xbutton.button == 1) {
936 view->screen->HideCursor();
937 view->screen->ToggleSelection();
938 view->text->SelectWord();
939 view->screen->ToggleSelection(false);
940 /* This will fit the cursor on the screen
948 if (view->buffer_ && view->screen && ev->xbutton.button == 1) {
949 view->screen->HideCursor();
950 view->screen->ToggleSelection();
951 view->text->CursorHome();
952 view->text->sel_cursor = view->text->cursor;
953 view->text->CursorEnd();
954 view->text->SetSelection();
955 view->screen->ToggleSelection(false);
956 /* This will fit the cursor on the screen
962 view->WorkAreaSelectionNotify(ob,
963 view->owner_->getForm()->window,
970 int BufferView::WorkAreaMotionNotify(FL_OBJECT * ob, Window,
971 int /*w*/, int /*h*/,
972 XEvent * ev, void * /*d*/)
974 if (buffer_ == 0) return 0;
975 if (!screen) return 0;
977 // Check for inset locking
978 if (the_locking_inset) {
979 LyXCursor cursor = text->cursor;
981 InsetMotionNotify(ev->xbutton.x - ob->x - cursor.x,
982 ev->xbutton.y - ob->y -
988 // Only use motion with button 1
989 if (!ev->xmotion.state & Button1MotionMask)
992 /* The selection possible is needed, that only motion events are
993 * used, where the bottom press event was on the drawing area too */
994 if (selection_possible) {
995 screen->HideCursor();
997 text->SetCursorFromCoordinates(ev->xbutton.x - ob->x,
998 ev->xbutton.y - ob->y +
1001 if (!text->selection)
1002 update(-3); // Maybe an empty line was deleted
1004 text->SetSelection();
1005 screen->ToggleToggle();
1006 if (screen->FitCursor())
1008 screen->ShowCursor();
1014 extern int bibitemMaxWidth(LyXFont const &);
1016 // Single-click on work area
1017 int BufferView::WorkAreaButtonPress(FL_OBJECT * ob, Window,
1018 int /*w*/, int /*h*/,
1019 XEvent * ev, void */*d*/)
1024 if (buffer_ == 0) return 0;
1025 if (!screen) return 0;
1027 int const x = ev->xbutton.x - ob->x;
1028 int const y = ev->xbutton.y - ob->y;
1029 // If we hit an inset, we have the inset coordinates in these
1030 // and inset_hit points to the inset. If we do not hit an
1031 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1034 Inset * inset_hit = checkInsetHit(inset_x, inset_y);
1036 // ok ok, this is a hack.
1037 int button = ev->xbutton.button;
1038 if (button == 4 || button == 5) goto wheel;
1041 if (the_locking_inset) {
1042 // We are in inset locking mode
1044 /* Check whether the inset was hit. If not reset mode,
1045 otherwise give the event to the inset */
1046 if (inset_hit != 0) {
1048 InsetButtonPress(inset_x, inset_y,
1052 unlockInset(the_locking_inset);
1056 selection_possible = true;
1057 screen->HideCursor();
1059 // Right button mouse click on a table
1061 (text->cursor.par->table ||
1062 text->MouseHitInTable(x, y + screen->first))) {
1063 // Set the cursor to the press-position
1064 text->SetCursorFromCoordinates(x, y + screen->first);
1067 // Only show the table popup if the hit is in
1069 if (!text->HitInTable(text->cursor.row, x))
1072 // Hit above or below the table?
1074 if (!text->selection) {
1075 screen->ToggleSelection();
1076 text->ClearSelection();
1077 text->FullRebreak();
1081 // Popup table popup when on a table.
1082 // This is obviously temporary, since we
1083 // should be able to popup various
1084 // context-sensitive-menus with the
1085 // the right mouse. So this should be done more
1086 // general in the future. Matthias.
1087 selection_possible = false;
1088 owner_->getLyXFunc()
1089 ->Dispatch(LFUN_LAYOUT_TABLE,
1095 int screen_first = screen->first;
1097 // Middle button press pastes if we have a selection
1098 bool paste_internally = false;
1099 if (button == 2 // && !buffer_->the_locking_inset
1100 && text->selection) {
1101 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
1102 paste_internally = true;
1105 // Clear the selection
1106 screen->ToggleSelection();
1107 text->ClearSelection();
1108 text->FullRebreak();
1112 // Single left click in math inset?
1113 if (inset_hit != 0 && inset_hit->Editable() == 2) {
1114 // Highly editable inset, like math
1115 selection_possible = false;
1116 owner_->updateLayoutChoice();
1117 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
1118 inset_hit->Edit(inset_x, inset_y);
1122 // Right click on a footnote flag opens float menu
1124 selection_possible = false;
1128 text->SetCursorFromCoordinates(x, y + screen_first);
1130 text->sel_cursor = text->cursor;
1131 text->cursor.x_fix = text->cursor.x;
1133 owner_->updateLayoutChoice();
1134 if (screen->FitCursor()){
1136 selection_possible = false;
1139 // Insert primary selection with middle mouse
1140 // if there is a local selection in the current buffer,
1142 if (button == 2) { // && !buffer_->the_locking_inset){
1143 if (paste_internally)
1144 owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
1146 owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
1148 selection_possible = false;
1155 // I am not quite sure if this is the correct place to put
1156 // this, but it will not cause any harm.
1157 // Patch from Mark Huang (markman@mit.edu) to make LyX
1158 // recognise button 4 and 5. This enables LyX use use
1159 // the scrollwhell on certain mice for something useful. (Lgb)
1160 // Added wheel acceleration detection code. (Rvdk)
1161 static Time lastTime = 0;
1162 int diff = ev->xbutton.time - lastTime;
1163 int scroll = int(1.0 + (4.0 / (abs(diff) + 1.0)) * 200.0);
1172 lastTime = ev->xbutton.time;
1183 int BufferView::WorkAreaButtonRelease(FL_OBJECT * ob, Window ,
1184 int /*w*/, int /*h*/,
1185 XEvent * ev, void * /*d*/)
1187 if (buffer_ == 0 || screen == 0) return 0;
1189 int const x = ev->xbutton.x - ob->x;
1190 int const y = ev->xbutton.y - ob->y;
1192 // If we hit an inset, we have the inset coordinates in these
1193 // and inset_hit points to the inset. If we do not hit an
1194 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1197 Inset * inset_hit = checkInsetHit(inset_x, inset_y);
1199 if (the_locking_inset) {
1200 // We are in inset locking mode.
1202 /* LyX does a kind of work-area grabbing for insets.
1203 Only a ButtonPress Event outside the inset will
1204 force a InsetUnlock. */
1206 InsetButtonRelease(inset_x, inset_y,
1207 ev->xbutton.button);
1211 selection_possible = false;
1212 if (text->cursor.par->table) {
1214 NumberOfCell(text->cursor.par,
1216 if (text->cursor.par->table->IsContRow(cell) &&
1217 text->cursor.par->table->
1218 CellHasContRow(text->cursor.par->table->
1219 GetCellAbove(cell))<0) {
1224 if (ev->xbutton.button >= 2)
1227 // Make sure that the press was not far from the release
1228 if ((abs(last_click_x - x) >= 5) ||
1229 (abs(last_click_y - y) >= 5)) {
1233 // Did we hit an editable inset?
1234 if (inset_hit != 0) {
1235 // Inset like error, notes and figures
1236 selection_possible = false;
1237 #ifdef WITH_WARNINGS
1238 #warning fix this proper in 0.13
1240 // Following a ref shouldn't issue
1241 // a push on the undo-stack
1242 // anylonger, now that we have
1243 // keybindings for following
1244 // references and returning from
1245 // references. IMHO though, it
1246 // should be the inset's own business
1247 // to push or not push on the undo
1248 // stack. They don't *have* to
1249 // alter the document...
1251 // ...or maybe the SetCursorParUndo()
1252 // below isn't necessary at all anylonger?
1253 if (inset_hit->LyxCode() == Inset::REF_CODE) {
1254 text->SetCursorParUndo();
1257 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
1258 inset_hit->Edit(inset_x, inset_y);
1262 // check whether we want to open a float
1266 if (text->cursor.pos <
1267 text->cursor.par->Last()) {
1268 c = text->cursor.par->
1269 GetChar(text->cursor.pos);
1271 if (c == LyXParagraph::META_FOOTNOTE
1272 || c == LyXParagraph::META_MARGIN
1273 || c == LyXParagraph::META_FIG
1274 || c == LyXParagraph::META_TAB
1275 || c == LyXParagraph::META_WIDE_FIG
1276 || c == LyXParagraph::META_WIDE_TAB
1277 || c == LyXParagraph::META_ALGORITHM){
1279 } else if (text->cursor.pos - 1 >= 0) {
1280 c = text->cursor.par->
1281 GetChar(text->cursor.pos - 1);
1282 if (c == LyXParagraph::META_FOOTNOTE
1283 || c == LyXParagraph::META_MARGIN
1284 || c == LyXParagraph::META_FIG
1285 || c == LyXParagraph::META_TAB
1286 || c == LyXParagraph::META_WIDE_FIG
1287 || c == LyXParagraph::META_WIDE_TAB
1288 || c == LyXParagraph::META_ALGORITHM){
1289 // We are one step too far to the right
1296 selection_possible = false;
1301 // Do we want to close a float? (click on the float-label)
1302 if (text->cursor.row->par->footnoteflag ==
1303 LyXParagraph::OPEN_FOOTNOTE
1304 && text->cursor.pos == 0
1305 && text->cursor.row->previous &&
1306 text->cursor.row->previous->par->
1307 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
1308 LyXFont font (LyXFont::ALL_SANE);
1309 font.setSize(LyXFont::SIZE_SMALL);
1311 int box_x = 20; // LYX_PAPER_MARGIN;
1312 box_x += font.textWidth("Mwide-figM", 10);
1314 int screen_first = screen->first;
1317 && y + screen_first > text->cursor.y -
1318 text->cursor.row->baseline
1319 && y + screen_first < text->cursor.y -
1320 text->cursor.row->baseline
1321 + font.maxAscent() * 1.2 + font.maxDescent() * 1.2) {
1323 selection_possible = false;
1328 // Maybe we want to edit a bibitem ale970302
1329 if (text->cursor.par->bibkey && x < 20 +
1330 bibitemMaxWidth(textclasslist
1331 .TextClass(buffer_->
1332 params.textclass).defaultfont())) {
1333 text->cursor.par->bibkey->Edit(0, 0);
1341 * Returns an inset if inset was hit. 0 otherwise.
1342 * If hit, the coordinates are changed relative to the inset.
1343 * Otherwise coordinates are not changed, and false is returned.
1345 Inset * BufferView::checkInsetHit(int & x, int & y)
1350 int y_tmp = y + getScreen()->first;
1352 LyXCursor cursor = text->cursor;
1353 if (cursor.pos < cursor.par->Last()
1354 && cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET
1355 && cursor.par->GetInset(cursor.pos)
1356 && cursor.par->GetInset(cursor.pos)->Editable()) {
1358 // Check whether the inset really was hit
1359 Inset * tmpinset = cursor.par->GetInset(cursor.pos);
1360 LyXFont font = text->GetFont(cursor.par, cursor.pos);
1362 && x < cursor.x + tmpinset->Width(font)
1363 && y_tmp > cursor.y - tmpinset->Ascent(font)
1364 && y_tmp < cursor.y + tmpinset->Descent(font)) {
1366 // The origin of an inset is on the baseline
1367 y = y_tmp - (cursor.y);
1370 } else if (cursor.pos - 1 >= 0
1371 && cursor.par->GetChar(cursor.pos - 1) == LyXParagraph::META_INSET
1372 && cursor.par->GetInset(cursor.pos - 1)
1373 && cursor.par->GetInset(cursor.pos - 1)->Editable()) {
1375 Inset * result = checkInsetHit(x, y);
1377 text->CursorRight();
1387 int BufferView::workAreaExpose()
1389 if (!work_area || !work_area->form->visible)
1392 // this is a hack to ensure that we only call this through
1393 // BufferView::redraw().
1398 static int work_area_width = work_area->w;
1399 static int work_area_height = work_area->h;
1401 bool widthChange = work_area->w != work_area_width;
1402 bool heightChange = work_area->h != work_area_height;
1404 // update from work area
1405 work_area_width = work_area->w;
1406 work_area_height = work_area->h;
1409 // All buffers need a resize
1410 bufferlist.resize();
1412 // Remove all texts from the textcache
1413 if (lyxerr.debugging())
1414 showTextCache("Expose delete all");
1415 while(!textcache.empty()) {
1416 LyXText * tt = textcache.front();
1417 textcache.erase(textcache.begin());
1420 } else if (heightChange) {
1421 // Rebuild image of current screen
1423 // fitCursor() ensures we don't jump back
1424 // to the start of the document on vertical
1428 // The main window size has changed, repaint most stuff
1430 // ...including the minibuffer
1431 owner_->getMiniBuffer()->Init();
1433 } else if (screen) screen->Redraw();
1435 // Grey box when we don't have a buffer
1436 fl_winset(FL_ObjWin(work_area));
1437 fl_rectangle(1, work_area->x, work_area->y,
1438 work_area->w, work_area->h, FL_GRAY63);
1441 // always make sure that the scrollbar is sane.
1443 owner_->updateLayoutChoice();
1448 // Callback for cursor timer
1449 void BufferView::CursorToggleCB(FL_OBJECT * ob, long)
1451 BufferView * view = static_cast<BufferView*>(ob->u_vdata);
1453 // Quite a nice place for asyncron Inset updating, isn't it?
1454 // Actually no! This is run even if no buffer exist... so (Lgb)
1455 if (view && !view->buffer_) {
1456 goto set_timer_and_return;
1460 // On my quest to solve the gs render hangups I am now
1461 // disabling the SIGHUP completely, and will do a wait
1462 // now and then instead. If the guess that xforms somehow
1463 // destroys something is true, this is likely (hopefully)
1464 // to solve the problem...at least I hope so. Lgb
1466 // ...Ok this seems to work...at least it does not make things
1467 // worse so far. However I still see gs processes that hangs.
1468 // I would really like to know _why_ they are hanging. Anyway
1469 // the solution without the SIGCHLD handler seems to be easier
1472 // When attaching gdb to a a running gs that hangs it shows
1473 // that it is waiting for input(?) Is it possible for us to
1474 // provide that input somehow? Or figure what it is expecing
1477 // One solution is to, after some time, look if there are some
1478 // old gs processes still running and if there are: kill them
1481 // Another solution is to provide the user an option to rerender
1482 // a picture. This would, for the picture in question, check if
1483 // there is a gs running for it, if so kill it, and start a new
1484 // rendering process.
1486 // these comments posted to lyx@via
1489 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1490 if (pid == -1) // error find out what is wrong
1491 ; // ignore it for now.
1493 sigchldhandler(pid, &status);
1495 if (InsetUpdateList)
1496 UpdateInsetUpdateList();
1498 if (view && !view->screen){
1499 goto set_timer_and_return;
1502 if (view->lyx_focus && view->work_area_focus) {
1503 if (!view->the_locking_inset) {
1504 view->screen->CursorToggle();
1506 view->the_locking_inset->
1507 ToggleInsetCursor();
1509 goto set_timer_and_return;
1511 // Make sure that the cursor is visible.
1512 if (!view->the_locking_inset) {
1513 view->screen->ShowCursor();
1515 if (!view->the_locking_inset->isCursorVisible())
1516 view->the_locking_inset->
1517 ToggleInsetCursor();
1519 // This is only run when work_area_focus or lyx_focus is false.
1522 XGetInputFocus(fl_display, &tmpwin, &tmp);
1523 // Commenting this out, we have not had problems with this
1524 // for a long time. We will probably work on this code later
1525 // and we can reenable this debug code then. Now it only
1526 // anoying when debugging. (Lgb)
1527 //if (lyxerr.debugging(Debug::INFO)) {
1528 // lyxerr << "tmpwin: " << tmpwin
1529 // << "\nwindow: " << view->owner_->getForm()->window
1530 // << "\nwork_area_focus: " << view->work_area_focus
1531 // << "\nlyx_focus : " << view->lyx_focus
1534 if (tmpwin != view->owner_->getForm()->window) {
1535 view->lyx_focus = false;
1538 view->lyx_focus = true;
1539 if (!view->work_area_focus)
1542 goto set_timer_and_return;
1546 set_timer_and_return:
1547 fl_set_timer(ob, 0.4);
1553 int BufferView::WorkAreaSelectionNotify(FL_OBJECT *, Window win,
1554 int /*w*/, int /*h*/,
1555 XEvent * event, void */*d*/)
1557 if (buffer_ == 0) return 0;
1558 if (event->type != SelectionNotify)
1564 unsigned char * uc = 0;
1566 screen->HideCursor();
1568 if (event->xselection.type == XA_STRING
1569 && event->xselection.property) {
1571 if (XGetWindowProperty(
1572 fl_display /* display */,
1574 event->xselection.property /* property */,
1575 0 /* long_offset */,
1576 0 /* long_length */,
1578 XA_STRING /* req_type */,
1579 &tmpatom /* actual_type_return */,
1580 &tmpint /* actual_format_return */,
1581 &ul1 /* nitems_return */,
1582 &ul2 /* bytes_after_return */,
1583 &uc /* prop_return */
1594 if (XGetWindowProperty(
1595 fl_display /* display */,
1597 event->xselection.property /* property */,
1598 0 /* long_offset */,
1599 ul2/4+1 /* long_length */,
1601 XA_STRING /* req_type */,
1602 &tmpatom /* actual_type_return */,
1603 &tmpint /* actual_format_return */,
1604 &ul1 /* nitems_return */,
1605 &ul2 /* bytes_after_return */,
1606 &uc /* prop_return */
1614 text->InsertStringA(reinterpret_cast<char*>(uc));
1616 text->InsertStringB(reinterpret_cast<char*>(uc));
1628 void BufferView::cursorPrevious()
1630 if (!text->cursor.row->previous) return;
1632 long y = getScreen()->first;
1633 Row * cursorrow = text->cursor.row;
1634 text->SetCursorFromCoordinates(text->cursor.x_fix, y);
1636 // This is to allow jumping over large insets
1637 if ((cursorrow == text->cursor.row))
1640 if (text->cursor.row->height < work_area->h)
1641 getScreen()->Draw(text->cursor.y
1642 - text->cursor.row->baseline
1643 + text->cursor.row->height
1644 - work_area->h +1 );
1648 void BufferView::cursorNext()
1650 if (!text->cursor.row->next) return;
1652 long y = getScreen()->first;
1653 text->GetRowNearY(y);
1654 Row * cursorrow = text->cursor.row;
1655 text->SetCursorFromCoordinates(text->cursor.x_fix, y + work_area->h);
1657 // This is to allow jumping over large insets
1658 if ((cursorrow == text->cursor.row))
1661 if (text->cursor.row->height < work_area->h)
1662 getScreen()->Draw(text->cursor.y
1663 - text->cursor.row->baseline);
1667 bool BufferView::available() const
1669 if (buffer_ && text) return true;
1674 void BufferView::beforeChange()
1676 getScreen()->ToggleSelection();
1677 text->ClearSelection();
1682 void BufferView::savePosition()
1684 backstack.push(buffer()->fileName(),
1690 void BufferView::restorePosition()
1692 if (backstack.empty()) return;
1695 string fname = backstack.pop(&x, &y);
1698 Buffer * b = bufferlist.exists(fname) ?
1699 bufferlist.getBuffer(fname) :
1700 bufferlist.loadLyXFile(fname); // don't ask, just load it
1702 text->SetCursorFromCoordinates(x, y);
1707 void BufferView::update(signed char f)
1709 owner()->updateLayoutChoice();
1711 if (!text->selection && f > -3)
1712 text->sel_cursor = text->cursor;
1715 text->FullRebreak();
1719 if (f != 3 && f != -3) {
1724 if (f == 1 || f == -1) {
1725 if (buffer()->isLyxClean()) {
1726 buffer()->markDirty();
1727 owner()->getMiniBuffer()->setTimer(4);
1729 buffer()->markDirty();
1735 void BufferView::smallUpdate(signed char f)
1737 getScreen()->SmallUpdate();
1738 if (getScreen()->TopCursorVisible()
1739 != getScreen()->first) {
1747 if (!text->selection)
1748 text->sel_cursor = text->cursor;
1750 if (f == 1 || f == -1) {
1751 if (buffer()->isLyxClean()) {
1752 buffer()->markDirty();
1753 owner()->getMiniBuffer()->setTimer(4);
1755 buffer()->markDirty();
1761 void BufferView::insetSleep()
1763 if (the_locking_inset && !inset_slept) {
1764 the_locking_inset->GetCursorPos(slx, sly);
1765 the_locking_inset->InsetUnlock();
1771 void BufferView::insetWakeup()
1773 if (the_locking_inset && inset_slept) {
1774 the_locking_inset->Edit(slx, sly);
1775 inset_slept = false;
1780 void BufferView::insetUnlock()
1782 if (the_locking_inset) {
1783 if (!inset_slept) the_locking_inset->InsetUnlock();
1784 the_locking_inset = 0;
1786 inset_slept = false;