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();
341 if (!ev || ev->xbutton.button == 0) break;
342 // Should really have used xbutton.state
343 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
344 area->workAreaButtonPress(ev->xbutton.x - ob->x,
345 ev->xbutton.y - ob->y,
346 x_button_state(ev->xbutton.button));
349 if (!ev || ev->xbutton.button == 0) break;
350 // Should really have used xbutton.state
351 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
352 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
353 ev->xbutton.y - ob->y,
354 x_button_state(ev->xbutton.button));
356 #if FL_VERSION < 1 && FL_REVISION < 89
361 if (!ev || ! area->scrollbar) break;
362 if (ev->xmotion.x != x_old ||
363 ev->xmotion.y != y_old ||
364 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
366 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
367 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
368 ev->xmotion.y - ob->y,
369 x_motion_state(ev->xbutton.state));
372 #if FL_VERSION < 1 && FL_REVISION < 89
378 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
382 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
383 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
384 // XForms < 0.89.5 does not have compose support
385 // so we are using our own compose support
386 LyXLookupString(ev, dummy, 1, &keysym);
388 XLookupString(xke, dummy, 1, &keysym, 0);
389 // int num_keys = XLookupString(xke, dummy, 10, &keysym, &xcs);
390 // lyxerr << "We have " << num_keys << " keys in the returned buffer" << endl;
391 // lyxerr << "Our dummy string is " << dummy << endl;
394 if (lyxerr.debugging(Debug::KEY)) {
395 char const * tmp = XKeysymToString(key);
396 char const * tmp2 = XKeysymToString(keysym);
397 string const stm = (tmp ? tmp : "");
398 string const stm2 = (tmp2 ? tmp2 : "");
400 lyxerr[Debug::KEY] << "XWorkArea: Key is `" << stm << "' ["
401 << key << "]" << endl;
402 lyxerr[Debug::KEY] << "XWorkArea: Keysym is `" << stm2 << "' ["
403 << keysym << "]" << endl;
406 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
407 if (keysym == NoSymbol) {
409 << "Empty kdb action (probably composing)"
413 KeySym ret_key = keysym;
415 // Note that we need this handling because of a bug
416 // in XForms 0.89, if this bug is resolved in the way I hope
417 // we can just use the keysym directly with out looking
418 // at key at all. (Lgb)
421 // We migth have to add more keysyms here also,
422 // we will do that as the issues arise. (Lgb)
423 if (keysym == XK_space) {
425 lyxerr[Debug::KEY] << "Using keysym [A]"
430 // It seems that this was a bit optimistic...
431 // With this hacking things seems to be better (Lgb)
432 //if (!iscntrl(key)) {
434 // lyxerr[Debug::KEY]
435 // << "Using key [B]\n"
437 // << static_cast<unsigned char>(key)
440 ret_key = (keysym ? keysym : key);
441 lyxerr[Debug::KEY] << "Using keysym [B]"
447 unsigned int const ret_state = xke->state;
449 // If you have a better way to handle "wild-output" of
450 // characters after the key has been released than the one
451 // below, please contact me. (Lgb)
452 static Time last_time_pressed;
453 static unsigned int last_key_pressed;
454 static unsigned int last_state_pressed;
455 lyxerr[Debug::KEY] << "Workarea Diff: "
456 << xke->time - last_time_pressed
458 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
459 && ret_state == last_state_pressed
460 && xke->keycode == last_key_pressed) {
462 << "Workarea: Purging X events." << endl;
463 //lyxerr << "Workarea Events: "
464 // << XEventsQueued(fl_get_display(), QueuedAlready)
466 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
467 XSync(fl_get_display(), 1);
468 // This purge make f.ex. scrolling stop immidiatly when
469 // releasing the PageDown button. The question is if
470 // this purging of XEvents can cause any harm...
471 // after some testing I can see no problems, but
472 // I'd like other reports too.
475 last_time_pressed = xke->time;
476 last_key_pressed = xke->keycode;
477 last_state_pressed = ret_state;
479 XLyXKeySym * xlk = new XLyXKeySym;
480 xlk->initFromKeySym(ret_key);
482 area->workAreaKeyPress(LyXKeySymPtr(xlk),
483 x_key_state(ret_state));
487 #if FL_VERSION > 0 || FL_REVISION >= 89
489 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
494 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
497 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
501 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
502 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
503 ev->xbutton.y - ob->y,
504 x_button_state(ev->xbutton.button));
508 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
509 area->workAreaTripleClick(ev->xbutton.x - ob->x,
510 ev->xbutton.y - ob->y,
511 x_button_state(ev->xbutton.button));
515 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
525 string clipboard_selection;
526 bool clipboard_read = false;
531 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
532 void const * data, long size)
534 clipboard_selection.erase();
537 clipboard_selection.reserve(size);
538 for (int i = 0; i < size; ++i)
539 clipboard_selection +=
540 static_cast<char const *>(data)[i];
541 clipboard_read = true;
550 int XWorkArea::event_cb(XEvent * xev)
554 case SelectionRequest:
555 lyxerr[Debug::GUI] << "X requested selection." << endl;
556 selectionRequested();
559 lyxerr[Debug::GUI] << "Lost selection." << endl;
567 void XWorkArea::haveSelection(bool yes) const
570 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
574 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
578 string const XWorkArea::getClipboard() const
580 clipboard_read = false;
582 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
587 while (!clipboard_read) {
588 if (fl_check_forms() == FL_EVENT) {
590 lyxerr << "Received unhandled X11 event" << endl;
591 lyxerr << "Type: 0x" << hex << ev.xany.type <<
592 " Target: 0x" << hex << ev.xany.window << endl;
595 return clipboard_selection;
599 void XWorkArea::putClipboard(string const & s) const
604 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);