3 * Read the file COPYING
8 * Full author contact details are available in file CREDITS
14 #pragma implementation
17 #include "XWorkArea.h"
20 #include "XLyXKeySym.h"
21 #include "ColorHandler.h"
22 #include "funcrequest.h"
24 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
25 #include "lyxlookup.h"
28 #include "support/filetools.h" // LibFileSearch
29 #include "support/lstrings.h"
30 #include "support/LAssert.h"
35 // xforms doesn't define this (but it should be in <forms.h>).
37 FL_APPEVENT_CB fl_set_preemptive_callback(Window, FL_APPEVENT_CB, void *);
49 XSync(fl_get_display(), 0);
53 void setXtermCursor(Window win)
56 static bool cursor_undefined = true;
57 if (cursor_undefined) {
58 cursor = XCreateFontCursor(fl_get_display(), XC_xterm);
59 XFlush(fl_get_display());
60 cursor_undefined = false;
62 XDefineCursor(fl_get_display(), win, cursor);
63 XFlush(fl_get_display());
67 mouse_button::state x_button_state(unsigned int button)
69 mouse_button::state b = mouse_button::none;
72 b = mouse_button::button1;
75 b = mouse_button::button2;
78 b = mouse_button::button3;
81 b = mouse_button::button4;
84 b = mouse_button::button5;
93 mouse_button::state x_motion_state(unsigned int state)
95 mouse_button::state b = mouse_button::none;
96 if (state & Button1MotionMask)
97 b |= mouse_button::button1;
98 if (state & Button2MotionMask)
99 b |= mouse_button::button2;
100 if (state & Button3MotionMask)
101 b |= mouse_button::button3;
102 if (state & Button4MotionMask)
103 b |= mouse_button::button4;
104 if (state & Button5MotionMask)
105 b |= mouse_button::button5;
110 key_modifier::state x_key_state(unsigned int state)
112 key_modifier::state k = key_modifier::none;
113 if (state & ControlMask)
114 k |= key_modifier::ctrl;
115 if (state & ShiftMask)
116 k |= key_modifier::shift;
117 if (state & Mod1Mask)
118 k |= key_modifier::alt;
127 // Just a bunch of C wrappers around static members of XWorkArea
129 void C_XWorkArea_scroll_cb(FL_OBJECT * ob, long)
131 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
137 int C_XWorkArea_work_area_handler(FL_OBJECT * ob, int event,
141 return XWorkArea::work_area_handler(ob, event,
146 int C_XWorkAreaEventCB(FL_FORM * form, void * xev) {
147 XWorkArea * wa = static_cast<XWorkArea*>(form->u_vdata);
148 return wa->event_cb(static_cast<XEvent*>(xev));
153 XWorkArea::XWorkArea(int x, int y, int w, int h)
154 : workareapixmap(0), painter_(*this)
156 fl_freeze_all_forms();
160 if (lyxerr.debugging(Debug::WORKAREA))
161 lyxerr << "\tbackground box: +"
162 << x << '+' << y << ' '
163 << w - 15 << 'x' << h << endl;
164 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
168 fl_set_object_resize(obj, FL_RESIZE_ALL);
169 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
171 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
174 fl_set_object_boxtype(obj, FL_UP_BOX);
175 fl_set_object_resize(obj, FL_RESIZE_ALL);
176 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
178 fl_set_object_callback(obj, C_XWorkArea_scroll_cb, 0);
179 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
180 fl_set_scrollbar_value(scrollbar, 0.0);
181 fl_set_scrollbar_size(scrollbar, scrollbar->h);
183 int const bw = int(abs(fl_get_border_width()));
185 // Create the workarea pixmap
186 // FIXME remove redraw(w - 15 - 2 * bw, h - 2 * bw);
188 if (lyxerr.debugging(Debug::WORKAREA))
189 lyxerr << "\tfree object: +"
190 << x + bw << '+' << y + bw << ' '
191 << w - 15 - 2 * bw << 'x'
192 << h - 2 * bw << endl;
194 // We add this object as late as possible to avoid problems
197 work_area = obj = fl_add_free(FL_ALL_FREE,
201 C_XWorkArea_work_area_handler);
202 obj->wantkey = FL_KEY_ALL;
205 fl_set_object_boxtype(obj,FL_DOWN_BOX);
206 fl_set_object_resize(obj, FL_RESIZE_ALL);
207 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
209 /// X selection hook - xforms gets it wrong
210 fl_current_form->u_vdata = this;
211 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_XWorkAreaEventCB);
213 fl_unfreeze_all_forms();
217 val.function = GXcopy;
218 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
223 XWorkArea::~XWorkArea()
225 XFreeGC(fl_get_display(), copy_gc);
227 XFreePixmap(fl_get_display(), workareapixmap);
232 void destroy_object(FL_OBJECT * obj)
240 fl_delete_object(obj);
246 void XWorkArea::redraw(int width, int height)
248 static int cur_width = -1;
249 static int cur_height = -1;
251 if (cur_width == width && cur_height == height && workareapixmap) {
252 XCopyArea(fl_get_display(),
253 getPixmap(), getWin(), copy_gc,
254 0, 0, width, height, xpos(), ypos());
261 if (lyxerr.debugging(Debug::WORKAREA)) {
262 lyxerr << "(Re)creating pixmap ("
263 << width << 'x' << height << ")" << endl;
266 if (workareapixmap) {
267 XFreePixmap(fl_get_display(), workareapixmap);
270 workareapixmap = XCreatePixmap(fl_get_display(),
271 RootWindow(fl_get_display(), 0),
274 fl_get_visual_depth());
280 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
282 // we need to cache this for scroll_cb
283 doc_height_ = height;
286 fl_set_scrollbar_value(scrollbar, 0.0);
287 fl_set_scrollbar_size(scrollbar, scrollbar->h);
291 long const work_height = workHeight();
293 lyxerr[Debug::GUI] << "scroll: height now " << height << endl;
294 lyxerr[Debug::GUI] << "scroll: work_height " << work_height << endl;
296 /* If the text is smaller than the working area, the scrollbar
297 * maximum must be the working area height. No scrolling will
299 if (height <= work_height) {
300 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !" << endl;
301 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
302 fl_set_scrollbar_value(scrollbar, pos);
303 fl_set_scrollbar_size(scrollbar, scrollbar->h);
307 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
308 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
310 fl_set_scrollbar_value(scrollbar, pos);
312 double const slider_size =
313 (height == 0) ? 1.0 : 1.0 / double(height);
315 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
319 // callback for scrollbar slider
320 void XWorkArea::scroll_cb()
322 double const val = fl_get_scrollbar_value(scrollbar);
323 lyxerr[Debug::GUI] << "scroll: val: " << val << endl;
324 lyxerr[Debug::GUI] << "scroll: height: " << scrollbar->h << endl;
325 lyxerr[Debug::GUI] << "scroll: docheight: " << doc_height_ << endl;
326 scrollDocView(int(val));
331 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
335 static int x_old = -1;
336 static int y_old = -1;
337 static double scrollbar_value_old = -1.0;
339 XEvent * ev = static_cast<XEvent*>(xev);
340 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
346 if (!area->work_area ||
347 !area->work_area->form->visible)
349 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
350 area->redraw(area->workWidth(), area->workHeight());
353 if (!ev || ev->xbutton.button == 0) break;
354 // Should really have used xbutton.state
355 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
357 FuncRequest(LFUN_MOUSE_PRESS, ev->xbutton.x - ob->x,
358 ev->xbutton.y - ob->y,
359 x_button_state(ev->xbutton.button)));
362 if (!ev || ev->xbutton.button == 0) break;
363 // Should really have used xbutton.state
364 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
366 FuncRequest(LFUN_MOUSE_RELEASE, ev->xbutton.x - ob->x,
367 ev->xbutton.y - ob->y,
368 x_button_state(ev->xbutton.button)));
370 #if FL_VERSION < 1 && FL_REVISION < 89
375 if (!ev || !area->scrollbar)
377 if (ev->xmotion.x != x_old ||
378 ev->xmotion.y != y_old ||
379 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
381 x_old = ev->xmotion.x;
382 y_old = ev->xmotion.y;
383 scrollbar_value_old = fl_get_scrollbar_value(area->scrollbar);
384 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
386 FuncRequest(LFUN_MOUSE_MOTION, ev->xbutton.x - ob->x,
387 ev->xbutton.y - ob->y,
388 x_button_state(ev->xbutton.button)));
391 #if FL_VERSION < 1 && FL_REVISION < 89
397 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
401 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
402 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
403 // XForms < 0.89.5 does not have compose support
404 // so we are using our own compose support
405 LyXLookupString(ev, dummy, 1, &keysym);
407 XLookupString(xke, dummy, 1, &keysym, 0);
408 // int num_keys = XLookupString(xke, dummy, 10, &keysym, &xcs);
409 // lyxerr << "We have " << num_keys << " keys in the returned buffer" << endl;
410 // lyxerr << "Our dummy string is " << dummy << endl;
413 if (lyxerr.debugging(Debug::KEY)) {
414 char const * tmp = XKeysymToString(key);
415 char const * tmp2 = XKeysymToString(keysym);
416 string const stm = (tmp ? tmp : "");
417 string const stm2 = (tmp2 ? tmp2 : "");
419 lyxerr[Debug::KEY] << "XWorkArea: Key is `" << stm << "' ["
420 << key << "]" << endl;
421 lyxerr[Debug::KEY] << "XWorkArea: Keysym is `" << stm2 << "' ["
422 << keysym << "]" << endl;
425 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
426 if (keysym == NoSymbol) {
428 << "Empty kdb action (probably composing)"
432 KeySym ret_key = keysym;
434 // Note that we need this handling because of a bug
435 // in XForms 0.89, if this bug is resolved in the way I hope
436 // we can just use the keysym directly with out looking
437 // at key at all. (Lgb)
440 // We migth have to add more keysyms here also,
441 // we will do that as the issues arise. (Lgb)
442 if (keysym == XK_space) {
444 lyxerr[Debug::KEY] << "Using keysym [A]"
449 // It seems that this was a bit optimistic...
450 // With this hacking things seems to be better (Lgb)
451 //if (!iscntrl(key)) {
453 // lyxerr[Debug::KEY]
454 // << "Using key [B]\n"
456 // << static_cast<unsigned char>(key)
459 ret_key = (keysym ? keysym : key);
460 lyxerr[Debug::KEY] << "Using keysym [B]"
466 unsigned int const ret_state = xke->state;
468 // If you have a better way to handle "wild-output" of
469 // characters after the key has been released than the one
470 // below, please contact me. (Lgb)
471 static Time last_time_pressed;
472 static unsigned int last_key_pressed;
473 static unsigned int last_state_pressed;
474 lyxerr[Debug::KEY] << "Workarea Diff: "
475 << xke->time - last_time_pressed
477 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
478 && ret_state == last_state_pressed
479 && xke->keycode == last_key_pressed) {
481 << "Workarea: Purging X events." << endl;
482 //lyxerr << "Workarea Events: "
483 // << XEventsQueued(fl_get_display(), QueuedAlready)
485 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
486 XSync(fl_get_display(), 1);
487 // This purge make f.ex. scrolling stop immidiatly when
488 // releasing the PageDown button. The question is if
489 // this purging of XEvents can cause any harm...
490 // after some testing I can see no problems, but
491 // I'd like other reports too.
494 last_time_pressed = xke->time;
495 last_key_pressed = xke->keycode;
496 last_state_pressed = ret_state;
498 XLyXKeySym * xlk = new XLyXKeySym;
499 xlk->initFromKeySym(ret_key);
501 area->workAreaKeyPress(LyXKeySymPtr(xlk),
502 x_key_state(ret_state));
506 #if FL_VERSION > 0 || FL_REVISION >= 89
508 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
513 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
516 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
520 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
521 FuncRequest cmd(LFUN_MOUSE_DOUBLE, ev->xbutton.x - ob->x,
522 ev->xbutton.y - ob->y,
523 x_button_state(ev->xbutton.button));
529 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
530 FuncRequest cmd(LFUN_MOUSE_TRIPLE, ev->xbutton.x - ob->x,
531 ev->xbutton.y - ob->y,
532 x_button_state(ev->xbutton.button));
538 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
548 string clipboard_selection;
549 bool clipboard_read = false;
554 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
555 void const * data, long size)
557 clipboard_selection.erase();
560 clipboard_selection.reserve(size);
561 for (int i = 0; i < size; ++i)
562 clipboard_selection +=
563 static_cast<char const *>(data)[i];
564 clipboard_read = true;
573 int XWorkArea::event_cb(XEvent * xev)
577 case SelectionRequest:
578 lyxerr[Debug::GUI] << "X requested selection." << endl;
579 selectionRequested();
582 lyxerr[Debug::GUI] << "Lost selection." << endl;
590 void XWorkArea::haveSelection(bool yes) const
593 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
597 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
601 string const XWorkArea::getClipboard() const
603 clipboard_read = false;
605 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
610 while (!clipboard_read) {
611 if (fl_check_forms() == FL_EVENT) {
613 lyxerr << "Received unhandled X11 event" << endl;
614 lyxerr << "Type: 0x" << hex << ev.xany.type <<
615 " Target: 0x" << hex << ev.xany.window << dec << endl;
618 return clipboard_selection;
622 void XWorkArea::putClipboard(string const & s) const
627 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);