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>
38 inline void waitForX(bool discard)
40 XSync(fl_get_display(), discard);
44 mouse_button::state x_button_state(unsigned int button)
46 mouse_button::state b = mouse_button::none;
49 b = mouse_button::button1;
52 b = mouse_button::button2;
55 b = mouse_button::button3;
58 b = mouse_button::button4;
61 b = mouse_button::button5;
68 key_modifier::state x_key_state(unsigned int state)
70 key_modifier::state k = key_modifier::none;
71 if (state & ControlMask)
72 k |= key_modifier::ctrl;
73 if (state & ShiftMask)
74 k |= key_modifier::shift;
76 k |= key_modifier::alt;
83 void C_scroll_cb(FL_OBJECT * ob, long)
85 XWorkArea * area = static_cast<XWorkArea *>(ob->u_vdata);
90 int C_work_area_handler(FL_OBJECT * ob, int event, FL_Coord, FL_Coord,
93 return XWorkArea::work_area_handler(ob, event, 0, 0, key, xev);
97 int C_event_cb(FL_FORM * form, void * xev)
99 XWorkArea * area = static_cast<XWorkArea *>(form->u_vdata);
100 return area->event_cb(static_cast<XEvent *>(xev));
107 XWorkArea::XWorkArea(LyXView & owner, int w, int h)
108 : workareapixmap(0), painter_(*this)
110 fl_freeze_all_forms();
115 // A frame around the work area.
116 frame = obj = fl_add_box(FL_BORDER_BOX, 0, 0, w, h, "");
117 fl_set_object_resize(obj, FL_RESIZE_ALL);
118 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
120 unsigned int r, g, b;
121 if (getRGBColor(LColor::background, r, g, b)) {
122 fl_mapcolor(FL_FREE_COL12, r, g, b);
123 fl_set_object_color(obj, FL_FREE_COL12, FL_MCOL);
127 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR, 0, 0, w, h, "");
128 fl_set_object_boxtype(obj, FL_UP_BOX);
129 fl_set_object_resize(obj, FL_RESIZE_ALL);
130 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
132 fl_set_object_callback(obj, C_scroll_cb, 0);
133 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
134 fl_set_scrollbar_value(scrollbar, 0.0);
135 fl_set_scrollbar_size(scrollbar, scrollbar->h);
137 // The work area itself
138 work_area = obj = fl_add_free(FL_ALL_FREE, 0, 0, w, h, "",
139 C_work_area_handler);
140 obj->wantkey = FL_KEY_ALL;
143 fl_set_object_boxtype(obj,FL_DOWN_BOX);
144 fl_set_object_resize(obj, FL_RESIZE_ALL);
145 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
147 // Hand control of the layout of these widgets to the
149 XFormsView & xview = dynamic_cast<XFormsView &>(owner);
150 BoxList & boxlist = xview.getBox(XFormsView::Center).children();
152 wa_box_ = &boxlist.push_back(Box(0,0));
153 wa_box_->set(Box::Horizontal);
155 Box & frame_box = widgets_.add(frame, wa_box_->children(), 0, 0);
156 frame_box.set(Box::Expand);
158 int const bw = int(abs(fl_get_border_width()));
159 Box & wa_box = embed(work_area, frame_box.children(), widgets_, bw);
160 wa_box.set(Box::Expand);
162 widgets_.add(scrollbar, wa_box_->children(), 17, 0);
164 xview.metricsUpdated.connect(boost::bind(&WidgetMap::updateMetrics,
167 /// X selection hook - xforms gets it wrong
168 fl_current_form->u_vdata = this;
169 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_event_cb);
171 fl_unfreeze_all_forms();
175 val.function = GXcopy;
176 val.graphics_exposures = false;
177 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
178 GCFunction | GCGraphicsExposures, &val);
182 XWorkArea::~XWorkArea()
184 XFreeGC(fl_get_display(), copy_gc);
186 XFreePixmap(fl_get_display(), workareapixmap);
190 void XWorkArea::updateGeometry(int width, int height)
192 static int cur_width = -1;
193 static int cur_height = -1;
195 if (cur_width == width && cur_height == height && workareapixmap)
201 if (lyxerr.debugging(Debug::WORKAREA)) {
202 lyxerr << "(Re)creating pixmap ("
203 << width << 'x' << height << ')' << endl;
206 if (workareapixmap) {
207 XFreePixmap(fl_get_display(), workareapixmap);
210 workareapixmap = XCreatePixmap(fl_get_display(),
211 RootWindow(fl_get_display(), 0),
214 fl_get_visual_depth());
220 void XWorkArea::paint(int x, int y, int w, int h)
222 lyxerr[Debug::WORKAREA]
223 << "XWorkarea::paint " << w << 'x' << h
224 << '+' << x << '+' << y << endl;
226 updateGeometry(workWidth(), workHeight());
227 XCopyArea(fl_get_display(),
228 getPixmap(), getWin(),
230 work_area->x + x, work_area->y + y);
234 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
236 // we need to cache this for scroll_cb
237 doc_height_ = height;
240 fl_set_scrollbar_value(scrollbar, 0.0);
241 fl_set_scrollbar_size(scrollbar, scrollbar->h);
245 long const work_height = workHeight();
247 if (lyxerr.debugging(Debug::GUI)) {
248 lyxerr << "scroll: height now " << height << '\n'
249 << "scroll: work_height " << work_height << endl;
252 /* If the text is smaller than the working area, the scrollbar
253 * maximum must be the working area height. No scrolling will
255 if (height <= work_height) {
256 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !"
258 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
259 fl_set_scrollbar_value(scrollbar, pos);
260 fl_set_scrollbar_size(scrollbar, scrollbar->h);
264 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
265 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
267 fl_set_scrollbar_value(scrollbar, pos);
269 double const slider_size =
270 (height == 0) ? 1.0 : 1.0 / double(height);
272 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
276 // callback for scrollbar slider
277 void XWorkArea::scroll_cb()
279 double const val = fl_get_scrollbar_value(scrollbar);
281 if (lyxerr.debugging(Debug::GUI)) {
282 lyxerr << "scroll: val: " << val << '\n'
283 << "scroll: height: " << scrollbar->h << '\n'
284 << "scroll: docheight: " << doc_height_ << endl;
287 scrollDocView(int(val));
292 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
296 XEvent * ev = static_cast<XEvent*>(xev);
297 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
305 if (!area->work_area || !area->work_area->form->visible)
309 lyxerr[Debug::WORKAREA]
310 << "work_area_handler, handling X11 "
312 << ev->xexpose.width << 'x'
313 << ev->xexpose.height << '+'
314 << ev->xexpose.x << '+'
315 << ev->xexpose.y << endl;
317 // X11 generates XEvents with x, y relative to the
318 // top left corner of the window.
319 // XScreen::expose emulates this behaviour.
320 // We therefore need to remove this offset before
321 // generating the pixmap.
322 int const x = ev->xexpose.x - ob->x;
323 int const y = ev->xexpose.y - ob->y;
326 ev->xexpose.width, ev->xexpose.height);
329 area->workWidth(), area->workHeight());
335 if (!ev || ev->xbutton.button == 0) break;
337 if (ev->xbutton.button == 4 || ev->xbutton.button == 5) {
338 static long last_wheel;
340 long cur_wheel = ev->xbutton.time;
341 if (last_wheel == cur_wheel)
344 last_wheel = cur_wheel;
347 fl_get_scrollbar_increment(area->scrollbar, &l, &r);
349 if (ev->xbutton.button == 4)
352 fl_set_scrollbar_value(
354 fl_get_scrollbar_value(area->scrollbar) + l);
360 // Should really have used xbutton.state
361 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
363 FuncRequest(LFUN_MOUSE_PRESS,
364 ev->xbutton.x - ob->x,
365 ev->xbutton.y - ob->y,
366 x_button_state(key)));
370 if (!ev || ev->xbutton.button == 0) break;
371 // Should really have used xbutton.state
373 if (ev->xbutton.button == 4 || ev->xbutton.button == 5) {
374 // We ingnore wheel event here
378 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
381 FuncRequest(LFUN_MOUSE_RELEASE,
382 ev->xbutton.x - ob->x,
383 ev->xbutton.y - ob->y,
384 x_button_state(key)));
388 if (!ev || !area->scrollbar)
391 int const drag_x = ev->xmotion.x;
392 int const drag_y = ev->xmotion.y;
393 int const area_y = ob->y;
394 int const area_h = ob->h;
396 // Check if the mouse is above or below the workarea
397 if (drag_y <= area_y || drag_y >= area_y + area_h) {
398 // The mouse button is depressed and we are outside the
399 // workarea. That means we are simultaneously selecting
400 // text and scrolling the view.
401 // Use a Timeout to react to a drag events only every
402 // 200ms. All intervening events are discarded,
403 // allowing the user to control position easily.
404 static int const discard_interval = 200;
405 static Timeout timeout(discard_interval);
407 if (timeout.running())
409 // The timeout is not running, so process the
410 // event, first starting the timeout to discard future
415 static int x_old = -1;
416 static int y_old = -1;
417 static double scrollbar_value_old = -1.0;
419 double const scrollbar_value =
420 fl_get_scrollbar_value(area->scrollbar);
422 if (drag_x != x_old || drag_y != y_old ||
423 scrollbar_value != scrollbar_value_old) {
426 scrollbar_value_old = scrollbar_value;
428 lyxerr[Debug::WORKAREA] << "Workarea event: DRAG"
431 // It transpires that ev->xbutton.button == 0 when
432 // the mouse is dragged, so it cannot be used to
433 // initialise x_button_state and hence FuncRequest.
435 // The 'key' that is passed into the function does
436 // contain the necessary info, however.
438 // It is for this reason that x_button_state has
439 // been modified to work with key
440 // rather than ev->xbutton.button.
442 // Angus 15 Oct 2002.
443 FuncRequest cmd(LFUN_MOUSE_MOTION,
444 ev->xbutton.x - ob->x,
445 ev->xbutton.y - ob->y,
446 x_button_state(key));
453 lyxerr[Debug::WORKAREA] << "Workarea event: KEYPRESS" << endl;
457 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
458 XLookupString(xke, dummy, 1, &keysym, 0);
460 if (lyxerr.debugging(Debug::KEY)) {
461 char const * const tmp = XKeysymToString(key);
462 char const * const tmp2 = XKeysymToString(keysym);
463 string const stm = (tmp ? tmp : string());
464 string const stm2 = (tmp2 ? tmp2 : string());
466 lyxerr << "XWorkArea: Key is `" << stm
467 << "' [" << key << "]\n"
468 << "XWorkArea: Keysym is `" << stm2
469 << "' [" << keysym << ']' << endl;
472 // Note that we need this handling because of a bug
473 // in XForms 0.89, if this bug is resolved in the way I hope
474 // we can just use the keysym directly without looking
475 // at key at all. (Lgb)
478 // We might have to add more keysyms here also,
479 // we will do that as the issues arise. (Lgb)
480 if (keysym == XK_space) {
482 lyxerr[Debug::KEY] << "Using keysym [A]"
487 // It seems that this was a bit optimistic...
488 // With this hacking things seems to be better (Lgb)
489 //if (!iscntrl(key)) {
491 // lyxerr[Debug::KEY]
492 // << "Using key [B]\n"
494 // << static_cast<unsigned char>(key)
497 ret_key = (keysym ? keysym : key);
498 lyxerr[Debug::KEY] << "Using keysym [B]"
503 unsigned int const ret_state = xke->state;
505 // If you have a better way to handle "wild-output" of
506 // characters after the key has been released than the one
507 // below, please contact me. (Lgb)
508 static Time last_time_pressed;
509 static unsigned int last_key_pressed;
510 static unsigned int last_state_pressed;
511 lyxerr[Debug::KEY] << "Workarea Diff: "
512 << xke->time - last_time_pressed
514 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
515 && ret_state == last_state_pressed
516 && xke->keycode == last_key_pressed) {
518 << "Workarea: Purging X events." << endl;
519 //lyxerr << "Workarea Events: "
520 // << XEventsQueued(fl_get_display(), QueuedAlready)
522 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
524 // This purge make f.ex. scrolling stop immediately when
525 // releasing the PageDown button. The question is if
526 // this purging of XEvents can cause any harm...
527 // after some testing I can see no problems, but
528 // I'd like other reports too.
531 last_time_pressed = xke->time;
532 last_key_pressed = xke->keycode;
533 last_state_pressed = ret_state;
535 XLyXKeySym * xlk = new XLyXKeySym;
536 xlk->initFromKeySym(ret_key);
538 area->workAreaKeyPress(LyXKeySymPtr(xlk),
539 x_key_state(ret_state));
544 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
548 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
549 fl_set_cursor(FL_ObjWin(area->work_area), XC_xterm);
553 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
554 // There should be no need for this. But there is.
555 fl_set_cursor(FL_ObjWin(area->work_area), FL_DEFAULT_CURSOR);
560 if (ev->xbutton.button == 4 || ev->xbutton.button == 5) {
561 // Ignore wheel events
566 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
567 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
568 ev->xbutton.x - ob->x,
569 ev->xbutton.y - ob->y,
570 x_button_state(key));
577 if (ev->xbutton.button == 4 || ev->xbutton.button == 5) {
578 // Ignore wheel events
582 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
583 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
584 ev->xbutton.x - ob->x,
585 ev->xbutton.y - ob->y,
586 x_button_state(key));
593 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
603 string clipboard_selection;
604 bool clipboard_read = false;
608 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
609 void const * data, long size)
611 clipboard_selection.erase();
614 clipboard_selection.reserve(size);
615 for (int i = 0; i < size; ++i)
616 clipboard_selection += static_cast<char const *>(data)[i];
617 clipboard_read = true;
625 int XWorkArea::event_cb(XEvent * xev)
628 case SelectionRequest:
629 lyxerr[Debug::GUI] << "X requested selection." << endl;
630 selectionRequested();
633 lyxerr[Debug::GUI] << "Lost selection." << endl;
641 void XWorkArea::haveSelection(bool yes) const
643 Window const owner = yes ? FL_ObjWin(work_area) : None;
644 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, owner, CurrentTime);
648 string const XWorkArea::getClipboard() const
650 clipboard_read = false;
652 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
657 while (!clipboard_read) {
658 if (fl_check_forms() == FL_EVENT) {
660 lyxerr << "Received unhandled X11 event\n"
661 << "Type: 0x" << hex << ev.xany.type
662 << " Target: 0x" << hex << ev.xany.window
666 return clipboard_selection;
670 void XWorkArea::putClipboard(string const & s) const
675 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);
678 } // namespace frontend