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"
32 void waitForX(bool discard)
34 XSync(fl_get_display(), discard);
38 mouse_button::state x_button_state(unsigned int button)
40 mouse_button::state b = mouse_button::none;
43 b = mouse_button::button1;
46 b = mouse_button::button2;
49 b = mouse_button::button3;
52 b = mouse_button::button4;
55 b = mouse_button::button5;
62 key_modifier::state x_key_state(unsigned int state)
64 key_modifier::state k = key_modifier::none;
65 if (state & ControlMask)
66 k |= key_modifier::ctrl;
67 if (state & ShiftMask)
68 k |= key_modifier::shift;
70 k |= key_modifier::alt;
77 void C_scroll_cb(FL_OBJECT * ob, long)
79 XWorkArea * area = static_cast<XWorkArea *>(ob->u_vdata);
84 int C_work_area_handler(FL_OBJECT * ob, int event, FL_Coord, FL_Coord,
87 return XWorkArea::work_area_handler(ob, event, 0, 0, key, xev);
91 int C_event_cb(FL_FORM * form, void * xev)
93 XWorkArea * area = static_cast<XWorkArea *>(form->u_vdata);
94 return area->event_cb(static_cast<XEvent *>(xev));
101 XWorkArea::XWorkArea(int x, int y, int w, int h)
102 : workareapixmap(0), painter_(*this)
104 if (lyxerr.debugging(Debug::WORKAREA)) {
105 lyxerr << "\tbackground box: +"
106 << x << '+' << y << ' '
107 << w - 15 << 'x' << h << endl;
110 fl_freeze_all_forms();
114 obj = fl_add_box(FL_BORDER_BOX, x, y, w - 15, h, "");
115 fl_set_object_resize(obj, FL_RESIZE_ALL);
116 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
118 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
119 x + w - 15, y, 17, h, "");
120 fl_set_object_boxtype(obj, FL_UP_BOX);
121 fl_set_object_resize(obj, FL_RESIZE_ALL);
122 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
124 fl_set_object_callback(obj, C_scroll_cb, 0);
125 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
126 fl_set_scrollbar_value(scrollbar, 0.0);
127 fl_set_scrollbar_size(scrollbar, scrollbar->h);
129 int const bw = int(abs(fl_get_border_width()));
131 // Create the workarea pixmap
132 // FIXME remove redraw(w - 15 - 2 * bw, h - 2 * bw);
134 if (lyxerr.debugging(Debug::WORKAREA))
135 lyxerr << "\tfree object: +"
136 << x + bw << '+' << y + bw << ' '
137 << w - 15 - 2 * bw << 'x'
138 << h - 2 * bw << endl;
140 // We add this object as late as possible to avoid problems
143 work_area = obj = fl_add_free(FL_ALL_FREE,
147 C_work_area_handler);
148 obj->wantkey = FL_KEY_ALL;
151 fl_set_object_boxtype(obj,FL_DOWN_BOX);
152 fl_set_object_resize(obj, FL_RESIZE_ALL);
153 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
155 /// X selection hook - xforms gets it wrong
156 fl_current_form->u_vdata = this;
157 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_event_cb);
159 fl_unfreeze_all_forms();
163 val.function = GXcopy;
164 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
169 XWorkArea::~XWorkArea()
171 XFreeGC(fl_get_display(), copy_gc);
173 XFreePixmap(fl_get_display(), workareapixmap);
177 void XWorkArea::redraw(int width, int height)
179 static int cur_width = -1;
180 static int cur_height = -1;
182 if (cur_width == width && cur_height == height && workareapixmap) {
183 XCopyArea(fl_get_display(),
184 getPixmap(), getWin(), copy_gc,
185 0, 0, width, height, xpos(), ypos());
192 if (lyxerr.debugging(Debug::WORKAREA)) {
193 lyxerr << "(Re)creating pixmap ("
194 << width << 'x' << height << ')' << endl;
197 if (workareapixmap) {
198 XFreePixmap(fl_get_display(), workareapixmap);
201 workareapixmap = XCreatePixmap(fl_get_display(),
202 RootWindow(fl_get_display(), 0),
205 fl_get_visual_depth());
211 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
213 // we need to cache this for scroll_cb
214 doc_height_ = height;
217 fl_set_scrollbar_value(scrollbar, 0.0);
218 fl_set_scrollbar_size(scrollbar, scrollbar->h);
222 long const work_height = workHeight();
224 if (lyxerr.debugging(Debug::GUI)) {
225 lyxerr << "scroll: height now " << height << '\n'
226 << "scroll: work_height " << work_height << endl;
229 /* If the text is smaller than the working area, the scrollbar
230 * maximum must be the working area height. No scrolling will
232 if (height <= work_height) {
233 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !"
235 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
236 fl_set_scrollbar_value(scrollbar, pos);
237 fl_set_scrollbar_size(scrollbar, scrollbar->h);
241 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
242 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
244 fl_set_scrollbar_value(scrollbar, pos);
246 double const slider_size =
247 (height == 0) ? 1.0 : 1.0 / double(height);
249 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
253 // callback for scrollbar slider
254 void XWorkArea::scroll_cb()
256 double const val = fl_get_scrollbar_value(scrollbar);
258 if (lyxerr.debugging(Debug::GUI)) {
259 lyxerr << "scroll: val: " << val << '\n'
260 << "scroll: height: " << scrollbar->h << '\n'
261 << "scroll: docheight: " << doc_height_ << endl;
264 scrollDocView(int(val));
269 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
273 XEvent * ev = static_cast<XEvent*>(xev);
274 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
280 if (!area->work_area || !area->work_area->form->visible)
282 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
283 area->redraw(area->workWidth(), area->workHeight());
286 if (!ev || ev->xbutton.button == 0) break;
287 // Should really have used xbutton.state
288 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
290 FuncRequest(LFUN_MOUSE_PRESS,
291 ev->xbutton.x - ob->x,
292 ev->xbutton.y - ob->y,
293 x_button_state(key)));
296 if (!ev || ev->xbutton.button == 0) break;
297 // Should really have used xbutton.state
298 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
300 FuncRequest(LFUN_MOUSE_RELEASE,
301 ev->xbutton.x - ob->x,
302 ev->xbutton.y - ob->y,
303 x_button_state(key)));
307 if (!ev || !area->scrollbar)
310 int const drag_x = ev->xmotion.x;
311 int const drag_y = ev->xmotion.y;
312 int const area_y = ob->y;
313 int const area_h = ob->h;
315 // Check if the mouse is above or below the workarea
316 if (drag_y <= area_y || drag_y >= area_y + area_h) {
317 // The mouse button is depressed and we are outside the
318 // workarea. That means we are simultaneously selecting
319 // text and scrolling the view.
320 // Use a Timeout to react to a drag events only every
321 // 200ms. All intervening events are discarded,
322 // allowing the user to control position easily.
323 static int const discard_interval = 200;
324 static Timeout timeout(discard_interval);
326 if (timeout.running())
328 // The timeout is not running, so process the
329 // event, first starting the timeout to discard future
334 static int x_old = -1;
335 static int y_old = -1;
336 static double scrollbar_value_old = -1.0;
338 double const scrollbar_value =
339 fl_get_scrollbar_value(area->scrollbar);
341 if (drag_x != x_old || drag_y != y_old ||
342 scrollbar_value != scrollbar_value_old) {
345 scrollbar_value_old = scrollbar_value;
347 lyxerr[Debug::WORKAREA] << "Workarea event: DRAG"
350 // It transpires that ev->xbutton.button == 0 when
351 // the mouse is dragged, so it cannot be used to
352 // initialise x_button_state and hence FuncRequest.
354 // The 'key' that is passed into the function does
355 // contain the necessary info, however.
357 // It is for this reason that x_button_state has
358 // been modified to work with key
359 // rather than ev->xbutton.button.
361 // Angus 15 Oct 2002.
362 FuncRequest cmd(LFUN_MOUSE_MOTION,
363 ev->xbutton.x - ob->x,
364 ev->xbutton.y - ob->y,
365 x_button_state(key));
373 lyxerr[Debug::WORKAREA] << "Workarea event: KEYPRESS" << endl;
377 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
378 XLookupString(xke, dummy, 1, &keysym, 0);
380 if (lyxerr.debugging(Debug::KEY)) {
381 char const * const tmp = XKeysymToString(key);
382 char const * const tmp2 = XKeysymToString(keysym);
383 string const stm = (tmp ? tmp : string());
384 string const stm2 = (tmp2 ? tmp2 : string());
386 lyxerr << "XWorkArea: Key is `" << stm
387 << "' [" << key << "]\n"
388 << "XWorkArea: Keysym is `" << stm2
389 << "' [" << keysym << ']' << endl;
392 // Note that we need this handling because of a bug
393 // in XForms 0.89, if this bug is resolved in the way I hope
394 // we can just use the keysym directly without looking
395 // at key at all. (Lgb)
398 // We might have to add more keysyms here also,
399 // we will do that as the issues arise. (Lgb)
400 if (keysym == XK_space) {
402 lyxerr[Debug::KEY] << "Using keysym [A]"
407 // It seems that this was a bit optimistic...
408 // With this hacking things seems to be better (Lgb)
409 //if (!iscntrl(key)) {
411 // lyxerr[Debug::KEY]
412 // << "Using key [B]\n"
414 // << static_cast<unsigned char>(key)
417 ret_key = (keysym ? keysym : key);
418 lyxerr[Debug::KEY] << "Using keysym [B]"
423 unsigned int const ret_state = xke->state;
425 // If you have a better way to handle "wild-output" of
426 // characters after the key has been released than the one
427 // below, please contact me. (Lgb)
428 static Time last_time_pressed;
429 static unsigned int last_key_pressed;
430 static unsigned int last_state_pressed;
431 lyxerr[Debug::KEY] << "Workarea Diff: "
432 << xke->time - last_time_pressed
434 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
435 && ret_state == last_state_pressed
436 && xke->keycode == last_key_pressed) {
438 << "Workarea: Purging X events." << endl;
439 //lyxerr << "Workarea Events: "
440 // << XEventsQueued(fl_get_display(), QueuedAlready)
442 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
444 // This purge make f.ex. scrolling stop immediately when
445 // releasing the PageDown button. The question is if
446 // this purging of XEvents can cause any harm...
447 // after some testing I can see no problems, but
448 // I'd like other reports too.
451 last_time_pressed = xke->time;
452 last_key_pressed = xke->keycode;
453 last_state_pressed = ret_state;
455 XLyXKeySym * xlk = new XLyXKeySym;
456 xlk->initFromKeySym(ret_key);
458 area->workAreaKeyPress(LyXKeySymPtr(xlk),
459 x_key_state(ret_state));
464 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
468 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
469 fl_set_cursor(FL_ObjWin(area->work_area), XC_xterm);
472 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
473 // There should be no need for this. But there is.
474 fl_set_cursor(FL_ObjWin(area->work_area), FL_DEFAULT_CURSOR);
478 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
479 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
480 ev->xbutton.x - ob->x,
481 ev->xbutton.y - ob->y,
482 x_button_state(key));
488 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
489 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
490 ev->xbutton.x - ob->x,
491 ev->xbutton.y - ob->y,
492 x_button_state(key));
498 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
508 string clipboard_selection;
509 bool clipboard_read = false;
513 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
514 void const * data, long size)
516 clipboard_selection.erase();
519 clipboard_selection.reserve(size);
520 for (int i = 0; i < size; ++i)
521 clipboard_selection += static_cast<char const *>(data)[i];
522 clipboard_read = true;
530 int XWorkArea::event_cb(XEvent * xev)
533 case SelectionRequest:
534 lyxerr[Debug::GUI] << "X requested selection." << endl;
535 selectionRequested();
538 lyxerr[Debug::GUI] << "Lost selection." << endl;
546 void XWorkArea::haveSelection(bool yes) const
548 Window const owner = yes ? FL_ObjWin(work_area) : None;
549 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, owner, CurrentTime);
553 string const XWorkArea::getClipboard() const
555 clipboard_read = false;
557 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
562 while (!clipboard_read) {
563 if (fl_check_forms() == FL_EVENT) {
565 lyxerr << "Received unhandled X11 event\n"
566 << "Type: 0x" << hex << ev.xany.type
567 << " Target: 0x" << hex << ev.xany.window
571 return clipboard_selection;
575 void XWorkArea::putClipboard(string const & s) const
580 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);