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"
17 #include "XLyXKeySym.h"
18 #include "funcrequest.h"
30 inline void waitForX(bool discard)
32 XSync(fl_get_display(), discard);
36 mouse_button::state x_button_state(unsigned int button)
38 mouse_button::state b = mouse_button::none;
41 b = mouse_button::button1;
44 b = mouse_button::button2;
47 b = mouse_button::button3;
50 b = mouse_button::button4;
53 b = mouse_button::button5;
60 key_modifier::state x_key_state(unsigned int state)
62 key_modifier::state k = key_modifier::none;
63 if (state & ControlMask)
64 k |= key_modifier::ctrl;
65 if (state & ShiftMask)
66 k |= key_modifier::shift;
68 k |= key_modifier::alt;
75 void C_scroll_cb(FL_OBJECT * ob, long)
77 XWorkArea * area = static_cast<XWorkArea *>(ob->u_vdata);
82 int C_work_area_handler(FL_OBJECT * ob, int event, FL_Coord, FL_Coord,
85 return XWorkArea::work_area_handler(ob, event, 0, 0, key, xev);
89 int C_event_cb(FL_FORM * form, void * xev)
91 XWorkArea * area = static_cast<XWorkArea *>(form->u_vdata);
92 return area->event_cb(static_cast<XEvent *>(xev));
99 XWorkArea::XWorkArea(int x, int y, int w, int h)
100 : workareapixmap(0), painter_(*this)
102 if (lyxerr.debugging(Debug::WORKAREA)) {
103 lyxerr << "\tbackground box: +"
104 << x << '+' << y << ' '
105 << w - 15 << 'x' << h << endl;
108 fl_freeze_all_forms();
112 obj = fl_add_box(FL_BORDER_BOX, x, y, w - 15, h, "");
113 fl_set_object_resize(obj, FL_RESIZE_ALL);
114 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
116 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
117 x + w - 15, y, 17, h, "");
118 fl_set_object_boxtype(obj, FL_UP_BOX);
119 fl_set_object_resize(obj, FL_RESIZE_ALL);
120 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
122 fl_set_object_callback(obj, C_scroll_cb, 0);
123 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
124 fl_set_scrollbar_value(scrollbar, 0.0);
125 fl_set_scrollbar_size(scrollbar, scrollbar->h);
127 int const bw = int(abs(fl_get_border_width()));
129 // Create the workarea pixmap
130 // FIXME remove redraw(w - 15 - 2 * bw, h - 2 * bw);
132 if (lyxerr.debugging(Debug::WORKAREA))
133 lyxerr << "\tfree object: +"
134 << x + bw << '+' << y + bw << ' '
135 << w - 15 - 2 * bw << 'x'
136 << h - 2 * bw << endl;
138 // We add this object as late as possible to avoid problems
141 work_area = obj = fl_add_free(FL_ALL_FREE,
145 C_work_area_handler);
146 obj->wantkey = FL_KEY_ALL;
149 fl_set_object_boxtype(obj,FL_DOWN_BOX);
150 fl_set_object_resize(obj, FL_RESIZE_ALL);
151 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
153 /// X selection hook - xforms gets it wrong
154 fl_current_form->u_vdata = this;
155 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_event_cb);
157 fl_unfreeze_all_forms();
161 val.function = GXcopy;
162 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
167 XWorkArea::~XWorkArea()
169 XFreeGC(fl_get_display(), copy_gc);
171 XFreePixmap(fl_get_display(), workareapixmap);
175 void XWorkArea::redraw(int width, int height)
177 static int cur_width = -1;
178 static int cur_height = -1;
180 if (cur_width == width && cur_height == height && workareapixmap) {
181 XCopyArea(fl_get_display(),
182 getPixmap(), getWin(), copy_gc,
183 0, 0, width, height, xpos(), ypos());
190 if (lyxerr.debugging(Debug::WORKAREA)) {
191 lyxerr << "(Re)creating pixmap ("
192 << width << 'x' << height << ')' << endl;
195 if (workareapixmap) {
196 XFreePixmap(fl_get_display(), workareapixmap);
199 workareapixmap = XCreatePixmap(fl_get_display(),
200 RootWindow(fl_get_display(), 0),
203 fl_get_visual_depth());
209 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
211 // we need to cache this for scroll_cb
212 doc_height_ = height;
215 fl_set_scrollbar_value(scrollbar, 0.0);
216 fl_set_scrollbar_size(scrollbar, scrollbar->h);
220 long const work_height = workHeight();
222 if (lyxerr.debugging(Debug::GUI)) {
223 lyxerr << "scroll: height now " << height << '\n'
224 << "scroll: work_height " << work_height << endl;
227 /* If the text is smaller than the working area, the scrollbar
228 * maximum must be the working area height. No scrolling will
230 if (height <= work_height) {
231 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !"
233 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
234 fl_set_scrollbar_value(scrollbar, pos);
235 fl_set_scrollbar_size(scrollbar, scrollbar->h);
239 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
240 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
242 fl_set_scrollbar_value(scrollbar, pos);
244 double const slider_size =
245 (height == 0) ? 1.0 : 1.0 / double(height);
247 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
251 // callback for scrollbar slider
252 void XWorkArea::scroll_cb()
254 double const val = fl_get_scrollbar_value(scrollbar);
256 if (lyxerr.debugging(Debug::GUI)) {
257 lyxerr << "scroll: val: " << val << '\n'
258 << "scroll: height: " << scrollbar->h << '\n'
259 << "scroll: docheight: " << doc_height_ << endl;
262 scrollDocView(int(val));
267 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
271 XEvent * ev = static_cast<XEvent*>(xev);
272 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
280 if (!area->work_area || !area->work_area->form->visible)
282 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
283 area->redraw(area->workWidth(), area->workHeight());
287 if (!ev || ev->xbutton.button == 0) break;
288 // Should really have used xbutton.state
289 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
291 FuncRequest(LFUN_MOUSE_PRESS,
292 ev->xbutton.x - ob->x,
293 ev->xbutton.y - ob->y,
294 x_button_state(key)));
298 if (!ev || ev->xbutton.button == 0) break;
299 // Should really have used xbutton.state
300 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
302 FuncRequest(LFUN_MOUSE_RELEASE,
303 ev->xbutton.x - ob->x,
304 ev->xbutton.y - ob->y,
305 x_button_state(key)));
309 if (!ev || !area->scrollbar)
312 int const drag_x = ev->xmotion.x;
313 int const drag_y = ev->xmotion.y;
314 int const area_y = ob->y;
315 int const area_h = ob->h;
317 // Check if the mouse is above or below the workarea
318 if (drag_y <= area_y || drag_y >= area_y + area_h) {
319 // The mouse button is depressed and we are outside the
320 // workarea. That means we are simultaneously selecting
321 // text and scrolling the view.
322 // Use a Timeout to react to a drag events only every
323 // 200ms. All intervening events are discarded,
324 // allowing the user to control position easily.
325 static int const discard_interval = 200;
326 static Timeout timeout(discard_interval);
328 if (timeout.running())
330 // The timeout is not running, so process the
331 // event, first starting the timeout to discard future
336 static int x_old = -1;
337 static int y_old = -1;
338 static double scrollbar_value_old = -1.0;
340 double const scrollbar_value =
341 fl_get_scrollbar_value(area->scrollbar);
343 if (drag_x != x_old || drag_y != y_old ||
344 scrollbar_value != scrollbar_value_old) {
347 scrollbar_value_old = scrollbar_value;
349 lyxerr[Debug::WORKAREA] << "Workarea event: DRAG"
352 // It transpires that ev->xbutton.button == 0 when
353 // the mouse is dragged, so it cannot be used to
354 // initialise x_button_state and hence FuncRequest.
356 // The 'key' that is passed into the function does
357 // contain the necessary info, however.
359 // It is for this reason that x_button_state has
360 // been modified to work with key
361 // rather than ev->xbutton.button.
363 // Angus 15 Oct 2002.
364 FuncRequest cmd(LFUN_MOUSE_MOTION,
365 ev->xbutton.x - ob->x,
366 ev->xbutton.y - ob->y,
367 x_button_state(key));
374 lyxerr[Debug::WORKAREA] << "Workarea event: KEYPRESS" << endl;
378 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
379 XLookupString(xke, dummy, 1, &keysym, 0);
381 if (lyxerr.debugging(Debug::KEY)) {
382 char const * const tmp = XKeysymToString(key);
383 char const * const tmp2 = XKeysymToString(keysym);
384 string const stm = (tmp ? tmp : string());
385 string const stm2 = (tmp2 ? tmp2 : string());
387 lyxerr << "XWorkArea: Key is `" << stm
388 << "' [" << key << "]\n"
389 << "XWorkArea: Keysym is `" << stm2
390 << "' [" << keysym << ']' << endl;
393 // Note that we need this handling because of a bug
394 // in XForms 0.89, if this bug is resolved in the way I hope
395 // we can just use the keysym directly without looking
396 // at key at all. (Lgb)
399 // We might have to add more keysyms here also,
400 // we will do that as the issues arise. (Lgb)
401 if (keysym == XK_space) {
403 lyxerr[Debug::KEY] << "Using keysym [A]"
408 // It seems that this was a bit optimistic...
409 // With this hacking things seems to be better (Lgb)
410 //if (!iscntrl(key)) {
412 // lyxerr[Debug::KEY]
413 // << "Using key [B]\n"
415 // << static_cast<unsigned char>(key)
418 ret_key = (keysym ? keysym : key);
419 lyxerr[Debug::KEY] << "Using keysym [B]"
424 unsigned int const ret_state = xke->state;
426 // If you have a better way to handle "wild-output" of
427 // characters after the key has been released than the one
428 // below, please contact me. (Lgb)
429 static Time last_time_pressed;
430 static unsigned int last_key_pressed;
431 static unsigned int last_state_pressed;
432 lyxerr[Debug::KEY] << "Workarea Diff: "
433 << xke->time - last_time_pressed
435 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
436 && ret_state == last_state_pressed
437 && xke->keycode == last_key_pressed) {
439 << "Workarea: Purging X events." << endl;
440 //lyxerr << "Workarea Events: "
441 // << XEventsQueued(fl_get_display(), QueuedAlready)
443 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
445 // This purge make f.ex. scrolling stop immediately when
446 // releasing the PageDown button. The question is if
447 // this purging of XEvents can cause any harm...
448 // after some testing I can see no problems, but
449 // I'd like other reports too.
452 last_time_pressed = xke->time;
453 last_key_pressed = xke->keycode;
454 last_state_pressed = ret_state;
456 XLyXKeySym * xlk = new XLyXKeySym;
457 xlk->initFromKeySym(ret_key);
459 area->workAreaKeyPress(LyXKeySymPtr(xlk),
460 x_key_state(ret_state));
465 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
469 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
470 fl_set_cursor(FL_ObjWin(area->work_area), XC_xterm);
474 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
475 // There should be no need for this. But there is.
476 fl_set_cursor(FL_ObjWin(area->work_area), FL_DEFAULT_CURSOR);
481 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
482 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
483 ev->xbutton.x - ob->x,
484 ev->xbutton.y - ob->y,
485 x_button_state(key));
492 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
493 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
494 ev->xbutton.x - ob->x,
495 ev->xbutton.y - ob->y,
496 x_button_state(key));
503 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
513 string clipboard_selection;
514 bool clipboard_read = false;
518 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
519 void const * data, long size)
521 clipboard_selection.erase();
524 clipboard_selection.reserve(size);
525 for (int i = 0; i < size; ++i)
526 clipboard_selection += static_cast<char const *>(data)[i];
527 clipboard_read = true;
535 int XWorkArea::event_cb(XEvent * xev)
538 case SelectionRequest:
539 lyxerr[Debug::GUI] << "X requested selection." << endl;
540 selectionRequested();
543 lyxerr[Debug::GUI] << "Lost selection." << endl;
551 void XWorkArea::haveSelection(bool yes) const
553 Window const owner = yes ? FL_ObjWin(work_area) : None;
554 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, owner, CurrentTime);
558 string const XWorkArea::getClipboard() const
560 clipboard_read = false;
562 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
567 while (!clipboard_read) {
568 if (fl_check_forms() == FL_EVENT) {
570 lyxerr << "Received unhandled X11 event\n"
571 << "Type: 0x" << hex << ev.xany.type
572 << " Target: 0x" << hex << ev.xany.window
576 return clipboard_selection;
580 void XWorkArea::putClipboard(string const & s) const
585 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);