3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS
15 #pragma implementation
18 #include "XWorkArea.h"
21 #include "XLyXKeySym.h"
22 #include "ColorHandler.h"
23 #include "funcrequest.h"
25 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
26 #include "lyxlookup.h"
29 #include "support/filetools.h" // LibFileSearch
30 #include "support/lstrings.h"
31 #include "support/LAssert.h"
36 // xforms doesn't define this (but it should be in <forms.h>).
38 FL_APPEVENT_CB fl_set_preemptive_callback(Window, FL_APPEVENT_CB, void *);
50 XSync(fl_get_display(), 0);
54 void setXtermCursor(Window win)
57 static bool cursor_undefined = true;
58 if (cursor_undefined) {
59 cursor = XCreateFontCursor(fl_get_display(), XC_xterm);
60 XFlush(fl_get_display());
61 cursor_undefined = false;
63 XDefineCursor(fl_get_display(), win, cursor);
64 XFlush(fl_get_display());
68 mouse_button::state x_button_state(unsigned int button)
70 mouse_button::state b = mouse_button::none;
73 b = mouse_button::button1;
76 b = mouse_button::button2;
79 b = mouse_button::button3;
82 b = mouse_button::button4;
85 b = mouse_button::button5;
94 mouse_button::state x_motion_state(unsigned int state)
96 mouse_button::state b = mouse_button::none;
97 if (state & Button1MotionMask)
98 b |= mouse_button::button1;
99 if (state & Button2MotionMask)
100 b |= mouse_button::button2;
101 if (state & Button3MotionMask)
102 b |= mouse_button::button3;
103 if (state & Button4MotionMask)
104 b |= mouse_button::button4;
105 if (state & Button5MotionMask)
106 b |= mouse_button::button5;
111 key_modifier::state x_key_state(unsigned int state)
113 key_modifier::state k = key_modifier::none;
114 if (state & ControlMask)
115 k |= key_modifier::ctrl;
116 if (state & ShiftMask)
117 k |= key_modifier::shift;
118 if (state & Mod1Mask)
119 k |= key_modifier::alt;
128 // Just a bunch of C wrappers around static members of XWorkArea
130 void C_XWorkArea_scroll_cb(FL_OBJECT * ob, long)
132 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
138 int C_XWorkArea_work_area_handler(FL_OBJECT * ob, int event,
142 return XWorkArea::work_area_handler(ob, event,
147 int C_XWorkAreaEventCB(FL_FORM * form, void * xev) {
148 XWorkArea * wa = static_cast<XWorkArea*>(form->u_vdata);
149 return wa->event_cb(static_cast<XEvent*>(xev));
154 XWorkArea::XWorkArea(int x, int y, int w, int h)
155 : workareapixmap(0), painter_(*this)
157 fl_freeze_all_forms();
161 if (lyxerr.debugging(Debug::WORKAREA))
162 lyxerr << "\tbackground box: +"
163 << x << '+' << y << ' '
164 << w - 15 << 'x' << h << endl;
165 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
169 fl_set_object_resize(obj, FL_RESIZE_ALL);
170 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
172 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
175 fl_set_object_boxtype(obj, FL_UP_BOX);
176 fl_set_object_resize(obj, FL_RESIZE_ALL);
177 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
179 fl_set_object_callback(obj, C_XWorkArea_scroll_cb, 0);
180 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
181 fl_set_scrollbar_value(scrollbar, 0.0);
182 fl_set_scrollbar_size(scrollbar, scrollbar->h);
184 int const bw = int(abs(fl_get_border_width()));
186 // Create the workarea pixmap
187 // FIXME remove redraw(w - 15 - 2 * bw, h - 2 * bw);
189 if (lyxerr.debugging(Debug::WORKAREA))
190 lyxerr << "\tfree object: +"
191 << x + bw << '+' << y + bw << ' '
192 << w - 15 - 2 * bw << 'x'
193 << h - 2 * bw << endl;
195 // We add this object as late as possible to avoid problems
198 work_area = obj = fl_add_free(FL_ALL_FREE,
202 C_XWorkArea_work_area_handler);
203 obj->wantkey = FL_KEY_ALL;
206 fl_set_object_boxtype(obj,FL_DOWN_BOX);
207 fl_set_object_resize(obj, FL_RESIZE_ALL);
208 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
210 /// X selection hook - xforms gets it wrong
211 fl_current_form->u_vdata = this;
212 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_XWorkAreaEventCB);
214 fl_unfreeze_all_forms();
218 val.function = GXcopy;
219 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
224 XWorkArea::~XWorkArea()
226 XFreeGC(fl_get_display(), copy_gc);
228 XFreePixmap(fl_get_display(), workareapixmap);
233 void destroy_object(FL_OBJECT * obj)
241 fl_delete_object(obj);
247 void XWorkArea::redraw(int width, int height)
249 static int cur_width = -1;
250 static int cur_height = -1;
252 if (cur_width == width && cur_height == height && workareapixmap) {
253 XCopyArea(fl_get_display(),
254 getPixmap(), getWin(), copy_gc,
255 0, 0, width, height, xpos(), ypos());
262 if (lyxerr.debugging(Debug::WORKAREA)) {
263 lyxerr << "(Re)creating pixmap ("
264 << width << 'x' << height << ")" << endl;
267 if (workareapixmap) {
268 XFreePixmap(fl_get_display(), workareapixmap);
271 workareapixmap = XCreatePixmap(fl_get_display(),
272 RootWindow(fl_get_display(), 0),
275 fl_get_visual_depth());
281 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
283 // we need to cache this for scroll_cb
284 doc_height_ = height;
287 fl_set_scrollbar_value(scrollbar, 0.0);
288 fl_set_scrollbar_size(scrollbar, scrollbar->h);
292 long const work_height = workHeight();
294 lyxerr[Debug::GUI] << "scroll: height now " << height << endl;
295 lyxerr[Debug::GUI] << "scroll: work_height " << work_height << endl;
297 /* If the text is smaller than the working area, the scrollbar
298 * maximum must be the working area height. No scrolling will
300 if (height <= work_height) {
301 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !" << endl;
302 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
303 fl_set_scrollbar_value(scrollbar, pos);
304 fl_set_scrollbar_size(scrollbar, scrollbar->h);
308 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
309 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
311 fl_set_scrollbar_value(scrollbar, pos);
313 double const slider_size =
314 (height == 0) ? 1.0 : 1.0 / double(height);
316 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
320 // callback for scrollbar slider
321 void XWorkArea::scroll_cb()
323 double const val = fl_get_scrollbar_value(scrollbar);
324 lyxerr[Debug::GUI] << "scroll: val: " << val << endl;
325 lyxerr[Debug::GUI] << "scroll: height: " << scrollbar->h << endl;
326 lyxerr[Debug::GUI] << "scroll: docheight: " << doc_height_ << endl;
327 scrollDocView(int(val));
332 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
336 static int x_old = -1;
337 static int y_old = -1;
338 static double scrollbar_value_old = -1.0;
340 XEvent * ev = static_cast<XEvent*>(xev);
341 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
347 if (!area->work_area ||
348 !area->work_area->form->visible)
350 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
351 area->redraw(area->workWidth(), area->workHeight());
354 if (!ev || ev->xbutton.button == 0) break;
355 // Should really have used xbutton.state
356 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
358 FuncRequest(LFUN_MOUSE_PRESS, ev->xbutton.x - ob->x,
359 ev->xbutton.y - ob->y,
360 x_button_state(ev->xbutton.button)));
363 if (!ev || ev->xbutton.button == 0) break;
364 // Should really have used xbutton.state
365 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
367 FuncRequest(LFUN_MOUSE_RELEASE, ev->xbutton.x - ob->x,
368 ev->xbutton.y - ob->y,
369 x_button_state(ev->xbutton.button)));
371 #if FL_VERSION < 1 && FL_REVISION < 89
376 if (!ev || !area->scrollbar)
378 if (ev->xmotion.x != x_old ||
379 ev->xmotion.y != y_old ||
380 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
382 x_old = ev->xmotion.x;
383 y_old = ev->xmotion.y;
384 scrollbar_value_old = fl_get_scrollbar_value(area->scrollbar);
385 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
387 FuncRequest(LFUN_MOUSE_MOTION, ev->xbutton.x - ob->x,
388 ev->xbutton.y - ob->y,
389 x_button_state(ev->xbutton.button)));
392 #if FL_VERSION < 1 && FL_REVISION < 89
398 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
402 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
403 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
404 // XForms < 0.89.5 does not have compose support
405 // so we are using our own compose support
406 LyXLookupString(ev, dummy, 1, &keysym);
408 XLookupString(xke, dummy, 1, &keysym, 0);
409 // int num_keys = XLookupString(xke, dummy, 10, &keysym, &xcs);
410 // lyxerr << "We have " << num_keys << " keys in the returned buffer" << endl;
411 // lyxerr << "Our dummy string is " << dummy << endl;
414 if (lyxerr.debugging(Debug::KEY)) {
415 char const * tmp = XKeysymToString(key);
416 char const * tmp2 = XKeysymToString(keysym);
417 string const stm = (tmp ? tmp : "");
418 string const stm2 = (tmp2 ? tmp2 : "");
420 lyxerr[Debug::KEY] << "XWorkArea: Key is `" << stm << "' ["
421 << key << "]" << endl;
422 lyxerr[Debug::KEY] << "XWorkArea: Keysym is `" << stm2 << "' ["
423 << keysym << "]" << endl;
426 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
427 if (keysym == NoSymbol) {
429 << "Empty kdb action (probably composing)"
433 KeySym ret_key = keysym;
435 // Note that we need this handling because of a bug
436 // in XForms 0.89, if this bug is resolved in the way I hope
437 // we can just use the keysym directly with out looking
438 // at key at all. (Lgb)
441 // We migth have to add more keysyms here also,
442 // we will do that as the issues arise. (Lgb)
443 if (keysym == XK_space) {
445 lyxerr[Debug::KEY] << "Using keysym [A]"
450 // It seems that this was a bit optimistic...
451 // With this hacking things seems to be better (Lgb)
452 //if (!iscntrl(key)) {
454 // lyxerr[Debug::KEY]
455 // << "Using key [B]\n"
457 // << static_cast<unsigned char>(key)
460 ret_key = (keysym ? keysym : key);
461 lyxerr[Debug::KEY] << "Using keysym [B]"
467 unsigned int const ret_state = xke->state;
469 // If you have a better way to handle "wild-output" of
470 // characters after the key has been released than the one
471 // below, please contact me. (Lgb)
472 static Time last_time_pressed;
473 static unsigned int last_key_pressed;
474 static unsigned int last_state_pressed;
475 lyxerr[Debug::KEY] << "Workarea Diff: "
476 << xke->time - last_time_pressed
478 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
479 && ret_state == last_state_pressed
480 && xke->keycode == last_key_pressed) {
482 << "Workarea: Purging X events." << endl;
483 //lyxerr << "Workarea Events: "
484 // << XEventsQueued(fl_get_display(), QueuedAlready)
486 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
487 XSync(fl_get_display(), 1);
488 // This purge make f.ex. scrolling stop immidiatly when
489 // releasing the PageDown button. The question is if
490 // this purging of XEvents can cause any harm...
491 // after some testing I can see no problems, but
492 // I'd like other reports too.
495 last_time_pressed = xke->time;
496 last_key_pressed = xke->keycode;
497 last_state_pressed = ret_state;
499 XLyXKeySym * xlk = new XLyXKeySym;
500 xlk->initFromKeySym(ret_key);
502 area->workAreaKeyPress(LyXKeySymPtr(xlk),
503 x_key_state(ret_state));
507 #if FL_VERSION > 0 || FL_REVISION >= 89
509 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
514 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
517 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
521 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
522 FuncRequest cmd(LFUN_MOUSE_DOUBLE, ev->xbutton.x - ob->x,
523 ev->xbutton.y - ob->y,
524 x_button_state(ev->xbutton.button));
530 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
531 FuncRequest cmd(LFUN_MOUSE_TRIPLE, ev->xbutton.x - ob->x,
532 ev->xbutton.y - ob->y,
533 x_button_state(ev->xbutton.button));
539 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
549 string clipboard_selection;
550 bool clipboard_read = false;
555 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
556 void const * data, long size)
558 clipboard_selection.erase();
561 clipboard_selection.reserve(size);
562 for (int i = 0; i < size; ++i)
563 clipboard_selection +=
564 static_cast<char const *>(data)[i];
565 clipboard_read = true;
574 int XWorkArea::event_cb(XEvent * xev)
578 case SelectionRequest:
579 lyxerr[Debug::GUI] << "X requested selection." << endl;
580 selectionRequested();
583 lyxerr[Debug::GUI] << "Lost selection." << endl;
591 void XWorkArea::haveSelection(bool yes) const
594 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
598 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
602 string const XWorkArea::getClipboard() const
604 clipboard_read = false;
606 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
611 while (!clipboard_read) {
612 if (fl_check_forms() == FL_EVENT) {
614 lyxerr << "Received unhandled X11 event" << endl;
615 lyxerr << "Type: 0x" << hex << ev.xany.type <<
616 " Target: 0x" << hex << ev.xany.window << dec << endl;
619 return clipboard_selection;
623 void XWorkArea::putClipboard(string const & s) const
628 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);