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 "XFormsView.h"
18 #include "XLyXKeySym.h"
21 #include "funcrequest.h"
25 #include <boost/bind.hpp>
27 using boost::shared_ptr;
40 inline void waitForX(bool discard)
42 XSync(fl_get_display(), discard);
46 mouse_button::state x_button_state(unsigned int button)
48 mouse_button::state b = mouse_button::none;
51 b = mouse_button::button1;
54 b = mouse_button::button2;
57 b = mouse_button::button3;
60 b = mouse_button::button4;
63 b = mouse_button::button5;
70 key_modifier::state x_key_state(unsigned int state)
72 key_modifier::state k = key_modifier::none;
73 if (state & ControlMask)
74 k |= key_modifier::ctrl;
75 if (state & ShiftMask)
76 k |= key_modifier::shift;
78 k |= key_modifier::alt;
85 void C_scroll_cb(FL_OBJECT * ob, long)
87 XWorkArea * area = static_cast<XWorkArea *>(ob->u_vdata);
92 int C_work_area_handler(FL_OBJECT * ob, int event, FL_Coord, FL_Coord,
95 return XWorkArea::work_area_handler(ob, event, 0, 0, key, xev);
99 int C_event_cb(FL_FORM * form, void * xev)
101 XWorkArea * area = static_cast<XWorkArea *>(form->u_vdata);
102 return area->event_cb(static_cast<XEvent *>(xev));
109 XWorkArea::XWorkArea(LyXView & owner, int w, int h)
110 : workareapixmap(0), painter_(*this)
112 fl_freeze_all_forms();
117 // A frame around the work area.
118 frame = obj = fl_add_box(FL_BORDER_BOX, 0, 0, w, h, "");
119 fl_set_object_resize(obj, FL_RESIZE_ALL);
120 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
122 unsigned int r, g, b;
123 if (getRGBColor(LColor::background, r, g, b)) {
124 fl_mapcolor(FL_FREE_COL12, r, g, b);
125 fl_set_object_color(obj, FL_FREE_COL12, FL_MCOL);
129 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR, 0, 0, w, h, "");
130 fl_set_object_boxtype(obj, FL_UP_BOX);
131 fl_set_object_resize(obj, FL_RESIZE_ALL);
132 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
134 fl_set_object_callback(obj, C_scroll_cb, 0);
135 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
136 fl_set_scrollbar_value(scrollbar, 0.0);
137 fl_set_scrollbar_size(scrollbar, scrollbar->h);
139 // The work area itself
140 work_area = obj = fl_add_free(FL_ALL_FREE, 0, 0, w, h, "",
141 C_work_area_handler);
142 obj->wantkey = FL_KEY_ALL;
145 fl_set_object_boxtype(obj,FL_DOWN_BOX);
146 fl_set_object_resize(obj, FL_RESIZE_ALL);
147 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
149 // Hand control of the layout of these widgets to the
151 XFormsView & xview = dynamic_cast<XFormsView &>(owner);
152 BoxList & boxlist = xview.getBox(XFormsView::Center)->children();
154 wa_box_ = boxlist.push_back(Box(0,0));
155 wa_box_->set(Box::Horizontal);
157 shared_ptr<Box> frame_box = widgets_.add(frame, wa_box_->children(), 0, 0);
158 frame_box->set(Box::Expand);
160 int const bw = int(abs(fl_get_border_width()));
161 shared_ptr<Box> wa_box = embed(work_area, frame_box->children(), widgets_, bw);
162 wa_box->set(Box::Expand);
164 widgets_.add(scrollbar, wa_box_->children(), 17, 0);
166 xview.metricsUpdated.connect(boost::bind(&WidgetMap::updateMetrics,
169 /// X selection hook - xforms gets it wrong
170 fl_current_form->u_vdata = this;
171 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_event_cb);
173 fl_unfreeze_all_forms();
177 val.function = GXcopy;
178 val.graphics_exposures = false;
179 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
180 GCFunction | GCGraphicsExposures, &val);
184 XWorkArea::~XWorkArea()
186 XFreeGC(fl_get_display(), copy_gc);
188 XFreePixmap(fl_get_display(), workareapixmap);
192 void XWorkArea::updateGeometry(int width, int height)
194 static int cur_width = -1;
195 static int cur_height = -1;
197 if (cur_width == width && cur_height == height && workareapixmap)
203 if (lyxerr.debugging(Debug::WORKAREA)) {
204 lyxerr << "(Re)creating pixmap ("
205 << width << 'x' << height << ')' << endl;
208 if (workareapixmap) {
209 XFreePixmap(fl_get_display(), workareapixmap);
212 workareapixmap = XCreatePixmap(fl_get_display(),
213 RootWindow(fl_get_display(), 0),
216 fl_get_visual_depth());
222 void XWorkArea::paint(int x, int y, int w, int h)
224 lyxerr[Debug::WORKAREA]
225 << "XWorkarea::paint " << w << 'x' << h
226 << '+' << x << '+' << y << endl;
228 updateGeometry(workWidth(), workHeight());
229 XCopyArea(fl_get_display(),
230 getPixmap(), getWin(),
232 work_area->x + x, work_area->y + y);
236 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
238 // we need to cache this for scroll_cb
239 doc_height_ = height;
242 fl_set_scrollbar_value(scrollbar, 0.0);
243 fl_set_scrollbar_size(scrollbar, scrollbar->h);
247 long const work_height = workHeight();
249 if (lyxerr.debugging(Debug::GUI)) {
250 lyxerr << "scroll: height now " << height << '\n'
251 << "scroll: work_height " << work_height << endl;
254 /* If the text is smaller than the working area, the scrollbar
255 * maximum must be the working area height. No scrolling will
257 if (height <= work_height) {
258 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !"
260 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
261 fl_set_scrollbar_value(scrollbar, pos);
262 fl_set_scrollbar_size(scrollbar, scrollbar->h);
266 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
267 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
269 fl_set_scrollbar_value(scrollbar, pos);
271 double const slider_size =
272 (height == 0) ? 1.0 : 1.0 / double(height);
274 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
278 // callback for scrollbar slider
279 void XWorkArea::scroll_cb()
281 double const val = fl_get_scrollbar_value(scrollbar);
283 if (lyxerr.debugging(Debug::GUI)) {
284 lyxerr << "scroll: val: " << val << '\n'
285 << "scroll: height: " << scrollbar->h << '\n'
286 << "scroll: docheight: " << doc_height_ << endl;
289 scrollDocView(int(val));
294 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
299 lyxerr[Debug::WORKAREA] << "Workarea event: EVENT: " << event << endl;
301 XEvent * ev = static_cast<XEvent*>(xev);
302 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
310 if (!area->work_area || !area->work_area->form->visible)
314 lyxerr[Debug::WORKAREA]
315 << "work_area_handler, handling X11 "
317 << ev->xexpose.width << 'x'
318 << ev->xexpose.height << '+'
319 << ev->xexpose.x << '+'
320 << ev->xexpose.y << endl;
322 // X11 generates XEvents with x, y relative to the
323 // top left corner of the window.
324 // XScreen::expose emulates this behaviour.
325 // We therefore need to remove this offset before
326 // generating the pixmap.
327 int const x = ev->xexpose.x - ob->x;
328 int const y = ev->xexpose.y - ob->y;
331 ev->xexpose.width, ev->xexpose.height);
334 area->workWidth(), area->workHeight());
340 if (!ev || ev->xbutton.button == 0) break;
342 if (ev->xbutton.button == 4 || ev->xbutton.button == 5) {
343 static long last_wheel;
345 long cur_wheel = ev->xbutton.time;
346 if (last_wheel == cur_wheel)
349 last_wheel = cur_wheel;
352 fl_get_scrollbar_increment(area->scrollbar, &l, &r);
354 if (ev->xbutton.button == 4)
357 fl_set_scrollbar_value(
359 fl_get_scrollbar_value(area->scrollbar) + l);
365 // Should really have used xbutton.state
366 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
368 FuncRequest(LFUN_MOUSE_PRESS,
369 ev->xbutton.x - ob->x,
370 ev->xbutton.y - ob->y,
371 x_button_state(key)));
375 if (!ev || ev->xbutton.button == 0) break;
376 // Should really have used xbutton.state
378 if (ev->xbutton.button == 4 || ev->xbutton.button == 5) {
379 // We ingnore wheel event here
383 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
386 FuncRequest(LFUN_MOUSE_RELEASE,
387 ev->xbutton.x - ob->x,
388 ev->xbutton.y - ob->y,
389 x_button_state(key)));
393 lyxerr[Debug::WORKAREA] << "Workarea event: DRAG 0" << endl;
395 if (!ev || !area->scrollbar)
398 int const drag_x = ev->xmotion.x;
399 int const drag_y = ev->xmotion.y;
400 int const area_y = ob->y;
401 int const area_h = ob->h;
403 // Check if the mouse is above or below the workarea
404 if (drag_y <= area_y || drag_y >= area_y + area_h) {
405 // The mouse button is depressed and we are outside the
406 // workarea. That means we are simultaneously selecting
407 // text and scrolling the view.
408 // Use a Timeout to react to a drag events only every
409 // 200ms. All intervening events are discarded,
410 // allowing the user to control position easily.
411 static int const discard_interval = 200;
412 static Timeout timeout(discard_interval);
414 if (timeout.running())
416 // The timeout is not running, so process the
417 // event, first starting the timeout to discard future
422 static int x_old = -1;
423 static int y_old = -1;
424 static double scrollbar_value_old = -1.0;
426 double const scrollbar_value =
427 fl_get_scrollbar_value(area->scrollbar);
429 if (drag_x != x_old || drag_y != y_old ||
430 scrollbar_value != scrollbar_value_old) {
433 scrollbar_value_old = scrollbar_value;
435 lyxerr[Debug::WORKAREA] << "Workarea event: DRAG"
438 // It transpires that ev->xbutton.button == 0 when
439 // the mouse is dragged, so it cannot be used to
440 // initialise x_button_state and hence FuncRequest.
442 // The 'key' that is passed into the function does
443 // contain the necessary info, however.
445 // It is for this reason that x_button_state has
446 // been modified to work with key
447 // rather than ev->xbutton.button.
449 // Angus 15 Oct 2002.
450 FuncRequest cmd(LFUN_MOUSE_MOTION,
451 ev->xbutton.x - ob->x,
452 ev->xbutton.y - ob->y,
453 x_button_state(key));
460 lyxerr[Debug::WORKAREA] << "Workarea event: KEYPRESS" << endl;
464 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
465 XLookupString(xke, dummy, 1, &keysym, 0);
467 if (lyxerr.debugging(Debug::KEY)) {
468 char const * const tmp = XKeysymToString(key);
469 char const * const tmp2 = XKeysymToString(keysym);
470 string const stm = (tmp ? tmp : string());
471 string const stm2 = (tmp2 ? tmp2 : string());
473 lyxerr << "XWorkArea: Key is `" << stm
474 << "' [" << key << "]\n"
475 << "XWorkArea: Keysym is `" << stm2
476 << "' [" << keysym << ']' << endl;
479 // Note that we need this handling because of a bug
480 // in XForms 0.89, if this bug is resolved in the way I hope
481 // we can just use the keysym directly without looking
482 // at key at all. (Lgb)
485 // We might have to add more keysyms here also,
486 // we will do that as the issues arise. (Lgb)
487 if (keysym == XK_space) {
489 lyxerr[Debug::KEY] << "Using keysym [A]"
494 // It seems that this was a bit optimistic...
495 // With this hacking things seems to be better (Lgb)
496 //if (!iscntrl(key)) {
498 // lyxerr[Debug::KEY]
499 // << "Using key [B]\n"
501 // << static_cast<unsigned char>(key)
504 ret_key = (keysym ? keysym : key);
505 lyxerr[Debug::KEY] << "Using keysym [B]"
510 unsigned int const ret_state = xke->state;
512 // If you have a better way to handle "wild-output" of
513 // characters after the key has been released than the one
514 // below, please contact me. (Lgb)
515 static Time last_time_pressed;
516 static unsigned int last_key_pressed;
517 static unsigned int last_state_pressed;
518 lyxerr[Debug::KEY] << "Workarea Diff: "
519 << xke->time - last_time_pressed
521 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
522 && ret_state == last_state_pressed
523 && xke->keycode == last_key_pressed) {
525 << "Workarea: Purging X events." << endl;
526 //lyxerr << "Workarea Events: "
527 // << XEventsQueued(fl_get_display(), QueuedAlready)
529 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
531 // This purge make f.ex. scrolling stop immediately when
532 // releasing the PageDown button. The question is if
533 // this purging of XEvents can cause any harm...
534 // after some testing I can see no problems, but
535 // I'd like other reports too.
538 last_time_pressed = xke->time;
539 last_key_pressed = xke->keycode;
540 last_state_pressed = ret_state;
542 XLyXKeySym * xlk = new XLyXKeySym;
543 xlk->initFromKeySym(ret_key);
545 area->workAreaKeyPress(LyXKeySymPtr(xlk),
546 x_key_state(ret_state));
551 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
555 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
556 fl_set_cursor(FL_ObjWin(area->work_area), XC_xterm);
560 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
561 // There should be no need for this. But there is.
562 fl_set_cursor(FL_ObjWin(area->work_area), FL_DEFAULT_CURSOR);
567 if (ev->xbutton.button == 4 || ev->xbutton.button == 5) {
568 // Ignore wheel events
573 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
574 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
575 ev->xbutton.x - ob->x,
576 ev->xbutton.y - ob->y,
577 x_button_state(key));
584 if (ev->xbutton.button == 4 || ev->xbutton.button == 5) {
585 // Ignore wheel events
589 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
590 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
591 ev->xbutton.x - ob->x,
592 ev->xbutton.y - ob->y,
593 x_button_state(key));
600 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
610 string clipboard_selection;
611 bool clipboard_read = false;
615 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
616 void const * data, long size)
618 clipboard_selection.erase();
621 clipboard_selection.reserve(size);
622 for (int i = 0; i < size; ++i)
623 clipboard_selection += static_cast<char const *>(data)[i];
624 clipboard_read = true;
632 int XWorkArea::event_cb(XEvent * xev)
635 case SelectionRequest:
636 lyxerr[Debug::GUI] << "X requested selection." << endl;
637 selectionRequested();
640 lyxerr[Debug::GUI] << "Lost selection." << endl;
648 void XWorkArea::haveSelection(bool yes) const
650 Window const owner = yes ? FL_ObjWin(work_area) : None;
651 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, owner, CurrentTime);
655 string const XWorkArea::getClipboard() const
657 clipboard_read = false;
659 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
664 while (!clipboard_read) {
665 if (fl_check_forms() == FL_EVENT) {
667 lyxerr << "Received unhandled X11 event\n"
668 << "Type: 0x" << hex << ev.xany.type
669 << " Target: 0x" << hex << ev.xany.window
673 return clipboard_selection;
677 void XWorkArea::putClipboard(string const & s) const
682 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);
685 } // namespace frontend