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"
31 void waitForX(bool discard)
33 XSync(fl_get_display(), discard);
37 mouse_button::state x_button_state(unsigned int button)
39 mouse_button::state b = mouse_button::none;
42 b = mouse_button::button1;
45 b = mouse_button::button2;
48 b = mouse_button::button3;
51 b = mouse_button::button4;
54 b = mouse_button::button5;
61 key_modifier::state x_key_state(unsigned int state)
63 key_modifier::state k = key_modifier::none;
64 if (state & ControlMask)
65 k |= key_modifier::ctrl;
66 if (state & ShiftMask)
67 k |= key_modifier::shift;
69 k |= key_modifier::alt;
76 void C_scroll_cb(FL_OBJECT * ob, long)
78 XWorkArea * area = static_cast<XWorkArea *>(ob->u_vdata);
83 int C_work_area_handler(FL_OBJECT * ob, int event, FL_Coord, FL_Coord,
86 return XWorkArea::work_area_handler(ob, event, 0, 0, key, xev);
90 int C_event_cb(FL_FORM * form, void * xev)
92 XWorkArea * area = static_cast<XWorkArea *>(form->u_vdata);
93 return area->event_cb(static_cast<XEvent *>(xev));
100 XWorkArea::XWorkArea(int x, int y, int w, int h)
101 : workareapixmap(0), painter_(*this)
103 if (lyxerr.debugging(Debug::WORKAREA)) {
104 lyxerr << "\tbackground box: +"
105 << x << '+' << y << ' '
106 << w - 15 << 'x' << h << endl;
109 fl_freeze_all_forms();
113 obj = fl_add_box(FL_BORDER_BOX, x, y, w - 15, h, "");
114 fl_set_object_resize(obj, FL_RESIZE_ALL);
115 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
117 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
118 x + w - 15, y, 17, h, "");
119 fl_set_object_boxtype(obj, FL_UP_BOX);
120 fl_set_object_resize(obj, FL_RESIZE_ALL);
121 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
123 fl_set_object_callback(obj, C_scroll_cb, 0);
124 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
125 fl_set_scrollbar_value(scrollbar, 0.0);
126 fl_set_scrollbar_size(scrollbar, scrollbar->h);
128 int const bw = int(abs(fl_get_border_width()));
130 // Create the workarea pixmap
131 // FIXME remove redraw(w - 15 - 2 * bw, h - 2 * bw);
133 if (lyxerr.debugging(Debug::WORKAREA))
134 lyxerr << "\tfree object: +"
135 << x + bw << '+' << y + bw << ' '
136 << w - 15 - 2 * bw << 'x'
137 << h - 2 * bw << endl;
139 // We add this object as late as possible to avoid problems
142 work_area = obj = fl_add_free(FL_ALL_FREE,
146 C_work_area_handler);
147 obj->wantkey = FL_KEY_ALL;
150 fl_set_object_boxtype(obj,FL_DOWN_BOX);
151 fl_set_object_resize(obj, FL_RESIZE_ALL);
152 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
154 /// X selection hook - xforms gets it wrong
155 fl_current_form->u_vdata = this;
156 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_event_cb);
158 fl_unfreeze_all_forms();
162 val.function = GXcopy;
163 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
168 XWorkArea::~XWorkArea()
170 XFreeGC(fl_get_display(), copy_gc);
172 XFreePixmap(fl_get_display(), workareapixmap);
176 void XWorkArea::redraw(int width, int height)
178 static int cur_width = -1;
179 static int cur_height = -1;
181 if (cur_width == width && cur_height == height && workareapixmap) {
182 XCopyArea(fl_get_display(),
183 getPixmap(), getWin(), copy_gc,
184 0, 0, width, height, xpos(), ypos());
191 if (lyxerr.debugging(Debug::WORKAREA)) {
192 lyxerr << "(Re)creating pixmap ("
193 << width << 'x' << height << ')' << endl;
196 if (workareapixmap) {
197 XFreePixmap(fl_get_display(), workareapixmap);
200 workareapixmap = XCreatePixmap(fl_get_display(),
201 RootWindow(fl_get_display(), 0),
204 fl_get_visual_depth());
210 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
212 // we need to cache this for scroll_cb
213 doc_height_ = height;
216 fl_set_scrollbar_value(scrollbar, 0.0);
217 fl_set_scrollbar_size(scrollbar, scrollbar->h);
221 long const work_height = workHeight();
223 if (lyxerr.debugging(Debug::GUI)) {
224 lyxerr << "scroll: height now " << height << '\n'
225 << "scroll: work_height " << work_height << endl;
228 /* If the text is smaller than the working area, the scrollbar
229 * maximum must be the working area height. No scrolling will
231 if (height <= work_height) {
232 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !"
234 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
235 fl_set_scrollbar_value(scrollbar, pos);
236 fl_set_scrollbar_size(scrollbar, scrollbar->h);
240 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
241 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
243 fl_set_scrollbar_value(scrollbar, pos);
245 double const slider_size =
246 (height == 0) ? 1.0 : 1.0 / double(height);
248 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
252 // callback for scrollbar slider
253 void XWorkArea::scroll_cb()
255 double const val = fl_get_scrollbar_value(scrollbar);
257 if (lyxerr.debugging(Debug::GUI)) {
258 lyxerr << "scroll: val: " << val << '\n'
259 << "scroll: height: " << scrollbar->h << '\n'
260 << "scroll: docheight: " << doc_height_ << endl;
263 scrollDocView(int(val));
268 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
272 XEvent * ev = static_cast<XEvent*>(xev);
273 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
279 if (!area->work_area || !area->work_area->form->visible)
281 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
282 area->redraw(area->workWidth(), area->workHeight());
285 if (!ev || ev->xbutton.button == 0) break;
286 // Should really have used xbutton.state
287 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
289 FuncRequest(LFUN_MOUSE_PRESS,
290 ev->xbutton.x - ob->x,
291 ev->xbutton.y - ob->y,
292 x_button_state(key)));
295 if (!ev || ev->xbutton.button == 0) break;
296 // Should really have used xbutton.state
297 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
299 FuncRequest(LFUN_MOUSE_RELEASE,
300 ev->xbutton.x - ob->x,
301 ev->xbutton.y - ob->y,
302 x_button_state(key)));
306 if (!ev || !area->scrollbar)
309 int const drag_x = ev->xmotion.x;
310 int const drag_y = ev->xmotion.y;
311 int const area_y = ob->y;
312 int const area_h = ob->h;
314 // Check if the mouse is above or below the workarea
315 if (drag_y <= area_y || drag_y >= area_y + area_h) {
316 // The mouse button is depressed and we are outside the
317 // workarea. That means we are simultaneously selecting
318 // text and scrolling the view.
319 // Use a Timeout to react to a drag events only every
320 // 200ms. All intervening events are discarded,
321 // allowing the user to control position easily.
322 static int const discard_interval = 200;
323 static Timeout timeout(discard_interval);
325 if (timeout.running())
327 // The timeout is not running, so process the
328 // event, first starting the timeout to discard future
333 static int x_old = -1;
334 static int y_old = -1;
335 static double scrollbar_value_old = -1.0;
337 double const scrollbar_value =
338 fl_get_scrollbar_value(area->scrollbar);
340 if (drag_x != x_old || drag_y != y_old ||
341 scrollbar_value != scrollbar_value_old) {
344 scrollbar_value_old = scrollbar_value;
346 lyxerr[Debug::WORKAREA] << "Workarea event: DRAG"
349 // It transpires that ev->xbutton.button == 0 when
350 // the mouse is dragged, so it cannot be used to
351 // initialise x_button_state and hence FuncRequest.
353 // The 'key' that is passed into the function does
354 // contain the necessary info, however.
356 // It is for this reason that x_button_state has
357 // been modified to work with key
358 // rather than ev->xbutton.button.
360 // Angus 15 Oct 2002.
361 FuncRequest cmd(LFUN_MOUSE_MOTION,
362 ev->xbutton.x - ob->x,
363 ev->xbutton.y - ob->y,
364 x_button_state(key));
372 lyxerr[Debug::WORKAREA] << "Workarea event: KEYPRESS" << endl;
376 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
377 XLookupString(xke, dummy, 1, &keysym, 0);
379 if (lyxerr.debugging(Debug::KEY)) {
380 char const * const tmp = XKeysymToString(key);
381 char const * const tmp2 = XKeysymToString(keysym);
382 string const stm = (tmp ? tmp : string());
383 string const stm2 = (tmp2 ? tmp2 : string());
385 lyxerr << "XWorkArea: Key is `" << stm
386 << "' [" << key << "]\n"
387 << "XWorkArea: Keysym is `" << stm2
388 << "' [" << keysym << ']' << endl;
391 // Note that we need this handling because of a bug
392 // in XForms 0.89, if this bug is resolved in the way I hope
393 // we can just use the keysym directly without looking
394 // at key at all. (Lgb)
397 // We might have to add more keysyms here also,
398 // we will do that as the issues arise. (Lgb)
399 if (keysym == XK_space) {
401 lyxerr[Debug::KEY] << "Using keysym [A]"
406 // It seems that this was a bit optimistic...
407 // With this hacking things seems to be better (Lgb)
408 //if (!iscntrl(key)) {
410 // lyxerr[Debug::KEY]
411 // << "Using key [B]\n"
413 // << static_cast<unsigned char>(key)
416 ret_key = (keysym ? keysym : key);
417 lyxerr[Debug::KEY] << "Using keysym [B]"
422 unsigned int const ret_state = xke->state;
424 // If you have a better way to handle "wild-output" of
425 // characters after the key has been released than the one
426 // below, please contact me. (Lgb)
427 static Time last_time_pressed;
428 static unsigned int last_key_pressed;
429 static unsigned int last_state_pressed;
430 lyxerr[Debug::KEY] << "Workarea Diff: "
431 << xke->time - last_time_pressed
433 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
434 && ret_state == last_state_pressed
435 && xke->keycode == last_key_pressed) {
437 << "Workarea: Purging X events." << endl;
438 //lyxerr << "Workarea Events: "
439 // << XEventsQueued(fl_get_display(), QueuedAlready)
441 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
443 // This purge make f.ex. scrolling stop immediately when
444 // releasing the PageDown button. The question is if
445 // this purging of XEvents can cause any harm...
446 // after some testing I can see no problems, but
447 // I'd like other reports too.
450 last_time_pressed = xke->time;
451 last_key_pressed = xke->keycode;
452 last_state_pressed = ret_state;
454 XLyXKeySym * xlk = new XLyXKeySym;
455 xlk->initFromKeySym(ret_key);
457 area->workAreaKeyPress(LyXKeySymPtr(xlk),
458 x_key_state(ret_state));
463 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
467 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
468 fl_set_cursor(FL_ObjWin(area->work_area), XC_xterm);
471 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
472 // There should be no need for this. But there is.
473 fl_set_cursor(FL_ObjWin(area->work_area), FL_DEFAULT_CURSOR);
477 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
478 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
479 ev->xbutton.x - ob->x,
480 ev->xbutton.y - ob->y,
481 x_button_state(key));
487 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
488 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
489 ev->xbutton.x - ob->x,
490 ev->xbutton.y - ob->y,
491 x_button_state(key));
497 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
507 string clipboard_selection;
508 bool clipboard_read = false;
512 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
513 void const * data, long size)
515 clipboard_selection.erase();
518 clipboard_selection.reserve(size);
519 for (int i = 0; i < size; ++i)
520 clipboard_selection += static_cast<char const *>(data)[i];
521 clipboard_read = true;
529 int XWorkArea::event_cb(XEvent * xev)
532 case SelectionRequest:
533 lyxerr[Debug::GUI] << "X requested selection." << endl;
534 selectionRequested();
537 lyxerr[Debug::GUI] << "Lost selection." << endl;
545 void XWorkArea::haveSelection(bool yes) const
547 Window const owner = yes ? FL_ObjWin(work_area) : None;
548 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, owner, CurrentTime);
552 string const XWorkArea::getClipboard() const
554 clipboard_read = false;
556 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
561 while (!clipboard_read) {
562 if (fl_check_forms() == FL_EVENT) {
564 lyxerr << "Received unhandled X11 event\n"
565 << "Type: 0x" << hex << ev.xany.type
566 << " Target: 0x" << hex << ev.xany.window
570 return clipboard_selection;
574 void XWorkArea::putClipboard(string const & s) const
579 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);