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 *);
47 XSync(fl_get_display(), 0);
51 void setXtermCursor(Window win)
54 static bool cursor_undefined = true;
55 if (cursor_undefined) {
56 cursor = XCreateFontCursor(fl_get_display(), XC_xterm);
57 XFlush(fl_get_display());
58 cursor_undefined = false;
60 XDefineCursor(fl_get_display(), win, cursor);
61 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;
91 mouse_button::state x_motion_state(unsigned int state)
93 mouse_button::state b = mouse_button::none;
94 if (state & Button1MotionMask)
95 b |= mouse_button::button1;
96 if (state & Button2MotionMask)
97 b |= mouse_button::button2;
98 if (state & Button3MotionMask)
99 b |= mouse_button::button3;
100 if (state & Button4MotionMask)
101 b |= mouse_button::button4;
102 if (state & Button5MotionMask)
103 b |= mouse_button::button5;
108 key_modifier::state x_key_state(unsigned int state)
110 key_modifier::state k = key_modifier::none;
111 if (state & ControlMask)
112 k |= key_modifier::ctrl;
113 if (state & ShiftMask)
114 k |= key_modifier::shift;
115 if (state & Mod1Mask)
116 k |= key_modifier::alt;
125 // Just a bunch of C wrappers around static members of XWorkArea
127 void C_XWorkArea_scroll_cb(FL_OBJECT * ob, long)
129 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
135 int C_XWorkArea_work_area_handler(FL_OBJECT * ob, int event,
139 return XWorkArea::work_area_handler(ob, event,
144 int C_XWorkAreaEventCB(FL_FORM * form, void * xev) {
145 XWorkArea * wa = static_cast<XWorkArea*>(form->u_vdata);
146 return wa->event_cb(static_cast<XEvent*>(xev));
151 XWorkArea::XWorkArea(int x, int y, int w, int h)
152 : workareapixmap(0), painter_(*this)
154 fl_freeze_all_forms();
158 if (lyxerr.debugging(Debug::WORKAREA))
159 lyxerr << "\tbackground box: +"
160 << x << '+' << y << ' '
161 << w - 15 << 'x' << h << endl;
162 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
166 fl_set_object_resize(obj, FL_RESIZE_ALL);
167 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
169 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
172 fl_set_object_boxtype(obj, FL_UP_BOX);
173 fl_set_object_resize(obj, FL_RESIZE_ALL);
174 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
176 fl_set_object_callback(obj, C_XWorkArea_scroll_cb, 0);
177 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
178 fl_set_scrollbar_value(scrollbar, 0.0);
179 fl_set_scrollbar_size(scrollbar, scrollbar->h);
181 int const bw = int(abs(fl_get_border_width()));
183 // Create the workarea pixmap
184 // FIXME remove redraw(w - 15 - 2 * bw, h - 2 * bw);
186 if (lyxerr.debugging(Debug::WORKAREA))
187 lyxerr << "\tfree object: +"
188 << x + bw << '+' << y + bw << ' '
189 << w - 15 - 2 * bw << 'x'
190 << h - 2 * bw << endl;
192 // We add this object as late as possible to avoid problems
195 work_area = obj = fl_add_free(FL_ALL_FREE,
199 C_XWorkArea_work_area_handler);
200 obj->wantkey = FL_KEY_ALL;
203 fl_set_object_boxtype(obj,FL_DOWN_BOX);
204 fl_set_object_resize(obj, FL_RESIZE_ALL);
205 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
207 /// X selection hook - xforms gets it wrong
208 fl_current_form->u_vdata = this;
209 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_XWorkAreaEventCB);
211 fl_unfreeze_all_forms();
215 val.function = GXcopy;
216 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
221 XWorkArea::~XWorkArea()
223 XFreeGC(fl_get_display(), copy_gc);
225 XFreePixmap(fl_get_display(), workareapixmap);
230 void destroy_object(FL_OBJECT * obj)
238 fl_delete_object(obj);
244 void XWorkArea::redraw(int width, int height)
246 static int cur_width = -1;
247 static int cur_height = -1;
249 if (cur_width == width && cur_height == height && workareapixmap) {
250 XCopyArea(fl_get_display(),
251 getPixmap(), getWin(), copy_gc,
252 0, 0, width, height, xpos(), ypos());
259 if (lyxerr.debugging(Debug::WORKAREA)) {
260 lyxerr << "(Re)creating pixmap ("
261 << width << 'x' << height << ")" << endl;
264 if (workareapixmap) {
265 XFreePixmap(fl_get_display(), workareapixmap);
268 workareapixmap = XCreatePixmap(fl_get_display(),
269 RootWindow(fl_get_display(), 0),
272 fl_get_visual_depth());
278 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
280 // we need to cache this for scroll_cb
281 doc_height_ = height;
284 fl_set_scrollbar_value(scrollbar, 0.0);
285 fl_set_scrollbar_size(scrollbar, scrollbar->h);
289 long const work_height = workHeight();
291 lyxerr[Debug::GUI] << "scroll: height now " << height << endl;
292 lyxerr[Debug::GUI] << "scroll: work_height " << work_height << endl;
294 /* If the text is smaller than the working area, the scrollbar
295 * maximum must be the working area height. No scrolling will
297 if (height <= work_height) {
298 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !" << endl;
299 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
300 fl_set_scrollbar_value(scrollbar, pos);
301 fl_set_scrollbar_size(scrollbar, scrollbar->h);
305 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
306 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
308 fl_set_scrollbar_value(scrollbar, pos);
310 double const slider_size =
311 (height == 0) ? 1.0 : 1.0 / double(height);
313 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
317 // callback for scrollbar slider
318 void XWorkArea::scroll_cb()
320 double const val = fl_get_scrollbar_value(scrollbar);
321 lyxerr[Debug::GUI] << "scroll: val: " << val << endl;
322 lyxerr[Debug::GUI] << "scroll: height: " << scrollbar->h << endl;
323 lyxerr[Debug::GUI] << "scroll: docheight: " << doc_height_ << endl;
324 scrollDocView(int(val));
329 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
333 static int x_old = -1;
334 static int y_old = -1;
335 static long scrollbar_value_old = -1;
337 XEvent * ev = static_cast<XEvent*>(xev);
338 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
344 if (!area->work_area ||
345 !area->work_area->form->visible)
347 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
348 area->redraw(area->workWidth(), area->workHeight());
351 if (!ev || ev->xbutton.button == 0) break;
352 // Should really have used xbutton.state
353 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
354 area->workAreaButtonPress(ev->xbutton.x - ob->x,
355 ev->xbutton.y - ob->y,
356 x_button_state(ev->xbutton.button));
359 if (!ev || ev->xbutton.button == 0) break;
360 // Should really have used xbutton.state
361 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
362 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
363 ev->xbutton.y - ob->y,
364 x_button_state(ev->xbutton.button));
366 #if FL_VERSION < 1 && FL_REVISION < 89
371 if (!ev || ! area->scrollbar) break;
372 if (ev->xmotion.x != x_old ||
373 ev->xmotion.y != y_old ||
374 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
376 x_old = ev->xmotion.x;
377 y_old = ev->xmotion.y;
378 scrollbar_value_old = fl_get_scrollbar_value(area->scrollbar);
379 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
380 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
381 ev->xmotion.y - ob->y,
382 x_motion_state(ev->xbutton.state));
385 #if FL_VERSION < 1 && FL_REVISION < 89
391 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
395 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
396 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
397 // XForms < 0.89.5 does not have compose support
398 // so we are using our own compose support
399 LyXLookupString(ev, dummy, 1, &keysym);
401 XLookupString(xke, dummy, 1, &keysym, 0);
402 // int num_keys = XLookupString(xke, dummy, 10, &keysym, &xcs);
403 // lyxerr << "We have " << num_keys << " keys in the returned buffer" << endl;
404 // lyxerr << "Our dummy string is " << dummy << endl;
407 if (lyxerr.debugging(Debug::KEY)) {
408 char const * tmp = XKeysymToString(key);
409 char const * tmp2 = XKeysymToString(keysym);
410 string const stm = (tmp ? tmp : "");
411 string const stm2 = (tmp2 ? tmp2 : "");
413 lyxerr[Debug::KEY] << "XWorkArea: Key is `" << stm << "' ["
414 << key << "]" << endl;
415 lyxerr[Debug::KEY] << "XWorkArea: Keysym is `" << stm2 << "' ["
416 << keysym << "]" << endl;
419 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
420 if (keysym == NoSymbol) {
422 << "Empty kdb action (probably composing)"
426 KeySym ret_key = keysym;
428 // Note that we need this handling because of a bug
429 // in XForms 0.89, if this bug is resolved in the way I hope
430 // we can just use the keysym directly with out looking
431 // at key at all. (Lgb)
434 // We migth have to add more keysyms here also,
435 // we will do that as the issues arise. (Lgb)
436 if (keysym == XK_space) {
438 lyxerr[Debug::KEY] << "Using keysym [A]"
443 // It seems that this was a bit optimistic...
444 // With this hacking things seems to be better (Lgb)
445 //if (!iscntrl(key)) {
447 // lyxerr[Debug::KEY]
448 // << "Using key [B]\n"
450 // << static_cast<unsigned char>(key)
453 ret_key = (keysym ? keysym : key);
454 lyxerr[Debug::KEY] << "Using keysym [B]"
460 unsigned int const ret_state = xke->state;
462 // If you have a better way to handle "wild-output" of
463 // characters after the key has been released than the one
464 // below, please contact me. (Lgb)
465 static Time last_time_pressed;
466 static unsigned int last_key_pressed;
467 static unsigned int last_state_pressed;
468 lyxerr[Debug::KEY] << "Workarea Diff: "
469 << xke->time - last_time_pressed
471 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
472 && ret_state == last_state_pressed
473 && xke->keycode == last_key_pressed) {
475 << "Workarea: Purging X events." << endl;
476 //lyxerr << "Workarea Events: "
477 // << XEventsQueued(fl_get_display(), QueuedAlready)
479 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
480 XSync(fl_get_display(), 1);
481 // This purge make f.ex. scrolling stop immidiatly when
482 // releasing the PageDown button. The question is if
483 // this purging of XEvents can cause any harm...
484 // after some testing I can see no problems, but
485 // I'd like other reports too.
488 last_time_pressed = xke->time;
489 last_key_pressed = xke->keycode;
490 last_state_pressed = ret_state;
492 XLyXKeySym * xlk = new XLyXKeySym;
493 xlk->initFromKeySym(ret_key);
495 area->workAreaKeyPress(LyXKeySymPtr(xlk),
496 x_key_state(ret_state));
500 #if FL_VERSION > 0 || FL_REVISION >= 89
502 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
507 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
510 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
514 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
515 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
516 ev->xbutton.y - ob->y,
517 x_button_state(ev->xbutton.button));
521 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
522 area->workAreaTripleClick(ev->xbutton.x - ob->x,
523 ev->xbutton.y - ob->y,
524 x_button_state(ev->xbutton.button));
528 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
538 string clipboard_selection;
539 bool clipboard_read = false;
544 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
545 void const * data, long size)
547 clipboard_selection.erase();
550 clipboard_selection.reserve(size);
551 for (int i = 0; i < size; ++i)
552 clipboard_selection +=
553 static_cast<char const *>(data)[i];
554 clipboard_read = true;
563 int XWorkArea::event_cb(XEvent * xev)
567 case SelectionRequest:
568 lyxerr[Debug::GUI] << "X requested selection." << endl;
569 selectionRequested();
572 lyxerr[Debug::GUI] << "Lost selection." << endl;
580 void XWorkArea::haveSelection(bool yes) const
583 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
587 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
591 string const XWorkArea::getClipboard() const
593 clipboard_read = false;
595 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
600 while (!clipboard_read) {
601 if (fl_check_forms() == FL_EVENT) {
603 lyxerr << "Received unhandled X11 event" << endl;
604 lyxerr << "Type: 0x" << hex << ev.xany.type <<
605 " Target: 0x" << hex << ev.xany.window << dec << endl;
608 return clipboard_selection;
612 void XWorkArea::putClipboard(string const & s) const
617 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);