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
15 #include "XWorkArea.h"
17 #include "XLyXKeySym.h"
18 #include "funcrequest.h"
29 void waitForX(bool discard)
31 XSync(fl_get_display(), discard);
35 mouse_button::state x_button_state(unsigned int button)
37 mouse_button::state b = mouse_button::none;
40 b = mouse_button::button1;
43 b = mouse_button::button2;
46 b = mouse_button::button3;
49 b = mouse_button::button4;
52 b = mouse_button::button5;
59 key_modifier::state x_key_state(unsigned int state)
61 key_modifier::state k = key_modifier::none;
62 if (state & ControlMask)
63 k |= key_modifier::ctrl;
64 if (state & ShiftMask)
65 k |= key_modifier::shift;
67 k |= key_modifier::alt;
74 void C_scroll_cb(FL_OBJECT * ob, long)
76 XWorkArea * area = static_cast<XWorkArea *>(ob->u_vdata);
81 int C_work_area_handler(FL_OBJECT * ob, int event, FL_Coord, FL_Coord,
84 return XWorkArea::work_area_handler(ob, event, 0, 0, key, xev);
88 int C_event_cb(FL_FORM * form, void * xev)
90 XWorkArea * area = static_cast<XWorkArea *>(form->u_vdata);
91 return area->event_cb(static_cast<XEvent *>(xev));
98 XWorkArea::XWorkArea(int x, int y, int w, int h)
99 : workareapixmap(0), painter_(*this)
101 if (lyxerr.debugging(Debug::WORKAREA)) {
102 lyxerr << "\tbackground box: +"
103 << x << '+' << y << ' '
104 << w - 15 << 'x' << h << endl;
107 fl_freeze_all_forms();
111 obj = fl_add_box(FL_BORDER_BOX, x, y, w - 15, h, "");
112 fl_set_object_resize(obj, FL_RESIZE_ALL);
113 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
115 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
116 x + w - 15, y, 17, h, "");
117 fl_set_object_boxtype(obj, FL_UP_BOX);
118 fl_set_object_resize(obj, FL_RESIZE_ALL);
119 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
121 fl_set_object_callback(obj, C_scroll_cb, 0);
122 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
123 fl_set_scrollbar_value(scrollbar, 0.0);
124 fl_set_scrollbar_size(scrollbar, scrollbar->h);
126 int const bw = int(abs(fl_get_border_width()));
128 // Create the workarea pixmap
129 // FIXME remove redraw(w - 15 - 2 * bw, h - 2 * bw);
131 if (lyxerr.debugging(Debug::WORKAREA))
132 lyxerr << "\tfree object: +"
133 << x + bw << '+' << y + bw << ' '
134 << w - 15 - 2 * bw << 'x'
135 << h - 2 * bw << endl;
137 // We add this object as late as possible to avoid problems
140 work_area = obj = fl_add_free(FL_ALL_FREE,
144 C_work_area_handler);
145 obj->wantkey = FL_KEY_ALL;
148 fl_set_object_boxtype(obj,FL_DOWN_BOX);
149 fl_set_object_resize(obj, FL_RESIZE_ALL);
150 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
152 /// X selection hook - xforms gets it wrong
153 fl_current_form->u_vdata = this;
154 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_event_cb);
156 fl_unfreeze_all_forms();
160 val.function = GXcopy;
161 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
166 XWorkArea::~XWorkArea()
168 XFreeGC(fl_get_display(), copy_gc);
170 XFreePixmap(fl_get_display(), workareapixmap);
174 void XWorkArea::redraw(int width, int height)
176 static int cur_width = -1;
177 static int cur_height = -1;
179 if (cur_width == width && cur_height == height && workareapixmap) {
180 XCopyArea(fl_get_display(),
181 getPixmap(), getWin(), copy_gc,
182 0, 0, width, height, xpos(), ypos());
189 if (lyxerr.debugging(Debug::WORKAREA)) {
190 lyxerr << "(Re)creating pixmap ("
191 << width << 'x' << height << ')' << endl;
194 if (workareapixmap) {
195 XFreePixmap(fl_get_display(), workareapixmap);
198 workareapixmap = XCreatePixmap(fl_get_display(),
199 RootWindow(fl_get_display(), 0),
202 fl_get_visual_depth());
208 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
210 // we need to cache this for scroll_cb
211 doc_height_ = height;
214 fl_set_scrollbar_value(scrollbar, 0.0);
215 fl_set_scrollbar_size(scrollbar, scrollbar->h);
219 long const work_height = workHeight();
221 if (lyxerr.debugging(Debug::GUI)) {
222 lyxerr << "scroll: height now " << height << '\n'
223 << "scroll: work_height " << work_height << endl;
226 /* If the text is smaller than the working area, the scrollbar
227 * maximum must be the working area height. No scrolling will
229 if (height <= work_height) {
230 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !"
232 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
233 fl_set_scrollbar_value(scrollbar, pos);
234 fl_set_scrollbar_size(scrollbar, scrollbar->h);
238 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
239 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
241 fl_set_scrollbar_value(scrollbar, pos);
243 double const slider_size =
244 (height == 0) ? 1.0 : 1.0 / double(height);
246 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
250 // callback for scrollbar slider
251 void XWorkArea::scroll_cb()
253 double const val = fl_get_scrollbar_value(scrollbar);
255 if (lyxerr.debugging(Debug::GUI)) {
256 lyxerr << "scroll: val: " << val << '\n'
257 << "scroll: height: " << scrollbar->h << '\n'
258 << "scroll: docheight: " << doc_height_ << endl;
261 scrollDocView(int(val));
266 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
270 XEvent * ev = static_cast<XEvent*>(xev);
271 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
277 if (!area->work_area || !area->work_area->form->visible)
279 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
280 area->redraw(area->workWidth(), area->workHeight());
283 if (!ev || ev->xbutton.button == 0) break;
284 // Should really have used xbutton.state
285 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
287 FuncRequest(LFUN_MOUSE_PRESS,
288 ev->xbutton.x - ob->x,
289 ev->xbutton.y - ob->y,
290 x_button_state(key)));
293 if (!ev || ev->xbutton.button == 0) break;
294 // Should really have used xbutton.state
295 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
297 FuncRequest(LFUN_MOUSE_RELEASE,
298 ev->xbutton.x - ob->x,
299 ev->xbutton.y - ob->y,
300 x_button_state(key)));
304 if (!ev || !area->scrollbar)
307 int const drag_x = ev->xmotion.x;
308 int const drag_y = ev->xmotion.y;
309 int const area_y = ob->y;
310 int const area_h = ob->h;
312 // Check if the mouse is above or below the workarea
313 if (drag_y <= area_y || drag_y >= area_y + area_h) {
314 // The mouse button is depressed and we are outside the
315 // workarea. That means we are simultaneously selecting
316 // text and scrolling the view.
317 // Use a Timeout to react to a drag events only every
318 // 200ms. All intervening events are discarded,
319 // allowing the user to control position easily.
320 static int const discard_interval = 200;
321 static Timeout timeout(discard_interval);
323 if (timeout.running())
325 // The timeout is not running, so process the
326 // event, first starting the timeout to discard future
331 static int x_old = -1;
332 static int y_old = -1;
333 static double scrollbar_value_old = -1.0;
335 double const scrollbar_value =
336 fl_get_scrollbar_value(area->scrollbar);
338 if (drag_x != x_old || drag_y != y_old ||
339 scrollbar_value != scrollbar_value_old) {
342 scrollbar_value_old = scrollbar_value;
344 lyxerr[Debug::WORKAREA] << "Workarea event: DRAG"
347 // It transpires that ev->xbutton.button == 0 when
348 // the mouse is dragged, so it cannot be used to
349 // initialise x_button_state and hence FuncRequest.
351 // The 'key' that is passed into the function does
352 // contain the necessary info, however.
354 // It is for this reason that x_button_state has
355 // been modified to work with key
356 // rather than ev->xbutton.button.
358 // Angus 15 Oct 2002.
359 FuncRequest cmd(LFUN_MOUSE_MOTION,
360 ev->xbutton.x - ob->x,
361 ev->xbutton.y - ob->y,
362 x_button_state(key));
370 lyxerr[Debug::WORKAREA] << "Workarea event: KEYPRESS" << endl;
374 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
375 XLookupString(xke, dummy, 1, &keysym, 0);
377 if (lyxerr.debugging(Debug::KEY)) {
378 char const * const tmp = XKeysymToString(key);
379 char const * const tmp2 = XKeysymToString(keysym);
380 string const stm = (tmp ? tmp : string());
381 string const stm2 = (tmp2 ? tmp2 : string());
383 lyxerr << "XWorkArea: Key is `" << stm
384 << "' [" << key << "]\n"
385 << "XWorkArea: Keysym is `" << stm2
386 << "' [" << keysym << ']' << endl;
389 // Note that we need this handling because of a bug
390 // in XForms 0.89, if this bug is resolved in the way I hope
391 // we can just use the keysym directly without looking
392 // at key at all. (Lgb)
395 // We might have to add more keysyms here also,
396 // we will do that as the issues arise. (Lgb)
397 if (keysym == XK_space) {
399 lyxerr[Debug::KEY] << "Using keysym [A]"
404 // It seems that this was a bit optimistic...
405 // With this hacking things seems to be better (Lgb)
406 //if (!iscntrl(key)) {
408 // lyxerr[Debug::KEY]
409 // << "Using key [B]\n"
411 // << static_cast<unsigned char>(key)
414 ret_key = (keysym ? keysym : key);
415 lyxerr[Debug::KEY] << "Using keysym [B]"
420 unsigned int const ret_state = xke->state;
422 // If you have a better way to handle "wild-output" of
423 // characters after the key has been released than the one
424 // below, please contact me. (Lgb)
425 static Time last_time_pressed;
426 static unsigned int last_key_pressed;
427 static unsigned int last_state_pressed;
428 lyxerr[Debug::KEY] << "Workarea Diff: "
429 << xke->time - last_time_pressed
431 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
432 && ret_state == last_state_pressed
433 && xke->keycode == last_key_pressed) {
435 << "Workarea: Purging X events." << endl;
436 //lyxerr << "Workarea Events: "
437 // << XEventsQueued(fl_get_display(), QueuedAlready)
439 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
441 // This purge make f.ex. scrolling stop immediately when
442 // releasing the PageDown button. The question is if
443 // this purging of XEvents can cause any harm...
444 // after some testing I can see no problems, but
445 // I'd like other reports too.
448 last_time_pressed = xke->time;
449 last_key_pressed = xke->keycode;
450 last_state_pressed = ret_state;
452 XLyXKeySym * xlk = new XLyXKeySym;
453 xlk->initFromKeySym(ret_key);
455 area->workAreaKeyPress(LyXKeySymPtr(xlk),
456 x_key_state(ret_state));
461 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
465 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
466 fl_set_cursor(FL_ObjWin(area->work_area), XC_xterm);
469 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
470 // There should be no need for this. But there is.
471 fl_set_cursor(FL_ObjWin(area->work_area), FL_DEFAULT_CURSOR);
475 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
476 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
477 ev->xbutton.x - ob->x,
478 ev->xbutton.y - ob->y,
479 x_button_state(key));
485 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
486 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
487 ev->xbutton.x - ob->x,
488 ev->xbutton.y - ob->y,
489 x_button_state(key));
495 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
505 string clipboard_selection;
506 bool clipboard_read = false;
510 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
511 void const * data, long size)
513 clipboard_selection.erase();
516 clipboard_selection.reserve(size);
517 for (int i = 0; i < size; ++i)
518 clipboard_selection += static_cast<char const *>(data)[i];
519 clipboard_read = true;
527 int XWorkArea::event_cb(XEvent * xev)
530 case SelectionRequest:
531 lyxerr[Debug::GUI] << "X requested selection." << endl;
532 selectionRequested();
535 lyxerr[Debug::GUI] << "Lost selection." << endl;
543 void XWorkArea::haveSelection(bool yes) const
545 Window const owner = yes ? FL_ObjWin(work_area) : None;
546 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, owner, CurrentTime);
550 string const XWorkArea::getClipboard() const
552 clipboard_read = false;
554 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
559 while (!clipboard_read) {
560 if (fl_check_forms() == FL_EVENT) {
562 lyxerr << "Received unhandled X11 event\n"
563 << "Type: 0x" << hex << ev.xany.type
564 << " Target: 0x" << hex << ev.xany.window
568 return clipboard_selection;
572 void XWorkArea::putClipboard(string const & s) const
577 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);