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 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
376 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
377 ev->xmotion.y - ob->y,
378 x_motion_state(ev->xbutton.state));
381 #if FL_VERSION < 1 && FL_REVISION < 89
387 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
391 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
392 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
393 // XForms < 0.89.5 does not have compose support
394 // so we are using our own compose support
395 LyXLookupString(ev, dummy, 1, &keysym);
397 XLookupString(xke, dummy, 1, &keysym, 0);
398 // int num_keys = XLookupString(xke, dummy, 10, &keysym, &xcs);
399 // lyxerr << "We have " << num_keys << " keys in the returned buffer" << endl;
400 // lyxerr << "Our dummy string is " << dummy << endl;
403 if (lyxerr.debugging(Debug::KEY)) {
404 char const * tmp = XKeysymToString(key);
405 char const * tmp2 = XKeysymToString(keysym);
406 string const stm = (tmp ? tmp : "");
407 string const stm2 = (tmp2 ? tmp2 : "");
409 lyxerr[Debug::KEY] << "XWorkArea: Key is `" << stm << "' ["
410 << key << "]" << endl;
411 lyxerr[Debug::KEY] << "XWorkArea: Keysym is `" << stm2 << "' ["
412 << keysym << "]" << endl;
415 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
416 if (keysym == NoSymbol) {
418 << "Empty kdb action (probably composing)"
422 KeySym ret_key = keysym;
424 // Note that we need this handling because of a bug
425 // in XForms 0.89, if this bug is resolved in the way I hope
426 // we can just use the keysym directly with out looking
427 // at key at all. (Lgb)
430 // We migth have to add more keysyms here also,
431 // we will do that as the issues arise. (Lgb)
432 if (keysym == XK_space) {
434 lyxerr[Debug::KEY] << "Using keysym [A]"
439 // It seems that this was a bit optimistic...
440 // With this hacking things seems to be better (Lgb)
441 //if (!iscntrl(key)) {
443 // lyxerr[Debug::KEY]
444 // << "Using key [B]\n"
446 // << static_cast<unsigned char>(key)
449 ret_key = (keysym ? keysym : key);
450 lyxerr[Debug::KEY] << "Using keysym [B]"
456 unsigned int const ret_state = xke->state;
458 // If you have a better way to handle "wild-output" of
459 // characters after the key has been released than the one
460 // below, please contact me. (Lgb)
461 static Time last_time_pressed;
462 static unsigned int last_key_pressed;
463 static unsigned int last_state_pressed;
464 lyxerr[Debug::KEY] << "Workarea Diff: "
465 << xke->time - last_time_pressed
467 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
468 && ret_state == last_state_pressed
469 && xke->keycode == last_key_pressed) {
471 << "Workarea: Purging X events." << endl;
472 //lyxerr << "Workarea Events: "
473 // << XEventsQueued(fl_get_display(), QueuedAlready)
475 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
476 XSync(fl_get_display(), 1);
477 // This purge make f.ex. scrolling stop immidiatly when
478 // releasing the PageDown button. The question is if
479 // this purging of XEvents can cause any harm...
480 // after some testing I can see no problems, but
481 // I'd like other reports too.
484 last_time_pressed = xke->time;
485 last_key_pressed = xke->keycode;
486 last_state_pressed = ret_state;
488 XLyXKeySym * xlk = new XLyXKeySym;
489 xlk->initFromKeySym(ret_key);
491 area->workAreaKeyPress(LyXKeySymPtr(xlk),
492 x_key_state(ret_state));
496 #if FL_VERSION > 0 || FL_REVISION >= 89
498 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
503 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
506 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
510 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
511 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
512 ev->xbutton.y - ob->y,
513 x_button_state(ev->xbutton.button));
517 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
518 area->workAreaTripleClick(ev->xbutton.x - ob->x,
519 ev->xbutton.y - ob->y,
520 x_button_state(ev->xbutton.button));
524 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
534 string clipboard_selection;
535 bool clipboard_read = false;
540 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
541 void const * data, long size)
543 clipboard_selection.erase();
546 clipboard_selection.reserve(size);
547 for (int i = 0; i < size; ++i)
548 clipboard_selection +=
549 static_cast<char const *>(data)[i];
550 clipboard_read = true;
559 int XWorkArea::event_cb(XEvent * xev)
563 case SelectionRequest:
564 lyxerr[Debug::GUI] << "X requested selection." << endl;
565 selectionRequested();
568 lyxerr[Debug::GUI] << "Lost selection." << endl;
576 void XWorkArea::haveSelection(bool yes) const
579 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
583 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
587 string const XWorkArea::getClipboard() const
589 clipboard_read = false;
591 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
596 while (!clipboard_read) {
597 if (fl_check_forms() == FL_EVENT) {
599 lyxerr << "Received unhandled X11 event" << endl;
600 lyxerr << "Type: 0x" << hex << ev.xany.type <<
601 " Target: 0x" << hex << ev.xany.window << endl;
604 return clipboard_selection;
608 void XWorkArea::putClipboard(string const & s) const
613 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);