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::setScrollbarParams(int height, int pos, int line_height)
341 // we need to cache this for scroll_cb
342 doc_height_ = height;
345 fl_set_scrollbar_value(scrollbar, 0.0);
346 fl_set_scrollbar_size(scrollbar, scrollbar->h);
350 long const work_height = workHeight();
352 lyxerr[Debug::GUI] << "scroll: height now " << height << endl;
353 lyxerr[Debug::GUI] << "scroll: work_height " << work_height << endl;
355 /* If the text is smaller than the working area, the scrollbar
356 * maximum must be the working area height. No scrolling will
358 if (height <= work_height) {
359 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !" << endl;
360 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
361 fl_set_scrollbar_value(scrollbar, pos);
362 fl_set_scrollbar_size(scrollbar, scrollbar->h);
366 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
367 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
369 fl_set_scrollbar_value(scrollbar, pos);
371 double const slider_size =
372 (height == 0) ? 1.0 : 1.0 / double(height);
374 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
378 // callback for scrollbar slider
379 void XWorkArea::scroll_cb()
381 double const val = fl_get_scrollbar_value(scrollbar);
382 lyxerr[Debug::GUI] << "scroll: val: " << val << endl;
383 lyxerr[Debug::GUI] << "scroll: height: " << scrollbar->h << endl;
384 lyxerr[Debug::GUI] << "scroll: docheight: " << doc_height_ << endl;
385 scrollDocView(int(val));
390 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
394 static int x_old = -1;
395 static int y_old = -1;
396 static long scrollbar_value_old = -1;
398 XEvent * ev = static_cast<XEvent*>(xev);
399 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
405 if (!area->work_area ||
406 !area->work_area->form->visible)
408 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
409 area->createPixmap(area->workWidth(), area->workHeight());
410 area->workAreaExpose();
413 if (!ev || ev->xbutton.button == 0) break;
414 // Should really have used xbutton.state
415 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
416 area->workAreaButtonPress(ev->xbutton.x - ob->x,
417 ev->xbutton.y - ob->y,
418 x_button_state(ev->xbutton.button));
421 if (!ev || ev->xbutton.button == 0) break;
422 // Should really have used xbutton.state
423 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
424 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
425 ev->xbutton.y - ob->y,
426 x_button_state(ev->xbutton.button));
428 #if FL_VERSION < 1 && FL_REVISION < 89
433 if (!ev || ! area->scrollbar) break;
434 if (ev->xmotion.x != x_old ||
435 ev->xmotion.y != y_old ||
436 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
438 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
439 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
440 ev->xmotion.y - ob->y,
441 x_motion_state(ev->xbutton.state));
444 #if FL_VERSION < 1 && FL_REVISION < 89
450 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
454 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
455 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
456 // XForms < 0.89.5 does not have compose support
457 // so we are using our own compose support
458 LyXLookupString(ev, dummy, 1, &keysym);
460 XLookupString(xke, dummy, 1, &keysym, 0);
461 // int num_keys = XLookupString(xke, dummy, 10, &keysym, &xcs);
462 // lyxerr << "We have " << num_keys << " keys in the returned buffer" << endl;
463 // lyxerr << "Our dummy string is " << dummy << endl;
466 if (lyxerr.debugging(Debug::KEY)) {
467 char const * tmp = XKeysymToString(key);
468 char const * tmp2 = XKeysymToString(keysym);
469 string const stm = (tmp ? tmp : "");
470 string const stm2 = (tmp2 ? tmp2 : "");
472 lyxerr[Debug::KEY] << "XWorkArea: Key is `" << stm << "' ["
473 << key << "]" << endl;
474 lyxerr[Debug::KEY] << "XWorkArea: Keysym is `" << stm2 << "' ["
475 << keysym << "]" << endl;
478 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
479 if (keysym == NoSymbol) {
481 << "Empty kdb action (probably composing)"
485 KeySym ret_key = keysym;
487 // Note that we need this handling because of a bug
488 // in XForms 0.89, if this bug is resolved in the way I hope
489 // we can just use the keysym directly with out looking
490 // at key at all. (Lgb)
493 // We migth have to add more keysyms here also,
494 // we will do that as the issues arise. (Lgb)
495 if (keysym == XK_space) {
497 lyxerr[Debug::KEY] << "Using keysym [A]"
502 // It seems that this was a bit optimistic...
503 // With this hacking things seems to be better (Lgb)
504 //if (!iscntrl(key)) {
506 // lyxerr[Debug::KEY]
507 // << "Using key [B]\n"
509 // << static_cast<unsigned char>(key)
512 ret_key = (keysym ? keysym : key);
513 lyxerr[Debug::KEY] << "Using keysym [B]"
519 unsigned int const ret_state = xke->state;
521 // If you have a better way to handle "wild-output" of
522 // characters after the key has been released than the one
523 // below, please contact me. (Lgb)
524 static Time last_time_pressed;
525 static unsigned int last_key_pressed;
526 static unsigned int last_state_pressed;
527 lyxerr[Debug::KEY] << "Workarea Diff: "
528 << xke->time - last_time_pressed
530 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
531 && ret_state == last_state_pressed
532 && xke->keycode == last_key_pressed) {
534 << "Workarea: Purging X events." << endl;
535 //lyxerr << "Workarea Events: "
536 // << XEventsQueued(fl_get_display(), QueuedAlready)
538 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
539 XSync(fl_get_display(), 1);
540 // This purge make f.ex. scrolling stop immidiatly when
541 // releasing the PageDown button. The question is if
542 // this purging of XEvents can cause any harm...
543 // after some testing I can see no problems, but
544 // I'd like other reports too.
547 last_time_pressed = xke->time;
548 last_key_pressed = xke->keycode;
549 last_state_pressed = ret_state;
551 XLyXKeySym * xlk = new XLyXKeySym;
552 xlk->initFromKeySym(ret_key);
554 area->workAreaKeyPress(LyXKeySymPtr(xlk),
555 x_key_state(ret_state));
559 #if FL_VERSION > 0 || FL_REVISION >= 89
561 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
566 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
569 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
573 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
574 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
575 ev->xbutton.y - ob->y,
576 x_button_state(ev->xbutton.button));
580 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
581 area->workAreaTripleClick(ev->xbutton.x - ob->x,
582 ev->xbutton.y - ob->y,
583 x_button_state(ev->xbutton.button));
587 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
597 string clipboard_selection;
598 bool clipboard_read = false;
603 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
604 void const * data, long size)
606 clipboard_selection.erase();
609 clipboard_selection.reserve(size);
610 for (int i = 0; i < size; ++i)
611 clipboard_selection +=
612 static_cast<char const *>(data)[i];
613 clipboard_read = true;
622 int XWorkArea::event_cb(XEvent * xev)
626 case SelectionRequest:
627 lyxerr[Debug::GUI] << "X requested selection." << endl;
628 selectionRequested();
631 lyxerr[Debug::GUI] << "Lost selection." << endl;
639 void XWorkArea::haveSelection(bool yes) const
642 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
646 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
650 string const XWorkArea::getClipboard() const
652 clipboard_read = false;
654 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
659 while (!clipboard_read) {
660 if (fl_check_forms() == FL_EVENT) {
662 lyxerr << "Received unhandled X11 event" << endl;
663 lyxerr << "Type: 0x" << hex << ev.xany.type <<
664 " Target: 0x" << hex << ev.xany.window << endl;
667 return clipboard_selection;
671 void XWorkArea::putClipboard(string const & s) const
676 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);