3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
7 * \author John Levon <moz@compsoc.man.ac.uk>
13 #pragma implementation
16 #include "XWorkArea.h"
19 #include "XLyXKeySym.h"
20 #include "ColorHandler.h"
21 #include "funcrequest.h"
23 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
24 #include "lyxlookup.h"
27 #include "support/filetools.h" // LibFileSearch
28 #include "support/lstrings.h"
29 #include "support/LAssert.h"
34 // xforms doesn't define this (but it should be in <forms.h>).
36 FL_APPEVENT_CB fl_set_preemptive_callback(Window, FL_APPEVENT_CB, void *);
48 XSync(fl_get_display(), 0);
52 void setXtermCursor(Window win)
55 static bool cursor_undefined = true;
56 if (cursor_undefined) {
57 cursor = XCreateFontCursor(fl_get_display(), XC_xterm);
58 XFlush(fl_get_display());
59 cursor_undefined = false;
61 XDefineCursor(fl_get_display(), win, cursor);
62 XFlush(fl_get_display());
66 mouse_button::state x_button_state(unsigned int button)
68 mouse_button::state b = mouse_button::none;
71 b = mouse_button::button1;
74 b = mouse_button::button2;
77 b = mouse_button::button3;
80 b = mouse_button::button4;
83 b = mouse_button::button5;
92 mouse_button::state x_motion_state(unsigned int state)
94 mouse_button::state b = mouse_button::none;
95 if (state & Button1MotionMask)
96 b |= mouse_button::button1;
97 if (state & Button2MotionMask)
98 b |= mouse_button::button2;
99 if (state & Button3MotionMask)
100 b |= mouse_button::button3;
101 if (state & Button4MotionMask)
102 b |= mouse_button::button4;
103 if (state & Button5MotionMask)
104 b |= mouse_button::button5;
109 key_modifier::state x_key_state(unsigned int state)
111 key_modifier::state k = key_modifier::none;
112 if (state & ControlMask)
113 k |= key_modifier::ctrl;
114 if (state & ShiftMask)
115 k |= key_modifier::shift;
116 if (state & Mod1Mask)
117 k |= key_modifier::alt;
126 // Just a bunch of C wrappers around static members of XWorkArea
128 void C_XWorkArea_scroll_cb(FL_OBJECT * ob, long)
130 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
136 int C_XWorkArea_work_area_handler(FL_OBJECT * ob, int event,
140 return XWorkArea::work_area_handler(ob, event,
145 int C_XWorkAreaEventCB(FL_FORM * form, void * xev) {
146 XWorkArea * wa = static_cast<XWorkArea*>(form->u_vdata);
147 return wa->event_cb(static_cast<XEvent*>(xev));
152 XWorkArea::XWorkArea(int x, int y, int w, int h)
153 : workareapixmap(0), painter_(*this)
155 fl_freeze_all_forms();
159 if (lyxerr.debugging(Debug::WORKAREA))
160 lyxerr << "\tbackground box: +"
161 << x << '+' << y << ' '
162 << w - 15 << 'x' << h << endl;
163 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
167 fl_set_object_resize(obj, FL_RESIZE_ALL);
168 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
170 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
173 fl_set_object_boxtype(obj, FL_UP_BOX);
174 fl_set_object_resize(obj, FL_RESIZE_ALL);
175 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
177 fl_set_object_callback(obj, C_XWorkArea_scroll_cb, 0);
178 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
179 fl_set_scrollbar_value(scrollbar, 0.0);
180 fl_set_scrollbar_size(scrollbar, scrollbar->h);
182 int const bw = int(abs(fl_get_border_width()));
184 // Create the workarea pixmap
185 // FIXME remove redraw(w - 15 - 2 * bw, h - 2 * bw);
187 if (lyxerr.debugging(Debug::WORKAREA))
188 lyxerr << "\tfree object: +"
189 << x + bw << '+' << y + bw << ' '
190 << w - 15 - 2 * bw << 'x'
191 << h - 2 * bw << endl;
193 // We add this object as late as possible to avoid problems
196 work_area = obj = fl_add_free(FL_ALL_FREE,
200 C_XWorkArea_work_area_handler);
201 obj->wantkey = FL_KEY_ALL;
204 fl_set_object_boxtype(obj,FL_DOWN_BOX);
205 fl_set_object_resize(obj, FL_RESIZE_ALL);
206 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
208 /// X selection hook - xforms gets it wrong
209 fl_current_form->u_vdata = this;
210 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_XWorkAreaEventCB);
212 fl_unfreeze_all_forms();
216 val.function = GXcopy;
217 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
222 XWorkArea::~XWorkArea()
224 XFreeGC(fl_get_display(), copy_gc);
226 XFreePixmap(fl_get_display(), workareapixmap);
231 void destroy_object(FL_OBJECT * obj)
239 fl_delete_object(obj);
245 void XWorkArea::redraw(int width, int height)
247 static int cur_width = -1;
248 static int cur_height = -1;
250 if (cur_width == width && cur_height == height && workareapixmap) {
251 XCopyArea(fl_get_display(),
252 getPixmap(), getWin(), copy_gc,
253 0, 0, width, height, xpos(), ypos());
260 if (lyxerr.debugging(Debug::WORKAREA)) {
261 lyxerr << "(Re)creating pixmap ("
262 << width << 'x' << height << ")" << endl;
265 if (workareapixmap) {
266 XFreePixmap(fl_get_display(), workareapixmap);
269 workareapixmap = XCreatePixmap(fl_get_display(),
270 RootWindow(fl_get_display(), 0),
273 fl_get_visual_depth());
279 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
281 // we need to cache this for scroll_cb
282 doc_height_ = height;
285 fl_set_scrollbar_value(scrollbar, 0.0);
286 fl_set_scrollbar_size(scrollbar, scrollbar->h);
290 long const work_height = workHeight();
292 lyxerr[Debug::GUI] << "scroll: height now " << height << endl;
293 lyxerr[Debug::GUI] << "scroll: work_height " << work_height << endl;
295 /* If the text is smaller than the working area, the scrollbar
296 * maximum must be the working area height. No scrolling will
298 if (height <= work_height) {
299 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !" << endl;
300 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
301 fl_set_scrollbar_value(scrollbar, pos);
302 fl_set_scrollbar_size(scrollbar, scrollbar->h);
306 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
307 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
309 fl_set_scrollbar_value(scrollbar, pos);
311 double const slider_size =
312 (height == 0) ? 1.0 : 1.0 / double(height);
314 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
318 // callback for scrollbar slider
319 void XWorkArea::scroll_cb()
321 double const val = fl_get_scrollbar_value(scrollbar);
322 lyxerr[Debug::GUI] << "scroll: val: " << val << endl;
323 lyxerr[Debug::GUI] << "scroll: height: " << scrollbar->h << endl;
324 lyxerr[Debug::GUI] << "scroll: docheight: " << doc_height_ << endl;
325 scrollDocView(int(val));
330 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
334 static int x_old = -1;
335 static int y_old = -1;
336 static double scrollbar_value_old = -1.0;
338 XEvent * ev = static_cast<XEvent*>(xev);
339 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
345 if (!area->work_area ||
346 !area->work_area->form->visible)
348 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
349 area->redraw(area->workWidth(), area->workHeight());
352 if (!ev || ev->xbutton.button == 0) break;
353 // Should really have used xbutton.state
354 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
356 FuncRequest(LFUN_MOUSE_PRESS, ev->xbutton.x - ob->x,
357 ev->xbutton.y - ob->y,
358 x_button_state(ev->xbutton.button)));
361 if (!ev || ev->xbutton.button == 0) break;
362 // Should really have used xbutton.state
363 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
365 FuncRequest(LFUN_MOUSE_RELEASE, ev->xbutton.x - ob->x,
366 ev->xbutton.y - ob->y,
367 x_button_state(ev->xbutton.button)));
369 #if FL_VERSION < 1 && FL_REVISION < 89
374 if (!ev || !area->scrollbar)
376 if (ev->xmotion.x != x_old ||
377 ev->xmotion.y != y_old ||
378 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
380 x_old = ev->xmotion.x;
381 y_old = ev->xmotion.y;
382 scrollbar_value_old = fl_get_scrollbar_value(area->scrollbar);
383 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
385 FuncRequest(LFUN_MOUSE_MOTION, ev->xbutton.x - ob->x,
386 ev->xbutton.y - ob->y,
387 x_button_state(ev->xbutton.button)));
390 #if FL_VERSION < 1 && FL_REVISION < 89
396 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
400 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
401 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
402 // XForms < 0.89.5 does not have compose support
403 // so we are using our own compose support
404 LyXLookupString(ev, dummy, 1, &keysym);
406 XLookupString(xke, dummy, 1, &keysym, 0);
407 // int num_keys = XLookupString(xke, dummy, 10, &keysym, &xcs);
408 // lyxerr << "We have " << num_keys << " keys in the returned buffer" << endl;
409 // lyxerr << "Our dummy string is " << dummy << endl;
412 if (lyxerr.debugging(Debug::KEY)) {
413 char const * tmp = XKeysymToString(key);
414 char const * tmp2 = XKeysymToString(keysym);
415 string const stm = (tmp ? tmp : "");
416 string const stm2 = (tmp2 ? tmp2 : "");
418 lyxerr[Debug::KEY] << "XWorkArea: Key is `" << stm << "' ["
419 << key << "]" << endl;
420 lyxerr[Debug::KEY] << "XWorkArea: Keysym is `" << stm2 << "' ["
421 << keysym << "]" << endl;
424 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
425 if (keysym == NoSymbol) {
427 << "Empty kdb action (probably composing)"
431 KeySym ret_key = keysym;
433 // Note that we need this handling because of a bug
434 // in XForms 0.89, if this bug is resolved in the way I hope
435 // we can just use the keysym directly with out looking
436 // at key at all. (Lgb)
439 // We migth have to add more keysyms here also,
440 // we will do that as the issues arise. (Lgb)
441 if (keysym == XK_space) {
443 lyxerr[Debug::KEY] << "Using keysym [A]"
448 // It seems that this was a bit optimistic...
449 // With this hacking things seems to be better (Lgb)
450 //if (!iscntrl(key)) {
452 // lyxerr[Debug::KEY]
453 // << "Using key [B]\n"
455 // << static_cast<unsigned char>(key)
458 ret_key = (keysym ? keysym : key);
459 lyxerr[Debug::KEY] << "Using keysym [B]"
465 unsigned int const ret_state = xke->state;
467 // If you have a better way to handle "wild-output" of
468 // characters after the key has been released than the one
469 // below, please contact me. (Lgb)
470 static Time last_time_pressed;
471 static unsigned int last_key_pressed;
472 static unsigned int last_state_pressed;
473 lyxerr[Debug::KEY] << "Workarea Diff: "
474 << xke->time - last_time_pressed
476 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
477 && ret_state == last_state_pressed
478 && xke->keycode == last_key_pressed) {
480 << "Workarea: Purging X events." << endl;
481 //lyxerr << "Workarea Events: "
482 // << XEventsQueued(fl_get_display(), QueuedAlready)
484 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
485 XSync(fl_get_display(), 1);
486 // This purge make f.ex. scrolling stop immidiatly when
487 // releasing the PageDown button. The question is if
488 // this purging of XEvents can cause any harm...
489 // after some testing I can see no problems, but
490 // I'd like other reports too.
493 last_time_pressed = xke->time;
494 last_key_pressed = xke->keycode;
495 last_state_pressed = ret_state;
497 XLyXKeySym * xlk = new XLyXKeySym;
498 xlk->initFromKeySym(ret_key);
500 area->workAreaKeyPress(LyXKeySymPtr(xlk),
501 x_key_state(ret_state));
505 #if FL_VERSION > 0 || FL_REVISION >= 89
507 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
512 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
515 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
519 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
520 FuncRequest cmd(LFUN_MOUSE_DOUBLE, ev->xbutton.x - ob->x,
521 ev->xbutton.y - ob->y,
522 x_button_state(ev->xbutton.button));
528 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
529 FuncRequest cmd(LFUN_MOUSE_TRIPLE, ev->xbutton.x - ob->x,
530 ev->xbutton.y - ob->y,
531 x_button_state(ev->xbutton.button));
537 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
547 string clipboard_selection;
548 bool clipboard_read = false;
553 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
554 void const * data, long size)
556 clipboard_selection.erase();
559 clipboard_selection.reserve(size);
560 for (int i = 0; i < size; ++i)
561 clipboard_selection +=
562 static_cast<char const *>(data)[i];
563 clipboard_read = true;
572 int XWorkArea::event_cb(XEvent * xev)
576 case SelectionRequest:
577 lyxerr[Debug::GUI] << "X requested selection." << endl;
578 selectionRequested();
581 lyxerr[Debug::GUI] << "Lost selection." << endl;
589 void XWorkArea::haveSelection(bool yes) const
592 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
596 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
600 string const XWorkArea::getClipboard() const
602 clipboard_read = false;
604 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
609 while (!clipboard_read) {
610 if (fl_check_forms() == FL_EVENT) {
612 lyxerr << "Received unhandled X11 event" << endl;
613 lyxerr << "Type: 0x" << hex << ev.xany.type <<
614 " Target: 0x" << hex << ev.xany.window << dec << endl;
617 return clipboard_selection;
621 void XWorkArea::putClipboard(string const & s) const
626 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);