3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
7 * \author John Levon <moz@compsoc.man.ac.uk>
13 #pragma implementation
16 #include "XWorkArea.h"
19 #include "lyxrc.h" // lyxrc.show_banner
20 #include "version.h" // lyx_version
21 #include "XLyXKeySym.h"
23 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
24 #include "lyxlookup.h"
27 #include "support/filetools.h" // LibFileSearch
28 #include "support/lstrings.h"
29 #include "support/LAssert.h"
34 // xforms doesn't define this (but it should be in <forms.h>).
36 FL_APPEVENT_CB fl_set_preemptive_callback(Window, FL_APPEVENT_CB, void *);
47 XSync(fl_get_display(), 0);
51 void setXtermCursor(Window win)
54 static bool cursor_undefined = true;
55 if (cursor_undefined) {
56 cursor = XCreateFontCursor(fl_get_display(), XC_xterm);
57 XFlush(fl_get_display());
58 cursor_undefined = false;
60 XDefineCursor(fl_get_display(), win, cursor);
61 XFlush(fl_get_display());
66 mouse_button::state x_button_state(unsigned int button)
68 mouse_button::state b = mouse_button::none;
71 b = mouse_button::button1;
74 b = mouse_button::button2;
77 b = mouse_button::button3;
80 b = mouse_button::button4;
83 b = mouse_button::button5;
93 mouse_button::state x_motion_state(unsigned int state)
95 mouse_button::state b = mouse_button::none;
96 if (state & Button1MotionMask)
97 b |= mouse_button::button1;
98 if (state & Button2MotionMask)
99 b |= mouse_button::button2;
100 if (state & Button3MotionMask)
101 b |= mouse_button::button3;
102 if (state & Button4MotionMask)
103 b |= mouse_button::button4;
104 if (state & Button5MotionMask)
105 b |= mouse_button::button5;
110 key_modifier::state x_key_state(unsigned int state)
112 key_modifier::state k = key_modifier::none;
113 if (state & ControlMask)
114 k |= key_modifier::ctrl;
115 if (state & ShiftMask)
116 k |= key_modifier::shift;
117 if (state & Mod1Mask)
118 k |= key_modifier::alt;
127 // Just a bunch of C wrappers around static members of XWorkArea
129 void C_XWorkArea_scroll_cb(FL_OBJECT * ob, long)
131 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
137 int C_XWorkArea_work_area_handler(FL_OBJECT * ob, int event,
141 return XWorkArea::work_area_handler(ob, event,
146 int C_XWorkAreaEventCB(FL_FORM * form, void * xev) {
147 XWorkArea * wa = static_cast<XWorkArea*>(form->u_vdata);
148 return wa->event_cb(static_cast<XEvent*>(xev));
153 XWorkArea::XWorkArea(int x, int y, int w, int h)
154 : splash_(0), splash_text_(0), workareapixmap(0), painter_(*this)
156 fl_freeze_all_forms();
162 if (lyxerr.debugging(Debug::WORKAREA))
163 lyxerr << "\tbackground box: +"
164 << x << '+' << y << ' '
165 << w - 15 << 'x' << h << endl;
166 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
170 fl_set_object_resize(obj, FL_RESIZE_ALL);
171 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
173 // Add a splash screen to the centre of the work area
174 string const splash_file = (lyxrc.show_banner) ?
175 LibFileSearch("images", "banner", "xpm") : string();
177 if (!splash_file.empty()) {
178 int const splash_w = 425;
179 int const splash_h = 290;
180 int const splash_x = x + (w - 15 - splash_w) / 2;
181 int const splash_y = y + (h - splash_h) / 2;
183 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
185 splash_w, splash_h, "");
186 fl_set_pixmapbutton_file(obj, splash_file.c_str());
187 fl_set_pixmapbutton_focus_outline(obj, 3);
188 fl_set_object_boxtype(obj, FL_NO_BOX);
190 int const text_x = splash_x + 260;
191 int const text_y = splash_y + 255;
193 fl_add_text(FL_NORMAL_TEXT, text_x, text_y, 160, 16,
195 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
196 fl_mapcolor(FL_FREE_COL2, 0x2b, 0x47, 0x82);
197 fl_mapcolor(FL_FREE_COL3, 0xe1, 0xd2, 0x9b);
198 fl_set_object_color(obj, FL_FREE_COL2, FL_FREE_COL2);
199 fl_set_object_lcol(obj, FL_FREE_COL3);
200 fl_set_object_lalign(obj, FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
201 fl_set_object_lstyle(obj, FL_BOLD_STYLE);
204 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
207 fl_set_object_boxtype(obj, FL_UP_BOX);
208 fl_set_object_resize(obj, FL_RESIZE_ALL);
209 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
211 fl_set_object_callback(obj, C_XWorkArea_scroll_cb, 0);
212 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
213 fl_set_scrollbar_value(scrollbar, 0.0);
214 fl_set_scrollbar_size(scrollbar, scrollbar->h);
219 int const bw = int(abs(fl_get_border_width()));
221 // Create the workarea pixmap
222 createPixmap(w - 15 - 2 * bw, h - 2 * bw);
224 // We add this object as late as possible to avoit problems
226 if (lyxerr.debugging(Debug::WORKAREA))
227 lyxerr << "\tfree object: +"
228 << x + bw << '+' << y + bw << ' '
229 << w - 15 - 2 * bw << 'x'
230 << h - 2 * bw << endl;
232 work_area = obj = fl_add_free(FL_ALL_FREE,
234 w - 15 - 2 * bw, // scrollbarwidth
236 C_XWorkArea_work_area_handler);
237 obj->wantkey = FL_KEY_ALL;
238 obj->u_vdata = this; /* This is how we pass the XWorkArea
239 to the work_area_handler. */
240 fl_set_object_boxtype(obj,FL_DOWN_BOX);
241 fl_set_object_resize(obj, FL_RESIZE_ALL);
242 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
244 /// X selection hook - xforms gets it wrong
245 fl_current_form->u_vdata = this;
246 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_XWorkAreaEventCB);
248 fl_unfreeze_all_forms();
252 XWorkArea::~XWorkArea()
255 XFreePixmap(fl_get_display(), workareapixmap);
260 void destroy_object(FL_OBJECT * obj)
268 fl_delete_object(obj);
274 void XWorkArea::createPixmap(int width, int height)
276 // Three calls to createPixmap are needed to draw the initial view
277 // of LyX. Any more and the splash is destroyed.
278 static int counter = 0;
279 if (++counter == 4) {
280 destroy_object(splash_);
282 destroy_object(splash_text_);
286 static int cur_width = -1;
287 static int cur_height = -1;
289 if (cur_width == width && cur_height == height && workareapixmap)
296 XFreePixmap(fl_get_display(), workareapixmap);
298 if (lyxerr.debugging(Debug::WORKAREA))
299 lyxerr << "Creating pixmap ("
300 << width << 'x' << height << ")" << endl;
302 workareapixmap = XCreatePixmap(fl_get_display(),
303 RootWindow(fl_get_display(), 0),
306 fl_get_visual_depth());
307 if (lyxerr.debugging(Debug::WORKAREA))
308 lyxerr << "\tpixmap=" << workareapixmap << endl;
312 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
314 // we need to cache this for scroll_cb
315 doc_height_ = height;
318 fl_set_scrollbar_value(scrollbar, 0.0);
319 fl_set_scrollbar_size(scrollbar, scrollbar->h);
323 long const work_height = workHeight();
325 lyxerr[Debug::GUI] << "scroll: height now " << height << endl;
326 lyxerr[Debug::GUI] << "scroll: work_height " << work_height << endl;
328 /* If the text is smaller than the working area, the scrollbar
329 * maximum must be the working area height. No scrolling will
331 if (height <= work_height) {
332 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !" << endl;
333 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
334 fl_set_scrollbar_value(scrollbar, pos);
335 fl_set_scrollbar_size(scrollbar, scrollbar->h);
339 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
340 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
342 fl_set_scrollbar_value(scrollbar, pos);
344 double const slider_size =
345 (height == 0) ? 1.0 : 1.0 / double(height);
347 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
351 // callback for scrollbar slider
352 void XWorkArea::scroll_cb()
354 double const val = fl_get_scrollbar_value(scrollbar);
355 lyxerr[Debug::GUI] << "scroll: val: " << val << endl;
356 lyxerr[Debug::GUI] << "scroll: height: " << scrollbar->h << endl;
357 lyxerr[Debug::GUI] << "scroll: docheight: " << doc_height_ << endl;
358 scrollDocView(int(val));
363 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
367 static int x_old = -1;
368 static int y_old = -1;
369 static long scrollbar_value_old = -1;
371 XEvent * ev = static_cast<XEvent*>(xev);
372 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
378 if (!area->work_area ||
379 !area->work_area->form->visible)
381 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
382 area->createPixmap(area->workWidth(), area->workHeight());
383 area->workAreaResize();
386 if (!ev || ev->xbutton.button == 0) break;
387 // Should really have used xbutton.state
388 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
389 area->workAreaButtonPress(ev->xbutton.x - ob->x,
390 ev->xbutton.y - ob->y,
391 x_button_state(ev->xbutton.button));
394 if (!ev || ev->xbutton.button == 0) break;
395 // Should really have used xbutton.state
396 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
397 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
398 ev->xbutton.y - ob->y,
399 x_button_state(ev->xbutton.button));
401 #if FL_VERSION < 1 && FL_REVISION < 89
406 if (!ev || ! area->scrollbar) break;
407 if (ev->xmotion.x != x_old ||
408 ev->xmotion.y != y_old ||
409 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
411 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
412 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
413 ev->xmotion.y - ob->y,
414 x_motion_state(ev->xbutton.state));
417 #if FL_VERSION < 1 && FL_REVISION < 89
423 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
427 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
428 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
429 // XForms < 0.89.5 does not have compose support
430 // so we are using our own compose support
431 LyXLookupString(ev, dummy, 1, &keysym);
433 XLookupString(xke, dummy, 1, &keysym, 0);
434 // int num_keys = XLookupString(xke, dummy, 10, &keysym, &xcs);
435 // lyxerr << "We have " << num_keys << " keys in the returned buffer" << endl;
436 // lyxerr << "Our dummy string is " << dummy << endl;
439 if (lyxerr.debugging(Debug::KEY)) {
440 char const * tmp = XKeysymToString(key);
441 char const * tmp2 = XKeysymToString(keysym);
442 string const stm = (tmp ? tmp : "");
443 string const stm2 = (tmp2 ? tmp2 : "");
445 lyxerr[Debug::KEY] << "XWorkArea: Key is `" << stm << "' ["
446 << key << "]" << endl;
447 lyxerr[Debug::KEY] << "XWorkArea: Keysym is `" << stm2 << "' ["
448 << keysym << "]" << endl;
451 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
452 if (keysym == NoSymbol) {
454 << "Empty kdb action (probably composing)"
458 KeySym ret_key = keysym;
460 // Note that we need this handling because of a bug
461 // in XForms 0.89, if this bug is resolved in the way I hope
462 // we can just use the keysym directly with out looking
463 // at key at all. (Lgb)
466 // We migth have to add more keysyms here also,
467 // we will do that as the issues arise. (Lgb)
468 if (keysym == XK_space) {
470 lyxerr[Debug::KEY] << "Using keysym [A]"
475 // It seems that this was a bit optimistic...
476 // With this hacking things seems to be better (Lgb)
477 //if (!iscntrl(key)) {
479 // lyxerr[Debug::KEY]
480 // << "Using key [B]\n"
482 // << static_cast<unsigned char>(key)
485 ret_key = (keysym ? keysym : key);
486 lyxerr[Debug::KEY] << "Using keysym [B]"
492 unsigned int const ret_state = xke->state;
494 // If you have a better way to handle "wild-output" of
495 // characters after the key has been released than the one
496 // below, please contact me. (Lgb)
497 static Time last_time_pressed;
498 static unsigned int last_key_pressed;
499 static unsigned int last_state_pressed;
500 lyxerr[Debug::KEY] << "Workarea Diff: "
501 << xke->time - last_time_pressed
503 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
504 && ret_state == last_state_pressed
505 && xke->keycode == last_key_pressed) {
507 << "Workarea: Purging X events." << endl;
508 //lyxerr << "Workarea Events: "
509 // << XEventsQueued(fl_get_display(), QueuedAlready)
511 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
512 XSync(fl_get_display(), 1);
513 // This purge make f.ex. scrolling stop immidiatly when
514 // releasing the PageDown button. The question is if
515 // this purging of XEvents can cause any harm...
516 // after some testing I can see no problems, but
517 // I'd like other reports too.
520 last_time_pressed = xke->time;
521 last_key_pressed = xke->keycode;
522 last_state_pressed = ret_state;
524 XLyXKeySym * xlk = new XLyXKeySym;
525 xlk->initFromKeySym(ret_key);
527 area->workAreaKeyPress(LyXKeySymPtr(xlk),
528 x_key_state(ret_state));
532 #if FL_VERSION > 0 || FL_REVISION >= 89
534 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
539 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
542 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
546 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
547 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
548 ev->xbutton.y - ob->y,
549 x_button_state(ev->xbutton.button));
553 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
554 area->workAreaTripleClick(ev->xbutton.x - ob->x,
555 ev->xbutton.y - ob->y,
556 x_button_state(ev->xbutton.button));
560 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
570 string clipboard_selection;
571 bool clipboard_read = false;
576 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
577 void const * data, long size)
579 clipboard_selection.erase();
582 clipboard_selection.reserve(size);
583 for (int i = 0; i < size; ++i)
584 clipboard_selection +=
585 static_cast<char const *>(data)[i];
586 clipboard_read = true;
595 int XWorkArea::event_cb(XEvent * xev)
599 case SelectionRequest:
600 lyxerr[Debug::GUI] << "X requested selection." << endl;
601 selectionRequested();
604 lyxerr[Debug::GUI] << "Lost selection." << endl;
612 void XWorkArea::haveSelection(bool yes) const
615 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
619 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), 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" << endl;
636 lyxerr << "Type: 0x" << hex << ev.xany.type <<
637 " Target: 0x" << hex << ev.xany.window << endl;
640 return clipboard_selection;
644 void XWorkArea::putClipboard(string const & s) const
649 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);