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 lyx::frontend::Box;
28 using lyx::frontend::BoxList;
29 using lyx::frontend::WidgetMap;
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 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 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 copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
183 XWorkArea::~XWorkArea()
185 XFreeGC(fl_get_display(), copy_gc);
187 XFreePixmap(fl_get_display(), workareapixmap);
191 void XWorkArea::redraw(int width, int height)
193 static int cur_width = -1;
194 static int cur_height = -1;
196 if (cur_width == width && cur_height == height && workareapixmap) {
197 XCopyArea(fl_get_display(),
198 getPixmap(), getWin(), copy_gc,
199 0, 0, width, height, xpos(), ypos());
206 if (lyxerr.debugging(Debug::WORKAREA)) {
207 lyxerr << "(Re)creating pixmap ("
208 << width << 'x' << height << ')' << endl;
211 if (workareapixmap) {
212 XFreePixmap(fl_get_display(), workareapixmap);
215 workareapixmap = XCreatePixmap(fl_get_display(),
216 RootWindow(fl_get_display(), 0),
219 fl_get_visual_depth());
225 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
227 // we need to cache this for scroll_cb
228 doc_height_ = height;
231 fl_set_scrollbar_value(scrollbar, 0.0);
232 fl_set_scrollbar_size(scrollbar, scrollbar->h);
236 long const work_height = workHeight();
238 if (lyxerr.debugging(Debug::GUI)) {
239 lyxerr << "scroll: height now " << height << '\n'
240 << "scroll: work_height " << work_height << endl;
243 /* If the text is smaller than the working area, the scrollbar
244 * maximum must be the working area height. No scrolling will
246 if (height <= work_height) {
247 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !"
249 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
250 fl_set_scrollbar_value(scrollbar, pos);
251 fl_set_scrollbar_size(scrollbar, scrollbar->h);
255 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
256 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
258 fl_set_scrollbar_value(scrollbar, pos);
260 double const slider_size =
261 (height == 0) ? 1.0 : 1.0 / double(height);
263 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
267 // callback for scrollbar slider
268 void XWorkArea::scroll_cb()
270 double const val = fl_get_scrollbar_value(scrollbar);
272 if (lyxerr.debugging(Debug::GUI)) {
273 lyxerr << "scroll: val: " << val << '\n'
274 << "scroll: height: " << scrollbar->h << '\n'
275 << "scroll: docheight: " << doc_height_ << endl;
278 scrollDocView(int(val));
283 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
287 XEvent * ev = static_cast<XEvent*>(xev);
288 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
296 if (!area->work_area || !area->work_area->form->visible)
298 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
299 area->redraw(area->workWidth(), area->workHeight());
303 if (!ev || ev->xbutton.button == 0) break;
304 // Should really have used xbutton.state
305 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
307 FuncRequest(LFUN_MOUSE_PRESS,
308 ev->xbutton.x - ob->x,
309 ev->xbutton.y - ob->y,
310 x_button_state(key)));
314 if (!ev || ev->xbutton.button == 0) break;
315 // Should really have used xbutton.state
316 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
318 FuncRequest(LFUN_MOUSE_RELEASE,
319 ev->xbutton.x - ob->x,
320 ev->xbutton.y - ob->y,
321 x_button_state(key)));
325 if (!ev || !area->scrollbar)
328 int const drag_x = ev->xmotion.x;
329 int const drag_y = ev->xmotion.y;
330 int const area_y = ob->y;
331 int const area_h = ob->h;
333 // Check if the mouse is above or below the workarea
334 if (drag_y <= area_y || drag_y >= area_y + area_h) {
335 // The mouse button is depressed and we are outside the
336 // workarea. That means we are simultaneously selecting
337 // text and scrolling the view.
338 // Use a Timeout to react to a drag events only every
339 // 200ms. All intervening events are discarded,
340 // allowing the user to control position easily.
341 static int const discard_interval = 200;
342 static Timeout timeout(discard_interval);
344 if (timeout.running())
346 // The timeout is not running, so process the
347 // event, first starting the timeout to discard future
352 static int x_old = -1;
353 static int y_old = -1;
354 static double scrollbar_value_old = -1.0;
356 double const scrollbar_value =
357 fl_get_scrollbar_value(area->scrollbar);
359 if (drag_x != x_old || drag_y != y_old ||
360 scrollbar_value != scrollbar_value_old) {
363 scrollbar_value_old = scrollbar_value;
365 lyxerr[Debug::WORKAREA] << "Workarea event: DRAG"
368 // It transpires that ev->xbutton.button == 0 when
369 // the mouse is dragged, so it cannot be used to
370 // initialise x_button_state and hence FuncRequest.
372 // The 'key' that is passed into the function does
373 // contain the necessary info, however.
375 // It is for this reason that x_button_state has
376 // been modified to work with key
377 // rather than ev->xbutton.button.
379 // Angus 15 Oct 2002.
380 FuncRequest cmd(LFUN_MOUSE_MOTION,
381 ev->xbutton.x - ob->x,
382 ev->xbutton.y - ob->y,
383 x_button_state(key));
390 lyxerr[Debug::WORKAREA] << "Workarea event: KEYPRESS" << endl;
394 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
395 XLookupString(xke, dummy, 1, &keysym, 0);
397 if (lyxerr.debugging(Debug::KEY)) {
398 char const * const tmp = XKeysymToString(key);
399 char const * const tmp2 = XKeysymToString(keysym);
400 string const stm = (tmp ? tmp : string());
401 string const stm2 = (tmp2 ? tmp2 : string());
403 lyxerr << "XWorkArea: Key is `" << stm
404 << "' [" << key << "]\n"
405 << "XWorkArea: Keysym is `" << stm2
406 << "' [" << keysym << ']' << endl;
409 // Note that we need this handling because of a bug
410 // in XForms 0.89, if this bug is resolved in the way I hope
411 // we can just use the keysym directly without looking
412 // at key at all. (Lgb)
415 // We might have to add more keysyms here also,
416 // we will do that as the issues arise. (Lgb)
417 if (keysym == XK_space) {
419 lyxerr[Debug::KEY] << "Using keysym [A]"
424 // It seems that this was a bit optimistic...
425 // With this hacking things seems to be better (Lgb)
426 //if (!iscntrl(key)) {
428 // lyxerr[Debug::KEY]
429 // << "Using key [B]\n"
431 // << static_cast<unsigned char>(key)
434 ret_key = (keysym ? keysym : key);
435 lyxerr[Debug::KEY] << "Using keysym [B]"
440 unsigned int const ret_state = xke->state;
442 // If you have a better way to handle "wild-output" of
443 // characters after the key has been released than the one
444 // below, please contact me. (Lgb)
445 static Time last_time_pressed;
446 static unsigned int last_key_pressed;
447 static unsigned int last_state_pressed;
448 lyxerr[Debug::KEY] << "Workarea Diff: "
449 << xke->time - last_time_pressed
451 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
452 && ret_state == last_state_pressed
453 && xke->keycode == last_key_pressed) {
455 << "Workarea: Purging X events." << endl;
456 //lyxerr << "Workarea Events: "
457 // << XEventsQueued(fl_get_display(), QueuedAlready)
459 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
461 // This purge make f.ex. scrolling stop immediately when
462 // releasing the PageDown button. The question is if
463 // this purging of XEvents can cause any harm...
464 // after some testing I can see no problems, but
465 // I'd like other reports too.
468 last_time_pressed = xke->time;
469 last_key_pressed = xke->keycode;
470 last_state_pressed = ret_state;
472 XLyXKeySym * xlk = new XLyXKeySym;
473 xlk->initFromKeySym(ret_key);
475 area->workAreaKeyPress(LyXKeySymPtr(xlk),
476 x_key_state(ret_state));
481 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
485 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
486 fl_set_cursor(FL_ObjWin(area->work_area), XC_xterm);
490 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
491 // There should be no need for this. But there is.
492 fl_set_cursor(FL_ObjWin(area->work_area), FL_DEFAULT_CURSOR);
497 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
498 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
499 ev->xbutton.x - ob->x,
500 ev->xbutton.y - ob->y,
501 x_button_state(key));
508 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
509 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
510 ev->xbutton.x - ob->x,
511 ev->xbutton.y - ob->y,
512 x_button_state(key));
519 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
529 string clipboard_selection;
530 bool clipboard_read = false;
534 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
535 void const * data, long size)
537 clipboard_selection.erase();
540 clipboard_selection.reserve(size);
541 for (int i = 0; i < size; ++i)
542 clipboard_selection += static_cast<char const *>(data)[i];
543 clipboard_read = true;
551 int XWorkArea::event_cb(XEvent * xev)
554 case SelectionRequest:
555 lyxerr[Debug::GUI] << "X requested selection." << endl;
556 selectionRequested();
559 lyxerr[Debug::GUI] << "Lost selection." << endl;
567 void XWorkArea::haveSelection(bool yes) const
569 Window const owner = yes ? FL_ObjWin(work_area) : None;
570 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, owner, CurrentTime);
574 string const XWorkArea::getClipboard() const
576 clipboard_read = false;
578 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
583 while (!clipboard_read) {
584 if (fl_check_forms() == FL_EVENT) {
586 lyxerr << "Received unhandled X11 event\n"
587 << "Type: 0x" << hex << ev.xany.type
588 << " Target: 0x" << hex << ev.xany.window
592 return clipboard_selection;
596 void XWorkArea::putClipboard(string const & s) const
601 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);