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.
14 #include "XWorkArea.h"
17 #include "XLyXKeySym.h"
18 #include "funcrequest.h"
30 void waitForX(bool discard)
32 XSync(fl_get_display(), discard);
36 mouse_button::state x_button_state(unsigned int button)
38 mouse_button::state b = mouse_button::none;
41 b = mouse_button::button1;
44 b = mouse_button::button2;
47 b = mouse_button::button3;
50 b = mouse_button::button4;
53 b = mouse_button::button5;
60 key_modifier::state x_key_state(unsigned int state)
62 key_modifier::state k = key_modifier::none;
63 if (state & ControlMask)
64 k |= key_modifier::ctrl;
65 if (state & ShiftMask)
66 k |= key_modifier::shift;
68 k |= key_modifier::alt;
75 void C_scroll_cb(FL_OBJECT * ob, long)
77 XWorkArea * area = static_cast<XWorkArea *>(ob->u_vdata);
82 int C_work_area_handler(FL_OBJECT * ob, int event, FL_Coord, FL_Coord,
85 return XWorkArea::work_area_handler(ob, event, 0, 0, key, xev);
89 int C_event_cb(FL_FORM * form, void * xev)
91 XWorkArea * area = static_cast<XWorkArea *>(form->u_vdata);
92 return area->event_cb(static_cast<XEvent *>(xev));
99 XWorkArea::XWorkArea(int x, int y, int w, int h)
100 : workareapixmap(0), painter_(*this)
102 if (lyxerr.debugging(Debug::WORKAREA)) {
103 lyxerr << "\tbackground box: +"
104 << x << '+' << y << ' '
105 << w - 15 << 'x' << h << endl;
108 fl_freeze_all_forms();
112 obj = fl_add_box(FL_BORDER_BOX, x, y, w - 15, h, "");
113 fl_set_object_resize(obj, FL_RESIZE_ALL);
114 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
116 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
117 x + w - 15, y, 17, h, "");
118 fl_set_object_boxtype(obj, FL_UP_BOX);
119 fl_set_object_resize(obj, FL_RESIZE_ALL);
120 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
122 fl_set_object_callback(obj, C_scroll_cb, 0);
123 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
124 fl_set_scrollbar_value(scrollbar, 0.0);
125 fl_set_scrollbar_size(scrollbar, scrollbar->h);
127 int const bw = int(abs(fl_get_border_width()));
129 // Create the workarea pixmap
130 // FIXME remove redraw(w - 15 - 2 * bw, h - 2 * bw);
132 if (lyxerr.debugging(Debug::WORKAREA))
133 lyxerr << "\tfree object: +"
134 << x + bw << '+' << y + bw << ' '
135 << w - 15 - 2 * bw << 'x'
136 << h - 2 * bw << endl;
138 // We add this object as late as possible to avoid problems
141 work_area = obj = fl_add_free(FL_ALL_FREE,
145 C_work_area_handler);
146 obj->wantkey = FL_KEY_ALL;
149 fl_set_object_boxtype(obj,FL_DOWN_BOX);
150 fl_set_object_resize(obj, FL_RESIZE_ALL);
151 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
153 /// X selection hook - xforms gets it wrong
154 fl_current_form->u_vdata = this;
155 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_event_cb);
157 fl_unfreeze_all_forms();
161 val.function = GXcopy;
162 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
167 XWorkArea::~XWorkArea()
169 XFreeGC(fl_get_display(), copy_gc);
171 XFreePixmap(fl_get_display(), workareapixmap);
175 void XWorkArea::redraw(int width, int height)
177 static int cur_width = -1;
178 static int cur_height = -1;
180 if (cur_width == width && cur_height == height && workareapixmap) {
181 XCopyArea(fl_get_display(),
182 getPixmap(), getWin(), copy_gc,
183 0, 0, width, height, xpos(), ypos());
190 if (lyxerr.debugging(Debug::WORKAREA)) {
191 lyxerr << "(Re)creating pixmap ("
192 << width << 'x' << height << ')' << endl;
195 if (workareapixmap) {
196 XFreePixmap(fl_get_display(), workareapixmap);
199 workareapixmap = XCreatePixmap(fl_get_display(),
200 RootWindow(fl_get_display(), 0),
203 fl_get_visual_depth());
209 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
211 // we need to cache this for scroll_cb
212 doc_height_ = height;
215 fl_set_scrollbar_value(scrollbar, 0.0);
216 fl_set_scrollbar_size(scrollbar, scrollbar->h);
220 long const work_height = workHeight();
222 if (lyxerr.debugging(Debug::GUI)) {
223 lyxerr << "scroll: height now " << height << '\n'
224 << "scroll: work_height " << work_height << endl;
227 /* If the text is smaller than the working area, the scrollbar
228 * maximum must be the working area height. No scrolling will
230 if (height <= work_height) {
231 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !"
233 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
234 fl_set_scrollbar_value(scrollbar, pos);
235 fl_set_scrollbar_size(scrollbar, scrollbar->h);
239 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
240 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
242 fl_set_scrollbar_value(scrollbar, pos);
244 double const slider_size =
245 (height == 0) ? 1.0 : 1.0 / double(height);
247 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
251 // callback for scrollbar slider
252 void XWorkArea::scroll_cb()
254 double const val = fl_get_scrollbar_value(scrollbar);
256 if (lyxerr.debugging(Debug::GUI)) {
257 lyxerr << "scroll: val: " << val << '\n'
258 << "scroll: height: " << scrollbar->h << '\n'
259 << "scroll: docheight: " << doc_height_ << endl;
262 scrollDocView(int(val));
267 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
271 XEvent * ev = static_cast<XEvent*>(xev);
272 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
278 if (!area->work_area || !area->work_area->form->visible)
280 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
281 area->redraw(area->workWidth(), area->workHeight());
284 if (!ev || ev->xbutton.button == 0) break;
285 // Should really have used xbutton.state
286 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
288 FuncRequest(LFUN_MOUSE_PRESS,
289 ev->xbutton.x - ob->x,
290 ev->xbutton.y - ob->y,
291 x_button_state(key)));
294 if (!ev || ev->xbutton.button == 0) break;
295 // Should really have used xbutton.state
296 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
298 FuncRequest(LFUN_MOUSE_RELEASE,
299 ev->xbutton.x - ob->x,
300 ev->xbutton.y - ob->y,
301 x_button_state(key)));
305 if (!ev || !area->scrollbar)
308 int const drag_x = ev->xmotion.x;
309 int const drag_y = ev->xmotion.y;
310 int const area_y = ob->y;
311 int const area_h = ob->h;
313 // Check if the mouse is above or below the workarea
314 if (drag_y <= area_y || drag_y >= area_y + area_h) {
315 // The mouse button is depressed and we are outside the
316 // workarea. That means we are simultaneously selecting
317 // text and scrolling the view.
318 // Use a Timeout to react to a drag events only every
319 // 200ms. All intervening events are discarded,
320 // allowing the user to control position easily.
321 static int const discard_interval = 200;
322 static Timeout timeout(discard_interval);
324 if (timeout.running())
326 // The timeout is not running, so process the
327 // event, first starting the timeout to discard future
332 static int x_old = -1;
333 static int y_old = -1;
334 static double scrollbar_value_old = -1.0;
336 double const scrollbar_value =
337 fl_get_scrollbar_value(area->scrollbar);
339 if (drag_x != x_old || drag_y != y_old ||
340 scrollbar_value != scrollbar_value_old) {
343 scrollbar_value_old = scrollbar_value;
345 lyxerr[Debug::WORKAREA] << "Workarea event: DRAG"
348 // It transpires that ev->xbutton.button == 0 when
349 // the mouse is dragged, so it cannot be used to
350 // initialise x_button_state and hence FuncRequest.
352 // The 'key' that is passed into the function does
353 // contain the necessary info, however.
355 // It is for this reason that x_button_state has
356 // been modified to work with key
357 // rather than ev->xbutton.button.
359 // Angus 15 Oct 2002.
360 FuncRequest cmd(LFUN_MOUSE_MOTION,
361 ev->xbutton.x - ob->x,
362 ev->xbutton.y - ob->y,
363 x_button_state(key));
371 lyxerr[Debug::WORKAREA] << "Workarea event: KEYPRESS" << endl;
375 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
376 XLookupString(xke, dummy, 1, &keysym, 0);
378 if (lyxerr.debugging(Debug::KEY)) {
379 char const * const tmp = XKeysymToString(key);
380 char const * const tmp2 = XKeysymToString(keysym);
381 string const stm = (tmp ? tmp : string());
382 string const stm2 = (tmp2 ? tmp2 : string());
384 lyxerr << "XWorkArea: Key is `" << stm
385 << "' [" << key << "]\n"
386 << "XWorkArea: Keysym is `" << stm2
387 << "' [" << keysym << ']' << endl;
390 // Note that we need this handling because of a bug
391 // in XForms 0.89, if this bug is resolved in the way I hope
392 // we can just use the keysym directly without looking
393 // at key at all. (Lgb)
396 // We might have to add more keysyms here also,
397 // we will do that as the issues arise. (Lgb)
398 if (keysym == XK_space) {
400 lyxerr[Debug::KEY] << "Using keysym [A]"
405 // It seems that this was a bit optimistic...
406 // With this hacking things seems to be better (Lgb)
407 //if (!iscntrl(key)) {
409 // lyxerr[Debug::KEY]
410 // << "Using key [B]\n"
412 // << static_cast<unsigned char>(key)
415 ret_key = (keysym ? keysym : key);
416 lyxerr[Debug::KEY] << "Using keysym [B]"
421 unsigned int const ret_state = xke->state;
423 // If you have a better way to handle "wild-output" of
424 // characters after the key has been released than the one
425 // below, please contact me. (Lgb)
426 static Time last_time_pressed;
427 static unsigned int last_key_pressed;
428 static unsigned int last_state_pressed;
429 lyxerr[Debug::KEY] << "Workarea Diff: "
430 << xke->time - last_time_pressed
432 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
433 && ret_state == last_state_pressed
434 && xke->keycode == last_key_pressed) {
436 << "Workarea: Purging X events." << endl;
437 //lyxerr << "Workarea Events: "
438 // << XEventsQueued(fl_get_display(), QueuedAlready)
440 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
442 // This purge make f.ex. scrolling stop immediately when
443 // releasing the PageDown button. The question is if
444 // this purging of XEvents can cause any harm...
445 // after some testing I can see no problems, but
446 // I'd like other reports too.
449 last_time_pressed = xke->time;
450 last_key_pressed = xke->keycode;
451 last_state_pressed = ret_state;
453 XLyXKeySym * xlk = new XLyXKeySym;
454 xlk->initFromKeySym(ret_key);
456 area->workAreaKeyPress(LyXKeySymPtr(xlk),
457 x_key_state(ret_state));
462 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
466 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
467 fl_set_cursor(FL_ObjWin(area->work_area), XC_xterm);
470 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
471 // There should be no need for this. But there is.
472 fl_set_cursor(FL_ObjWin(area->work_area), FL_DEFAULT_CURSOR);
476 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
477 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
478 ev->xbutton.x - ob->x,
479 ev->xbutton.y - ob->y,
480 x_button_state(key));
486 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
487 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
488 ev->xbutton.x - ob->x,
489 ev->xbutton.y - ob->y,
490 x_button_state(key));
496 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
506 string clipboard_selection;
507 bool clipboard_read = false;
511 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
512 void const * data, long size)
514 clipboard_selection.erase();
517 clipboard_selection.reserve(size);
518 for (int i = 0; i < size; ++i)
519 clipboard_selection += static_cast<char const *>(data)[i];
520 clipboard_read = true;
528 int XWorkArea::event_cb(XEvent * xev)
531 case SelectionRequest:
532 lyxerr[Debug::GUI] << "X requested selection." << endl;
533 selectionRequested();
536 lyxerr[Debug::GUI] << "Lost selection." << endl;
544 void XWorkArea::haveSelection(bool yes) const
546 Window const owner = yes ? FL_ObjWin(work_area) : None;
547 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, owner, CurrentTime);
551 string const XWorkArea::getClipboard() const
553 clipboard_read = false;
555 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
560 while (!clipboard_read) {
561 if (fl_check_forms() == FL_EVENT) {
563 lyxerr << "Received unhandled X11 event\n"
564 << "Type: 0x" << hex << ev.xany.type
565 << " Target: 0x" << hex << ev.xany.window
569 return clipboard_selection;
573 void XWorkArea::putClipboard(string const & s) const
578 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);