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 key_modifier::state x_key_state(unsigned int state)
96 key_modifier::state k = key_modifier::none;
97 if (state & ControlMask)
98 k |= key_modifier::ctrl;
99 if (state & ShiftMask)
100 k |= key_modifier::shift;
101 if (state & Mod1Mask)
102 k |= key_modifier::alt;
111 // Just a bunch of C wrappers around static members of XWorkArea
113 void C_XWorkArea_scroll_cb(FL_OBJECT * ob, long)
115 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
121 int C_XWorkArea_work_area_handler(FL_OBJECT * ob, int event,
125 return XWorkArea::work_area_handler(ob, event,
130 int C_XWorkAreaEventCB(FL_FORM * form, void * xev) {
131 XWorkArea * wa = static_cast<XWorkArea*>(form->u_vdata);
132 return wa->event_cb(static_cast<XEvent*>(xev));
137 XWorkArea::XWorkArea(int x, int y, int w, int h)
138 : workareapixmap(0), painter_(*this)
140 fl_freeze_all_forms();
144 if (lyxerr.debugging(Debug::WORKAREA))
145 lyxerr << "\tbackground box: +"
146 << x << '+' << y << ' '
147 << w - 15 << 'x' << h << endl;
148 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
152 fl_set_object_resize(obj, FL_RESIZE_ALL);
153 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
155 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
158 fl_set_object_boxtype(obj, FL_UP_BOX);
159 fl_set_object_resize(obj, FL_RESIZE_ALL);
160 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
162 fl_set_object_callback(obj, C_XWorkArea_scroll_cb, 0);
163 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
164 fl_set_scrollbar_value(scrollbar, 0.0);
165 fl_set_scrollbar_size(scrollbar, scrollbar->h);
167 int const bw = int(abs(fl_get_border_width()));
169 // Create the workarea pixmap
170 // FIXME remove redraw(w - 15 - 2 * bw, h - 2 * bw);
172 if (lyxerr.debugging(Debug::WORKAREA))
173 lyxerr << "\tfree object: +"
174 << x + bw << '+' << y + bw << ' '
175 << w - 15 - 2 * bw << 'x'
176 << h - 2 * bw << endl;
178 // We add this object as late as possible to avoid problems
181 work_area = obj = fl_add_free(FL_ALL_FREE,
185 C_XWorkArea_work_area_handler);
186 obj->wantkey = FL_KEY_ALL;
189 fl_set_object_boxtype(obj,FL_DOWN_BOX);
190 fl_set_object_resize(obj, FL_RESIZE_ALL);
191 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
193 /// X selection hook - xforms gets it wrong
194 fl_current_form->u_vdata = this;
195 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_XWorkAreaEventCB);
197 fl_unfreeze_all_forms();
201 val.function = GXcopy;
202 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
207 XWorkArea::~XWorkArea()
209 XFreeGC(fl_get_display(), copy_gc);
211 XFreePixmap(fl_get_display(), workareapixmap);
216 void destroy_object(FL_OBJECT * obj)
224 fl_delete_object(obj);
230 void XWorkArea::redraw(int width, int height)
232 static int cur_width = -1;
233 static int cur_height = -1;
235 if (cur_width == width && cur_height == height && workareapixmap) {
236 XCopyArea(fl_get_display(),
237 getPixmap(), getWin(), copy_gc,
238 0, 0, width, height, xpos(), ypos());
245 if (lyxerr.debugging(Debug::WORKAREA)) {
246 lyxerr << "(Re)creating pixmap ("
247 << width << 'x' << height << ')' << endl;
250 if (workareapixmap) {
251 XFreePixmap(fl_get_display(), workareapixmap);
254 workareapixmap = XCreatePixmap(fl_get_display(),
255 RootWindow(fl_get_display(), 0),
258 fl_get_visual_depth());
264 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
266 // we need to cache this for scroll_cb
267 doc_height_ = height;
270 fl_set_scrollbar_value(scrollbar, 0.0);
271 fl_set_scrollbar_size(scrollbar, scrollbar->h);
275 long const work_height = workHeight();
277 lyxerr[Debug::GUI] << "scroll: height now " << height << endl;
278 lyxerr[Debug::GUI] << "scroll: work_height " << work_height << endl;
280 /* If the text is smaller than the working area, the scrollbar
281 * maximum must be the working area height. No scrolling will
283 if (height <= work_height) {
284 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !" << endl;
285 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
286 fl_set_scrollbar_value(scrollbar, pos);
287 fl_set_scrollbar_size(scrollbar, scrollbar->h);
291 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
292 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
294 fl_set_scrollbar_value(scrollbar, pos);
296 double const slider_size =
297 (height == 0) ? 1.0 : 1.0 / double(height);
299 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
303 // callback for scrollbar slider
304 void XWorkArea::scroll_cb()
306 double const val = fl_get_scrollbar_value(scrollbar);
307 lyxerr[Debug::GUI] << "scroll: val: " << val << endl;
308 lyxerr[Debug::GUI] << "scroll: height: " << scrollbar->h << endl;
309 lyxerr[Debug::GUI] << "scroll: docheight: " << doc_height_ << endl;
310 scrollDocView(int(val));
315 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
319 static int x_old = -1;
320 static int y_old = -1;
321 static double scrollbar_value_old = -1.0;
323 XEvent * ev = static_cast<XEvent*>(xev);
324 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
330 if (!area->work_area ||
331 !area->work_area->form->visible)
333 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
334 area->redraw(area->workWidth(), area->workHeight());
337 if (!ev || ev->xbutton.button == 0) break;
338 // Should really have used xbutton.state
339 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
341 FuncRequest(LFUN_MOUSE_PRESS,
342 ev->xbutton.x - ob->x,
343 ev->xbutton.y - ob->y,
344 x_button_state(key)));
347 if (!ev || ev->xbutton.button == 0) break;
348 // Should really have used xbutton.state
349 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
351 FuncRequest(LFUN_MOUSE_RELEASE,
352 ev->xbutton.x - ob->x,
353 ev->xbutton.y - ob->y,
354 x_button_state(key)));
356 #if FL_VERSION < 1 && FL_REVISION < 89
361 if (!ev || !area->scrollbar)
363 if (ev->xmotion.x != x_old ||
364 ev->xmotion.y != y_old ||
365 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
367 x_old = ev->xmotion.x;
368 y_old = ev->xmotion.y;
369 scrollbar_value_old = fl_get_scrollbar_value(area->scrollbar);
370 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
372 // It transpires that ev->xbutton.button == 0 when
373 // the mouse is dragged, so it cannot be used to
374 // initialise x_button_state and hence FuncRequest.
376 // The 'key' that is passed into the function does
377 // contain the necessary info, however.
379 // It is for this reason that x_button_state has
380 // been modified to work with key
381 // rather than ev->xbutton.button.
383 // Angus 15 Oct 2002.
384 FuncRequest cmd(LFUN_MOUSE_MOTION,
385 ev->xbutton.x - ob->x,
386 ev->xbutton.y - ob->y,
387 x_button_state(key));
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,
523 ev->xbutton.x - ob->x,
524 ev->xbutton.y - ob->y,
525 x_button_state(key));
531 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
532 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
533 ev->xbutton.x - ob->x,
534 ev->xbutton.y - ob->y,
535 x_button_state(key));
541 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
551 string clipboard_selection;
552 bool clipboard_read = false;
557 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
558 void const * data, long size)
560 clipboard_selection.erase();
563 clipboard_selection.reserve(size);
564 for (int i = 0; i < size; ++i)
565 clipboard_selection +=
566 static_cast<char const *>(data)[i];
567 clipboard_read = true;
576 int XWorkArea::event_cb(XEvent * xev)
580 case SelectionRequest:
581 lyxerr[Debug::GUI] << "X requested selection." << endl;
582 selectionRequested();
585 lyxerr[Debug::GUI] << "Lost selection." << endl;
593 void XWorkArea::haveSelection(bool yes) const
596 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
600 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
604 string const XWorkArea::getClipboard() const
606 clipboard_read = false;
608 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
613 while (!clipboard_read) {
614 if (fl_check_forms() == FL_EVENT) {
616 lyxerr << "Received unhandled X11 event" << endl;
617 lyxerr << "Type: 0x" << hex << ev.xany.type <<
618 " Target: 0x" << hex << ev.xany.window << dec << endl;
621 return clipboard_selection;
625 void XWorkArea::putClipboard(string const & s) const
630 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);