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);
259 void XWorkArea::resize(int xpos, int ypos, int width, int height)
261 fl_freeze_all_forms();
263 int const bw = int(abs(fl_get_border_width()));
266 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
271 fl_set_object_geometry(scrollbar, xpos + width - 15,
274 // Create the workarea pixmap
275 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
278 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
282 fl_unfreeze_all_forms();
287 void destroy_object(FL_OBJECT * obj)
295 fl_delete_object(obj);
301 void XWorkArea::createPixmap(int width, int height)
303 // Three calls to createPixmap are needed to draw the initial view
304 // of LyX. Any more and the splash is destroyed.
305 static int counter = 0;
306 if (++counter == 4) {
307 destroy_object(splash_);
309 destroy_object(splash_text_);
313 static int cur_width = -1;
314 static int cur_height = -1;
316 if (cur_width == width && cur_height == height && workareapixmap)
323 XFreePixmap(fl_get_display(), workareapixmap);
325 if (lyxerr.debugging(Debug::WORKAREA))
326 lyxerr << "Creating pixmap ("
327 << width << 'x' << height << ")" << endl;
329 workareapixmap = XCreatePixmap(fl_get_display(),
330 RootWindow(fl_get_display(), 0),
333 fl_get_visual_depth());
334 if (lyxerr.debugging(Debug::WORKAREA))
335 lyxerr << "\tpixmap=" << workareapixmap << endl;
339 void XWorkArea::greyOut() const
342 fl_winset(FL_ObjWin(work_area));
343 fl_rectangle(1, work_area->x, work_area->y,
344 work_area->w, work_area->h, FL_GRAY63);
349 void XWorkArea::setFocus() const
351 fl_set_focus_object(work_area->form, work_area);
355 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
357 // we need to cache this for scroll_cb
358 doc_height_ = height;
361 fl_set_scrollbar_value(scrollbar, 0.0);
362 fl_set_scrollbar_size(scrollbar, scrollbar->h);
366 long const work_height = workHeight();
368 lyxerr[Debug::GUI] << "scroll: height now " << height << endl;
369 lyxerr[Debug::GUI] << "scroll: work_height " << work_height << endl;
371 /* If the text is smaller than the working area, the scrollbar
372 * maximum must be the working area height. No scrolling will
374 if (height <= work_height) {
375 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !" << endl;
376 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
377 fl_set_scrollbar_value(scrollbar, pos);
378 fl_set_scrollbar_size(scrollbar, scrollbar->h);
382 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
383 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
385 fl_set_scrollbar_value(scrollbar, pos);
387 double const slider_size =
388 (height == 0) ? 1.0 : 1.0 / double(height);
390 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
394 // callback for scrollbar slider
395 void XWorkArea::scroll_cb()
397 double const val = fl_get_scrollbar_value(scrollbar);
398 lyxerr[Debug::GUI] << "scroll: val: " << val << endl;
399 lyxerr[Debug::GUI] << "scroll: height: " << scrollbar->h << endl;
400 lyxerr[Debug::GUI] << "scroll: docheight: " << doc_height_ << endl;
401 scrollDocView(int(val));
406 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
410 static int x_old = -1;
411 static int y_old = -1;
412 static long scrollbar_value_old = -1;
414 XEvent * ev = static_cast<XEvent*>(xev);
415 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
421 if (!area->work_area ||
422 !area->work_area->form->visible)
424 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
425 area->createPixmap(area->workWidth(), area->workHeight());
426 area->workAreaExpose();
429 if (!ev || ev->xbutton.button == 0) break;
430 // Should really have used xbutton.state
431 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
432 area->workAreaButtonPress(ev->xbutton.x - ob->x,
433 ev->xbutton.y - ob->y,
434 x_button_state(ev->xbutton.button));
437 if (!ev || ev->xbutton.button == 0) break;
438 // Should really have used xbutton.state
439 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
440 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
441 ev->xbutton.y - ob->y,
442 x_button_state(ev->xbutton.button));
444 #if FL_VERSION < 1 && FL_REVISION < 89
449 if (!ev || ! area->scrollbar) break;
450 if (ev->xmotion.x != x_old ||
451 ev->xmotion.y != y_old ||
452 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
454 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
455 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
456 ev->xmotion.y - ob->y,
457 x_motion_state(ev->xbutton.state));
460 #if FL_VERSION < 1 && FL_REVISION < 89
466 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
470 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
471 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
472 // XForms < 0.89.5 does not have compose support
473 // so we are using our own compose support
474 LyXLookupString(ev, dummy, 1, &keysym);
476 XLookupString(xke, dummy, 1, &keysym, 0);
477 // int num_keys = XLookupString(xke, dummy, 10, &keysym, &xcs);
478 // lyxerr << "We have " << num_keys << " keys in the returned buffer" << endl;
479 // lyxerr << "Our dummy string is " << dummy << endl;
482 if (lyxerr.debugging(Debug::KEY)) {
483 char const * tmp = XKeysymToString(key);
484 char const * tmp2 = XKeysymToString(keysym);
485 string const stm = (tmp ? tmp : "");
486 string const stm2 = (tmp2 ? tmp2 : "");
488 lyxerr[Debug::KEY] << "XWorkArea: Key is `" << stm << "' ["
489 << key << "]" << endl;
490 lyxerr[Debug::KEY] << "XWorkArea: Keysym is `" << stm2 << "' ["
491 << keysym << "]" << endl;
494 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
495 if (keysym == NoSymbol) {
497 << "Empty kdb action (probably composing)"
501 KeySym ret_key = keysym;
503 // Note that we need this handling because of a bug
504 // in XForms 0.89, if this bug is resolved in the way I hope
505 // we can just use the keysym directly with out looking
506 // at key at all. (Lgb)
509 // We migth have to add more keysyms here also,
510 // we will do that as the issues arise. (Lgb)
511 if (keysym == XK_space) {
513 lyxerr[Debug::KEY] << "Using keysym [A]"
518 // It seems that this was a bit optimistic...
519 // With this hacking things seems to be better (Lgb)
520 //if (!iscntrl(key)) {
522 // lyxerr[Debug::KEY]
523 // << "Using key [B]\n"
525 // << static_cast<unsigned char>(key)
528 ret_key = (keysym ? keysym : key);
529 lyxerr[Debug::KEY] << "Using keysym [B]"
535 unsigned int const ret_state = xke->state;
537 // If you have a better way to handle "wild-output" of
538 // characters after the key has been released than the one
539 // below, please contact me. (Lgb)
540 static Time last_time_pressed;
541 static unsigned int last_key_pressed;
542 static unsigned int last_state_pressed;
543 lyxerr[Debug::KEY] << "Workarea Diff: "
544 << xke->time - last_time_pressed
546 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
547 && ret_state == last_state_pressed
548 && xke->keycode == last_key_pressed) {
550 << "Workarea: Purging X events." << endl;
551 //lyxerr << "Workarea Events: "
552 // << XEventsQueued(fl_get_display(), QueuedAlready)
554 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
555 XSync(fl_get_display(), 1);
556 // This purge make f.ex. scrolling stop immidiatly when
557 // releasing the PageDown button. The question is if
558 // this purging of XEvents can cause any harm...
559 // after some testing I can see no problems, but
560 // I'd like other reports too.
563 last_time_pressed = xke->time;
564 last_key_pressed = xke->keycode;
565 last_state_pressed = ret_state;
567 XLyXKeySym * xlk = new XLyXKeySym;
568 xlk->initFromKeySym(ret_key);
570 area->workAreaKeyPress(LyXKeySymPtr(xlk),
571 x_key_state(ret_state));
575 #if FL_VERSION > 0 || FL_REVISION >= 89
577 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
582 lyxerr[Debug::WORKAREA] << "Workarea event: FOCUS" << endl;
583 area->workAreaFocus();
586 lyxerr[Debug::WORKAREA] << "Workarea event: UNFOCUS" << endl;
587 area->workAreaUnfocus();
590 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
593 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
597 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
598 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
599 ev->xbutton.y - ob->y,
600 x_button_state(ev->xbutton.button));
604 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
605 area->workAreaTripleClick(ev->xbutton.x - ob->x,
606 ev->xbutton.y - ob->y,
607 x_button_state(ev->xbutton.button));
611 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
621 string clipboard_selection;
622 bool clipboard_read = false;
627 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
628 void const * data, long size)
630 clipboard_selection.erase();
633 clipboard_selection.reserve(size);
634 for (int i = 0; i < size; ++i)
635 clipboard_selection +=
636 static_cast<char const *>(data)[i];
637 clipboard_read = true;
646 int XWorkArea::event_cb(XEvent * xev)
650 case SelectionRequest:
651 lyxerr[Debug::GUI] << "X requested selection." << endl;
652 selectionRequested();
655 lyxerr[Debug::GUI] << "Lost selection." << endl;
663 void XWorkArea::haveSelection(bool yes) const
666 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
670 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
674 string const XWorkArea::getClipboard() const
676 clipboard_read = false;
678 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
683 while (!clipboard_read) {
684 if (fl_check_forms() == FL_EVENT) {
686 lyxerr << "Received unhandled X11 event" << endl;
687 lyxerr << "Type: 0x" << hex << ev.xany.type <<
688 " Target: 0x" << hex << ev.xany.window << endl;
691 return clipboard_selection;
695 void XWorkArea::putClipboard(string const & s) const
700 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);