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 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR, 0, 0, w, h, "");
121 fl_set_object_boxtype(obj, FL_UP_BOX);
122 fl_set_object_resize(obj, FL_RESIZE_ALL);
123 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
125 fl_set_object_callback(obj, C_scroll_cb, 0);
126 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
127 fl_set_scrollbar_value(scrollbar, 0.0);
128 fl_set_scrollbar_size(scrollbar, scrollbar->h);
130 // The work area itself
131 work_area = obj = fl_add_free(FL_ALL_FREE, 0, 0, w, h, "",
132 C_work_area_handler);
133 obj->wantkey = FL_KEY_ALL;
136 fl_set_object_boxtype(obj,FL_DOWN_BOX);
137 fl_set_object_resize(obj, FL_RESIZE_ALL);
138 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
140 // Hand control of the layout of these widgets to the
142 XFormsView & xview = dynamic_cast<XFormsView &>(owner);
143 BoxList & boxlist = xview.getBox(XFormsView::Center).children();
145 wa_box_ = &boxlist.push_back(Box(0,0));
146 wa_box_->set(Box::Horizontal);
148 Box & frame_box = widgets_.add(frame, wa_box_->children(), 0, 0);
149 frame_box.set(Box::Expand);
151 int const bw = int(abs(fl_get_border_width()));
152 Box & wa_box = embed(work_area, frame_box.children(), widgets_, bw);
153 wa_box.set(Box::Expand);
155 widgets_.add(scrollbar, wa_box_->children(), 17, 0);
157 xview.metricsUpdated.connect(boost::bind(&WidgetMap::updateMetrics,
160 /// X selection hook - xforms gets it wrong
161 fl_current_form->u_vdata = this;
162 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_event_cb);
164 fl_unfreeze_all_forms();
168 val.function = GXcopy;
169 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
174 XWorkArea::~XWorkArea()
176 XFreeGC(fl_get_display(), copy_gc);
178 XFreePixmap(fl_get_display(), workareapixmap);
182 void XWorkArea::redraw(int width, int height)
184 static int cur_width = -1;
185 static int cur_height = -1;
187 if (cur_width == width && cur_height == height && workareapixmap) {
188 XCopyArea(fl_get_display(),
189 getPixmap(), getWin(), copy_gc,
190 0, 0, width, height, xpos(), ypos());
197 if (lyxerr.debugging(Debug::WORKAREA)) {
198 lyxerr << "(Re)creating pixmap ("
199 << width << 'x' << height << ')' << endl;
202 if (workareapixmap) {
203 XFreePixmap(fl_get_display(), workareapixmap);
206 workareapixmap = XCreatePixmap(fl_get_display(),
207 RootWindow(fl_get_display(), 0),
210 fl_get_visual_depth());
216 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
218 // we need to cache this for scroll_cb
219 doc_height_ = height;
222 fl_set_scrollbar_value(scrollbar, 0.0);
223 fl_set_scrollbar_size(scrollbar, scrollbar->h);
227 long const work_height = workHeight();
229 if (lyxerr.debugging(Debug::GUI)) {
230 lyxerr << "scroll: height now " << height << '\n'
231 << "scroll: work_height " << work_height << endl;
234 /* If the text is smaller than the working area, the scrollbar
235 * maximum must be the working area height. No scrolling will
237 if (height <= work_height) {
238 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !"
240 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
241 fl_set_scrollbar_value(scrollbar, pos);
242 fl_set_scrollbar_size(scrollbar, scrollbar->h);
246 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
247 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
249 fl_set_scrollbar_value(scrollbar, pos);
251 double const slider_size =
252 (height == 0) ? 1.0 : 1.0 / double(height);
254 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
258 // callback for scrollbar slider
259 void XWorkArea::scroll_cb()
261 double const val = fl_get_scrollbar_value(scrollbar);
263 if (lyxerr.debugging(Debug::GUI)) {
264 lyxerr << "scroll: val: " << val << '\n'
265 << "scroll: height: " << scrollbar->h << '\n'
266 << "scroll: docheight: " << doc_height_ << endl;
269 scrollDocView(int(val));
274 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
278 XEvent * ev = static_cast<XEvent*>(xev);
279 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
287 if (!area->work_area || !area->work_area->form->visible)
289 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
290 area->redraw(area->workWidth(), area->workHeight());
294 if (!ev || ev->xbutton.button == 0) break;
295 // Should really have used xbutton.state
296 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
298 FuncRequest(LFUN_MOUSE_PRESS,
299 ev->xbutton.x - ob->x,
300 ev->xbutton.y - ob->y,
301 x_button_state(key)));
305 if (!ev || ev->xbutton.button == 0) break;
306 // Should really have used xbutton.state
307 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
309 FuncRequest(LFUN_MOUSE_RELEASE,
310 ev->xbutton.x - ob->x,
311 ev->xbutton.y - ob->y,
312 x_button_state(key)));
316 if (!ev || !area->scrollbar)
319 int const drag_x = ev->xmotion.x;
320 int const drag_y = ev->xmotion.y;
321 int const area_y = ob->y;
322 int const area_h = ob->h;
324 // Check if the mouse is above or below the workarea
325 if (drag_y <= area_y || drag_y >= area_y + area_h) {
326 // The mouse button is depressed and we are outside the
327 // workarea. That means we are simultaneously selecting
328 // text and scrolling the view.
329 // Use a Timeout to react to a drag events only every
330 // 200ms. All intervening events are discarded,
331 // allowing the user to control position easily.
332 static int const discard_interval = 200;
333 static Timeout timeout(discard_interval);
335 if (timeout.running())
337 // The timeout is not running, so process the
338 // event, first starting the timeout to discard future
343 static int x_old = -1;
344 static int y_old = -1;
345 static double scrollbar_value_old = -1.0;
347 double const scrollbar_value =
348 fl_get_scrollbar_value(area->scrollbar);
350 if (drag_x != x_old || drag_y != y_old ||
351 scrollbar_value != scrollbar_value_old) {
354 scrollbar_value_old = scrollbar_value;
356 lyxerr[Debug::WORKAREA] << "Workarea event: DRAG"
359 // It transpires that ev->xbutton.button == 0 when
360 // the mouse is dragged, so it cannot be used to
361 // initialise x_button_state and hence FuncRequest.
363 // The 'key' that is passed into the function does
364 // contain the necessary info, however.
366 // It is for this reason that x_button_state has
367 // been modified to work with key
368 // rather than ev->xbutton.button.
370 // Angus 15 Oct 2002.
371 FuncRequest cmd(LFUN_MOUSE_MOTION,
372 ev->xbutton.x - ob->x,
373 ev->xbutton.y - ob->y,
374 x_button_state(key));
381 lyxerr[Debug::WORKAREA] << "Workarea event: KEYPRESS" << endl;
385 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
386 XLookupString(xke, dummy, 1, &keysym, 0);
388 if (lyxerr.debugging(Debug::KEY)) {
389 char const * const tmp = XKeysymToString(key);
390 char const * const tmp2 = XKeysymToString(keysym);
391 string const stm = (tmp ? tmp : string());
392 string const stm2 = (tmp2 ? tmp2 : string());
394 lyxerr << "XWorkArea: Key is `" << stm
395 << "' [" << key << "]\n"
396 << "XWorkArea: Keysym is `" << stm2
397 << "' [" << keysym << ']' << endl;
400 // Note that we need this handling because of a bug
401 // in XForms 0.89, if this bug is resolved in the way I hope
402 // we can just use the keysym directly without looking
403 // at key at all. (Lgb)
406 // We might have to add more keysyms here also,
407 // we will do that as the issues arise. (Lgb)
408 if (keysym == XK_space) {
410 lyxerr[Debug::KEY] << "Using keysym [A]"
415 // It seems that this was a bit optimistic...
416 // With this hacking things seems to be better (Lgb)
417 //if (!iscntrl(key)) {
419 // lyxerr[Debug::KEY]
420 // << "Using key [B]\n"
422 // << static_cast<unsigned char>(key)
425 ret_key = (keysym ? keysym : key);
426 lyxerr[Debug::KEY] << "Using keysym [B]"
431 unsigned int const ret_state = xke->state;
433 // If you have a better way to handle "wild-output" of
434 // characters after the key has been released than the one
435 // below, please contact me. (Lgb)
436 static Time last_time_pressed;
437 static unsigned int last_key_pressed;
438 static unsigned int last_state_pressed;
439 lyxerr[Debug::KEY] << "Workarea Diff: "
440 << xke->time - last_time_pressed
442 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
443 && ret_state == last_state_pressed
444 && xke->keycode == last_key_pressed) {
446 << "Workarea: Purging X events." << endl;
447 //lyxerr << "Workarea Events: "
448 // << XEventsQueued(fl_get_display(), QueuedAlready)
450 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
452 // This purge make f.ex. scrolling stop immediately when
453 // releasing the PageDown button. The question is if
454 // this purging of XEvents can cause any harm...
455 // after some testing I can see no problems, but
456 // I'd like other reports too.
459 last_time_pressed = xke->time;
460 last_key_pressed = xke->keycode;
461 last_state_pressed = ret_state;
463 XLyXKeySym * xlk = new XLyXKeySym;
464 xlk->initFromKeySym(ret_key);
466 area->workAreaKeyPress(LyXKeySymPtr(xlk),
467 x_key_state(ret_state));
472 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
476 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
477 fl_set_cursor(FL_ObjWin(area->work_area), XC_xterm);
481 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
482 // There should be no need for this. But there is.
483 fl_set_cursor(FL_ObjWin(area->work_area), FL_DEFAULT_CURSOR);
488 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
489 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
490 ev->xbutton.x - ob->x,
491 ev->xbutton.y - ob->y,
492 x_button_state(key));
499 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
500 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
501 ev->xbutton.x - ob->x,
502 ev->xbutton.y - ob->y,
503 x_button_state(key));
510 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
520 string clipboard_selection;
521 bool clipboard_read = false;
525 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
526 void const * data, long size)
528 clipboard_selection.erase();
531 clipboard_selection.reserve(size);
532 for (int i = 0; i < size; ++i)
533 clipboard_selection += static_cast<char const *>(data)[i];
534 clipboard_read = true;
542 int XWorkArea::event_cb(XEvent * xev)
545 case SelectionRequest:
546 lyxerr[Debug::GUI] << "X requested selection." << endl;
547 selectionRequested();
550 lyxerr[Debug::GUI] << "Lost selection." << endl;
558 void XWorkArea::haveSelection(bool yes) const
560 Window const owner = yes ? FL_ObjWin(work_area) : None;
561 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, owner, CurrentTime);
565 string const XWorkArea::getClipboard() const
567 clipboard_read = false;
569 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
574 while (!clipboard_read) {
575 if (fl_check_forms() == FL_EVENT) {
577 lyxerr << "Received unhandled X11 event\n"
578 << "Type: 0x" << hex << ev.xany.type
579 << " Target: 0x" << hex << ev.xany.window
583 return clipboard_selection;
587 void XWorkArea::putClipboard(string const & s) const
592 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);