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());
64 mouse_button::state x_button_state(unsigned int button)
66 mouse_button::state b = mouse_button::none;
69 b = mouse_button::button1;
72 b = mouse_button::button2;
75 b = mouse_button::button3;
78 b = mouse_button::button4;
81 b = mouse_button::button5;
90 mouse_button::state x_motion_state(unsigned int state)
92 mouse_button::state b = mouse_button::none;
93 if (state & Button1MotionMask)
94 b |= mouse_button::button1;
95 if (state & Button2MotionMask)
96 b |= mouse_button::button2;
97 if (state & Button3MotionMask)
98 b |= mouse_button::button3;
99 if (state & Button4MotionMask)
100 b |= mouse_button::button4;
101 if (state & Button5MotionMask)
102 b |= mouse_button::button5;
107 key_modifier::state x_key_state(unsigned int state)
109 key_modifier::state k = key_modifier::none;
110 if (state & ControlMask)
111 k |= key_modifier::ctrl;
112 if (state & ShiftMask)
113 k |= key_modifier::shift;
114 if (state & Mod1Mask)
115 k |= key_modifier::alt;
124 // Just a bunch of C wrappers around static members of XWorkArea
126 void C_XWorkArea_scroll_cb(FL_OBJECT * ob, long)
128 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
134 int C_XWorkArea_work_area_handler(FL_OBJECT * ob, int event,
138 return XWorkArea::work_area_handler(ob, event,
143 int C_XWorkAreaEventCB(FL_FORM * form, void * xev) {
144 XWorkArea * wa = static_cast<XWorkArea*>(form->u_vdata);
145 return wa->event_cb(static_cast<XEvent*>(xev));
150 XWorkArea::XWorkArea(int x, int y, int w, int h)
151 : workareapixmap(0), painter_(*this)
153 fl_freeze_all_forms();
157 if (lyxerr.debugging(Debug::WORKAREA))
158 lyxerr << "\tbackground box: +"
159 << x << '+' << y << ' '
160 << w - 15 << 'x' << h << endl;
161 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
165 fl_set_object_resize(obj, FL_RESIZE_ALL);
166 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
168 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
171 fl_set_object_boxtype(obj, FL_UP_BOX);
172 fl_set_object_resize(obj, FL_RESIZE_ALL);
173 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
175 fl_set_object_callback(obj, C_XWorkArea_scroll_cb, 0);
176 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
177 fl_set_scrollbar_value(scrollbar, 0.0);
178 fl_set_scrollbar_size(scrollbar, scrollbar->h);
180 int const bw = int(abs(fl_get_border_width()));
182 // Create the workarea pixmap
183 // FIXME remove redraw(w - 15 - 2 * bw, h - 2 * bw);
185 if (lyxerr.debugging(Debug::WORKAREA))
186 lyxerr << "\tfree object: +"
187 << x + bw << '+' << y + bw << ' '
188 << w - 15 - 2 * bw << 'x'
189 << h - 2 * bw << endl;
191 // We add this object as late as possible to avoid problems
194 work_area = obj = fl_add_free(FL_ALL_FREE,
198 C_XWorkArea_work_area_handler);
199 obj->wantkey = FL_KEY_ALL;
202 fl_set_object_boxtype(obj,FL_DOWN_BOX);
203 fl_set_object_resize(obj, FL_RESIZE_ALL);
204 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
206 /// X selection hook - xforms gets it wrong
207 fl_current_form->u_vdata = this;
208 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_XWorkAreaEventCB);
210 fl_unfreeze_all_forms();
214 val.function = GXcopy;
215 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
220 XWorkArea::~XWorkArea()
222 XFreeGC(fl_get_display(), copy_gc);
224 XFreePixmap(fl_get_display(), workareapixmap);
229 void destroy_object(FL_OBJECT * obj)
237 fl_delete_object(obj);
243 void XWorkArea::redraw(int width, int height)
245 static int cur_width = -1;
246 static int cur_height = -1;
248 if (cur_width == width && cur_height == height && workareapixmap) {
249 XCopyArea(fl_get_display(),
250 getPixmap(), getWin(), copy_gc,
251 0, 0, width, height, xpos(), ypos());
258 if (lyxerr.debugging(Debug::WORKAREA)) {
259 lyxerr << "(Re)creating pixmap ("
260 << width << 'x' << height << ")" << endl;
263 if (workareapixmap) {
264 XFreePixmap(fl_get_display(), workareapixmap);
267 workareapixmap = XCreatePixmap(fl_get_display(),
268 RootWindow(fl_get_display(), 0),
271 fl_get_visual_depth());
277 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
279 // we need to cache this for scroll_cb
280 doc_height_ = height;
283 fl_set_scrollbar_value(scrollbar, 0.0);
284 fl_set_scrollbar_size(scrollbar, scrollbar->h);
288 long const work_height = workHeight();
290 lyxerr[Debug::GUI] << "scroll: height now " << height << endl;
291 lyxerr[Debug::GUI] << "scroll: work_height " << work_height << endl;
293 /* If the text is smaller than the working area, the scrollbar
294 * maximum must be the working area height. No scrolling will
296 if (height <= work_height) {
297 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !" << endl;
298 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
299 fl_set_scrollbar_value(scrollbar, pos);
300 fl_set_scrollbar_size(scrollbar, scrollbar->h);
304 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
305 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
307 fl_set_scrollbar_value(scrollbar, pos);
309 double const slider_size =
310 (height == 0) ? 1.0 : 1.0 / double(height);
312 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
316 // callback for scrollbar slider
317 void XWorkArea::scroll_cb()
319 double const val = fl_get_scrollbar_value(scrollbar);
320 lyxerr[Debug::GUI] << "scroll: val: " << val << endl;
321 lyxerr[Debug::GUI] << "scroll: height: " << scrollbar->h << endl;
322 lyxerr[Debug::GUI] << "scroll: docheight: " << doc_height_ << endl;
323 scrollDocView(int(val));
328 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
332 static int x_old = -1;
333 static int y_old = -1;
334 static long scrollbar_value_old = -1;
336 XEvent * ev = static_cast<XEvent*>(xev);
337 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
343 if (!area->work_area ||
344 !area->work_area->form->visible)
346 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
347 area->redraw(area->workWidth(), area->workHeight());
350 if (!ev || ev->xbutton.button == 0) break;
351 // Should really have used xbutton.state
352 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
353 area->workAreaButtonPress(ev->xbutton.x - ob->x,
354 ev->xbutton.y - ob->y,
355 x_button_state(ev->xbutton.button));
358 if (!ev || ev->xbutton.button == 0) break;
359 // Should really have used xbutton.state
360 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
361 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
362 ev->xbutton.y - ob->y,
363 x_button_state(ev->xbutton.button));
365 #if FL_VERSION < 1 && FL_REVISION < 89
370 if (!ev || ! area->scrollbar) break;
371 if (ev->xmotion.x != x_old ||
372 ev->xmotion.y != y_old ||
373 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
375 x_old = ev->xmotion.x;
376 y_old = ev->xmotion.y;
377 scrollbar_value_old = fl_get_scrollbar_value(area->scrollbar);
378 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
379 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
380 ev->xmotion.y - ob->y,
381 x_motion_state(ev->xbutton.state));
384 #if FL_VERSION < 1 && FL_REVISION < 89
390 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
394 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
395 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
396 // XForms < 0.89.5 does not have compose support
397 // so we are using our own compose support
398 LyXLookupString(ev, dummy, 1, &keysym);
400 XLookupString(xke, dummy, 1, &keysym, 0);
401 // int num_keys = XLookupString(xke, dummy, 10, &keysym, &xcs);
402 // lyxerr << "We have " << num_keys << " keys in the returned buffer" << endl;
403 // lyxerr << "Our dummy string is " << dummy << endl;
406 if (lyxerr.debugging(Debug::KEY)) {
407 char const * tmp = XKeysymToString(key);
408 char const * tmp2 = XKeysymToString(keysym);
409 string const stm = (tmp ? tmp : "");
410 string const stm2 = (tmp2 ? tmp2 : "");
412 lyxerr[Debug::KEY] << "XWorkArea: Key is `" << stm << "' ["
413 << key << "]" << endl;
414 lyxerr[Debug::KEY] << "XWorkArea: Keysym is `" << stm2 << "' ["
415 << keysym << "]" << endl;
418 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
419 if (keysym == NoSymbol) {
421 << "Empty kdb action (probably composing)"
425 KeySym ret_key = keysym;
427 // Note that we need this handling because of a bug
428 // in XForms 0.89, if this bug is resolved in the way I hope
429 // we can just use the keysym directly with out looking
430 // at key at all. (Lgb)
433 // We migth have to add more keysyms here also,
434 // we will do that as the issues arise. (Lgb)
435 if (keysym == XK_space) {
437 lyxerr[Debug::KEY] << "Using keysym [A]"
442 // It seems that this was a bit optimistic...
443 // With this hacking things seems to be better (Lgb)
444 //if (!iscntrl(key)) {
446 // lyxerr[Debug::KEY]
447 // << "Using key [B]\n"
449 // << static_cast<unsigned char>(key)
452 ret_key = (keysym ? keysym : key);
453 lyxerr[Debug::KEY] << "Using keysym [B]"
459 unsigned int const ret_state = xke->state;
461 // If you have a better way to handle "wild-output" of
462 // characters after the key has been released than the one
463 // below, please contact me. (Lgb)
464 static Time last_time_pressed;
465 static unsigned int last_key_pressed;
466 static unsigned int last_state_pressed;
467 lyxerr[Debug::KEY] << "Workarea Diff: "
468 << xke->time - last_time_pressed
470 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
471 && ret_state == last_state_pressed
472 && xke->keycode == last_key_pressed) {
474 << "Workarea: Purging X events." << endl;
475 //lyxerr << "Workarea Events: "
476 // << XEventsQueued(fl_get_display(), QueuedAlready)
478 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
479 XSync(fl_get_display(), 1);
480 // This purge make f.ex. scrolling stop immidiatly when
481 // releasing the PageDown button. The question is if
482 // this purging of XEvents can cause any harm...
483 // after some testing I can see no problems, but
484 // I'd like other reports too.
487 last_time_pressed = xke->time;
488 last_key_pressed = xke->keycode;
489 last_state_pressed = ret_state;
491 XLyXKeySym * xlk = new XLyXKeySym;
492 xlk->initFromKeySym(ret_key);
494 area->workAreaKeyPress(LyXKeySymPtr(xlk),
495 x_key_state(ret_state));
499 #if FL_VERSION > 0 || FL_REVISION >= 89
501 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
506 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
509 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
513 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
514 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
515 ev->xbutton.y - ob->y,
516 x_button_state(ev->xbutton.button));
520 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
521 area->workAreaTripleClick(ev->xbutton.x - ob->x,
522 ev->xbutton.y - ob->y,
523 x_button_state(ev->xbutton.button));
527 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
537 string clipboard_selection;
538 bool clipboard_read = false;
543 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
544 void const * data, long size)
546 clipboard_selection.erase();
549 clipboard_selection.reserve(size);
550 for (int i = 0; i < size; ++i)
551 clipboard_selection +=
552 static_cast<char const *>(data)[i];
553 clipboard_read = true;
562 int XWorkArea::event_cb(XEvent * xev)
566 case SelectionRequest:
567 lyxerr[Debug::GUI] << "X requested selection." << endl;
568 selectionRequested();
571 lyxerr[Debug::GUI] << "Lost selection." << endl;
579 void XWorkArea::haveSelection(bool yes) const
582 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
586 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
590 string const XWorkArea::getClipboard() const
592 clipboard_read = false;
594 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
599 while (!clipboard_read) {
600 if (fl_check_forms() == FL_EVENT) {
602 lyxerr << "Received unhandled X11 event" << endl;
603 lyxerr << "Type: 0x" << hex << ev.xany.type <<
604 " Target: 0x" << hex << ev.xany.window << endl;
607 return clipboard_selection;
611 void XWorkArea::putClipboard(string const & s) const
616 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);