3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
14 #include "XWorkArea.h"
15 #include "XFormsView.h"
18 #include "XLyXKeySym.h"
19 #include "funcrequest.h"
22 #include <boost/bind.hpp>
24 using lyx::frontend::Box;
25 using lyx::frontend::BoxList;
26 using lyx::frontend::WidgetMap;
37 inline void waitForX(bool discard)
39 XSync(fl_get_display(), discard);
43 mouse_button::state x_button_state(unsigned int button)
45 mouse_button::state b = mouse_button::none;
48 b = mouse_button::button1;
51 b = mouse_button::button2;
54 b = mouse_button::button3;
57 b = mouse_button::button4;
60 b = mouse_button::button5;
67 key_modifier::state x_key_state(unsigned int state)
69 key_modifier::state k = key_modifier::none;
70 if (state & ControlMask)
71 k |= key_modifier::ctrl;
72 if (state & ShiftMask)
73 k |= key_modifier::shift;
75 k |= key_modifier::alt;
82 void C_scroll_cb(FL_OBJECT * ob, long)
84 XWorkArea * area = static_cast<XWorkArea *>(ob->u_vdata);
89 int C_work_area_handler(FL_OBJECT * ob, int event, FL_Coord, FL_Coord,
92 return XWorkArea::work_area_handler(ob, event, 0, 0, key, xev);
96 int C_event_cb(FL_FORM * form, void * xev)
98 XWorkArea * area = static_cast<XWorkArea *>(form->u_vdata);
99 return area->event_cb(static_cast<XEvent *>(xev));
106 XWorkArea::XWorkArea(LyXView & owner, int w, int h)
107 : workareapixmap(0), painter_(*this)
109 fl_freeze_all_forms();
114 // A frame around the work area.
115 frame = obj = fl_add_box(FL_BORDER_BOX, 0, 0, w, h, "");
116 fl_set_object_resize(obj, FL_RESIZE_ALL);
117 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
120 #warning "I've set this to red as an incentive to address the drawing issues \
121 of the XForms frontend. Angus 4 May, 2004"
123 fl_set_object_color(obj, FL_RED, FL_MCOL);
126 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR, 0, 0, w, h, "");
127 fl_set_object_boxtype(obj, FL_UP_BOX);
128 fl_set_object_resize(obj, FL_RESIZE_ALL);
129 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
131 fl_set_object_callback(obj, C_scroll_cb, 0);
132 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
133 fl_set_scrollbar_value(scrollbar, 0.0);
134 fl_set_scrollbar_size(scrollbar, scrollbar->h);
136 // The work area itself
137 work_area = obj = fl_add_free(FL_ALL_FREE, 0, 0, w, h, "",
138 C_work_area_handler);
139 obj->wantkey = FL_KEY_ALL;
142 fl_set_object_boxtype(obj,FL_DOWN_BOX);
143 fl_set_object_resize(obj, FL_RESIZE_ALL);
144 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
146 // Hand control of the layout of these widgets to the
148 XFormsView & xview = dynamic_cast<XFormsView &>(owner);
149 BoxList & boxlist = xview.getBox(XFormsView::Center).children();
151 wa_box_ = &boxlist.push_back(Box(0,0));
152 wa_box_->set(Box::Horizontal);
154 Box & frame_box = widgets_.add(frame, wa_box_->children(), 0, 0);
155 frame_box.set(Box::Expand);
157 int const bw = int(abs(fl_get_border_width()));
158 Box & wa_box = embed(work_area, frame_box.children(), widgets_, bw);
159 wa_box.set(Box::Expand);
161 widgets_.add(scrollbar, wa_box_->children(), 17, 0);
163 xview.metricsUpdated.connect(boost::bind(&WidgetMap::updateMetrics,
166 /// X selection hook - xforms gets it wrong
167 fl_current_form->u_vdata = this;
168 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_event_cb);
170 fl_unfreeze_all_forms();
174 val.function = GXcopy;
175 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
180 XWorkArea::~XWorkArea()
182 XFreeGC(fl_get_display(), copy_gc);
184 XFreePixmap(fl_get_display(), workareapixmap);
188 void XWorkArea::redraw(int width, int height)
190 static int cur_width = -1;
191 static int cur_height = -1;
193 if (cur_width == width && cur_height == height && workareapixmap) {
194 XCopyArea(fl_get_display(),
195 getPixmap(), getWin(), copy_gc,
196 0, 0, width, height, xpos(), ypos());
203 if (lyxerr.debugging(Debug::WORKAREA)) {
204 lyxerr << "(Re)creating pixmap ("
205 << width << 'x' << height << ')' << endl;
208 if (workareapixmap) {
209 XFreePixmap(fl_get_display(), workareapixmap);
212 workareapixmap = XCreatePixmap(fl_get_display(),
213 RootWindow(fl_get_display(), 0),
216 fl_get_visual_depth());
222 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
224 // we need to cache this for scroll_cb
225 doc_height_ = height;
228 fl_set_scrollbar_value(scrollbar, 0.0);
229 fl_set_scrollbar_size(scrollbar, scrollbar->h);
233 long const work_height = workHeight();
235 if (lyxerr.debugging(Debug::GUI)) {
236 lyxerr << "scroll: height now " << height << '\n'
237 << "scroll: work_height " << work_height << endl;
240 /* If the text is smaller than the working area, the scrollbar
241 * maximum must be the working area height. No scrolling will
243 if (height <= work_height) {
244 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !"
246 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
247 fl_set_scrollbar_value(scrollbar, pos);
248 fl_set_scrollbar_size(scrollbar, scrollbar->h);
252 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
253 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
255 fl_set_scrollbar_value(scrollbar, pos);
257 double const slider_size =
258 (height == 0) ? 1.0 : 1.0 / double(height);
260 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
264 // callback for scrollbar slider
265 void XWorkArea::scroll_cb()
267 double const val = fl_get_scrollbar_value(scrollbar);
269 if (lyxerr.debugging(Debug::GUI)) {
270 lyxerr << "scroll: val: " << val << '\n'
271 << "scroll: height: " << scrollbar->h << '\n'
272 << "scroll: docheight: " << doc_height_ << endl;
275 scrollDocView(int(val));
280 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
284 XEvent * ev = static_cast<XEvent*>(xev);
285 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
293 if (!area->work_area || !area->work_area->form->visible)
295 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
296 area->redraw(area->workWidth(), area->workHeight());
300 if (!ev || ev->xbutton.button == 0) break;
301 // Should really have used xbutton.state
302 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
304 FuncRequest(LFUN_MOUSE_PRESS,
305 ev->xbutton.x - ob->x,
306 ev->xbutton.y - ob->y,
307 x_button_state(key)));
311 if (!ev || ev->xbutton.button == 0) break;
312 // Should really have used xbutton.state
313 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
315 FuncRequest(LFUN_MOUSE_RELEASE,
316 ev->xbutton.x - ob->x,
317 ev->xbutton.y - ob->y,
318 x_button_state(key)));
322 if (!ev || !area->scrollbar)
325 int const drag_x = ev->xmotion.x;
326 int const drag_y = ev->xmotion.y;
327 int const area_y = ob->y;
328 int const area_h = ob->h;
330 // Check if the mouse is above or below the workarea
331 if (drag_y <= area_y || drag_y >= area_y + area_h) {
332 // The mouse button is depressed and we are outside the
333 // workarea. That means we are simultaneously selecting
334 // text and scrolling the view.
335 // Use a Timeout to react to a drag events only every
336 // 200ms. All intervening events are discarded,
337 // allowing the user to control position easily.
338 static int const discard_interval = 200;
339 static Timeout timeout(discard_interval);
341 if (timeout.running())
343 // The timeout is not running, so process the
344 // event, first starting the timeout to discard future
349 static int x_old = -1;
350 static int y_old = -1;
351 static double scrollbar_value_old = -1.0;
353 double const scrollbar_value =
354 fl_get_scrollbar_value(area->scrollbar);
356 if (drag_x != x_old || drag_y != y_old ||
357 scrollbar_value != scrollbar_value_old) {
360 scrollbar_value_old = scrollbar_value;
362 lyxerr[Debug::WORKAREA] << "Workarea event: DRAG"
365 // It transpires that ev->xbutton.button == 0 when
366 // the mouse is dragged, so it cannot be used to
367 // initialise x_button_state and hence FuncRequest.
369 // The 'key' that is passed into the function does
370 // contain the necessary info, however.
372 // It is for this reason that x_button_state has
373 // been modified to work with key
374 // rather than ev->xbutton.button.
376 // Angus 15 Oct 2002.
377 FuncRequest cmd(LFUN_MOUSE_MOTION,
378 ev->xbutton.x - ob->x,
379 ev->xbutton.y - ob->y,
380 x_button_state(key));
387 lyxerr[Debug::WORKAREA] << "Workarea event: KEYPRESS" << endl;
391 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
392 XLookupString(xke, dummy, 1, &keysym, 0);
394 if (lyxerr.debugging(Debug::KEY)) {
395 char const * const tmp = XKeysymToString(key);
396 char const * const tmp2 = XKeysymToString(keysym);
397 string const stm = (tmp ? tmp : string());
398 string const stm2 = (tmp2 ? tmp2 : string());
400 lyxerr << "XWorkArea: Key is `" << stm
401 << "' [" << key << "]\n"
402 << "XWorkArea: Keysym is `" << stm2
403 << "' [" << keysym << ']' << endl;
406 // Note that we need this handling because of a bug
407 // in XForms 0.89, if this bug is resolved in the way I hope
408 // we can just use the keysym directly without looking
409 // at key at all. (Lgb)
412 // We might have to add more keysyms here also,
413 // we will do that as the issues arise. (Lgb)
414 if (keysym == XK_space) {
416 lyxerr[Debug::KEY] << "Using keysym [A]"
421 // It seems that this was a bit optimistic...
422 // With this hacking things seems to be better (Lgb)
423 //if (!iscntrl(key)) {
425 // lyxerr[Debug::KEY]
426 // << "Using key [B]\n"
428 // << static_cast<unsigned char>(key)
431 ret_key = (keysym ? keysym : key);
432 lyxerr[Debug::KEY] << "Using keysym [B]"
437 unsigned int const ret_state = xke->state;
439 // If you have a better way to handle "wild-output" of
440 // characters after the key has been released than the one
441 // below, please contact me. (Lgb)
442 static Time last_time_pressed;
443 static unsigned int last_key_pressed;
444 static unsigned int last_state_pressed;
445 lyxerr[Debug::KEY] << "Workarea Diff: "
446 << xke->time - last_time_pressed
448 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
449 && ret_state == last_state_pressed
450 && xke->keycode == last_key_pressed) {
452 << "Workarea: Purging X events." << endl;
453 //lyxerr << "Workarea Events: "
454 // << XEventsQueued(fl_get_display(), QueuedAlready)
456 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
458 // This purge make f.ex. scrolling stop immediately when
459 // releasing the PageDown button. The question is if
460 // this purging of XEvents can cause any harm...
461 // after some testing I can see no problems, but
462 // I'd like other reports too.
465 last_time_pressed = xke->time;
466 last_key_pressed = xke->keycode;
467 last_state_pressed = ret_state;
469 XLyXKeySym * xlk = new XLyXKeySym;
470 xlk->initFromKeySym(ret_key);
472 area->workAreaKeyPress(LyXKeySymPtr(xlk),
473 x_key_state(ret_state));
478 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
482 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
483 fl_set_cursor(FL_ObjWin(area->work_area), XC_xterm);
487 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
488 // There should be no need for this. But there is.
489 fl_set_cursor(FL_ObjWin(area->work_area), FL_DEFAULT_CURSOR);
494 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
495 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
496 ev->xbutton.x - ob->x,
497 ev->xbutton.y - ob->y,
498 x_button_state(key));
505 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
506 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
507 ev->xbutton.x - ob->x,
508 ev->xbutton.y - ob->y,
509 x_button_state(key));
516 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
526 string clipboard_selection;
527 bool clipboard_read = false;
531 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
532 void const * data, long size)
534 clipboard_selection.erase();
537 clipboard_selection.reserve(size);
538 for (int i = 0; i < size; ++i)
539 clipboard_selection += static_cast<char const *>(data)[i];
540 clipboard_read = true;
548 int XWorkArea::event_cb(XEvent * xev)
551 case SelectionRequest:
552 lyxerr[Debug::GUI] << "X requested selection." << endl;
553 selectionRequested();
556 lyxerr[Debug::GUI] << "Lost selection." << endl;
564 void XWorkArea::haveSelection(bool yes) const
566 Window const owner = yes ? FL_ObjWin(work_area) : None;
567 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, owner, CurrentTime);
571 string const XWorkArea::getClipboard() const
573 clipboard_read = false;
575 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
580 while (!clipboard_read) {
581 if (fl_check_forms() == FL_EVENT) {
583 lyxerr << "Received unhandled X11 event\n"
584 << "Type: 0x" << hex << ev.xany.type
585 << " Target: 0x" << hex << ev.xany.window
589 return clipboard_selection;
593 void XWorkArea::putClipboard(string const & s) const
598 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);