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"
45 extern BufferList bufferlist;
46 void sigchldhandler(pid_t pid, int * status);
48 extern void SetXtermCursor(Window win);
49 extern bool input_prohibited;
50 extern bool selection_possible;
51 extern void BeforeChange();
52 extern char ascii_type;
53 extern int UnlockInset(UpdatableInset * inset);
54 extern void ToggleFloat();
55 extern void MenuPasteSelection(char at);
56 extern InsetUpdateStruct * InsetUpdateList;
57 extern void UpdateInsetUpdateList();
58 extern void FreeUpdateTimer();
60 // This is _very_ temporary
61 FL_OBJECT * figinset_canvas;
63 BufferView::BufferView(LyXView * o, int xpos, int ypos,
64 int width, int height)
76 current_scrollbar_value = 0;
77 create_view(xpos, ypos, width, height);
78 // Activate the timer for the cursor
79 fl_set_timer(timer_cursor, 0.4);
80 fl_set_focus_object(owner_->getForm(), work_area);
81 work_area_focus = true;
86 BufferView::~BufferView()
92 void BufferView::buffer(Buffer * b)
94 lyxerr[Debug::INFO] << "Setting buffer in BufferView" << endl;
96 buffer_->InsetSleep();
97 buffer_->delUser(this);
102 // Set current buffer
105 if (bufferlist.getState() == BufferList::CLOSING) return;
108 // screen is always deleted when the buffer is changed.
113 // If we are closing the buffer, use the first buffer as current
115 buffer_ = bufferlist.first();
119 lyxerr[Debug::INFO] << " Buffer addr: " << buffer_ << endl;
120 buffer_->addUser(this);
121 owner_->getMenus()->showMenus();
122 // If we don't have a text object for this, we make one
124 resizeCurrentBuffer();
129 screen->first = screen->TopCursorVisible();
131 updateAllVisibleBufferRelatedPopups();
132 buffer_->InsetWakeup();
134 lyxerr[Debug::INFO] << " No Buffer!" << endl;
135 owner_->getMenus()->hideMenus();
137 fl_redraw_object(work_area);
139 // should update layoutchoice even if we don't have a buffer.
140 owner_->updateLayoutChoice();
141 owner_->getMiniBuffer()->Init();
142 owner_->updateWindowTitle();
146 void BufferView::updateScreen()
148 // Regenerate the screen.
151 screen = new LyXScreen(FL_ObjWin(work_area),
160 void BufferView::resize()
162 // This will resize the buffer. (Asger)
164 resizeCurrentBuffer();
168 static bool lgb_hack = false;
170 void BufferView::redraw()
172 lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
174 fl_redraw_object(work_area);
175 fl_redraw_object(scrollbar);
176 fl_redraw_object(button_down);
177 fl_redraw_object(button_up);
182 void BufferView::fitCursor()
184 if (screen) screen->FitCursor();
188 void BufferView::update()
190 if (screen) screen->Update();
194 void BufferView::updateScrollbar()
196 /* If the text is smaller than the working area, the scrollbar
197 * maximum must be the working area height. No scrolling will
201 fl_set_slider_value(scrollbar, 0);
202 fl_set_slider_size(scrollbar, scrollbar->h);
206 static long max2 = 0;
207 static long height2 = 0;
215 cbsf = screen->first;
217 // check if anything has changed.
219 height2 == work_area->h &&
220 current_scrollbar_value == cbsf)
224 height2 = work_area->h;
225 current_scrollbar_value = cbsf;
227 if (cbth <= height2) { // text is smaller than screen
228 fl_set_slider_size(scrollbar, scrollbar->h);
232 long maximum_height = work_area->h * 3 / 4 + cbth;
236 double hfloat = work_area->h;
237 double maxfloat = maximum_height;
239 fl_set_slider_value(scrollbar, value);
240 fl_set_slider_bounds(scrollbar, 0,
241 maximum_height - work_area->h);
243 double lineh = text->DefaultHeight();
244 fl_set_slider_increment(scrollbar, work_area->h-lineh, lineh);
247 if ((hfloat / maxfloat) * float(height2) < 3)
248 fl_set_slider_size(scrollbar,
251 fl_set_slider_size(scrollbar,
254 fl_set_slider_size(scrollbar, hfloat);
255 fl_set_slider_precision(scrollbar, 0);
259 void BufferView::redoCurrentBuffer()
261 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
262 if (buffer_ && text) {
264 owner_->updateLayoutChoice();
269 int BufferView::resizeCurrentBuffer()
271 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
273 LyXParagraph * par = 0;
274 LyXParagraph * selstartpar = 0;
275 LyXParagraph * selendpar = 0;
284 owner_->getMiniBuffer()->Set(_("Formatting document..."));
287 par = text->cursor.par;
288 pos = text->cursor.pos;
289 selstartpar = text->sel_start_cursor.par;
290 selstartpos = text->sel_start_cursor.pos;
291 selendpar = text->sel_end_cursor.par;
292 selendpos = text->sel_end_cursor.pos;
293 selection = text->selection;
294 mark_set = text->mark_set;
297 text = new LyXText(work_area->w, buffer_);
302 text->selection = true;
303 /* at this point just to avoid the Delete-Empty-Paragraph
304 * Mechanism when setting the cursor */
305 text->mark_set = mark_set;
307 text->SetCursor(selstartpar, selstartpos);
308 text->sel_cursor = text->cursor;
309 text->SetCursor(selendpar, selendpos);
310 text->SetSelection();
311 text->SetCursor(par, pos);
313 text->SetCursor(par, pos);
314 text->sel_cursor = text->cursor;
315 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 (!text->GotoNextError()) {
346 || text->cursor.par != text->FirstParagraph()) {
348 text->cursor.par = text->FirstParagraph();
349 text->cursor.pos = 0;
350 if (!text->GotoNextError()) {
352 owner_->getMiniBuffer()
353 ->Set(_("No more errors"));
357 owner_->getMiniBuffer()->Set(_("No more errors"));
362 text->sel_cursor = text->cursor;
367 // Just a bunch of C wrappers around static members of BufferView
368 void C_BufferView_UpCB(FL_OBJECT * ob, long buf)
370 BufferView::UpCB(ob, buf);
374 void C_BufferView_DownCB(FL_OBJECT * ob, long buf)
376 BufferView::DownCB(ob, buf);
380 void C_BufferView_ScrollCB(FL_OBJECT * ob, long buf)
382 BufferView::ScrollCB(ob, buf);
386 void C_BufferView_CursorToggleCB(FL_OBJECT * ob, long buf)
388 BufferView::CursorToggleCB(ob, buf);
392 int C_BufferView_work_area_handler(FL_OBJECT * ob, int event,
396 return BufferView::work_area_handler(ob, event,
402 void BufferView::create_view(int xpos, int ypos, int width, int height)
405 const int bw = abs(fl_get_border_width());
407 // a hack for the figinsets (Matthias)
408 // This one first, then it will probably be invisible. (Lgb)
409 ::figinset_canvas = figinset_canvas = obj =
410 fl_add_canvas(FL_NORMAL_CANVAS,
413 fl_set_object_boxtype(obj, FL_NO_BOX);
414 fl_set_object_resize(obj, FL_RESIZE_ALL);
415 fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
418 obj = fl_add_box(FL_BORDER_BOX, xpos, ypos,
421 fl_set_object_resize(obj, FL_RESIZE_ALL);
422 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
425 work_area = obj = fl_add_free(FL_INPUT_FREE,
427 width-15-2*bw /* scrollbarwidth */,
429 C_BufferView_work_area_handler);
430 obj->wantkey = FL_KEY_TAB;
431 obj->u_vdata = this; /* This is how we pass the BufferView
432 to the work_area_handler. */
433 fl_set_object_boxtype(obj, FL_DOWN_BOX);
434 fl_set_object_resize(obj, FL_RESIZE_ALL);
435 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
441 // up - scrollbar button
443 fl_set_border_width(-1);
445 fl_set_border_width(-2); // to get visible feedback
447 button_up = 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, NorthEastGravity, NorthEastGravity);
455 fl_set_object_callback(obj, C_BufferView_UpCB, 0);
457 fl_set_pixmapbutton_data(obj, const_cast<char**>(up_xpm));
460 // Remove the blue feedback rectangle
461 fl_set_pixmapbutton_focus_outline(obj, 0);
464 // the scrollbar slider
465 fl_set_border_width(-bw);
466 scrollbar = obj = fl_add_slider(FL_VERT_SLIDER,
470 fl_set_object_color(obj, FL_COL1, FL_MCOL);
471 fl_set_object_boxtype(obj, FL_UP_BOX);
472 fl_set_object_resize(obj, FL_RESIZE_ALL);
473 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
474 fl_set_object_callback(obj, C_BufferView_ScrollCB, 0);
477 // down - scrollbar button
479 fl_set_border_width(-1);
481 fl_set_border_width(-2); // to get visible feedback
483 button_down = obj = fl_add_pixmapbutton(FL_TOUCH_BUTTON,
487 fl_set_object_boxtype(obj, FL_UP_BOX);
488 fl_set_object_color(obj, FL_MCOL, FL_BLUE);
489 fl_set_object_resize(obj, FL_RESIZE_ALL);
490 fl_set_object_gravity(obj, SouthEastGravity, SouthEastGravity);
491 fl_set_object_callback(obj, C_BufferView_DownCB, 0);
493 fl_set_pixmapbutton_data(obj, const_cast<char**>(down_xpm));
494 fl_set_border_width(-bw);
497 // Remove the blue feedback rectangle
498 fl_set_pixmapbutton_focus_outline(obj, 0);
506 timer_cursor = obj = fl_add_timer(FL_HIDDEN_TIMER,
507 0, 0, 0, 0, "Timer");
508 fl_set_object_callback(obj, C_BufferView_CursorToggleCB, 0);
513 // Callback for scrollbar up button
514 void BufferView::UpCB(FL_OBJECT * ob, long)
516 BufferView * view = static_cast<BufferView*>(ob->u_vdata);
518 if (view->buffer_ == 0) return;
520 static long time = 0;
521 XEvent const * ev2 = fl_last_event();
522 if (ev2->type == ButtonPress || ev2->type == ButtonRelease)
524 int button = fl_get_button_numb(ob);
527 view->ScrollUpOnePage(time++); break;
529 view->ScrollDownOnePage(time++); break;
531 view->ScrollUp(time++); break;
543 a = XInternAtom(fl_display, "WAIT_FOR_X", False);
546 XSetWindowAttributes attr;
547 mask = CWOverrideRedirect;
548 attr.override_redirect = 1;
549 w = XCreateWindow(fl_display, fl_root,
550 0, 0, 1, 1, 0, CopyFromParent,
551 InputOnly, CopyFromParent, mask, &attr);
552 XSelectInput(fl_display, w, PropertyChangeMask);
553 XMapWindow(fl_display, w);
556 XChangeProperty(fl_display, w, a, a, 8,
558 reinterpret_cast<unsigned char*>(""), 0);
559 XWindowEvent(fl_display, w, PropertyChangeMask, &ev);
561 XSync(fl_get_display(), 0);
565 // Callback for scrollbar slider
566 void BufferView::ScrollCB(FL_OBJECT * ob, long)
568 BufferView * view = static_cast<BufferView*>(ob->u_vdata);
569 extern bool cursor_follows_scrollbar;
571 if (view->buffer_ == 0) return;
573 view->current_scrollbar_value = long(fl_get_slider_value(ob));
574 if (view->current_scrollbar_value < 0)
575 view->current_scrollbar_value = 0;
580 view->screen->Draw(view->current_scrollbar_value);
582 if (cursor_follows_scrollbar) {
583 LyXText * vbt = view->text;
584 int height = vbt->DefaultHeight();
586 if (vbt->cursor.y < view->screen->first + height) {
587 vbt->SetCursorFromCoordinates(0,
588 view->screen->first +
591 else if (vbt->cursor.y >
592 view->screen->first + view->work_area->h - height) {
593 vbt->SetCursorFromCoordinates(0,
594 view->screen->first +
603 // Callback for scrollbar down button
604 void BufferView::DownCB(FL_OBJECT * ob, long)
606 BufferView * view = static_cast<BufferView*>(ob->u_vdata);
608 if (view->buffer_ == 0) return;
611 static long time = 0;
612 ev2 = fl_last_event();
613 if (ev2->type == ButtonPress || ev2->type == ButtonRelease)
615 int button = fl_get_button_numb(ob);
618 view->ScrollUpOnePage(time++); break;
620 view->ScrollDownOnePage(time++); break;
622 view->ScrollDown(time++); break;
627 int BufferView::ScrollUp(long time)
629 if (buffer_ == 0) return 0;
630 if (!screen) return 0;
632 double value = fl_get_slider_value(scrollbar);
634 if (value == 0) return 0;
636 float add_value = (text->DefaultHeight()
637 + float(time) * float(time) * 0.125);
639 if (add_value > work_area->h)
640 add_value = float(work_area->h -
641 text->DefaultHeight());
648 fl_set_slider_value(scrollbar, value);
650 ScrollCB(scrollbar, 0);
655 int BufferView::ScrollDown(long time)
657 if (buffer_ == 0) return 0;
658 if (!screen) return 0;
660 double value= fl_get_slider_value(scrollbar);
662 fl_get_slider_bounds(scrollbar, &min, &max);
664 if (value == max) return 0;
666 float add_value = (text->DefaultHeight()
667 + float(time) * float(time) * 0.125);
669 if (add_value > work_area->h)
670 add_value = float(work_area->h -
671 text->DefaultHeight());
678 fl_set_slider_value(scrollbar, value);
680 ScrollCB(scrollbar, 0);
685 void BufferView::ScrollUpOnePage(long /*time*/)
687 if (buffer_ == 0) return;
690 long y = screen->first;
694 Row * row = text->GetRowNearY(y);
696 y = y - work_area->h + row->height;
698 fl_set_slider_value(scrollbar, y);
700 ScrollCB(scrollbar, 0);
704 void BufferView::ScrollDownOnePage(long /*time*/)
706 if (buffer_ == 0) return;
710 fl_get_slider_bounds(scrollbar, &min, &max);
711 long y = screen->first;
713 if (y > text->height - work_area->h)
717 text->GetRowNearY(y);
719 fl_set_slider_value(scrollbar, y);
721 ScrollCB(scrollbar, 0);
725 int BufferView::work_area_handler(FL_OBJECT * ob, int event,
727 int /*key*/, void * xev)
729 static int x_old = -1;
730 static int y_old = -1;
731 static long scrollbar_value_old = -1;
733 XEvent * ev = static_cast<XEvent*>(xev);
734 BufferView * view = static_cast<BufferView*>(ob->u_vdata);
736 // If we don't have a view yet; return
737 if (!view || quitting) return 0;
741 view->workAreaExpose();
744 view->WorkAreaButtonPress(ob, 0, 0, 0, ev, 0);
747 view->WorkAreaButtonRelease(ob, 0, 0, 0, ev, 0);
750 if (ev->xmotion.x != x_old ||
751 ev->xmotion.y != y_old ||
752 view->current_scrollbar_value != scrollbar_value_old) {
753 x_old = ev->xmotion.x;
754 y_old = ev->xmotion.y;
755 scrollbar_value_old = view->current_scrollbar_value;
756 view->WorkAreaMotionNotify(ob, 0, 0, 0, ev, 0);
759 // Done by the raw callback:
761 // WorkAreaKeyPress(ob, 0, 0, 0, ev, 0); break;
763 if (!view->owner_->getMiniBuffer()->shows_no_match)
764 view->owner_->getMiniBuffer()->Init();
765 view->owner_->getMiniBuffer()->shows_no_match = false;
766 view->work_area_focus = true;
767 fl_set_timer(view->timer_cursor, 0.4);
770 view->owner_->getMiniBuffer()->ExecCommand();
771 view->work_area_focus = false;
774 SetXtermCursor(view->owner_->getForm()->window);
776 view->lyx_focus = true;
777 fl_set_timer(view->timer_cursor, 0.4);
780 if (!input_prohibited)
781 XUndefineCursor(fl_display,
782 view->owner_->getForm()->window);
783 view->lyx_focus = false; // This is not an absolute truth
784 // but if it is not true, it will be changed within a blink
785 // of an eye. ... Not good enough... use regulare timeperiod
786 //fl_set_timer(view->timer_cursor, 0.01); // 0.1 sec blink
787 fl_set_timer(view->timer_cursor, 0.4); // 0.4 sec blink
791 if (view->buffer_ && !view->buffer_->the_locking_inset) {
792 if (view->screen && ev->xbutton.button == 1) {
793 view->screen->HideCursor();
794 view->screen->ToggleSelection();
795 view->text->SelectWord();
796 view->screen->ToggleSelection(false);
797 /* This will fit the cursor on the screen
805 if (view->buffer_ && view->screen && ev->xbutton.button == 1) {
806 view->screen->HideCursor();
807 view->screen->ToggleSelection();
808 view->text->CursorHome();
809 view->text->sel_cursor = view->text->cursor;
810 view->text->CursorEnd();
811 view->text->SetSelection();
812 view->screen->ToggleSelection(false);
813 /* This will fit the cursor on the screen
819 view->WorkAreaSelectionNotify(ob,
820 view->owner_->getForm()->window,
827 int BufferView::WorkAreaMotionNotify(FL_OBJECT *ob, Window,
828 int /*w*/, int /*h*/,
829 XEvent * ev, void */*d*/)
832 if (buffer_ == 0) return 0;
833 if (!screen) return 0;
835 // Check for inset locking
836 if (buffer_->the_locking_inset) {
837 LyXCursor cursor = text->cursor;
838 buffer_->the_locking_inset->
839 InsetMotionNotify(ev->xbutton.x - ob->x - cursor.x,
840 ev->xbutton.y - ob->y -
846 // Only use motion with button 1
847 if (!ev->xmotion.state & Button1MotionMask)
850 /* The selection possible is needed, that only motion events are
851 * used, where the bottom press event was on the drawing area too */
852 if (selection_possible) {
853 screen->HideCursor();
855 text->SetCursorFromCoordinates(ev->xbutton.x - ob->x,
856 ev->xbutton.y - ob->y +
859 if (!text->selection)
860 update(-3); // Maybe an empty line was deleted
862 text->SetSelection();
863 screen->ToggleToggle();
864 if (screen->FitCursor())
866 screen->ShowCursor();
872 extern int bibitemMaxWidth(LyXFont const &);
874 // Single-click on work area
875 int BufferView::WorkAreaButtonPress(FL_OBJECT * ob, Window,
876 int /*w*/, int /*h*/,
877 XEvent * ev, void */*d*/)
882 if (buffer_ == 0) return 0;
883 if (!screen) return 0;
885 int const x = ev->xbutton.x - ob->x;
886 int const y = ev->xbutton.y - ob->y;
887 // If we hit an inset, we have the inset coordinates in these
888 // and inset_hit points to the inset. If we do not hit an
889 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
892 Inset * inset_hit = checkInsetHit(inset_x, inset_y);
894 // ok ok, this is a hack.
895 int button = ev->xbutton.button;
896 if (button == 4 || button == 5) goto wheel;
900 if (buffer_->the_locking_inset) {
901 // We are in inset locking mode
903 /* Check whether the inset was hit. If not reset mode,
904 otherwise give the event to the inset */
905 if (inset_hit != 0) {
906 buffer_->the_locking_inset->
907 InsetButtonPress(inset_x, inset_y,
911 UnlockInset(buffer_->the_locking_inset);
915 selection_possible = true;
916 screen->HideCursor();
918 // Right button mouse click on a table
920 (text->cursor.par->table ||
921 text->MouseHitInTable(x, y + screen->first))) {
922 // Set the cursor to the press-position
923 text->SetCursorFromCoordinates(x, y + screen->first);
926 // Only show the table popup if the hit is in
928 if (!text->HitInTable(text->cursor.row, x))
931 // Hit above or below the table?
933 if (!text->selection) {
934 screen->ToggleSelection();
935 text->ClearSelection();
940 // Popup table popup when on a table.
941 // This is obviously temporary, since we
942 // should be able to popup various
943 // context-sensitive-menus with the
944 // the right mouse. So this should be done more
945 // general in the future. Matthias.
946 selection_possible = false;
948 ->Dispatch(LFUN_LAYOUT_TABLE,
954 int screen_first = screen->first;
956 // Middle button press pastes if we have a selection
957 bool paste_internally = false;
958 if (button == 2 // && !buffer_->the_locking_inset
959 && text->selection) {
960 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
961 paste_internally = true;
964 // Clear the selection
965 screen->ToggleSelection();
966 text->ClearSelection();
971 // Single left click in math inset?
972 if (inset_hit != 0 && inset_hit->Editable() == 2) {
973 // Highly editable inset, like math
974 selection_possible = false;
975 owner_->updateLayoutChoice();
976 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
977 inset_hit->Edit(inset_x, inset_y);
981 // Right click on a footnote flag opens float menu
983 selection_possible = false;
987 text->SetCursorFromCoordinates(x, y + screen_first);
989 text->sel_cursor = text->cursor;
990 text->cursor.x_fix = text->cursor.x;
992 owner_->updateLayoutChoice();
993 if (screen->FitCursor()){
995 selection_possible = false;
998 // Insert primary selection with middle mouse
999 // if there is a local selection in the current buffer,
1001 if (button == 2) { // && !buffer_->the_locking_inset){
1002 if (paste_internally)
1003 owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
1005 owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
1007 selection_possible = false;
1014 // I am not quite sure if this is the correct place to put
1015 // this, but it will not cause any harm.
1016 // Patch from Mark Huang (markman@mit.edu) to make LyX
1017 // recognise button 4 and 5. This enables LyX use use
1018 // the scrollwhell on certain mice for something useful. (Lgb)
1019 // Added wheel acceleration detection code. (Rvdk)
1020 static Time lastTime = 0;
1021 int diff = ev->xbutton.time - lastTime;
1022 int scroll = int(1.0 + (4.0 / (abs(diff) + 1.0)) * 200.0);
1031 lastTime = ev->xbutton.time;
1042 int BufferView::WorkAreaButtonRelease(FL_OBJECT * ob, Window ,
1043 int /*w*/, int /*h*/,
1044 XEvent * ev, void * /*d*/)
1046 if (buffer_ == 0 || screen == 0) return 0;
1048 int const x = ev->xbutton.x - ob->x;
1049 int const y = ev->xbutton.y - ob->y;
1051 // If we hit an inset, we have the inset coordinates in these
1052 // and inset_hit points to the inset. If we do not hit an
1053 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1056 Inset * inset_hit = checkInsetHit(inset_x, inset_y);
1058 if (buffer_->the_locking_inset) {
1059 // We are in inset locking mode.
1061 /* LyX does a kind of work-area grabbing for insets.
1062 Only a ButtonPress Event outside the inset will
1063 force a InsetUnlock. */
1064 buffer_->the_locking_inset->
1065 InsetButtonRelease(inset_x, inset_y,
1066 ev->xbutton.button);
1070 selection_possible = false;
1071 if (text->cursor.par->table) {
1073 NumberOfCell(text->cursor.par,
1075 if (text->cursor.par->table->IsContRow(cell) &&
1076 text->cursor.par->table->
1077 CellHasContRow(text->cursor.par->table->
1078 GetCellAbove(cell))<0) {
1083 if (ev->xbutton.button >= 2)
1086 // Make sure that the press was not far from the release
1087 if ((abs(last_click_x - x) >= 5) ||
1088 (abs(last_click_y - y) >= 5)) {
1092 // Did we hit an editable inset?
1093 if (inset_hit != 0) {
1094 // Inset like error, notes and figures
1095 selection_possible = false;
1096 #ifdef WITH_WARNINGS
1097 #warning fix this proper in 0.13
1099 // Following a ref shouldn't issue
1100 // a push on the undo-stack
1101 // anylonger, now that we have
1102 // keybindings for following
1103 // references and returning from
1104 // references. IMHO though, it
1105 // should be the inset's own business
1106 // to push or not push on the undo
1107 // stack. They don't *have* to
1108 // alter the document...
1110 // ...or maybe the SetCursorParUndo()
1111 // below isn't necessary at all anylonger?
1112 if (inset_hit->LyxCode() == Inset::REF_CODE) {
1113 text->SetCursorParUndo();
1116 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
1117 inset_hit->Edit(inset_x, inset_y);
1121 // check whether we want to open a float
1125 if (text->cursor.pos <
1126 text->cursor.par->Last()) {
1127 c = text->cursor.par->
1128 GetChar(text->cursor.pos);
1130 if (c == LyXParagraph::META_FOOTNOTE
1131 || c == LyXParagraph::META_MARGIN
1132 || c == LyXParagraph::META_FIG
1133 || c == LyXParagraph::META_TAB
1134 || c == LyXParagraph::META_WIDE_FIG
1135 || c == LyXParagraph::META_WIDE_TAB
1136 || c == LyXParagraph::META_ALGORITHM){
1138 } else if (text->cursor.pos - 1 >= 0) {
1139 c = text->cursor.par->
1140 GetChar(text->cursor.pos - 1);
1141 if (c == LyXParagraph::META_FOOTNOTE
1142 || c == LyXParagraph::META_MARGIN
1143 || c == LyXParagraph::META_FIG
1144 || c == LyXParagraph::META_TAB
1145 || c == LyXParagraph::META_WIDE_FIG
1146 || c == LyXParagraph::META_WIDE_TAB
1147 || c == LyXParagraph::META_ALGORITHM){
1148 // We are one step too far to the right
1155 selection_possible = false;
1160 // Do we want to close a float? (click on the float-label)
1161 if (text->cursor.row->par->footnoteflag ==
1162 LyXParagraph::OPEN_FOOTNOTE
1163 && text->cursor.pos == 0
1164 && text->cursor.row->previous &&
1165 text->cursor.row->previous->par->
1166 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
1167 LyXFont font (LyXFont::ALL_SANE);
1168 font.setSize(LyXFont::SIZE_SMALL);
1170 int box_x = 20; // LYX_PAPER_MARGIN;
1171 box_x += font.textWidth("Mwide-figM", 10);
1173 int screen_first = screen->first;
1176 && y + screen_first > text->cursor.y -
1177 text->cursor.row->baseline
1178 && y + screen_first < text->cursor.y -
1179 text->cursor.row->baseline
1180 + font.maxAscent() * 1.2 + font.maxDescent() * 1.2) {
1182 selection_possible = false;
1187 // Maybe we want to edit a bibitem ale970302
1188 if (text->cursor.par->bibkey && x < 20 +
1189 bibitemMaxWidth(textclasslist
1190 .TextClass(buffer_->
1191 params.textclass).defaultfont())) {
1192 text->cursor.par->bibkey->Edit(0, 0);
1200 * Returns an inset if inset was hit. 0 otherwise.
1201 * If hit, the coordinates are changed relative to the inset.
1202 * Otherwise coordinates are not changed, and false is returned.
1204 Inset * BufferView::checkInsetHit(int & x, int & y)
1209 int y_tmp = y + getScreen()->first;
1211 LyXCursor cursor = text->cursor;
1212 if (cursor.pos < cursor.par->Last()
1213 && cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET
1214 && cursor.par->GetInset(cursor.pos)
1215 && cursor.par->GetInset(cursor.pos)->Editable()) {
1217 // Check whether the inset really was hit
1218 Inset * tmpinset = cursor.par->GetInset(cursor.pos);
1219 LyXFont font = text->GetFont(cursor.par, cursor.pos);
1221 && x < cursor.x + tmpinset->Width(font)
1222 && y_tmp > cursor.y - tmpinset->Ascent(font)
1223 && y_tmp < cursor.y + tmpinset->Descent(font)) {
1225 // The origin of an inset is on the baseline
1226 y = y_tmp - (cursor.y);
1229 } else if (cursor.pos - 1 >= 0
1230 && cursor.par->GetChar(cursor.pos - 1) == LyXParagraph::META_INSET
1231 && cursor.par->GetInset(cursor.pos - 1)
1232 && cursor.par->GetInset(cursor.pos - 1)->Editable()) {
1234 Inset * result = checkInsetHit(x, y);
1236 text->CursorRight();
1246 int BufferView::workAreaExpose()
1248 if (!work_area || !work_area->form->visible)
1251 // this is a hack to ensure that we only call this through
1252 // BufferView::redraw().
1257 static int work_area_width = work_area->w;
1258 static int work_area_height = work_area->h;
1260 bool widthChange = work_area->w != work_area_width;
1261 bool heightChange = work_area->h != work_area_height;
1263 // update from work area
1264 work_area_width = work_area->w;
1265 work_area_height = work_area->h;
1268 // All buffers need a resize
1269 bufferlist.resize();
1270 } else if (heightChange) {
1271 // Rebuild image of current screen
1273 // fitCursor() ensures we don't jump back
1274 // to the start of the document on vertical
1278 // The main window size has changed, repaint most stuff
1280 // ...including the minibuffer
1281 owner_->getMiniBuffer()->Init();
1283 } else if (screen) screen->Redraw();
1285 // Grey box when we don't have a buffer
1286 fl_winset(FL_ObjWin(work_area));
1287 fl_rectangle(1, work_area->x, work_area->y,
1288 work_area->w, work_area->h, FL_GRAY63);
1291 // always make sure that the scrollbar is sane.
1293 owner_->updateLayoutChoice();
1298 // Callback for cursor timer
1299 void BufferView::CursorToggleCB(FL_OBJECT * ob, long)
1301 BufferView * view = static_cast<BufferView*>(ob->u_vdata);
1303 /* quite a nice place for asyncron Inset updating, isn't it? */
1304 // actually no! This is run even if no buffer exist... so (Lgb)
1305 if (view && !view->buffer_) {
1306 goto set_timer_and_return;
1310 // On my quest to solve the gs rendre hangups I am now
1311 // disabling the SIGHUP completely, and will do a wait
1312 // now and then instead. If the guess that xforms somehow
1313 // destroys something is true, this is likely (hopefully)
1314 // to solve the problem...at least I hope so. Lgb
1316 // ...Ok this seems to work...at least it does not make things
1317 // worse so far. However I still see gs processes that hangs.
1318 // I would really like to know _why_ they are hanging. Anyway
1319 // the solution without the SIGCHLD handler seems to be easier
1322 // When attaching gdb to a a running gs that hangs it shows
1323 // that it is waiting for input(?) Is it possible for us to
1324 // provide that input somehow? Or figure what it is expecing
1327 // One solution is to, after some time, look if there are some
1328 // old gs processes still running and if there are: kill them
1331 // Another solution is to provide the user an option to rerender
1332 // a picture. This would, for the picture in question, check if
1333 // there is a gs running for it, if so kill it, and start a new
1334 // rendering process.
1336 // these comments posted to lyx@via
1339 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1340 if (pid == -1) // error find out what is wrong
1341 ; // ignore it for now.
1343 sigchldhandler(pid, &status);
1345 if (InsetUpdateList)
1346 UpdateInsetUpdateList();
1348 if (view && !view->screen){
1349 goto set_timer_and_return;
1352 if (view->lyx_focus && view->work_area_focus) {
1353 if (!view->buffer_->the_locking_inset){
1354 view->screen->CursorToggle();
1356 view->buffer_->the_locking_inset->
1357 ToggleInsetCursor();
1359 goto set_timer_and_return;
1361 // Make sure that the cursor is visible.
1362 if (!view->buffer_->the_locking_inset){
1363 view->screen->ShowCursor();
1365 if (!view->buffer_->the_locking_inset->isCursorVisible())
1366 view->buffer_->the_locking_inset->
1367 ToggleInsetCursor();
1370 // This is only run when work_area_focus or lyx_focus is false.
1373 XGetInputFocus(fl_display, &tmpwin, &tmp);
1374 if (lyxerr.debugging(Debug::INFO)) {
1375 lyxerr << "tmpwin: " << tmpwin
1376 << "\nwindow: " << view->owner_->getForm()->window
1377 << "\nwork_area_focus: " << view->work_area_focus
1378 << "\nlyx_focus : " << view->lyx_focus
1381 if (tmpwin != view->owner_->getForm()->window) {
1382 view->lyx_focus = false;
1385 view->lyx_focus = true;
1386 if (!view->work_area_focus)
1389 goto set_timer_and_return;
1393 set_timer_and_return:
1394 fl_set_timer(ob, 0.4);
1400 int BufferView::WorkAreaSelectionNotify(FL_OBJECT *, Window win,
1401 int /*w*/, int /*h*/,
1402 XEvent * event, void */*d*/)
1404 if (buffer_ == 0) return 0;
1405 if (event->type != SelectionNotify)
1411 unsigned char * uc = 0;
1413 screen->HideCursor();
1415 if (event->xselection.type == XA_STRING
1416 && event->xselection.property) {
1418 if (XGetWindowProperty(
1419 fl_display /* display */,
1421 event->xselection.property /* property */,
1422 0 /* long_offset */,
1423 0 /* long_length */,
1425 XA_STRING /* req_type */,
1426 &tmpatom /* actual_type_return */,
1427 &tmpint /* actual_format_return */,
1428 &ul1 /* nitems_return */,
1429 &ul2 /* bytes_after_return */,
1430 &uc /* prop_return */
1441 if (XGetWindowProperty(
1442 fl_display /* display */,
1444 event->xselection.property /* property */,
1445 0 /* long_offset */,
1446 ul2/4+1 /* long_length */,
1448 XA_STRING /* req_type */,
1449 &tmpatom /* actual_type_return */,
1450 &tmpint /* actual_format_return */,
1451 &ul1 /* nitems_return */,
1452 &ul2 /* bytes_after_return */,
1453 &uc /* prop_return */
1461 text->InsertStringA(reinterpret_cast<char*>(uc));
1463 text->InsertStringB(reinterpret_cast<char*>(uc));
1475 void BufferView::cursorPrevious()
1477 if (!text->cursor.row->previous) return;
1479 long y = getScreen()->first;
1480 Row * cursorrow = text->cursor.row;
1481 text->SetCursorFromCoordinates(text->cursor.x_fix, y);
1483 // this is to allow jumping over large insets
1484 if ((cursorrow == text->cursor.row))
1487 if (text->cursor.row->height < work_area->h)
1488 getScreen()->Draw(text->cursor.y
1489 - text->cursor.row->baseline
1490 + text->cursor.row->height
1491 - work_area->h +1 );
1495 void BufferView::cursorNext()
1497 if (!text->cursor.row->next) return;
1499 long y = getScreen()->first;
1500 text->GetRowNearY(y);
1501 Row * cursorrow = text->cursor.row;
1502 text->SetCursorFromCoordinates(text->cursor.x_fix, y + work_area->h);
1504 /* this is to allow jumping over large insets */
1505 if ((cursorrow == text->cursor.row))
1508 if (text->cursor.row->height < work_area->h)
1509 getScreen()->Draw(text->cursor.y
1510 - text->cursor.row->baseline);
1514 bool BufferView::available() const
1516 if (buffer_ && text) return true;
1521 void BufferView::savePosition()
1523 backstack.push(buffer()->fileName(),
1529 void BufferView::restorePosition()
1531 if (backstack.empty()) return;
1534 string fname = backstack.pop(&x, &y);
1537 Buffer * b = bufferlist.exists(fname) ?
1538 bufferlist.getBuffer(fname) :
1539 bufferlist.loadLyXFile(fname); // don't ask, just load it
1541 text->SetCursorFromCoordinates(x, y);
1546 // candidate for move to BufferView
1547 void BufferView::update(signed char f)
1549 owner()->updateLayoutChoice();
1551 if (!text->selection && f > -3)
1552 text->sel_cursor = text->cursor;
1555 text->FullRebreak();
1559 if (f != 3 && f != -3) {
1564 if (f == 1 || f == -1) {
1565 if (buffer()->isLyxClean()) {
1566 buffer()->markDirty();
1567 owner()->getMiniBuffer()->setTimer(4);
1569 buffer()->markDirty();