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"
15 #include "BufferView.h"
18 #include "XFormsView.h"
19 #include "XLyXKeySym.h"
23 #include "funcrequest.h"
27 #include <boost/bind.hpp>
29 using boost::shared_ptr;
42 inline void waitForX(bool discard)
44 XSync(fl_get_display(), discard);
48 mouse_button::state x_button_state(unsigned int button)
50 mouse_button::state b = mouse_button::none;
53 b = mouse_button::button1;
56 b = mouse_button::button2;
59 b = mouse_button::button3;
62 b = mouse_button::button4;
65 b = mouse_button::button5;
72 key_modifier::state x_key_state(unsigned int state)
74 key_modifier::state k = key_modifier::none;
75 if (state & ControlMask)
76 k |= key_modifier::ctrl;
77 if (state & ShiftMask)
78 k |= key_modifier::shift;
80 k |= key_modifier::alt;
87 void C_scroll_cb(FL_OBJECT * ob, long)
89 XWorkArea * area = static_cast<XWorkArea *>(ob->u_vdata);
94 int C_work_area_handler(FL_OBJECT * ob, int event, FL_Coord, FL_Coord,
97 return XWorkArea::work_area_handler(ob, event, 0, 0, key, xev);
101 int C_event_cb(FL_FORM * form, void * xev)
103 XWorkArea * area = static_cast<XWorkArea *>(form->u_vdata);
104 return area->event_cb(static_cast<XEvent *>(xev));
111 XWorkArea::XWorkArea(LyXView & owner, int w, int h)
112 : view_(owner), workareapixmap(0), painter_(*this)
114 fl_freeze_all_forms();
119 // A frame around the work area.
120 frame = obj = fl_add_box(FL_BORDER_BOX, 0, 0, w, h, "");
121 fl_set_object_resize(obj, FL_RESIZE_ALL);
122 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
125 if (lyx_gui::getRGBColor(LColor::background, col)) {
126 fl_mapcolor(FL_FREE_COL12, col.r, col.g, col.b);
127 fl_set_object_color(obj, FL_FREE_COL12, FL_MCOL);
131 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR, 0, 0, w, h, "");
132 fl_set_object_boxtype(obj, FL_UP_BOX);
133 fl_set_object_resize(obj, FL_RESIZE_ALL);
134 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
136 fl_set_object_callback(obj, C_scroll_cb, 0);
137 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
138 fl_set_scrollbar_value(scrollbar, 0.0);
139 fl_set_scrollbar_size(scrollbar, scrollbar->h);
141 // The work area itself
142 work_area = obj = fl_add_free(FL_ALL_FREE, 0, 0, w, h, "",
143 C_work_area_handler);
144 obj->wantkey = FL_KEY_ALL;
147 fl_set_object_boxtype(obj,FL_DOWN_BOX);
148 fl_set_object_resize(obj, FL_RESIZE_ALL);
149 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
151 // Hand control of the layout of these widgets to the
153 XFormsView & xview = dynamic_cast<XFormsView &>(owner);
154 BoxList & boxlist = xview.getBox(XFormsView::Center)->children();
156 wa_box_ = boxlist.push_back(Box(0,0));
157 wa_box_->set(Box::Horizontal);
159 shared_ptr<Box> frame_box = widgets_.add(frame, wa_box_->children(), 0, 0);
160 frame_box->set(Box::Expand);
162 int const bw = int(abs(fl_get_border_width()));
163 shared_ptr<Box> wa_box = embed(work_area, frame_box->children(), widgets_, bw);
164 wa_box->set(Box::Expand);
166 widgets_.add(scrollbar, wa_box_->children(), 17, 0);
168 xview.metricsUpdated.connect(boost::bind(&WidgetMap::updateMetrics,
171 /// X selection hook - xforms gets it wrong
172 fl_current_form->u_vdata = this;
173 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_event_cb);
175 fl_unfreeze_all_forms();
179 val.function = GXcopy;
180 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
185 XWorkArea::~XWorkArea()
187 XFreeGC(fl_get_display(), copy_gc);
189 XFreePixmap(fl_get_display(), workareapixmap);
193 void XWorkArea::redraw(int width, int height)
195 static int cur_width = -1;
196 static int cur_height = -1;
198 if (cur_width == width && cur_height == height && workareapixmap) {
199 XCopyArea(fl_get_display(),
200 getPixmap(), getWin(), copy_gc,
201 0, 0, width, height, xpos(), ypos());
208 if (lyxerr.debugging(Debug::WORKAREA)) {
209 lyxerr << "(Re)creating pixmap ("
210 << width << 'x' << height << ')' << endl;
213 if (workareapixmap) {
214 XFreePixmap(fl_get_display(), workareapixmap);
217 workareapixmap = XCreatePixmap(fl_get_display(),
218 RootWindow(fl_get_display(), 0),
221 fl_get_visual_depth());
223 view_.view()->workAreaResize();
227 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
229 // we need to cache this for scroll_cb
230 doc_height_ = height;
233 fl_set_scrollbar_value(scrollbar, 0.0);
234 fl_set_scrollbar_size(scrollbar, scrollbar->h);
238 long const work_height = workHeight();
240 if (lyxerr.debugging(Debug::GUI)) {
241 lyxerr << "scroll: height now " << height << '\n'
242 << "scroll: work_height " << work_height << endl;
245 /* If the text is smaller than the working area, the scrollbar
246 * maximum must be the working area height. No scrolling will
248 if (height <= work_height) {
249 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !"
251 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
252 fl_set_scrollbar_value(scrollbar, pos);
253 fl_set_scrollbar_size(scrollbar, scrollbar->h);
257 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
258 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
260 fl_set_scrollbar_value(scrollbar, pos);
262 double const slider_size =
263 (height == 0) ? 1.0 : 1.0 / double(height);
265 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
269 // callback for scrollbar slider
270 void XWorkArea::scroll_cb()
272 double const val = fl_get_scrollbar_value(scrollbar);
274 if (lyxerr.debugging(Debug::GUI)) {
275 lyxerr << "scroll: val: " << val << '\n'
276 << "scroll: height: " << scrollbar->h << '\n'
277 << "scroll: docheight: " << doc_height_ << endl;
280 view_.view()->scrollDocView(int(val));
285 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
289 if (event != 10 && event != 11)
290 lyxerr[Debug::WORKAREA] << "Workarea event: EVENT: " << event << endl;
292 XEvent * ev = static_cast<XEvent*>(xev);
293 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
301 if (!area->work_area || !area->work_area->form->visible)
303 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
304 area->redraw(area->workWidth(), area->workHeight());
308 if (!ev || ev->xbutton.button == 0) break;
310 if (ev->xbutton.button == 4 || ev->xbutton.button == 5) {
311 static long last_wheel;
313 long cur_wheel = ev->xbutton.time;
314 if (last_wheel == cur_wheel)
317 last_wheel = cur_wheel;
320 fl_get_scrollbar_increment(area->scrollbar, &l, &r);
322 if (ev->xbutton.button == 4)
325 fl_set_scrollbar_value(
327 fl_get_scrollbar_value(area->scrollbar) + l);
333 // Should really have used xbutton.state
334 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
335 area->view_.view()->workAreaDispatch(
336 FuncRequest(LFUN_MOUSE_PRESS,
337 ev->xbutton.x - ob->x,
338 ev->xbutton.y - ob->y,
339 x_button_state(key)));
343 if (!ev || ev->xbutton.button == 0) break;
344 // Should really have used xbutton.state
346 if (ev->xbutton.button == 4 || ev->xbutton.button == 5) {
347 // We ingnore wheel event here
351 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
353 area->view_.view()->workAreaDispatch(
354 FuncRequest(LFUN_MOUSE_RELEASE,
355 ev->xbutton.x - ob->x,
356 ev->xbutton.y - ob->y,
357 x_button_state(key)));
361 lyxerr[Debug::WORKAREA] << "Workarea event: DRAG 0" << endl;
363 if (!ev || !area->scrollbar)
366 int const drag_x = ev->xmotion.x;
367 int const drag_y = ev->xmotion.y;
368 int const area_y = ob->y;
369 int const area_h = ob->h;
371 // Check if the mouse is above or below the workarea
372 if (drag_y <= area_y || drag_y >= area_y + area_h) {
373 // The mouse button is depressed and we are outside the
374 // workarea. That means we are simultaneously selecting
375 // text and scrolling the view.
376 // Use a Timeout to react to a drag events only every
377 // 200ms. All intervening events are discarded,
378 // allowing the user to control position easily.
379 static int const discard_interval = 200;
380 static Timeout timeout(discard_interval);
382 if (timeout.running())
384 // The timeout is not running, so process the
385 // event, first starting the timeout to discard future
390 static int x_old = -1;
391 static int y_old = -1;
392 static double scrollbar_value_old = -1.0;
394 double const scrollbar_value =
395 fl_get_scrollbar_value(area->scrollbar);
397 if (drag_x != x_old || drag_y != y_old ||
398 scrollbar_value != scrollbar_value_old) {
401 scrollbar_value_old = scrollbar_value;
403 lyxerr[Debug::WORKAREA] << "Workarea event: DRAG"
406 // It transpires that ev->xbutton.button == 0 when
407 // the mouse is dragged, so it cannot be used to
408 // initialise x_button_state and hence FuncRequest.
410 // The 'key' that is passed into the function does
411 // contain the necessary info, however.
413 // It is for this reason that x_button_state has
414 // been modified to work with key
415 // rather than ev->xbutton.button.
417 // Angus 15 Oct 2002.
418 FuncRequest cmd(LFUN_MOUSE_MOTION,
419 ev->xbutton.x - ob->x,
420 ev->xbutton.y - ob->y,
421 x_button_state(key));
422 area->view_.view()->workAreaDispatch(cmd);
428 lyxerr[Debug::WORKAREA] << "Workarea event: KEYPRESS" << endl;
432 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
433 XLookupString(xke, dummy, 1, &keysym, 0);
435 if (lyxerr.debugging(Debug::KEY)) {
436 char const * const tmp = XKeysymToString(key);
437 char const * const tmp2 = XKeysymToString(keysym);
438 string const stm = (tmp ? tmp : string());
439 string const stm2 = (tmp2 ? tmp2 : string());
441 lyxerr << "XWorkArea: Key is `" << stm
442 << "' [" << key << "]\n"
443 << "XWorkArea: Keysym is `" << stm2
444 << "' [" << keysym << ']' << endl;
447 // Note that we need this handling because of a bug
448 // in XForms 0.89, if this bug is resolved in the way I hope
449 // we can just use the keysym directly without looking
450 // at key at all. (Lgb)
453 // We might have to add more keysyms here also,
454 // we will do that as the issues arise. (Lgb)
455 if (keysym == XK_space) {
457 lyxerr[Debug::KEY] << "Using keysym [A]"
462 // It seems that this was a bit optimistic...
463 // With this hacking things seems to be better (Lgb)
464 //if (!iscntrl(key)) {
466 // lyxerr[Debug::KEY]
467 // << "Using key [B]\n"
469 // << static_cast<unsigned char>(key)
472 ret_key = (keysym ? keysym : key);
473 lyxerr[Debug::KEY] << "Using keysym [B]"
478 unsigned int const ret_state = xke->state;
480 // If you have a better way to handle "wild-output" of
481 // characters after the key has been released than the one
482 // below, please contact me. (Lgb)
483 static Time last_time_pressed;
484 static unsigned int last_key_pressed;
485 static unsigned int last_state_pressed;
486 lyxerr[Debug::KEY] << "Workarea Diff: "
487 << xke->time - last_time_pressed
489 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
490 && ret_state == last_state_pressed
491 && xke->keycode == last_key_pressed) {
493 << "Workarea: Purging X events." << endl;
494 //lyxerr << "Workarea Events: "
495 // << XEventsQueued(fl_get_display(), QueuedAlready)
497 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
499 // This purge make f.ex. scrolling stop immediately when
500 // releasing the PageDown button. The question is if
501 // this purging of XEvents can cause any harm...
502 // after some testing I can see no problems, but
503 // I'd like other reports too.
506 last_time_pressed = xke->time;
507 last_key_pressed = xke->keycode;
508 last_state_pressed = ret_state;
510 XLyXKeySym * xlk = new XLyXKeySym;
511 xlk->initFromKeySym(ret_key);
513 area->view_.view()->workAreaKeyPress(LyXKeySymPtr(xlk),
514 x_key_state(ret_state));
519 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
523 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
524 fl_set_cursor(FL_ObjWin(area->work_area), XC_xterm);
528 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
529 // There should be no need for this. But there is.
530 fl_set_cursor(FL_ObjWin(area->work_area), FL_DEFAULT_CURSOR);
535 if (ev->xbutton.button == 4 || ev->xbutton.button == 5) {
536 // Ignore wheel events
541 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
542 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
543 ev->xbutton.x - ob->x,
544 ev->xbutton.y - ob->y,
545 x_button_state(key));
546 area->view_.view()->workAreaDispatch(cmd);
552 if (ev->xbutton.button == 4 || ev->xbutton.button == 5) {
553 // Ignore wheel events
557 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
558 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
559 ev->xbutton.x - ob->x,
560 ev->xbutton.y - ob->y,
561 x_button_state(key));
562 area->view_.view()->workAreaDispatch(cmd);
568 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
578 string clipboard_selection;
579 bool clipboard_read = false;
583 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
584 void const * data, long size)
586 clipboard_selection.erase();
589 clipboard_selection.reserve(size);
590 for (int i = 0; i < size; ++i)
591 clipboard_selection += static_cast<char const *>(data)[i];
592 clipboard_read = true;
600 int XWorkArea::event_cb(XEvent * xev)
603 case SelectionRequest:
604 lyxerr[Debug::GUI] << "X requested selection." << endl;
605 view_.view()->selectionRequested();
608 lyxerr[Debug::GUI] << "Lost selection." << endl;
609 view_.view()->selectionLost();
616 void XWorkArea::haveSelection(bool yes)
618 Window const owner = yes ? FL_ObjWin(work_area) : None;
619 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, owner, CurrentTime);
623 string const XWorkArea::getClipboard() const
625 clipboard_read = false;
627 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
632 while (!clipboard_read) {
633 if (fl_check_forms() == FL_EVENT) {
635 lyxerr << "Received unhandled X11 event\n"
636 << "Type: 0x" << hex << ev.xany.type
637 << " Target: 0x" << hex << ev.xany.window
641 return clipboard_selection;
645 void XWorkArea::putClipboard(string const & s)
650 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);
653 } // namespace frontend