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"
20 #include "XLyXKeySym.h"
21 #include "funcrequest.h"
24 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
25 #include "lyxlookup.h"
36 void waitForX(bool discard)
38 XSync(fl_get_display(), discard);
42 mouse_button::state x_button_state(unsigned int button)
44 mouse_button::state b = mouse_button::none;
47 b = mouse_button::button1;
50 b = mouse_button::button2;
53 b = mouse_button::button3;
56 b = mouse_button::button4;
59 b = mouse_button::button5;
66 key_modifier::state x_key_state(unsigned int state)
68 key_modifier::state k = key_modifier::none;
69 if (state & ControlMask)
70 k |= key_modifier::ctrl;
71 if (state & ShiftMask)
72 k |= key_modifier::shift;
74 k |= key_modifier::alt;
81 void C_scroll_cb(FL_OBJECT * ob, long)
83 XWorkArea * area = static_cast<XWorkArea *>(ob->u_vdata);
88 int C_work_area_handler(FL_OBJECT * ob, int event, FL_Coord, FL_Coord,
91 return XWorkArea::work_area_handler(ob, event, 0, 0, key, xev);
95 int C_event_cb(FL_FORM * form, void * xev)
97 XWorkArea * area = static_cast<XWorkArea *>(form->u_vdata);
98 return area->event_cb(static_cast<XEvent *>(xev));
105 XWorkArea::XWorkArea(int x, int y, int w, int h)
106 : workareapixmap(0), painter_(*this)
108 if (lyxerr.debugging(Debug::WORKAREA)) {
109 lyxerr << "\tbackground box: +"
110 << x << '+' << y << ' '
111 << w - 15 << 'x' << h << endl;
114 fl_freeze_all_forms();
118 obj = fl_add_box(FL_BORDER_BOX, x, y, w - 15, h, "");
119 fl_set_object_resize(obj, FL_RESIZE_ALL);
120 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
122 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
123 x + w - 15, y, 17, h, "");
124 fl_set_object_boxtype(obj, FL_UP_BOX);
125 fl_set_object_resize(obj, FL_RESIZE_ALL);
126 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
128 fl_set_object_callback(obj, C_scroll_cb, 0);
129 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
130 fl_set_scrollbar_value(scrollbar, 0.0);
131 fl_set_scrollbar_size(scrollbar, scrollbar->h);
133 int const bw = int(abs(fl_get_border_width()));
135 // Create the workarea pixmap
136 // FIXME remove redraw(w - 15 - 2 * bw, h - 2 * bw);
138 if (lyxerr.debugging(Debug::WORKAREA))
139 lyxerr << "\tfree object: +"
140 << x + bw << '+' << y + bw << ' '
141 << w - 15 - 2 * bw << 'x'
142 << h - 2 * bw << endl;
144 // We add this object as late as possible to avoid problems
147 work_area = obj = fl_add_free(FL_ALL_FREE,
151 C_work_area_handler);
152 obj->wantkey = FL_KEY_ALL;
155 fl_set_object_boxtype(obj,FL_DOWN_BOX);
156 fl_set_object_resize(obj, FL_RESIZE_ALL);
157 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
159 /// X selection hook - xforms gets it wrong
160 fl_current_form->u_vdata = this;
161 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_event_cb);
163 fl_unfreeze_all_forms();
167 val.function = GXcopy;
168 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
173 XWorkArea::~XWorkArea()
175 XFreeGC(fl_get_display(), copy_gc);
177 XFreePixmap(fl_get_display(), workareapixmap);
181 void XWorkArea::redraw(int width, int height)
183 static int cur_width = -1;
184 static int cur_height = -1;
186 if (cur_width == width && cur_height == height && workareapixmap) {
187 XCopyArea(fl_get_display(),
188 getPixmap(), getWin(), copy_gc,
189 0, 0, width, height, xpos(), ypos());
196 if (lyxerr.debugging(Debug::WORKAREA)) {
197 lyxerr << "(Re)creating pixmap ("
198 << width << 'x' << height << ')' << endl;
201 if (workareapixmap) {
202 XFreePixmap(fl_get_display(), workareapixmap);
205 workareapixmap = XCreatePixmap(fl_get_display(),
206 RootWindow(fl_get_display(), 0),
209 fl_get_visual_depth());
215 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
217 // we need to cache this for scroll_cb
218 doc_height_ = height;
221 fl_set_scrollbar_value(scrollbar, 0.0);
222 fl_set_scrollbar_size(scrollbar, scrollbar->h);
226 long const work_height = workHeight();
228 if (lyxerr.debugging(Debug::GUI)) {
229 lyxerr << "scroll: height now " << height << '\n'
230 << "scroll: work_height " << work_height << endl;
233 /* If the text is smaller than the working area, the scrollbar
234 * maximum must be the working area height. No scrolling will
236 if (height <= work_height) {
237 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !"
239 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
240 fl_set_scrollbar_value(scrollbar, pos);
241 fl_set_scrollbar_size(scrollbar, scrollbar->h);
245 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
246 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
248 fl_set_scrollbar_value(scrollbar, pos);
250 double const slider_size =
251 (height == 0) ? 1.0 : 1.0 / double(height);
253 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
257 // callback for scrollbar slider
258 void XWorkArea::scroll_cb()
260 double const val = fl_get_scrollbar_value(scrollbar);
262 if (lyxerr.debugging(Debug::GUI)) {
263 lyxerr << "scroll: val: " << val << '\n'
264 << "scroll: height: " << scrollbar->h << '\n'
265 << "scroll: docheight: " << doc_height_ << endl;
268 scrollDocView(int(val));
273 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
277 XEvent * ev = static_cast<XEvent*>(xev);
278 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
284 if (!area->work_area || !area->work_area->form->visible)
286 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
287 area->redraw(area->workWidth(), area->workHeight());
290 if (!ev || ev->xbutton.button == 0) break;
291 // Should really have used xbutton.state
292 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
294 FuncRequest(LFUN_MOUSE_PRESS,
295 ev->xbutton.x - ob->x,
296 ev->xbutton.y - ob->y,
297 x_button_state(key)));
300 if (!ev || ev->xbutton.button == 0) break;
301 // Should really have used xbutton.state
302 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
304 FuncRequest(LFUN_MOUSE_RELEASE,
305 ev->xbutton.x - ob->x,
306 ev->xbutton.y - ob->y,
307 x_button_state(key)));
309 #if FL_VERSION < 1 && FL_REVISION < 89
315 if (!ev || !area->scrollbar)
318 int const drag_x = ev->xmotion.x;
319 int const drag_y = ev->xmotion.y;
320 int const area_y = ob->y;
321 int const area_h = ob->h;
323 // Check if the mouse is above or below the workarea
324 if (drag_y <= area_y || drag_y >= area_y + area_h) {
325 // The mouse button is depressed and we are outside the
326 // workarea. That means we are simultaneously selecting
327 // text and scrolling the view.
328 // Use a Timeout to react to a drag events only every
329 // 200ms. All intervening events are discarded,
330 // allowing the user to control position easily.
331 static int const discard_interval = 200;
332 static Timeout timeout(discard_interval);
334 if (timeout.running())
336 // The timeout is not running, so process the
337 // event, first starting the timeout to discard future
342 static int x_old = -1;
343 static int y_old = -1;
344 static double scrollbar_value_old = -1.0;
346 double const scrollbar_value =
347 fl_get_scrollbar_value(area->scrollbar);
349 if (drag_x != x_old || drag_y != y_old ||
350 scrollbar_value != scrollbar_value_old) {
353 scrollbar_value_old = scrollbar_value;
355 lyxerr[Debug::WORKAREA] << "Workarea event: DRAG"
358 // It transpires that ev->xbutton.button == 0 when
359 // the mouse is dragged, so it cannot be used to
360 // initialise x_button_state and hence FuncRequest.
362 // The 'key' that is passed into the function does
363 // contain the necessary info, however.
365 // It is for this reason that x_button_state has
366 // been modified to work with key
367 // rather than ev->xbutton.button.
369 // Angus 15 Oct 2002.
370 FuncRequest cmd(LFUN_MOUSE_MOTION,
371 ev->xbutton.x - ob->x,
372 ev->xbutton.y - ob->y,
373 x_button_state(key));
379 #if FL_VERSION < 1 && FL_REVISION < 89
385 lyxerr[Debug::WORKAREA] << "Workarea event: KEYPRESS" << endl;
389 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
390 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
391 // XForms < 0.89.5 does not have compose support
392 // so we are using our own compose support
393 LyXLookupString(ev, dummy, 1, &keysym);
395 XLookupString(xke, dummy, 1, &keysym, 0);
398 if (lyxerr.debugging(Debug::KEY)) {
399 char const * const tmp = XKeysymToString(key);
400 char const * const tmp2 = XKeysymToString(keysym);
401 string const stm = (tmp ? tmp : string());
402 string const stm2 = (tmp2 ? tmp2 : string());
404 lyxerr << "XWorkArea: Key is `" << stm
405 << "' [" << key << "]\n"
406 << "XWorkArea: Keysym is `" << stm2
407 << "' [" << keysym << ']' << endl;
410 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
411 if (keysym == NoSymbol) {
413 << "Empty kdb action (probably composing)"
417 KeySym ret_key = keysym;
419 // Note that we need this handling because of a bug
420 // in XForms 0.89, if this bug is resolved in the way I hope
421 // we can just use the keysym directly without looking
422 // at key at all. (Lgb)
425 // We might have to add more keysyms here also,
426 // we will do that as the issues arise. (Lgb)
427 if (keysym == XK_space) {
429 lyxerr[Debug::KEY] << "Using keysym [A]"
434 // It seems that this was a bit optimistic...
435 // With this hacking things seems to be better (Lgb)
436 //if (!iscntrl(key)) {
438 // lyxerr[Debug::KEY]
439 // << "Using key [B]\n"
441 // << static_cast<unsigned char>(key)
444 ret_key = (keysym ? keysym : key);
445 lyxerr[Debug::KEY] << "Using keysym [B]"
451 unsigned int const ret_state = xke->state;
453 // If you have a better way to handle "wild-output" of
454 // characters after the key has been released than the one
455 // below, please contact me. (Lgb)
456 static Time last_time_pressed;
457 static unsigned int last_key_pressed;
458 static unsigned int last_state_pressed;
459 lyxerr[Debug::KEY] << "Workarea Diff: "
460 << xke->time - last_time_pressed
462 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
463 && ret_state == last_state_pressed
464 && xke->keycode == last_key_pressed) {
466 << "Workarea: Purging X events." << endl;
467 //lyxerr << "Workarea Events: "
468 // << XEventsQueued(fl_get_display(), QueuedAlready)
470 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
472 // This purge make f.ex. scrolling stop immediately when
473 // releasing the PageDown button. The question is if
474 // this purging of XEvents can cause any harm...
475 // after some testing I can see no problems, but
476 // I'd like other reports too.
479 last_time_pressed = xke->time;
480 last_key_pressed = xke->keycode;
481 last_state_pressed = ret_state;
483 XLyXKeySym * xlk = new XLyXKeySym;
484 xlk->initFromKeySym(ret_key);
486 area->workAreaKeyPress(LyXKeySymPtr(xlk),
487 x_key_state(ret_state));
491 #if FL_VERSION > 0 || FL_REVISION >= 89
493 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
498 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
499 fl_set_cursor(FL_ObjWin(area->work_area), XC_xterm);
502 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
503 // There should be no need for this. But there is.
504 fl_set_cursor(FL_ObjWin(area->work_area), FL_DEFAULT_CURSOR);
508 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
509 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
510 ev->xbutton.x - ob->x,
511 ev->xbutton.y - ob->y,
512 x_button_state(key));
518 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
519 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
520 ev->xbutton.x - ob->x,
521 ev->xbutton.y - ob->y,
522 x_button_state(key));
528 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
538 string clipboard_selection;
539 bool clipboard_read = false;
543 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
544 void const * data, long size)
546 clipboard_selection.erase();
549 clipboard_selection.reserve(size);
550 for (int i = 0; i < size; ++i)
551 clipboard_selection += static_cast<char const *>(data)[i];
552 clipboard_read = true;
560 int XWorkArea::event_cb(XEvent * xev)
563 case SelectionRequest:
564 lyxerr[Debug::GUI] << "X requested selection." << endl;
565 selectionRequested();
568 lyxerr[Debug::GUI] << "Lost selection." << endl;
576 void XWorkArea::haveSelection(bool yes) const
578 Window const owner = yes ? FL_ObjWin(work_area) : None;
579 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, owner, CurrentTime);
583 string const XWorkArea::getClipboard() const
585 clipboard_read = false;
587 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
592 while (!clipboard_read) {
593 if (fl_check_forms() == FL_EVENT) {
595 lyxerr << "Received unhandled X11 event\n"
596 << "Type: 0x" << hex << ev.xany.type
597 << " Target: 0x" << hex << ev.xany.window
601 return clipboard_selection;
605 void XWorkArea::putClipboard(string const & s) const
610 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);