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"
22 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
23 #include "lyxlookup.h"
26 #include "support/filetools.h" // LibFileSearch
27 #include "support/lstrings.h"
28 #include "support/LAssert.h"
33 // xforms doesn't define this (but it should be in <forms.h>).
35 FL_APPEVENT_CB fl_set_preemptive_callback(Window, FL_APPEVENT_CB, void *);
46 XSync(fl_get_display(), 0);
50 void setXtermCursor(Window win)
53 static bool cursor_undefined = true;
54 if (cursor_undefined) {
55 cursor = XCreateFontCursor(fl_get_display(), XC_xterm);
56 XFlush(fl_get_display());
57 cursor_undefined = false;
59 XDefineCursor(fl_get_display(), win, cursor);
60 XFlush(fl_get_display());
65 mouse_button::state x_button_state(unsigned int button)
67 mouse_button::state b = mouse_button::none;
70 b = mouse_button::button1;
73 b = mouse_button::button2;
76 b = mouse_button::button3;
79 b = mouse_button::button4;
82 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 createPixmap(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 XWorkArea::~XWorkArea()
219 XFreePixmap(fl_get_display(), workareapixmap);
224 void destroy_object(FL_OBJECT * obj)
232 fl_delete_object(obj);
238 void XWorkArea::createPixmap(int width, int height)
240 static int cur_width = -1;
241 static int cur_height = -1;
243 if (cur_width == width && cur_height == height && workareapixmap)
250 XFreePixmap(fl_get_display(), workareapixmap);
252 if (lyxerr.debugging(Debug::WORKAREA))
253 lyxerr << "Creating pixmap ("
254 << width << 'x' << height << ")" << endl;
256 workareapixmap = XCreatePixmap(fl_get_display(),
257 RootWindow(fl_get_display(), 0),
260 fl_get_visual_depth());
261 if (lyxerr.debugging(Debug::WORKAREA))
262 lyxerr << "\tpixmap=" << workareapixmap << endl;
266 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
268 // we need to cache this for scroll_cb
269 doc_height_ = height;
272 fl_set_scrollbar_value(scrollbar, 0.0);
273 fl_set_scrollbar_size(scrollbar, scrollbar->h);
277 long const work_height = workHeight();
279 lyxerr[Debug::GUI] << "scroll: height now " << height << endl;
280 lyxerr[Debug::GUI] << "scroll: work_height " << work_height << endl;
282 /* If the text is smaller than the working area, the scrollbar
283 * maximum must be the working area height. No scrolling will
285 if (height <= work_height) {
286 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !" << endl;
287 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
288 fl_set_scrollbar_value(scrollbar, pos);
289 fl_set_scrollbar_size(scrollbar, scrollbar->h);
293 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
294 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
296 fl_set_scrollbar_value(scrollbar, pos);
298 double const slider_size =
299 (height == 0) ? 1.0 : 1.0 / double(height);
301 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
305 // callback for scrollbar slider
306 void XWorkArea::scroll_cb()
308 double const val = fl_get_scrollbar_value(scrollbar);
309 lyxerr[Debug::GUI] << "scroll: val: " << val << endl;
310 lyxerr[Debug::GUI] << "scroll: height: " << scrollbar->h << endl;
311 lyxerr[Debug::GUI] << "scroll: docheight: " << doc_height_ << endl;
312 scrollDocView(int(val));
317 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
321 static int x_old = -1;
322 static int y_old = -1;
323 static long scrollbar_value_old = -1;
325 XEvent * ev = static_cast<XEvent*>(xev);
326 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
332 if (!area->work_area ||
333 !area->work_area->form->visible)
335 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
336 area->createPixmap(area->workWidth(), area->workHeight());
337 area->workAreaResize();
340 if (!ev || ev->xbutton.button == 0) break;
341 // Should really have used xbutton.state
342 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
343 area->workAreaButtonPress(ev->xbutton.x - ob->x,
344 ev->xbutton.y - ob->y,
345 x_button_state(ev->xbutton.button));
348 if (!ev || ev->xbutton.button == 0) break;
349 // Should really have used xbutton.state
350 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
351 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
352 ev->xbutton.y - ob->y,
353 x_button_state(ev->xbutton.button));
355 #if FL_VERSION < 1 && FL_REVISION < 89
360 if (!ev || ! area->scrollbar) break;
361 if (ev->xmotion.x != x_old ||
362 ev->xmotion.y != y_old ||
363 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
365 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
366 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
367 ev->xmotion.y - ob->y,
368 x_motion_state(ev->xbutton.state));
371 #if FL_VERSION < 1 && FL_REVISION < 89
377 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
381 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
382 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
383 // XForms < 0.89.5 does not have compose support
384 // so we are using our own compose support
385 LyXLookupString(ev, dummy, 1, &keysym);
387 XLookupString(xke, dummy, 1, &keysym, 0);
388 // int num_keys = XLookupString(xke, dummy, 10, &keysym, &xcs);
389 // lyxerr << "We have " << num_keys << " keys in the returned buffer" << endl;
390 // lyxerr << "Our dummy string is " << dummy << endl;
393 if (lyxerr.debugging(Debug::KEY)) {
394 char const * tmp = XKeysymToString(key);
395 char const * tmp2 = XKeysymToString(keysym);
396 string const stm = (tmp ? tmp : "");
397 string const stm2 = (tmp2 ? tmp2 : "");
399 lyxerr[Debug::KEY] << "XWorkArea: Key is `" << stm << "' ["
400 << key << "]" << endl;
401 lyxerr[Debug::KEY] << "XWorkArea: Keysym is `" << stm2 << "' ["
402 << keysym << "]" << endl;
405 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
406 if (keysym == NoSymbol) {
408 << "Empty kdb action (probably composing)"
412 KeySym ret_key = keysym;
414 // Note that we need this handling because of a bug
415 // in XForms 0.89, if this bug is resolved in the way I hope
416 // we can just use the keysym directly with out looking
417 // at key at all. (Lgb)
420 // We migth have to add more keysyms here also,
421 // we will do that as the issues arise. (Lgb)
422 if (keysym == XK_space) {
424 lyxerr[Debug::KEY] << "Using keysym [A]"
429 // It seems that this was a bit optimistic...
430 // With this hacking things seems to be better (Lgb)
431 //if (!iscntrl(key)) {
433 // lyxerr[Debug::KEY]
434 // << "Using key [B]\n"
436 // << static_cast<unsigned char>(key)
439 ret_key = (keysym ? keysym : key);
440 lyxerr[Debug::KEY] << "Using keysym [B]"
446 unsigned int const ret_state = xke->state;
448 // If you have a better way to handle "wild-output" of
449 // characters after the key has been released than the one
450 // below, please contact me. (Lgb)
451 static Time last_time_pressed;
452 static unsigned int last_key_pressed;
453 static unsigned int last_state_pressed;
454 lyxerr[Debug::KEY] << "Workarea Diff: "
455 << xke->time - last_time_pressed
457 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
458 && ret_state == last_state_pressed
459 && xke->keycode == last_key_pressed) {
461 << "Workarea: Purging X events." << endl;
462 //lyxerr << "Workarea Events: "
463 // << XEventsQueued(fl_get_display(), QueuedAlready)
465 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
466 XSync(fl_get_display(), 1);
467 // This purge make f.ex. scrolling stop immidiatly when
468 // releasing the PageDown button. The question is if
469 // this purging of XEvents can cause any harm...
470 // after some testing I can see no problems, but
471 // I'd like other reports too.
474 last_time_pressed = xke->time;
475 last_key_pressed = xke->keycode;
476 last_state_pressed = ret_state;
478 XLyXKeySym * xlk = new XLyXKeySym;
479 xlk->initFromKeySym(ret_key);
481 area->workAreaKeyPress(LyXKeySymPtr(xlk),
482 x_key_state(ret_state));
486 #if FL_VERSION > 0 || FL_REVISION >= 89
488 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
493 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
496 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
500 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
501 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
502 ev->xbutton.y - ob->y,
503 x_button_state(ev->xbutton.button));
507 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
508 area->workAreaTripleClick(ev->xbutton.x - ob->x,
509 ev->xbutton.y - ob->y,
510 x_button_state(ev->xbutton.button));
514 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
524 string clipboard_selection;
525 bool clipboard_read = false;
530 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
531 void const * data, long size)
533 clipboard_selection.erase();
536 clipboard_selection.reserve(size);
537 for (int i = 0; i < size; ++i)
538 clipboard_selection +=
539 static_cast<char const *>(data)[i];
540 clipboard_read = true;
549 int XWorkArea::event_cb(XEvent * xev)
553 case SelectionRequest:
554 lyxerr[Debug::GUI] << "X requested selection." << endl;
555 selectionRequested();
558 lyxerr[Debug::GUI] << "Lost selection." << endl;
566 void XWorkArea::haveSelection(bool yes) const
569 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
573 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
577 string const XWorkArea::getClipboard() const
579 clipboard_read = false;
581 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
586 while (!clipboard_read) {
587 if (fl_check_forms() == FL_EVENT) {
589 lyxerr << "Received unhandled X11 event" << endl;
590 lyxerr << "Type: 0x" << hex << ev.xany.type <<
591 " Target: 0x" << hex << ev.xany.window << endl;
594 return clipboard_selection;
598 void XWorkArea::putClipboard(string const & s) const
603 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);