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"
26 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
27 #include "lyxlookup.h"
30 #include "support/filetools.h" // LibFileSearch
31 #include "support/lstrings.h"
32 #include "support/LAssert.h"
37 // xforms doesn't define this (but it should be in <forms.h>).
39 FL_APPEVENT_CB fl_set_preemptive_callback(Window, FL_APPEVENT_CB, void *);
51 XSync(fl_get_display(), 0);
55 void setXtermCursor(Window win)
58 static bool cursor_undefined = true;
59 if (cursor_undefined) {
60 cursor = XCreateFontCursor(fl_get_display(), XC_xterm);
61 XFlush(fl_get_display());
62 cursor_undefined = false;
64 XDefineCursor(fl_get_display(), win, cursor);
65 XFlush(fl_get_display());
69 mouse_button::state x_button_state(unsigned int button)
71 mouse_button::state b = mouse_button::none;
74 b = mouse_button::button1;
77 b = mouse_button::button2;
80 b = mouse_button::button3;
83 b = mouse_button::button4;
86 b = mouse_button::button5;
95 key_modifier::state x_key_state(unsigned int state)
97 key_modifier::state k = key_modifier::none;
98 if (state & ControlMask)
99 k |= key_modifier::ctrl;
100 if (state & ShiftMask)
101 k |= key_modifier::shift;
102 if (state & Mod1Mask)
103 k |= key_modifier::alt;
112 // Just a bunch of C wrappers around static members of XWorkArea
114 void C_XWorkArea_scroll_cb(FL_OBJECT * ob, long)
116 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
122 int C_XWorkArea_work_area_handler(FL_OBJECT * ob, int event,
126 return XWorkArea::work_area_handler(ob, event,
131 int C_XWorkAreaEventCB(FL_FORM * form, void * xev) {
132 XWorkArea * wa = static_cast<XWorkArea*>(form->u_vdata);
133 return wa->event_cb(static_cast<XEvent*>(xev));
138 XWorkArea::XWorkArea(int x, int y, int w, int h)
139 : workareapixmap(0), painter_(*this)
141 fl_freeze_all_forms();
145 if (lyxerr.debugging(Debug::WORKAREA))
146 lyxerr << "\tbackground box: +"
147 << x << '+' << y << ' '
148 << w - 15 << 'x' << h << endl;
149 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
153 fl_set_object_resize(obj, FL_RESIZE_ALL);
154 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
156 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
159 fl_set_object_boxtype(obj, FL_UP_BOX);
160 fl_set_object_resize(obj, FL_RESIZE_ALL);
161 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
163 fl_set_object_callback(obj, C_XWorkArea_scroll_cb, 0);
164 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
165 fl_set_scrollbar_value(scrollbar, 0.0);
166 fl_set_scrollbar_size(scrollbar, scrollbar->h);
168 int const bw = int(abs(fl_get_border_width()));
170 // Create the workarea pixmap
171 // FIXME remove redraw(w - 15 - 2 * bw, h - 2 * bw);
173 if (lyxerr.debugging(Debug::WORKAREA))
174 lyxerr << "\tfree object: +"
175 << x + bw << '+' << y + bw << ' '
176 << w - 15 - 2 * bw << 'x'
177 << h - 2 * bw << endl;
179 // We add this object as late as possible to avoid problems
182 work_area = obj = fl_add_free(FL_ALL_FREE,
186 C_XWorkArea_work_area_handler);
187 obj->wantkey = FL_KEY_ALL;
190 fl_set_object_boxtype(obj,FL_DOWN_BOX);
191 fl_set_object_resize(obj, FL_RESIZE_ALL);
192 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
194 /// X selection hook - xforms gets it wrong
195 fl_current_form->u_vdata = this;
196 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_XWorkAreaEventCB);
198 fl_unfreeze_all_forms();
202 val.function = GXcopy;
203 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
208 XWorkArea::~XWorkArea()
210 XFreeGC(fl_get_display(), copy_gc);
212 XFreePixmap(fl_get_display(), workareapixmap);
217 void destroy_object(FL_OBJECT * obj)
225 fl_delete_object(obj);
231 void XWorkArea::redraw(int width, int height)
233 static int cur_width = -1;
234 static int cur_height = -1;
236 if (cur_width == width && cur_height == height && workareapixmap) {
237 XCopyArea(fl_get_display(),
238 getPixmap(), getWin(), copy_gc,
239 0, 0, width, height, xpos(), ypos());
246 if (lyxerr.debugging(Debug::WORKAREA)) {
247 lyxerr << "(Re)creating pixmap ("
248 << width << 'x' << height << ')' << endl;
251 if (workareapixmap) {
252 XFreePixmap(fl_get_display(), workareapixmap);
255 workareapixmap = XCreatePixmap(fl_get_display(),
256 RootWindow(fl_get_display(), 0),
259 fl_get_visual_depth());
265 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
267 // we need to cache this for scroll_cb
268 doc_height_ = height;
271 fl_set_scrollbar_value(scrollbar, 0.0);
272 fl_set_scrollbar_size(scrollbar, scrollbar->h);
276 long const work_height = workHeight();
278 lyxerr[Debug::GUI] << "scroll: height now " << height << endl;
279 lyxerr[Debug::GUI] << "scroll: work_height " << work_height << endl;
281 /* If the text is smaller than the working area, the scrollbar
282 * maximum must be the working area height. No scrolling will
284 if (height <= work_height) {
285 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !" << endl;
286 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
287 fl_set_scrollbar_value(scrollbar, pos);
288 fl_set_scrollbar_size(scrollbar, scrollbar->h);
292 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
293 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
295 fl_set_scrollbar_value(scrollbar, pos);
297 double const slider_size =
298 (height == 0) ? 1.0 : 1.0 / double(height);
300 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
304 // callback for scrollbar slider
305 void XWorkArea::scroll_cb()
307 double const val = fl_get_scrollbar_value(scrollbar);
308 lyxerr[Debug::GUI] << "scroll: val: " << val << endl;
309 lyxerr[Debug::GUI] << "scroll: height: " << scrollbar->h << endl;
310 lyxerr[Debug::GUI] << "scroll: docheight: " << doc_height_ << endl;
311 scrollDocView(int(val));
316 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
320 static int x_old = -1;
321 static int y_old = -1;
322 static double scrollbar_value_old = -1.0;
323 static int const motion_interval = 200;
324 static Timeout timdel(motion_interval);
326 XEvent * ev = static_cast<XEvent*>(xev);
327 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
333 if (!area->work_area ||
334 !area->work_area->form->visible)
336 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
337 area->redraw(area->workWidth(), area->workHeight());
340 if (!ev || ev->xbutton.button == 0) break;
341 // Should really have used xbutton.state
342 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
344 FuncRequest(LFUN_MOUSE_PRESS,
345 ev->xbutton.x - ob->x,
346 ev->xbutton.y - ob->y,
347 x_button_state(key)));
350 if (!ev || ev->xbutton.button == 0) break;
351 // Should really have used xbutton.state
352 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
354 FuncRequest(LFUN_MOUSE_RELEASE,
355 ev->xbutton.x - ob->x,
356 ev->xbutton.y - ob->y,
357 x_button_state(key)));
359 #if FL_VERSION < 1 && FL_REVISION < 89
365 int const drag_y = ev->xmotion.y;
366 int const area_y = ob->y;
367 int const area_h = ob->h;
369 // Check if the mouse is above or below the workarea
370 if (drag_y >= area_y + area_h || drag_y <= area_y) {
371 // we are outside, then we must not give too many
373 if (timdel.running())
379 if (!ev || !area->scrollbar)
381 if (ev->xmotion.x != x_old ||
382 ev->xmotion.y != y_old ||
383 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
385 x_old = ev->xmotion.x;
386 y_old = ev->xmotion.y;
387 scrollbar_value_old = fl_get_scrollbar_value(area->scrollbar);
388 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
390 // It transpires that ev->xbutton.button == 0 when
391 // the mouse is dragged, so it cannot be used to
392 // initialise x_button_state and hence FuncRequest.
394 // The 'key' that is passed into the function does
395 // contain the necessary info, however.
397 // It is for this reason that x_button_state has
398 // been modified to work with key
399 // rather than ev->xbutton.button.
401 // Angus 15 Oct 2002.
402 FuncRequest cmd(LFUN_MOUSE_MOTION,
403 ev->xbutton.x - ob->x,
404 ev->xbutton.y - ob->y,
405 x_button_state(key));
412 #if FL_VERSION < 1 && FL_REVISION < 89
418 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
422 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
423 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
424 // XForms < 0.89.5 does not have compose support
425 // so we are using our own compose support
426 LyXLookupString(ev, dummy, 1, &keysym);
428 XLookupString(xke, dummy, 1, &keysym, 0);
429 // int num_keys = XLookupString(xke, dummy, 10, &keysym, &xcs);
430 // lyxerr << "We have " << num_keys << " keys in the returned buffer" << endl;
431 // lyxerr << "Our dummy string is " << dummy << endl;
434 if (lyxerr.debugging(Debug::KEY)) {
435 char const * tmp = XKeysymToString(key);
436 char const * tmp2 = XKeysymToString(keysym);
437 string const stm = (tmp ? tmp : "");
438 string const stm2 = (tmp2 ? tmp2 : "");
440 lyxerr[Debug::KEY] << "XWorkArea: Key is `" << stm
441 << "' [" << key << ']' << endl;
442 lyxerr[Debug::KEY] << "XWorkArea: Keysym is `" << stm2
443 << "' [" << keysym << ']' << endl;
446 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
447 if (keysym == NoSymbol) {
449 << "Empty kdb action (probably composing)"
453 KeySym ret_key = keysym;
455 // Note that we need this handling because of a bug
456 // in XForms 0.89, if this bug is resolved in the way I hope
457 // we can just use the keysym directly with out looking
458 // at key at all. (Lgb)
461 // We migth have to add more keysyms here also,
462 // we will do that as the issues arise. (Lgb)
463 if (keysym == XK_space) {
465 lyxerr[Debug::KEY] << "Using keysym [A]"
470 // It seems that this was a bit optimistic...
471 // With this hacking things seems to be better (Lgb)
472 //if (!iscntrl(key)) {
474 // lyxerr[Debug::KEY]
475 // << "Using key [B]\n"
477 // << static_cast<unsigned char>(key)
480 ret_key = (keysym ? keysym : key);
481 lyxerr[Debug::KEY] << "Using keysym [B]"
487 unsigned int const ret_state = xke->state;
489 // If you have a better way to handle "wild-output" of
490 // characters after the key has been released than the one
491 // below, please contact me. (Lgb)
492 static Time last_time_pressed;
493 static unsigned int last_key_pressed;
494 static unsigned int last_state_pressed;
495 lyxerr[Debug::KEY] << "Workarea Diff: "
496 << xke->time - last_time_pressed
498 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
499 && ret_state == last_state_pressed
500 && xke->keycode == last_key_pressed) {
502 << "Workarea: Purging X events." << endl;
503 //lyxerr << "Workarea Events: "
504 // << XEventsQueued(fl_get_display(), QueuedAlready)
506 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
507 XSync(fl_get_display(), 1);
508 // This purge make f.ex. scrolling stop immidiatly when
509 // releasing the PageDown button. The question is if
510 // this purging of XEvents can cause any harm...
511 // after some testing I can see no problems, but
512 // I'd like other reports too.
515 last_time_pressed = xke->time;
516 last_key_pressed = xke->keycode;
517 last_state_pressed = ret_state;
519 XLyXKeySym * xlk = new XLyXKeySym;
520 xlk->initFromKeySym(ret_key);
522 area->workAreaKeyPress(LyXKeySymPtr(xlk),
523 x_key_state(ret_state));
527 #if FL_VERSION > 0 || FL_REVISION >= 89
529 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
534 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
537 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
541 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
542 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
543 ev->xbutton.x - ob->x,
544 ev->xbutton.y - ob->y,
545 x_button_state(key));
551 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
552 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
553 ev->xbutton.x - ob->x,
554 ev->xbutton.y - ob->y,
555 x_button_state(key));
561 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
571 string clipboard_selection;
572 bool clipboard_read = false;
577 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
578 void const * data, long size)
580 clipboard_selection.erase();
583 clipboard_selection.reserve(size);
584 for (int i = 0; i < size; ++i)
585 clipboard_selection +=
586 static_cast<char const *>(data)[i];
587 clipboard_read = true;
596 int XWorkArea::event_cb(XEvent * xev)
600 case SelectionRequest:
601 lyxerr[Debug::GUI] << "X requested selection." << endl;
602 selectionRequested();
605 lyxerr[Debug::GUI] << "Lost selection." << endl;
613 void XWorkArea::haveSelection(bool yes) const
616 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
620 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
624 string const XWorkArea::getClipboard() const
626 clipboard_read = false;
628 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
633 while (!clipboard_read) {
634 if (fl_check_forms() == FL_EVENT) {
636 lyxerr << "Received unhandled X11 event" << endl;
637 lyxerr << "Type: 0x" << hex << ev.xany.type <<
638 " Target: 0x" << hex << ev.xany.window << dec << endl;
641 return clipboard_selection;
645 void XWorkArea::putClipboard(string const & s) const
650 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);