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 = 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);
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?
896 if (!_buffer->text->selection) {
897 screen->ToggleSelection();
898 _buffer->text->ClearSelection();
899 _buffer->text->FullRebreak();
903 // Popup table popup when on a table.
904 // This is obviously temporary, since we should be
906 // popup various context-sensitive-menus with the
907 // the right mouse. So this should be done more
908 // general in the future. Matthias.
909 selection_possible = false;
910 _owner->getLyXFunc()->Dispatch(LFUN_LAYOUT_TABLE,
916 int screen_first = screen->first;
918 // Middle button press pastes if we have a selection
919 bool paste_internally = false;
920 if (button == 2 // && !_buffer->the_locking_inset
921 && _buffer->text->selection) {
922 _owner->getLyXFunc()->Dispatch(LFUN_COPY);
923 paste_internally = true;
926 // Clear the selection
927 screen->ToggleSelection();
928 _buffer->text->ClearSelection();
929 _buffer->text->FullRebreak();
933 // Single left click in math inset?
934 if (inset_hit != 0 && inset_hit->Editable() == 2) {
935 // Highly editable inset, like math
936 selection_possible = false;
937 _owner->updateLayoutChoice();
938 _owner->getMiniBuffer()->Set(inset_hit->EditMessage());
939 inset_hit->Edit(inset_x, inset_y);
943 // Right click on a footnote flag opens float menu
945 selection_possible = false;
949 _buffer->text->SetCursorFromCoordinates(x, y + screen_first);
950 _buffer->text->FinishUndo();
951 _buffer->text->sel_cursor = _buffer->text->cursor;
952 _buffer->text->cursor.x_fix = _buffer->text->cursor.x;
954 _owner->updateLayoutChoice();
955 if (screen->FitCursor()){
957 selection_possible = false;
960 // Insert primary selection with middle mouse
961 // if there is a local selection in the current buffer, insert this
962 if (button == 2) { // && !_buffer->the_locking_inset){
963 if (paste_internally)
964 _owner->getLyXFunc()->Dispatch(LFUN_PASTE);
966 _owner->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
968 selection_possible = false;
974 // I am not quite sure if this is the correct place to put this,
975 // but it will not cause any harm.
976 // Patch from Mark Huang (markman@mit.edu) to make LyX recognise
977 // button 4 and 5. This enables LyX use use the scrollwhell on
978 // certain mice for something useful. (Lgb)
979 // Added wheel acceleration detection code. (Rvdk)
980 static Time lastTime = 0;
981 int diff = ev->xbutton.time - lastTime;
982 int scroll = int(1.0 + (4.0/(abs(diff)+1.0))*200.0);
991 lastTime = ev->xbutton.time;
1002 int BufferView::WorkAreaButtonRelease(FL_OBJECT *ob, Window ,
1003 int /*w*/, int /*h*/, XEvent *ev, void */*d*/)
1005 if (_buffer == 0 || screen == 0) return 0;
1007 int const x = ev->xbutton.x - ob->x;
1008 int const y = ev->xbutton.y - ob->y;
1010 // If we hit an inset, we have the inset coordinates in these
1011 // and inset_hit points to the inset. If we do not hit an
1012 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1015 Inset * inset_hit = checkInsetHit(inset_x, inset_y);
1017 if (_buffer->the_locking_inset) {
1018 // We are in inset locking mode.
1020 /* LyX does a kind of work-area grabbing for insets.
1021 Only a ButtonPress Event outside the inset will
1022 force a InsetUnlock. */
1023 _buffer->the_locking_inset->
1024 InsetButtonRelease(inset_x, inset_y,
1025 ev->xbutton.button);
1029 selection_possible = false;
1030 if (_buffer->text->cursor.par->table) {
1031 int cell = _buffer->text->
1032 NumberOfCell(_buffer->text->cursor.par,
1033 _buffer->text->cursor.pos);
1034 if (_buffer->text->cursor.par->table->IsContRow(cell) &&
1035 _buffer->text->cursor.par->table->
1036 CellHasContRow(_buffer->text->cursor.par->table->
1037 GetCellAbove(cell))<0) {
1038 _buffer->text->CursorUp();
1042 if (ev->xbutton.button >= 2)
1045 // Make sure that the press was not far from the release
1046 if ((abs(last_click_x - x) >= 5) ||
1047 (abs(last_click_y - y) >= 5)) {
1051 // Did we hit an editable inset?
1052 if (inset_hit != 0) {
1053 // Inset like error, notes and figures
1054 selection_possible = false;
1055 #ifdef WITH_WARNINGS
1056 #warning fix this proper in 0.13
1058 // Following a ref shouldn't issue
1059 // a push on the undo-stack
1060 // anylonger, now that we have
1061 // keybindings for following
1062 // references and returning from
1063 // references. IMHO though, it
1064 // should be the inset's own business
1065 // to push or not push on the undo
1066 // stack. They don't *have* to
1067 // alter the document...
1069 // ...or maybe the SetCursorParUndo()
1070 // below isn't necessary at all anylonger?
1071 if (inset_hit->LyxCode() == Inset::REF_CODE) {
1072 _buffer->text->SetCursorParUndo();
1075 _owner->getMiniBuffer()->Set(inset_hit->EditMessage());
1076 inset_hit->Edit(inset_x, inset_y);
1080 // check whether we want to open a float
1081 if (_buffer->text) {
1084 if (_buffer->text->cursor.pos <
1085 _buffer->text->cursor.par->Last()) {
1086 c = _buffer->text->cursor.par->
1087 GetChar(_buffer->text->cursor.pos);
1089 if (c == LYX_META_FOOTNOTE || c == LYX_META_MARGIN
1090 || c == LYX_META_FIG || c == LYX_META_TAB
1091 || c == LYX_META_WIDE_FIG || c == LYX_META_WIDE_TAB
1092 || c == LYX_META_ALGORITHM){
1094 } else if (_buffer->text->cursor.pos - 1 >= 0) {
1095 c = _buffer->text->cursor.par->
1096 GetChar(_buffer->text->cursor.pos - 1);
1097 if (c == LYX_META_FOOTNOTE || c == LYX_META_MARGIN
1098 || c == LYX_META_FIG || c == LYX_META_TAB
1099 || c == LYX_META_WIDE_FIG
1100 || c == LYX_META_WIDE_TAB
1101 || c == LYX_META_ALGORITHM){
1102 // We are one step too far to the right
1103 _buffer->text->CursorLeft();
1109 selection_possible = false;
1114 // Do we want to close a float? (click on the float-label)
1115 if (_buffer->text->cursor.row->par->footnoteflag ==
1116 LyXParagraph::OPEN_FOOTNOTE
1117 && _buffer->text->cursor.pos == 0
1118 && _buffer->text->cursor.row->previous &&
1119 _buffer->text->cursor.row->previous->par->
1120 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
1121 LyXFont font (LyXFont::ALL_SANE);
1122 font.setSize(LyXFont::SIZE_SMALL);
1124 int box_x = 20; // LYX_PAPER_MARGIN;
1125 box_x += font.textWidth("Mwide-figM", 10);
1127 int screen_first = screen->first;
1130 && y + screen_first > _buffer->text->cursor.y -
1131 _buffer->text->cursor.row->baseline
1132 && y + screen_first < _buffer->text->cursor.y -
1133 _buffer->text->cursor.row->baseline
1134 + font.maxAscent()*1.2 + font.maxDescent()*1.2) {
1136 selection_possible = false;
1141 // Maybe we want to edit a bibitem ale970302
1142 if (_buffer->text->cursor.par->bibkey && x < 20 +
1143 bibitemMaxWidth(lyxstyle.TextClass(_buffer->
1144 params.textclass)->defaultfont)) {
1145 _buffer->text->cursor.par->bibkey->Edit(0, 0);
1153 * Returns an inset if inset was hit. 0 otherwise.
1154 * If hit, the coordinates are changed relative to the inset.
1155 * Otherwise coordinates are not changed, and false is returned.
1157 Inset * BufferView::checkInsetHit(int &x, int &y)
1162 int y_tmp = y + getScreen()->first;
1164 LyXCursor cursor = _buffer->text->cursor;
1165 if (cursor.pos < cursor.par->Last()
1166 && cursor.par->GetChar(cursor.pos) == LYX_META_INSET
1167 && cursor.par->GetInset(cursor.pos)
1168 && cursor.par->GetInset(cursor.pos)->Editable()) {
1170 // Check whether the inset really was hit
1171 Inset* tmpinset = cursor.par->GetInset(cursor.pos);
1172 LyXFont font = _buffer->text->GetFont(cursor.par, cursor.pos);
1174 && x < cursor.x + tmpinset->Width(font)
1175 && y_tmp > cursor.y - tmpinset->Ascent(font)
1176 && y_tmp < cursor.y + tmpinset->Descent(font)) {
1178 // The origin of an inset is on the baseline
1179 y = y_tmp - (cursor.y);
1182 } else if (cursor.pos - 1 >= 0
1183 && cursor.par->GetChar(cursor.pos - 1) == LYX_META_INSET
1184 && cursor.par->GetInset(cursor.pos - 1)
1185 && cursor.par->GetInset(cursor.pos - 1)->Editable()) {
1186 _buffer->text->CursorLeft();
1187 Inset * result = checkInsetHit(x, y);
1189 _buffer->text->CursorRight();
1199 int BufferView::workAreaExpose()
1201 if (!work_area || !work_area->form->visible)
1204 // this is a hack to ensure that we only call this through
1205 // BufferView::redraw().
1210 static int work_area_width = work_area->w;
1211 static int work_area_height = work_area->h;
1213 bool widthChange = work_area->w != work_area_width;
1214 bool heightChange = work_area->h != work_area_height;
1216 // update from work area
1217 work_area_width = work_area->w;
1218 work_area_height = work_area->h;
1221 // All buffers need a resize
1222 bufferlist.resize();
1223 } else if (heightChange) {
1224 // Rebuild image of current screen
1226 // fitCursor() ensures we don't jump back
1227 // to the start of the document on vertical
1231 // The main window size has changed, repaint most stuff
1233 // ...including the minibuffer
1234 _owner->getMiniBuffer()->Init();
1236 } else if (screen) screen->Redraw();
1238 // Grey box when we don't have a buffer
1239 fl_winset(FL_ObjWin(work_area));
1240 fl_rectangle(1, work_area->x, work_area->y,
1241 work_area->w, work_area->h, FL_GRAY63);
1244 // always make sure that the scrollbar is sane.
1246 _owner->updateLayoutChoice();
1251 // Callback for cursor timer
1252 void BufferView::CursorToggleCB(FL_OBJECT *ob, long)
1254 BufferView *view = (BufferView*) ob->u_vdata;
1256 /* quite a nice place for asyncron Inset updating, isn't it? */
1257 // actually no! This is run even if no buffer exist... so (Lgb)
1258 if (view && !view->_buffer) {
1259 goto set_timer_and_return;
1261 #ifdef WITH_WARNINGS
1265 // On my quest to solve the gs rendre hangups I am now
1266 // disabling the SIGHUP completely, and will do a wait
1267 // now and then instead. If the guess that xforms somehow
1268 // destroys something is true, this is likely (hopefully)
1269 // to solve the problem...at least I hope so. Lgb
1271 // ...Ok this seems to work...at least it does not make things
1272 // worse so far. However I still see gs processes that hangs.
1273 // I would really like to know _why_ they are hanging. Anyway
1274 // the solution without the SIGCHLD handler seems to be easier
1277 // When attaching gdb to a a running gs that hangs it shows
1278 // that it is waiting for input(?) Is it possible for us to
1279 // provide that input somehow? Or figure what it is expecing
1282 // One solution is to, after some time, look if there are some
1283 // old gs processes still running and if there are: kill them
1286 // Another solution is to provide the user an option to rerender
1287 // a picture. This would, for the picture in question, check if
1288 // there is a gs running for it, if so kill it, and start a new
1289 // rendering process.
1291 // these comments posted to lyx@via
1295 int pid = waitpid((pid_t)0, &status, WNOHANG);
1296 if (pid == -1) // error find out what is wrong
1297 ; // ignore it for now.
1299 sigchldhandler(pid, &status);
1302 if (InsetUpdateList)
1303 UpdateInsetUpdateList();
1305 if (view && !view->screen){
1306 goto set_timer_and_return;
1309 if (view->lyx_focus && view->work_area_focus) {
1310 if (!view->_buffer->the_locking_inset){
1311 view->screen->CursorToggle();
1313 view->_buffer->the_locking_inset->
1314 ToggleInsetCursor();
1316 goto set_timer_and_return;
1318 // Make sure that the cursor is visible.
1319 if (!view->_buffer->the_locking_inset){
1320 view->screen->ShowCursor();
1322 if (!view->_buffer->the_locking_inset->isCursorVisible())
1323 view->_buffer->the_locking_inset->
1324 ToggleInsetCursor();
1327 // This is only run when work_area_focus or lyx_focus is false.
1330 XGetInputFocus(fl_display, &tmpwin, &tmp);
1331 lyxerr.debug(string("tmpwin: ") + tostr(tmpwin));
1332 lyxerr.debug(string("window: ")
1333 + tostr(view->_owner->getForm()->window));
1334 lyxerr.debug(string("work_area_focus: ")
1335 + tostr(view->work_area_focus));
1336 lyxerr.debug(string("lyx_focus : ")
1337 + tostr(view->lyx_focus));
1338 if (tmpwin != view->_owner->getForm()->window) {
1339 view->lyx_focus = false;
1342 view->lyx_focus = true;
1343 if (!view->work_area_focus)
1346 goto set_timer_and_return;
1350 set_timer_and_return:
1351 fl_set_timer(ob, 0.4);
1357 int BufferView::WorkAreaSelectionNotify(FL_OBJECT *, Window win,
1358 int /*w*/, int /*h*/, XEvent *event, void */*d*/)
1360 if (_buffer == 0) return 0;
1361 if (event->type != SelectionNotify)
1367 unsigned char* uc = 0;
1369 screen->HideCursor();
1371 if (event->xselection.type == XA_STRING
1372 && event->xselection.property) {
1374 if (XGetWindowProperty(
1375 fl_display /* display */,
1377 event->xselection.property /* property */,
1378 0 /* long_offset */,
1379 0 /* long_length */,
1381 XA_STRING /* req_type */,
1382 &tmpatom /* actual_type_return */,
1383 &tmpint /* actual_format_return */,
1384 &ul1 /* nitems_return */,
1385 &ul2 /* bytes_after_return */,
1386 &uc /* prop_return */
1397 if (XGetWindowProperty(
1398 fl_display /* display */,
1400 event->xselection.property /* property */,
1401 0 /* long_offset */,
1402 ul2/4+1 /* long_length */,
1404 XA_STRING /* req_type */,
1405 &tmpatom /* actual_type_return */,
1406 &tmpint /* actual_format_return */,
1407 &ul1 /* nitems_return */,
1408 &ul2 /* bytes_after_return */,
1409 &uc /* prop_return */
1418 InsertStringA((char*)uc);
1421 InsertStringB((char*)uc);
1432 void BufferView::cursorPrevious()
1434 if (!currentBuffer()->text->cursor.row->previous) return;
1436 long y = getScreen()->first;
1437 Row* cursorrow = currentBuffer()->text->cursor.row;
1438 currentBuffer()->text->
1439 SetCursorFromCoordinates(currentBuffer()->text->
1442 currentBuffer()->text->FinishUndo();
1443 /* this is to allow jumping over large insets */
1444 if ((cursorrow == currentBuffer()->text->cursor.row))
1445 currentBuffer()->text->CursorUp();
1447 if (currentBuffer()->text->cursor.row->height < work_area->h)
1448 getScreen()->Draw(currentBuffer()->text->cursor.y
1449 - currentBuffer()->text->cursor.row->baseline
1450 + currentBuffer()->text->cursor.row->height
1451 - work_area->h +1 );
1455 void BufferView::cursorNext()
1457 if (!currentBuffer()->text->cursor.row->next) return;
1459 long y = getScreen()->first;
1460 currentBuffer()->text->GetRowNearY(y);
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->CursorDown();
1471 if (currentBuffer()->text->cursor.row->height < work_area->h)
1472 getScreen()->Draw(currentBuffer()->text->cursor.y
1473 - currentBuffer()->text->cursor.row->baseline);
1477 bool BufferView::available() const
1479 if (_buffer && _buffer->text) return true;
1484 void BufferView::savePosition()
1486 backstack->push(currentBuffer()->getFileName(),
1487 currentBuffer()->text->cursor.x,
1488 currentBuffer()->text->cursor.y);
1492 void BufferView::restorePosition()
1495 string fname = backstack->pop(&x, &y);
1498 Buffer *b = (bufferlist.exists(fname)) ? bufferlist.getBuffer(fname):
1499 bufferlist.loadLyXFile(fname); // don't ask, just load it
1501 currentBuffer()->text->SetCursorFromCoordinates(x, y);
1502 currentBuffer()->update(0);