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 * ======================================================*/
20 #include "support/lstrings.h"
23 #pragma implementation
26 #include "commandtags.h"
27 #include "BufferView.h"
28 #include "bufferlist.h"
31 #include "insets/lyxinset.h"
32 #include "minibuffer.h"
33 #include "lyxscreen.h"
38 #include "lyx_gui_misc.h"
39 #include "BackStack.h"
44 extern BufferList bufferlist;
45 void sigchldhandler(pid_t pid, int *status);
47 extern void SetXtermCursor(Window win);
48 extern bool input_prohibited;
49 extern bool selection_possible;
50 extern void BeforeChange();
51 extern char ascii_type;
52 extern int UnlockInset(UpdatableInset* inset);
53 extern void ToggleFloat();
54 extern void MenuPasteSelection(char at);
55 extern InsetUpdateStruct *InsetUpdateList;
56 extern void UpdateInsetUpdateList();
58 // This is _very_ temporary
59 FL_OBJECT *figinset_canvas;
61 BufferView::BufferView(LyXView *o, int xpos, int ypos,
62 int width, int height)
74 current_scrollbar_value = 0;
75 create_view(xpos, ypos, width, height);
76 // Activate the timer for the cursor
77 fl_set_timer(timer_cursor, 0.4);
78 fl_set_focus_object(_owner->getForm(), work_area);
79 work_area_focus = true;
81 backstack = new BackStack(16);
85 BufferView::~BufferView()
91 void BufferView::setBuffer(Buffer *b)
93 lyxerr.debug("Setting buffer in BufferView");
95 _buffer->InsetSleep();
96 _buffer->delUser(this);
102 if (bufferlist.getState() == BufferList::CLOSING) return;
109 // If we are closing the buffer, use the first buffer as current
111 _buffer = bufferlist.first();
115 lyxerr.debug(string(" Buffer addr: ") + tostr(_buffer));
116 _buffer->addUser(this);
117 _owner->getMenus()->showMenus();
118 // If we don't have a text object for this, we make one
119 if (_buffer->text == 0)
120 resizeCurrentBuffer();
125 screen->first = screen->TopCursorVisible();
127 updateAllVisibleBufferRelatedPopups();
128 _buffer->InsetWakeup();
130 lyxerr.debug(" No Buffer!");
131 _owner->getMenus()->hideMenus();
134 fl_redraw_object(work_area);
136 // should update layoutchoice even if we don't have a buffer.
137 _owner->updateLayoutChoice();
138 _owner->getMiniBuffer()->Init();
139 _owner->updateWindowTitle();
143 void BufferView::updateScreen()
145 // Regenerate the screen.
148 screen = new LyXScreen(FL_ObjWin(work_area),
157 void BufferView::resize()
159 // This will resize the buffer. (Asger)
161 resizeCurrentBuffer();
165 static bool lgb_hack = false;
167 void BufferView::redraw()
169 lyxerr.debug("BufferView::redraw()");
171 fl_redraw_object(work_area);
172 fl_redraw_object(scrollbar);
173 fl_redraw_object(button_down);
174 fl_redraw_object(button_up);
179 void BufferView::fitCursor()
181 if (screen) screen->FitCursor();
185 void BufferView::update()
187 if (screen) screen->Update();
191 void BufferView::updateScrollbar()
193 /* If the text is smaller than the working area, the scrollbar
194 * maximum must be the working area height. No scrolling will
198 fl_set_slider_value(scrollbar, 0);
199 fl_set_slider_size(scrollbar, scrollbar->h);
203 static long max2 = 0;
204 static long height2 = 0;
210 cbth = _buffer->text->height;
212 cbsf = screen->first;
214 // check if anything has changed.
216 height2 == work_area->h &&
217 current_scrollbar_value == cbsf)
221 height2 = work_area->h;
222 current_scrollbar_value = cbsf;
224 if (cbth <= height2) { // text is smaller than screen
225 fl_set_slider_size(scrollbar, scrollbar->h);
229 long maximum_height = work_area->h * 3/4 + cbth;
232 /* set the scrollbar */
233 double hfloat = work_area->h;
234 double maxfloat = maximum_height;
236 fl_set_slider_value(scrollbar, value);
237 fl_set_slider_bounds(scrollbar, 0,
238 maximum_height - work_area->h);
240 double lineh = _buffer->text->DefaultHeight();
241 fl_set_slider_increment(scrollbar,work_area->h-lineh,lineh);
244 if ((hfloat/maxfloat) * (float) height2 < 3)
245 fl_set_slider_size(scrollbar,
248 fl_set_slider_size(scrollbar,
251 fl_set_slider_size(scrollbar, hfloat);
252 fl_set_slider_precision(scrollbar, 0);
256 void BufferView::redoCurrentBuffer()
258 lyxerr.debug("BufferView::redoCurrentBuffer");
259 if (_buffer && _buffer->text) {
261 _owner->updateLayoutChoice();
266 int BufferView::resizeCurrentBuffer()
268 lyxerr.debug("resizeCurrentBuffer");
270 LyXParagraph *par = 0;
271 LyXParagraph *selstartpar = 0;
272 LyXParagraph *selendpar = 0;
281 _owner->getMiniBuffer()->Set(_("Formatting document..."));
284 par = _buffer->text->cursor.par;
285 pos = _buffer->text->cursor.pos;
286 selstartpar = _buffer->text->sel_start_cursor.par;
287 selstartpos = _buffer->text->sel_start_cursor.pos;
288 selendpar = _buffer->text->sel_end_cursor.par;
289 selendpos = _buffer->text->sel_end_cursor.pos;
290 selection = _buffer->text->selection;
291 mark_set = _buffer->text->mark_set;
292 delete _buffer->text;
294 _buffer->text = new LyXText(work_area->w, _buffer);
299 _buffer->text->selection = true;
300 /* at this point just
301 * to avoid the Delete-
304 * setting the cursor */
305 _buffer->text->mark_set = mark_set;
307 _buffer->text->SetCursor(selstartpar, selstartpos);
308 _buffer->text->sel_cursor = _buffer->text->cursor;
309 _buffer->text->SetCursor(selendpar, selendpos);
310 _buffer->text->SetSelection();
311 _buffer->text->SetCursor(par, pos);
313 _buffer->text->SetCursor(par, pos);
314 _buffer->text->sel_cursor = _buffer->text->cursor;
315 _buffer->text->selection = false;
318 screen->first = screen->TopCursorVisible(); /* this will scroll the
319 * screen such that the
324 _owner->getMiniBuffer()->Init();
327 // Now if the title form still exist kill it
334 void BufferView::gotoError()
339 screen->HideCursor();
344 if (!_buffer->text->GotoNextError()) {
345 if (_buffer->text->cursor.pos
346 || _buffer->text->cursor.par !=
347 _buffer->text->FirstParagraph()) {
348 tmp = _buffer->text->cursor;
349 _buffer->text->cursor.par =
350 _buffer->text->FirstParagraph();
351 _buffer->text->cursor.pos = 0;
352 if (!_buffer->text->GotoNextError()) {
353 _buffer->text->cursor = tmp;
354 _owner->getMiniBuffer()->Set(_("No more errors"));
358 _owner->getMiniBuffer()->Set(_("No more errors"));
363 _buffer->text->sel_cursor =
364 _buffer->text->cursor;
368 void BufferView::create_view(int xpos, int ypos, int width, int height)
371 const int bw = abs(fl_get_border_width());
373 // a hack for the figinsets (Matthias)
374 // This one first, then it will probably be invisible. (Lgb)
375 ::figinset_canvas = figinset_canvas = obj =
376 fl_add_canvas(FL_NORMAL_CANVAS,
379 fl_set_object_boxtype(obj,FL_NO_BOX);
380 fl_set_object_resize(obj, FL_RESIZE_ALL);
381 fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
384 obj = fl_add_box(FL_BORDER_BOX, xpos, ypos,
387 fl_set_object_resize(obj, FL_RESIZE_ALL);
388 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
391 work_area = obj = fl_add_free(FL_INPUT_FREE,
393 width-15-2*bw /* scrollbarwidth */,
396 obj->wantkey = FL_KEY_TAB;
397 obj->u_vdata = (void*) this; /* This is how we pass the BufferView
398 to the work_area_handler. */
399 fl_set_object_boxtype(obj,FL_DOWN_BOX);
400 fl_set_object_resize(obj, FL_RESIZE_ALL);
401 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
407 // up - scrollbar button
409 fl_set_border_width(-1);
411 fl_set_border_width(-2); // to get visible feedback
413 button_up = obj = fl_add_pixmapbutton(FL_TOUCH_BUTTON,
417 fl_set_object_boxtype(obj,FL_UP_BOX);
418 fl_set_object_color(obj,FL_MCOL,FL_BLUE);
419 fl_set_object_resize(obj, FL_RESIZE_ALL);
420 fl_set_object_gravity(obj,NorthEastGravity, NorthEastGravity);
421 fl_set_object_callback(obj,UpCB,(long)this);
422 fl_set_pixmapbutton_data(obj, up_xpm);
425 // Remove the blue feedback rectangle
426 fl_set_pixmapbutton_focus_outline(obj,0);
429 // the scrollbar slider
430 fl_set_border_width(-bw);
431 scrollbar = obj = fl_add_slider(FL_VERT_SLIDER,
435 fl_set_object_color(obj,FL_COL1,FL_MCOL);
436 fl_set_object_boxtype(obj, FL_UP_BOX);
437 fl_set_object_resize(obj, FL_RESIZE_ALL);
438 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
439 fl_set_object_callback(obj,ScrollCB,(long)this);
441 // down - scrollbar button
443 fl_set_border_width(-1);
445 fl_set_border_width(-2); // to get visible feedback
447 button_down = obj = fl_add_pixmapbutton(FL_TOUCH_BUTTON,
451 fl_set_object_boxtype(obj,FL_UP_BOX);
452 fl_set_object_color(obj,FL_MCOL,FL_BLUE);
453 fl_set_object_resize(obj, FL_RESIZE_ALL);
454 fl_set_object_gravity(obj, SouthEastGravity, SouthEastGravity);
455 fl_set_object_callback(obj,DownCB,(long)this);
456 fl_set_pixmapbutton_data(obj, down_xpm);
457 fl_set_border_width(-bw);
460 // Remove the blue feedback rectangle
461 fl_set_pixmapbutton_focus_outline(obj,0);
469 timer_cursor = obj = fl_add_timer(FL_HIDDEN_TIMER,
471 fl_set_object_callback(obj,CursorToggleCB,0);
472 obj->u_vdata = (void*) this;
476 // Callback for scrollbar up button
477 void BufferView::UpCB(FL_OBJECT *ob, long buf)
479 BufferView *view = (BufferView*) buf;
481 if (view->_buffer == 0) return;
484 static long time = 0;
485 ev2 = fl_last_event();
486 if (ev2->type == ButtonPress || ev2->type == ButtonRelease)
488 int button = fl_get_button_numb(ob);
491 view->ScrollUpOnePage(time++); break;
493 view->ScrollDownOnePage(time++); break;
495 view->ScrollUp(time++); break;
506 a = XInternAtom(fl_display, "WAIT_FOR_X", False);
509 XSetWindowAttributes attr;
510 mask = CWOverrideRedirect;
511 attr.override_redirect = 1;
512 w = XCreateWindow(fl_display, fl_root,
513 0, 0, 1, 1, 0, CopyFromParent,
514 InputOnly, CopyFromParent, mask, &attr);
515 XSelectInput(fl_display, w, PropertyChangeMask);
516 XMapWindow(fl_display, w);
519 XChangeProperty(fl_display, w, a, a, 8,
520 PropModeAppend, (unsigned char *)"", 0);
521 XWindowEvent(fl_display, w, PropertyChangeMask, &ev);
525 // Callback for scrollbar slider
526 void BufferView::ScrollCB(FL_OBJECT *ob, long buf)
528 BufferView *view = (BufferView*) buf;
529 extern bool cursor_follows_scrollbar;
531 if (view->_buffer == 0) return;
533 view->current_scrollbar_value = (long)fl_get_slider_value(ob);
534 if (view->current_scrollbar_value < 0)
535 view->current_scrollbar_value = 0;
540 view->screen->Draw(view->current_scrollbar_value);
542 if (cursor_follows_scrollbar) {
543 LyXText * vbt = view->_buffer->text;
544 int height = vbt->DefaultHeight();
546 if (vbt->cursor.y < view->screen->first + height) {
547 vbt->SetCursorFromCoordinates(0,
548 view->screen->first +
551 else if (vbt->cursor.y >
552 view->screen->first + view->work_area->h - height) {
553 vbt->SetCursorFromCoordinates(0,
554 view->screen->first +
563 // Callback for scrollbar down button
564 void BufferView::DownCB(FL_OBJECT *ob, long buf)
566 BufferView *view = (BufferView*) buf;
568 if (view->_buffer == 0) return;
571 static long time = 0;
572 ev2 = fl_last_event();
573 if (ev2->type == ButtonPress || ev2->type == ButtonRelease)
575 int button = fl_get_button_numb(ob);
578 view->ScrollUpOnePage(time++); break;
580 view->ScrollDownOnePage(time++); break;
582 view->ScrollDown(time++); break;
587 int BufferView::ScrollUp(long time)
589 if (_buffer == 0) return 0;
593 double value= fl_get_slider_value(scrollbar);
598 float add_value = (_buffer->text->DefaultHeight()
599 + (float)(time) * (float)(time) * 0.125);
601 if (add_value > work_area->h)
602 add_value = (float) (work_area->h -
603 _buffer->text->DefaultHeight());
610 fl_set_slider_value(scrollbar, value);
612 ScrollCB(scrollbar,(long)this);
617 int BufferView::ScrollDown(long time)
619 if (_buffer == 0) return 0;
623 double value= fl_get_slider_value(scrollbar);
625 fl_get_slider_bounds(scrollbar, &min, &max);
630 float add_value = (_buffer->text->DefaultHeight()
631 + (float)(time) * (float)(time) * 0.125);
633 if (add_value > work_area->h)
634 add_value = (float) (work_area->h -
635 _buffer->text->DefaultHeight());
642 fl_set_slider_value(scrollbar, value);
644 ScrollCB(scrollbar,(long)this);
649 void BufferView::ScrollUpOnePage(long /*time*/)
651 if (_buffer == 0) return;
655 long y = screen->first;
659 Row* row = _buffer->text->GetRowNearY(y);
660 y = y - work_area->h + row->height;
662 fl_set_slider_value(scrollbar, y);
664 ScrollCB(scrollbar,(long)this);
668 void BufferView::ScrollDownOnePage(long /*time*/)
670 if (_buffer == 0) return;
675 fl_get_slider_bounds(scrollbar, &min, &max);
676 long y = screen->first;
678 if (y > _buffer->text->height - work_area->h)
682 _buffer->text->GetRowNearY(y);
684 fl_set_slider_value(scrollbar, y);
686 ScrollCB(scrollbar,(long)this);
690 int BufferView::work_area_handler(FL_OBJECT * ob, int event,
692 int /*key*/, void *xev)
694 static int x_old = -1;
695 static int y_old = -1;
696 static long scrollbar_value_old = -1;
698 XEvent* ev = (XEvent*) xev;
699 BufferView *view = (BufferView*) ob->u_vdata;
701 // If we don't have a view yet; return
702 if (!view || quitting) return 0;
706 view->workAreaExpose();
709 view->WorkAreaButtonPress(ob, 0,0,0,ev,0);
712 view->WorkAreaButtonRelease(ob, 0,0,0,ev,0);
715 if (ev->xmotion.x != x_old ||
716 ev->xmotion.y != y_old ||
717 view->current_scrollbar_value != scrollbar_value_old) {
718 x_old = ev->xmotion.x;
719 y_old = ev->xmotion.y;
720 scrollbar_value_old = view->current_scrollbar_value;
721 view->WorkAreaMotionNotify(ob, 0,0,0,ev,0);
724 // Done by the raw callback:
725 // case FL_KEYBOARD: WorkAreaKeyPress(ob, 0,0,0,ev,0); break;
727 if (!view->_owner->getMiniBuffer()->shows_no_match)
728 view->_owner->getMiniBuffer()->Init();
729 view->_owner->getMiniBuffer()->shows_no_match = false;
730 view->work_area_focus = true;
731 fl_set_timer(view->timer_cursor, 0.4);
734 view->_owner->getMiniBuffer()->ExecCommand();
735 view->work_area_focus = false;
738 SetXtermCursor(view->_owner->getForm()->window);
740 view->lyx_focus = true;
741 fl_set_timer(view->timer_cursor, 0.4);
744 if (!input_prohibited)
745 XUndefineCursor(fl_display,
746 view->_owner->getForm()->window);
747 view->lyx_focus = false; // This is not an absolute truth
748 // but if it is not true, it will be changed within a blink
749 // of an eye. ... Not good enough... use regulare timeperiod
750 //fl_set_timer(view->timer_cursor, 0.01); // 0.1 sec blink
751 fl_set_timer(view->timer_cursor, 0.4); // 0.4 sec blink
755 if (view->_buffer && !view->_buffer->the_locking_inset) {
756 if (view->screen && ev->xbutton.button == 1) {
757 view->screen->HideCursor();
758 view->screen->ToggleSelection();
759 view->_buffer->text->SelectWord();
760 view->screen->ToggleSelection(false);
761 /* This will fit the cursor on the screen
763 view->_buffer->update(0);
769 if (view->_buffer && view->screen && ev->xbutton.button == 1) {
770 view->screen->HideCursor();
771 view->screen->ToggleSelection();
772 view->_buffer->text->CursorHome();
773 view->_buffer->text->sel_cursor =
774 view->_buffer->text->cursor;
775 view->_buffer->text->CursorEnd();
776 view->_buffer->text->SetSelection();
777 view->screen->ToggleSelection(false);
778 /* This will fit the cursor on the screen
780 view->_buffer->update(0);
784 view->WorkAreaSelectionNotify(ob,
785 view->_owner->getForm()->window,
792 int BufferView::WorkAreaMotionNotify(FL_OBJECT *ob, Window,
793 int /*w*/, int /*h*/,
794 XEvent *ev, void */*d*/)
797 if (_buffer == 0) return 0;
798 if (!screen) return 0;
800 // Check for inset locking
801 if (_buffer->the_locking_inset) {
802 LyXCursor cursor = _buffer->text->cursor;
803 _buffer->the_locking_inset->
804 InsetMotionNotify(ev->xbutton.x - ob->x - cursor.x,
805 ev->xbutton.y - ob->y -
811 // Only use motion with button 1
812 if (!ev->xmotion.state & Button1MotionMask)
815 /* The selection possible is needed, that only motion events are
816 * used, where the bottom press event was on the drawing area too */
817 if (selection_possible) {
818 screen->HideCursor();
821 SetCursorFromCoordinates(ev->xbutton.x - ob->x,
822 ev->xbutton.y - ob->y +
825 if (!_buffer->text->selection)
826 _buffer->update(-3); // Maybe an empty line was deleted
828 _buffer->text->SetSelection();
829 screen->ToggleToggle();
830 if (screen->FitCursor())
832 screen->ShowCursor();
838 extern int bibitemMaxWidth(const class LyXFont &);
840 // Single-click on work area
841 int BufferView::WorkAreaButtonPress(FL_OBJECT *ob, Window,
842 int /*w*/, int /*h*/, XEvent *ev, void */*d*/)
847 if (_buffer == 0) return 0;
848 if (!screen) return 0;
850 int const x = ev->xbutton.x - ob->x;
851 int const y = ev->xbutton.y - ob->y;
852 // If we hit an inset, we have the inset coordinates in these
853 // and inset_hit points to the inset. If we do not hit an
854 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
857 Inset * inset_hit = checkInsetHit(inset_x, inset_y);
859 // ok ok, this is a hack.
860 int button = ev->xbutton.button;
861 if (button == 4 || button == 5) goto wheel;
865 if (_buffer->the_locking_inset) {
866 // We are in inset locking mode
868 /* Check whether the inset was hit. If not reset mode,
869 otherwise give the event to the inset */
870 if (inset_hit != 0) {
871 _buffer->the_locking_inset->
872 InsetButtonPress(inset_x, inset_y, button);
875 UnlockInset(_buffer->the_locking_inset);
879 selection_possible = true;
880 screen->HideCursor();
882 // Right button mouse click on a table
884 (_buffer->text->cursor.par->table ||
885 _buffer->text->MouseHitInTable(x, y+screen->first))) {
886 // Set the cursor to the press-position
887 _buffer->text->SetCursorFromCoordinates(x, y + screen->first);
890 // Only show the table popup if the hit is in the table, too
891 if (!_buffer->text->HitInTable(_buffer->text->cursor.row, x))
894 // Hit above or below the table?
897 long y_tmp = y + screen->first;
899 Row* row = _buffer->text->GetRowNearY(y_tmp);
901 // Isn't this empty code anyway? (Lgb)
902 if (row->par != _buffer->text->cursor.par)
908 if (!_buffer->text->selection) {
909 screen->ToggleSelection();
910 _buffer->text->ClearSelection();
911 _buffer->text->FullRebreak();
915 // Popup table popup when on a table.
916 // This is obviously temporary, since we should be
918 // popup various context-sensitive-menus with the
919 // the right mouse. So this should be done more
920 // general in the future. Matthias.
921 selection_possible = false;
922 _owner->getLyXFunc()->Dispatch(LFUN_LAYOUT_TABLE,
928 int screen_first = screen->first;
930 // Middle button press pastes if we have a selection
931 bool paste_internally = false;
932 if (button == 2 // && !_buffer->the_locking_inset
933 && _buffer->text->selection) {
934 _owner->getLyXFunc()->Dispatch(LFUN_COPY);
935 paste_internally = true;
938 // Clear the selection
939 screen->ToggleSelection();
940 _buffer->text->ClearSelection();
941 _buffer->text->FullRebreak();
945 // Single left click in math inset?
946 if (inset_hit != 0 && inset_hit->Editable() == 2) {
947 // Highly editable inset, like math
948 selection_possible = false;
949 _owner->updateLayoutChoice();
950 _owner->getMiniBuffer()->Set(inset_hit->EditMessage());
951 inset_hit->Edit(inset_x, inset_y);
955 // Right click on a footnote flag opens float menu
958 // Isn't this empty code anyway? (Lgb)
959 if (_buffer->text->cursor.par->footnoteflag ==
960 LyXParagraph::OPEN_FOOTNOTE) {
961 selection_possible = false;
962 lyxerr.debug("LyX: Sorry not implemented yet.");
969 selection_possible = false;
973 _buffer->text->SetCursorFromCoordinates(x, y + screen_first);
974 _buffer->text->FinishUndo();
975 _buffer->text->sel_cursor = _buffer->text->cursor;
976 _buffer->text->cursor.x_fix = _buffer->text->cursor.x;
978 _owner->updateLayoutChoice();
979 if (screen->FitCursor()){
981 selection_possible = false;
984 // Insert primary selection with middle mouse
985 // if there is a local selection in the current buffer, insert this
986 if (button == 2) { // && !_buffer->the_locking_inset){
987 if (paste_internally)
988 _owner->getLyXFunc()->Dispatch(LFUN_PASTE);
990 _owner->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
992 selection_possible = false;
998 // I am not quite sure if this is the correct place to put this,
999 // but it will not cause any harm.
1000 // Patch from Mark Huang (markman@mit.edu) to make LyX recognise
1001 // button 4 and 5. This enables LyX use use the scrollwhell on
1002 // certain mice for something useful. (Lgb)
1003 // Added wheel acceleration detection code. (Rvdk)
1004 static Time lastTime = 0;
1005 int diff = ev->xbutton.time - lastTime;
1006 int scroll = int(1.0 + (4.0/(abs(diff)+1.0))*200.0);
1015 lastTime = ev->xbutton.time;
1026 int BufferView::WorkAreaButtonRelease(FL_OBJECT *ob, Window ,
1027 int /*w*/, int /*h*/, XEvent *ev, void */*d*/)
1029 if (_buffer == 0 || screen == 0) return 0;
1031 int const x = ev->xbutton.x - ob->x;
1032 int const y = ev->xbutton.y - ob->y;
1034 // If we hit an inset, we have the inset coordinates in these
1035 // and inset_hit points to the inset. If we do not hit an
1036 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1039 Inset * inset_hit = checkInsetHit(inset_x, inset_y);
1041 if (_buffer->the_locking_inset) {
1042 // We are in inset locking mode.
1044 /* LyX does a kind of work-area grabbing for insets.
1045 Only a ButtonPress Event outside the inset will
1046 force a InsetUnlock. */
1047 _buffer->the_locking_inset->
1048 InsetButtonRelease(inset_x, inset_y,
1049 ev->xbutton.button);
1053 selection_possible = false;
1054 if (_buffer->text->cursor.par->table) {
1055 int cell = _buffer->text->
1056 NumberOfCell(_buffer->text->cursor.par,
1057 _buffer->text->cursor.pos);
1058 if (_buffer->text->cursor.par->table->IsContRow(cell) &&
1059 _buffer->text->cursor.par->table->
1060 CellHasContRow(_buffer->text->cursor.par->table->
1061 GetCellAbove(cell))<0) {
1062 _buffer->text->CursorUp();
1066 if (ev->xbutton.button >= 2)
1069 // Make sure that the press was not far from the release
1070 if ((abs(last_click_x - x) >= 5) ||
1071 (abs(last_click_y - y) >= 5)) {
1075 // Did we hit an editable inset?
1076 if (inset_hit != 0) {
1077 // Inset like error, notes and figures
1078 selection_possible = false;
1079 #ifdef WITH_WARNINGS
1080 #warning fix this proper in 0.13
1082 // Following a ref shouldn't issue
1083 // a push on the undo-stack
1084 // anylonger, now that we have
1085 // keybindings for following
1086 // references and returning from
1087 // references. IMHO though, it
1088 // should be the inset's own business
1089 // to push or not push on the undo
1090 // stack. They don't *have* to
1091 // alter the document...
1093 // ...or maybe the SetCursorParUndo()
1094 // below isn't necessary at all anylonger?
1095 if (inset_hit->LyxCode() == Inset::REF_CODE) {
1096 _buffer->text->SetCursorParUndo();
1099 _owner->getMiniBuffer()->Set(inset_hit->EditMessage());
1100 inset_hit->Edit(inset_x, inset_y);
1104 // check whether we want to open a float
1105 if (_buffer->text) {
1108 if (_buffer->text->cursor.pos <
1109 _buffer->text->cursor.par->Last()) {
1110 c = _buffer->text->cursor.par->
1111 GetChar(_buffer->text->cursor.pos);
1113 if (c == LYX_META_FOOTNOTE || c == LYX_META_MARGIN
1114 || c == LYX_META_FIG || c == LYX_META_TAB
1115 || c == LYX_META_WIDE_FIG || c == LYX_META_WIDE_TAB
1116 || c == LYX_META_ALGORITHM){
1118 } else if (_buffer->text->cursor.pos - 1 >= 0) {
1119 c = _buffer->text->cursor.par->
1120 GetChar(_buffer->text->cursor.pos - 1);
1121 if (c == LYX_META_FOOTNOTE || c == LYX_META_MARGIN
1122 || c == LYX_META_FIG || c == LYX_META_TAB
1123 || c == LYX_META_WIDE_FIG
1124 || c == LYX_META_WIDE_TAB
1125 || c == LYX_META_ALGORITHM){
1126 // We are one step too far to the right
1127 _buffer->text->CursorLeft();
1133 selection_possible = false;
1138 // Do we want to close a float? (click on the float-label)
1139 if (_buffer->text->cursor.row->par->footnoteflag ==
1140 LyXParagraph::OPEN_FOOTNOTE
1141 && _buffer->text->cursor.pos == 0
1142 && _buffer->text->cursor.row->previous &&
1143 _buffer->text->cursor.row->previous->par->
1144 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
1145 LyXFont font (LyXFont::ALL_SANE);
1146 font.setSize(LyXFont::SIZE_SMALL);
1148 int box_x = 20; // LYX_PAPER_MARGIN;
1149 box_x += font.textWidth("Mwide-figM", 10);
1151 int screen_first = screen->first;
1154 && y + screen_first > _buffer->text->cursor.y -
1155 _buffer->text->cursor.row->baseline
1156 && y + screen_first < _buffer->text->cursor.y -
1157 _buffer->text->cursor.row->baseline
1158 + font.maxAscent()*1.2 + font.maxDescent()*1.2) {
1160 selection_possible = false;
1165 // Maybe we want to edit a bibitem ale970302
1166 if (_buffer->text->cursor.par->bibkey && x < 20 +
1167 bibitemMaxWidth(lyxstyle.TextClass(_buffer->
1168 params.textclass)->defaultfont)) {
1169 _buffer->text->cursor.par->bibkey->Edit(0, 0);
1177 * Returns an inset if inset was hit. 0 otherwise.
1178 * If hit, the coordinates are changed relative to the inset.
1179 * Otherwise coordinates are not changed, and false is returned.
1181 Inset * BufferView::checkInsetHit(int &x, int &y)
1186 int y_tmp = y + getScreen()->first;
1188 LyXCursor cursor = _buffer->text->cursor;
1189 if (cursor.pos < cursor.par->Last()
1190 && cursor.par->GetChar(cursor.pos) == LYX_META_INSET
1191 && cursor.par->GetInset(cursor.pos)
1192 && cursor.par->GetInset(cursor.pos)->Editable()) {
1194 // Check whether the inset really was hit
1195 Inset* tmpinset = cursor.par->GetInset(cursor.pos);
1196 LyXFont font = _buffer->text->GetFont(cursor.par, cursor.pos);
1198 && x < cursor.x + tmpinset->Width(font)
1199 && y_tmp > cursor.y - tmpinset->Ascent(font)
1200 && y_tmp < cursor.y + tmpinset->Descent(font)) {
1202 // The origin of an inset is on the baseline
1203 y = y_tmp - (cursor.y);
1206 } else if (cursor.pos - 1 >= 0
1207 && cursor.par->GetChar(cursor.pos - 1) == LYX_META_INSET
1208 && cursor.par->GetInset(cursor.pos - 1)
1209 && cursor.par->GetInset(cursor.pos - 1)->Editable()) {
1210 _buffer->text->CursorLeft();
1211 Inset * result = checkInsetHit(x, y);
1213 _buffer->text->CursorRight();
1223 int BufferView::workAreaExpose()
1225 if (!work_area || !work_area->form->visible)
1228 // this is a hack to ensure that we only call this through
1229 // BufferView::redraw().
1234 static int work_area_width = work_area->w;
1235 static int work_area_height = work_area->h;
1237 bool widthChange = work_area->w != work_area_width;
1238 bool heightChange = work_area->h != work_area_height;
1240 // update from work area
1241 work_area_width = work_area->w;
1242 work_area_height = work_area->h;
1245 // All buffers need a resize
1246 bufferlist.resize();
1247 } else if (heightChange) {
1248 // Rebuild image of current screen
1250 // fitCursor() ensures we don't jump back
1251 // to the start of the document on vertical
1255 // The main window size has changed, repaint most stuff
1257 // ...including the minibuffer
1258 _owner->getMiniBuffer()->Init();
1260 } else if (screen) screen->Redraw();
1262 // Grey box when we don't have a buffer
1263 fl_winset(FL_ObjWin(work_area));
1264 fl_rectangle(1, work_area->x, work_area->y,
1265 work_area->w, work_area->h, FL_GRAY63);
1268 // always make sure that the scrollbar is sane.
1270 _owner->updateLayoutChoice();
1275 // Callback for cursor timer
1276 void BufferView::CursorToggleCB(FL_OBJECT *ob, long)
1278 BufferView *view = (BufferView*) ob->u_vdata;
1280 /* quite a nice place for asyncron Inset updating, isn't it? */
1281 // actually no! This is run even if no buffer exist... so (Lgb)
1282 if (view && !view->_buffer) {
1283 goto set_timer_and_return;
1285 #ifdef WITH_WARNINGS
1289 // On my quest to solve the gs rendre hangups I am now
1290 // disabling the SIGHUP completely, and will do a wait
1291 // now and then instead. If the guess that xforms somehow
1292 // destroys something is true, this is likely (hopefully)
1293 // to solve the problem...at least I hope so. Lgb
1295 // ...Ok this seems to work...at least it does not make things
1296 // worse so far. However I still see gs processes that hangs.
1297 // I would really like to know _why_ they are hanging. Anyway
1298 // the solution without the SIGCHLD handler seems to be easier
1301 // When attaching gdb to a a running gs that hangs it shows
1302 // that it is waiting for input(?) Is it possible for us to
1303 // provide that input somehow? Or figure what it is expecing
1306 // One solution is to, after some time, look if there are some
1307 // old gs processes still running and if there are: kill them
1310 // Another solution is to provide the user an option to rerender
1311 // a picture. This would, for the picture in question, check if
1312 // there is a gs running for it, if so kill it, and start a new
1313 // rendering process.
1315 // these comments posted to lyx@via
1319 int pid = waitpid((pid_t)0, &status, WNOHANG);
1320 if (pid == -1) // error find out what is wrong
1321 ; // ignore it for now.
1323 sigchldhandler(pid, &status);
1326 if (InsetUpdateList)
1327 UpdateInsetUpdateList();
1329 if (view && !view->screen){
1330 goto set_timer_and_return;
1333 if (view->lyx_focus && view->work_area_focus) {
1334 if (!view->_buffer->the_locking_inset){
1335 view->screen->CursorToggle();
1337 view->_buffer->the_locking_inset->
1338 ToggleInsetCursor();
1340 goto set_timer_and_return;
1342 // Make sure that the cursor is visible.
1343 if (!view->_buffer->the_locking_inset){
1344 view->screen->ShowCursor();
1346 if (!view->_buffer->the_locking_inset->isCursorVisible())
1347 view->_buffer->the_locking_inset->
1348 ToggleInsetCursor();
1351 // This is only run when work_area_focus or lyx_focus is false.
1354 XGetInputFocus(fl_display, &tmpwin, &tmp);
1355 lyxerr.debug(string("tmpwin: ") + tostr(tmpwin));
1356 lyxerr.debug(string("window: ")
1357 + tostr(view->_owner->getForm()->window));
1358 lyxerr.debug(string("work_area_focus: ")
1359 + tostr(view->work_area_focus));
1360 lyxerr.debug(string("lyx_focus : ")
1361 + tostr(view->lyx_focus));
1362 if (tmpwin != view->_owner->getForm()->window) {
1363 view->lyx_focus = false;
1366 view->lyx_focus = true;
1367 if (!view->work_area_focus)
1370 goto set_timer_and_return;
1374 set_timer_and_return:
1375 fl_set_timer(ob, 0.4);
1381 int BufferView::WorkAreaSelectionNotify(FL_OBJECT *, Window win,
1382 int /*w*/, int /*h*/, XEvent *event, void */*d*/)
1384 if (_buffer == 0) return 0;
1385 if (event->type != SelectionNotify)
1391 unsigned char* uc = 0;
1393 screen->HideCursor();
1395 if (event->xselection.type == XA_STRING
1396 && event->xselection.property) {
1398 if (XGetWindowProperty(
1399 fl_display /* display */,
1401 event->xselection.property /* property */,
1402 0 /* long_offset */,
1403 0 /* long_length */,
1405 XA_STRING /* req_type */,
1406 &tmpatom /* actual_type_return */,
1407 &tmpint /* actual_format_return */,
1408 &ul1 /* nitems_return */,
1409 &ul2 /* bytes_after_return */,
1410 &uc /* prop_return */
1421 if (XGetWindowProperty(
1422 fl_display /* display */,
1424 event->xselection.property /* property */,
1425 0 /* long_offset */,
1426 ul2/4+1 /* long_length */,
1428 XA_STRING /* req_type */,
1429 &tmpatom /* actual_type_return */,
1430 &tmpint /* actual_format_return */,
1431 &ul1 /* nitems_return */,
1432 &ul2 /* bytes_after_return */,
1433 &uc /* prop_return */
1442 InsertStringA((char*)uc);
1445 InsertStringB((char*)uc);
1456 void BufferView::cursorPrevious()
1458 if (!currentBuffer()->text->cursor.row->previous) return;
1460 long y = getScreen()->first;
1461 Row* cursorrow = currentBuffer()->text->cursor.row;
1462 currentBuffer()->text->
1463 SetCursorFromCoordinates(currentBuffer()->text->
1466 currentBuffer()->text->FinishUndo();
1467 /* this is to allow jumping over large insets */
1468 if ((cursorrow == currentBuffer()->text->cursor.row))
1469 currentBuffer()->text->CursorUp();
1471 if (currentBuffer()->text->cursor.row->height < work_area->h)
1472 getScreen()->Draw(currentBuffer()->text->cursor.y
1473 - currentBuffer()->text->cursor.row->baseline
1474 + currentBuffer()->text->cursor.row->height
1475 - work_area->h +1 );
1479 void BufferView::cursorNext()
1481 if (!currentBuffer()->text->cursor.row->next) return;
1483 long y = getScreen()->first;
1484 currentBuffer()->text->GetRowNearY(y);
1485 Row* cursorrow = currentBuffer()->text->cursor.row;
1486 currentBuffer()->text->
1487 SetCursorFromCoordinates(currentBuffer()->text->
1490 currentBuffer()->text->FinishUndo();
1491 /* this is to allow jumping over large insets */
1492 if ((cursorrow == currentBuffer()->text->cursor.row))
1493 currentBuffer()->text->CursorDown();
1495 if (currentBuffer()->text->cursor.row->height < work_area->h)
1496 getScreen()->Draw(currentBuffer()->text->cursor.y
1497 - currentBuffer()->text->cursor.row->baseline);
1501 bool BufferView::available() const
1503 if (_buffer && _buffer->text) return true;
1508 void BufferView::savePosition()
1510 backstack->push(currentBuffer()->getFileName(),
1511 currentBuffer()->text->cursor.x,
1512 currentBuffer()->text->cursor.y);
1516 void BufferView::restorePosition()
1519 string fname = backstack->pop(&x, &y);
1522 Buffer *b = (bufferlist.exists(fname)) ? bufferlist.getBuffer(fname):
1523 bufferlist.loadLyXFile(fname); // don't ask, just load it
1525 currentBuffer()->text->SetCursorFromCoordinates(x, y);
1526 currentBuffer()->update(0);