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 int const 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,
426 xpos + bw, ypos + bw,
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,
469 15, height - 30, "");
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*/)
831 if (buffer_ == 0) return 0;
832 if (!screen) return 0;
834 // Check for inset locking
835 if (buffer_->the_locking_inset) {
836 LyXCursor cursor = text->cursor;
837 buffer_->the_locking_inset->
838 InsetMotionNotify(ev->xbutton.x - ob->x - cursor.x,
839 ev->xbutton.y - ob->y -
845 // Only use motion with button 1
846 if (!ev->xmotion.state & Button1MotionMask)
849 /* The selection possible is needed, that only motion events are
850 * used, where the bottom press event was on the drawing area too */
851 if (selection_possible) {
852 screen->HideCursor();
854 text->SetCursorFromCoordinates(ev->xbutton.x - ob->x,
855 ev->xbutton.y - ob->y +
858 if (!text->selection)
859 update(-3); // Maybe an empty line was deleted
861 text->SetSelection();
862 screen->ToggleToggle();
863 if (screen->FitCursor())
865 screen->ShowCursor();
871 extern int bibitemMaxWidth(LyXFont const &);
873 // Single-click on work area
874 int BufferView::WorkAreaButtonPress(FL_OBJECT * ob, Window,
875 int /*w*/, int /*h*/,
876 XEvent * ev, void */*d*/)
881 if (buffer_ == 0) return 0;
882 if (!screen) return 0;
884 int const x = ev->xbutton.x - ob->x;
885 int const y = ev->xbutton.y - ob->y;
886 // If we hit an inset, we have the inset coordinates in these
887 // and inset_hit points to the inset. If we do not hit an
888 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
891 Inset * inset_hit = checkInsetHit(inset_x, inset_y);
893 // ok ok, this is a hack.
894 int button = ev->xbutton.button;
895 if (button == 4 || button == 5) goto wheel;
899 if (buffer_->the_locking_inset) {
900 // We are in inset locking mode
902 /* Check whether the inset was hit. If not reset mode,
903 otherwise give the event to the inset */
904 if (inset_hit != 0) {
905 buffer_->the_locking_inset->
906 InsetButtonPress(inset_x, inset_y,
910 UnlockInset(buffer_->the_locking_inset);
914 selection_possible = true;
915 screen->HideCursor();
917 // Right button mouse click on a table
919 (text->cursor.par->table ||
920 text->MouseHitInTable(x, y + screen->first))) {
921 // Set the cursor to the press-position
922 text->SetCursorFromCoordinates(x, y + screen->first);
925 // Only show the table popup if the hit is in
927 if (!text->HitInTable(text->cursor.row, x))
930 // Hit above or below the table?
932 if (!text->selection) {
933 screen->ToggleSelection();
934 text->ClearSelection();
939 // Popup table popup when on a table.
940 // This is obviously temporary, since we
941 // should be able to popup various
942 // context-sensitive-menus with the
943 // the right mouse. So this should be done more
944 // general in the future. Matthias.
945 selection_possible = false;
947 ->Dispatch(LFUN_LAYOUT_TABLE,
953 int screen_first = screen->first;
955 // Middle button press pastes if we have a selection
956 bool paste_internally = false;
957 if (button == 2 // && !buffer_->the_locking_inset
958 && text->selection) {
959 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
960 paste_internally = true;
963 // Clear the selection
964 screen->ToggleSelection();
965 text->ClearSelection();
970 // Single left click in math inset?
971 if (inset_hit != 0 && inset_hit->Editable() == 2) {
972 // Highly editable inset, like math
973 selection_possible = false;
974 owner_->updateLayoutChoice();
975 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
976 inset_hit->Edit(inset_x, inset_y);
980 // Right click on a footnote flag opens float menu
982 selection_possible = false;
986 text->SetCursorFromCoordinates(x, y + screen_first);
988 text->sel_cursor = text->cursor;
989 text->cursor.x_fix = text->cursor.x;
991 owner_->updateLayoutChoice();
992 if (screen->FitCursor()){
994 selection_possible = false;
997 // Insert primary selection with middle mouse
998 // if there is a local selection in the current buffer,
1000 if (button == 2) { // && !buffer_->the_locking_inset){
1001 if (paste_internally)
1002 owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
1004 owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
1006 selection_possible = false;
1013 // I am not quite sure if this is the correct place to put
1014 // this, but it will not cause any harm.
1015 // Patch from Mark Huang (markman@mit.edu) to make LyX
1016 // recognise button 4 and 5. This enables LyX use use
1017 // the scrollwhell on certain mice for something useful. (Lgb)
1018 // Added wheel acceleration detection code. (Rvdk)
1019 static Time lastTime = 0;
1020 int diff = ev->xbutton.time - lastTime;
1021 int scroll = int(1.0 + (4.0 / (abs(diff) + 1.0)) * 200.0);
1030 lastTime = ev->xbutton.time;
1041 int BufferView::WorkAreaButtonRelease(FL_OBJECT * ob, Window ,
1042 int /*w*/, int /*h*/,
1043 XEvent * ev, void * /*d*/)
1045 if (buffer_ == 0 || screen == 0) return 0;
1047 int const x = ev->xbutton.x - ob->x;
1048 int const y = ev->xbutton.y - ob->y;
1050 // If we hit an inset, we have the inset coordinates in these
1051 // and inset_hit points to the inset. If we do not hit an
1052 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1055 Inset * inset_hit = checkInsetHit(inset_x, inset_y);
1057 if (buffer_->the_locking_inset) {
1058 // We are in inset locking mode.
1060 /* LyX does a kind of work-area grabbing for insets.
1061 Only a ButtonPress Event outside the inset will
1062 force a InsetUnlock. */
1063 buffer_->the_locking_inset->
1064 InsetButtonRelease(inset_x, inset_y,
1065 ev->xbutton.button);
1069 selection_possible = false;
1070 if (text->cursor.par->table) {
1072 NumberOfCell(text->cursor.par,
1074 if (text->cursor.par->table->IsContRow(cell) &&
1075 text->cursor.par->table->
1076 CellHasContRow(text->cursor.par->table->
1077 GetCellAbove(cell))<0) {
1082 if (ev->xbutton.button >= 2)
1085 // Make sure that the press was not far from the release
1086 if ((abs(last_click_x - x) >= 5) ||
1087 (abs(last_click_y - y) >= 5)) {
1091 // Did we hit an editable inset?
1092 if (inset_hit != 0) {
1093 // Inset like error, notes and figures
1094 selection_possible = false;
1095 #ifdef WITH_WARNINGS
1096 #warning fix this proper in 0.13
1098 // Following a ref shouldn't issue
1099 // a push on the undo-stack
1100 // anylonger, now that we have
1101 // keybindings for following
1102 // references and returning from
1103 // references. IMHO though, it
1104 // should be the inset's own business
1105 // to push or not push on the undo
1106 // stack. They don't *have* to
1107 // alter the document...
1109 // ...or maybe the SetCursorParUndo()
1110 // below isn't necessary at all anylonger?
1111 if (inset_hit->LyxCode() == Inset::REF_CODE) {
1112 text->SetCursorParUndo();
1115 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
1116 inset_hit->Edit(inset_x, inset_y);
1120 // check whether we want to open a float
1124 if (text->cursor.pos <
1125 text->cursor.par->Last()) {
1126 c = text->cursor.par->
1127 GetChar(text->cursor.pos);
1129 if (c == LyXParagraph::META_FOOTNOTE
1130 || c == LyXParagraph::META_MARGIN
1131 || c == LyXParagraph::META_FIG
1132 || c == LyXParagraph::META_TAB
1133 || c == LyXParagraph::META_WIDE_FIG
1134 || c == LyXParagraph::META_WIDE_TAB
1135 || c == LyXParagraph::META_ALGORITHM){
1137 } else if (text->cursor.pos - 1 >= 0) {
1138 c = text->cursor.par->
1139 GetChar(text->cursor.pos - 1);
1140 if (c == LyXParagraph::META_FOOTNOTE
1141 || c == LyXParagraph::META_MARGIN
1142 || c == LyXParagraph::META_FIG
1143 || c == LyXParagraph::META_TAB
1144 || c == LyXParagraph::META_WIDE_FIG
1145 || c == LyXParagraph::META_WIDE_TAB
1146 || c == LyXParagraph::META_ALGORITHM){
1147 // We are one step too far to the right
1154 selection_possible = false;
1159 // Do we want to close a float? (click on the float-label)
1160 if (text->cursor.row->par->footnoteflag ==
1161 LyXParagraph::OPEN_FOOTNOTE
1162 && text->cursor.pos == 0
1163 && text->cursor.row->previous &&
1164 text->cursor.row->previous->par->
1165 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
1166 LyXFont font (LyXFont::ALL_SANE);
1167 font.setSize(LyXFont::SIZE_SMALL);
1169 int box_x = 20; // LYX_PAPER_MARGIN;
1170 box_x += font.textWidth("Mwide-figM", 10);
1172 int screen_first = screen->first;
1175 && y + screen_first > text->cursor.y -
1176 text->cursor.row->baseline
1177 && y + screen_first < text->cursor.y -
1178 text->cursor.row->baseline
1179 + font.maxAscent() * 1.2 + font.maxDescent() * 1.2) {
1181 selection_possible = false;
1186 // Maybe we want to edit a bibitem ale970302
1187 if (text->cursor.par->bibkey && x < 20 +
1188 bibitemMaxWidth(textclasslist
1189 .TextClass(buffer_->
1190 params.textclass).defaultfont())) {
1191 text->cursor.par->bibkey->Edit(0, 0);
1199 * Returns an inset if inset was hit. 0 otherwise.
1200 * If hit, the coordinates are changed relative to the inset.
1201 * Otherwise coordinates are not changed, and false is returned.
1203 Inset * BufferView::checkInsetHit(int & x, int & y)
1208 int y_tmp = y + getScreen()->first;
1210 LyXCursor cursor = text->cursor;
1211 if (cursor.pos < cursor.par->Last()
1212 && cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET
1213 && cursor.par->GetInset(cursor.pos)
1214 && cursor.par->GetInset(cursor.pos)->Editable()) {
1216 // Check whether the inset really was hit
1217 Inset * tmpinset = cursor.par->GetInset(cursor.pos);
1218 LyXFont font = text->GetFont(cursor.par, cursor.pos);
1220 && x < cursor.x + tmpinset->Width(font)
1221 && y_tmp > cursor.y - tmpinset->Ascent(font)
1222 && y_tmp < cursor.y + tmpinset->Descent(font)) {
1224 // The origin of an inset is on the baseline
1225 y = y_tmp - (cursor.y);
1228 } else if (cursor.pos - 1 >= 0
1229 && cursor.par->GetChar(cursor.pos - 1) == LyXParagraph::META_INSET
1230 && cursor.par->GetInset(cursor.pos - 1)
1231 && cursor.par->GetInset(cursor.pos - 1)->Editable()) {
1233 Inset * result = checkInsetHit(x, y);
1235 text->CursorRight();
1245 int BufferView::workAreaExpose()
1247 if (!work_area || !work_area->form->visible)
1250 // this is a hack to ensure that we only call this through
1251 // BufferView::redraw().
1256 static int work_area_width = work_area->w;
1257 static int work_area_height = work_area->h;
1259 bool widthChange = work_area->w != work_area_width;
1260 bool heightChange = work_area->h != work_area_height;
1262 // update from work area
1263 work_area_width = work_area->w;
1264 work_area_height = work_area->h;
1267 // All buffers need a resize
1268 bufferlist.resize();
1269 } else if (heightChange) {
1270 // Rebuild image of current screen
1272 // fitCursor() ensures we don't jump back
1273 // to the start of the document on vertical
1277 // The main window size has changed, repaint most stuff
1279 // ...including the minibuffer
1280 owner_->getMiniBuffer()->Init();
1282 } else if (screen) screen->Redraw();
1284 // Grey box when we don't have a buffer
1285 fl_winset(FL_ObjWin(work_area));
1286 fl_rectangle(1, work_area->x, work_area->y,
1287 work_area->w, work_area->h, FL_GRAY63);
1290 // always make sure that the scrollbar is sane.
1292 owner_->updateLayoutChoice();
1297 // Callback for cursor timer
1298 void BufferView::CursorToggleCB(FL_OBJECT * ob, long)
1300 BufferView * view = static_cast<BufferView*>(ob->u_vdata);
1302 // Quite a nice place for asyncron Inset updating, isn't it?
1303 // Actually no! This is run even if no buffer exist... so (Lgb)
1304 if (view && !view->buffer_) {
1305 goto set_timer_and_return;
1309 // On my quest to solve the gs render hangups I am now
1310 // disabling the SIGHUP completely, and will do a wait
1311 // now and then instead. If the guess that xforms somehow
1312 // destroys something is true, this is likely (hopefully)
1313 // to solve the problem...at least I hope so. Lgb
1315 // ...Ok this seems to work...at least it does not make things
1316 // worse so far. However I still see gs processes that hangs.
1317 // I would really like to know _why_ they are hanging. Anyway
1318 // the solution without the SIGCHLD handler seems to be easier
1321 // When attaching gdb to a a running gs that hangs it shows
1322 // that it is waiting for input(?) Is it possible for us to
1323 // provide that input somehow? Or figure what it is expecing
1326 // One solution is to, after some time, look if there are some
1327 // old gs processes still running and if there are: kill them
1330 // Another solution is to provide the user an option to rerender
1331 // a picture. This would, for the picture in question, check if
1332 // there is a gs running for it, if so kill it, and start a new
1333 // rendering process.
1335 // these comments posted to lyx@via
1338 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1339 if (pid == -1) // error find out what is wrong
1340 ; // ignore it for now.
1342 sigchldhandler(pid, &status);
1344 if (InsetUpdateList)
1345 UpdateInsetUpdateList();
1347 if (view && !view->screen){
1348 goto set_timer_and_return;
1351 if (view->lyx_focus && view->work_area_focus) {
1352 if (!view->buffer_->the_locking_inset) {
1353 view->screen->CursorToggle();
1355 view->buffer_->the_locking_inset->
1356 ToggleInsetCursor();
1358 goto set_timer_and_return;
1360 // Make sure that the cursor is visible.
1361 if (!view->buffer_->the_locking_inset) {
1362 view->screen->ShowCursor();
1364 if (!view->buffer_->the_locking_inset->isCursorVisible())
1365 view->buffer_->the_locking_inset->
1366 ToggleInsetCursor();
1369 // This is only run when work_area_focus or lyx_focus is false.
1372 XGetInputFocus(fl_display, &tmpwin, &tmp);
1373 if (lyxerr.debugging(Debug::INFO)) {
1374 lyxerr << "tmpwin: " << tmpwin
1375 << "\nwindow: " << view->owner_->getForm()->window
1376 << "\nwork_area_focus: " << view->work_area_focus
1377 << "\nlyx_focus : " << view->lyx_focus
1380 if (tmpwin != view->owner_->getForm()->window) {
1381 view->lyx_focus = false;
1384 view->lyx_focus = true;
1385 if (!view->work_area_focus)
1388 goto set_timer_and_return;
1392 set_timer_and_return:
1393 fl_set_timer(ob, 0.4);
1399 int BufferView::WorkAreaSelectionNotify(FL_OBJECT *, Window win,
1400 int /*w*/, int /*h*/,
1401 XEvent * event, void */*d*/)
1403 if (buffer_ == 0) return 0;
1404 if (event->type != SelectionNotify)
1410 unsigned char * uc = 0;
1412 screen->HideCursor();
1414 if (event->xselection.type == XA_STRING
1415 && event->xselection.property) {
1417 if (XGetWindowProperty(
1418 fl_display /* display */,
1420 event->xselection.property /* property */,
1421 0 /* long_offset */,
1422 0 /* long_length */,
1424 XA_STRING /* req_type */,
1425 &tmpatom /* actual_type_return */,
1426 &tmpint /* actual_format_return */,
1427 &ul1 /* nitems_return */,
1428 &ul2 /* bytes_after_return */,
1429 &uc /* prop_return */
1440 if (XGetWindowProperty(
1441 fl_display /* display */,
1443 event->xselection.property /* property */,
1444 0 /* long_offset */,
1445 ul2/4+1 /* long_length */,
1447 XA_STRING /* req_type */,
1448 &tmpatom /* actual_type_return */,
1449 &tmpint /* actual_format_return */,
1450 &ul1 /* nitems_return */,
1451 &ul2 /* bytes_after_return */,
1452 &uc /* prop_return */
1460 text->InsertStringA(reinterpret_cast<char*>(uc));
1462 text->InsertStringB(reinterpret_cast<char*>(uc));
1474 void BufferView::cursorPrevious()
1476 if (!text->cursor.row->previous) return;
1478 long y = getScreen()->first;
1479 Row * cursorrow = text->cursor.row;
1480 text->SetCursorFromCoordinates(text->cursor.x_fix, y);
1482 // This is to allow jumping over large insets
1483 if ((cursorrow == text->cursor.row))
1486 if (text->cursor.row->height < work_area->h)
1487 getScreen()->Draw(text->cursor.y
1488 - text->cursor.row->baseline
1489 + text->cursor.row->height
1490 - work_area->h +1 );
1494 void BufferView::cursorNext()
1496 if (!text->cursor.row->next) return;
1498 long y = getScreen()->first;
1499 text->GetRowNearY(y);
1500 Row * cursorrow = text->cursor.row;
1501 text->SetCursorFromCoordinates(text->cursor.x_fix, y + work_area->h);
1503 // This is to allow jumping over large insets
1504 if ((cursorrow == text->cursor.row))
1507 if (text->cursor.row->height < work_area->h)
1508 getScreen()->Draw(text->cursor.y
1509 - text->cursor.row->baseline);
1513 bool BufferView::available() const
1515 if (buffer_ && text) return true;
1520 void BufferView::savePosition()
1522 backstack.push(buffer()->fileName(),
1528 void BufferView::restorePosition()
1530 if (backstack.empty()) return;
1533 string fname = backstack.pop(&x, &y);
1536 Buffer * b = bufferlist.exists(fname) ?
1537 bufferlist.getBuffer(fname) :
1538 bufferlist.loadLyXFile(fname); // don't ask, just load it
1540 text->SetCursorFromCoordinates(x, y);
1545 void BufferView::update(signed char f)
1547 owner()->updateLayoutChoice();
1549 if (!text->selection && f > -3)
1550 text->sel_cursor = text->cursor;
1553 text->FullRebreak();
1557 if (f != 3 && f != -3) {
1562 if (f == 1 || f == -1) {
1563 if (buffer()->isLyxClean()) {
1564 buffer()->markDirty();
1565 owner()->getMiniBuffer()->setTimer(4);
1567 buffer()->markDirty();