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
22 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
23 #include "lyxlookup.h"
26 #include "support/filetools.h" // LibFileSearch
27 #include "support/lstrings.h"
28 #include "support/LAssert.h"
33 // xforms doesn't define this (but it should be in <forms.h>).
35 FL_APPEVENT_CB fl_set_preemptive_callback(Window, FL_APPEVENT_CB, void *);
46 XSync(fl_get_display(), 0);
50 void setXtermCursor(Window win)
53 static bool cursor_undefined = true;
54 if (cursor_undefined) {
55 cursor = XCreateFontCursor(fl_get_display(), XC_xterm);
56 XFlush(fl_get_display());
57 cursor_undefined = false;
59 XDefineCursor(fl_get_display(), win, cursor);
60 XFlush(fl_get_display());
65 mouse_button::state x_button_state(unsigned int button)
67 mouse_button::state b = mouse_button::none;
70 b = mouse_button::button1;
73 b = mouse_button::button2;
76 b = mouse_button::button3;
79 b = mouse_button::button4;
82 b = mouse_button::button5;
92 mouse_button::state x_motion_state(unsigned int state)
94 mouse_button::state b = mouse_button::none;
95 if (state & Button1MotionMask)
96 b |= mouse_button::button1;
97 if (state & Button2MotionMask)
98 b |= mouse_button::button2;
99 if (state & Button3MotionMask)
100 b |= mouse_button::button3;
101 if (state & Button4MotionMask)
102 b |= mouse_button::button4;
103 if (state & Button5MotionMask)
104 b |= mouse_button::button5;
109 key_modifier::state x_key_state(unsigned int state)
111 key_modifier::state k = key_modifier::none;
112 if (state & ControlMask)
113 k |= key_modifier::ctrl;
114 if (state & ShiftMask)
115 k |= key_modifier::shift;
116 if (state & Mod1Mask)
117 k |= key_modifier::alt;
126 // Just a bunch of C wrappers around static members of WorkArea
128 void C_WorkArea_scroll_cb(FL_OBJECT * ob, long)
130 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
136 int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
140 return WorkArea::work_area_handler(ob, event,
145 int C_WorkAreaEventCB(FL_FORM * form, void * xev) {
146 WorkArea * wa = static_cast<WorkArea*>(form->u_vdata);
147 return wa->event_cb(static_cast<XEvent*>(xev));
152 WorkArea::WorkArea(int x, int y, int w, int h)
153 : splash_(0), splash_text_(0), workareapixmap(0), painter_(*this)
155 fl_freeze_all_forms();
161 if (lyxerr.debugging(Debug::WORKAREA))
162 lyxerr << "\tbackground box: +"
163 << x << '+' << y << ' '
164 << w - 15 << 'x' << h << endl;
165 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
169 fl_set_object_resize(obj, FL_RESIZE_ALL);
170 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
172 // Add a splash screen to the centre of the work area
173 string const splash_file = (lyxrc.show_banner) ?
174 LibFileSearch("images", "banner", "xpm") : string();
176 if (!splash_file.empty()) {
177 int const splash_w = 425;
178 int const splash_h = 290;
179 int const splash_x = x + (w - 15 - splash_w) / 2;
180 int const splash_y = y + (h - splash_h) / 2;
182 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
184 splash_w, splash_h, "");
185 fl_set_pixmapbutton_file(obj, splash_file.c_str());
186 fl_set_pixmapbutton_focus_outline(obj, 3);
187 fl_set_object_boxtype(obj, FL_NO_BOX);
189 int const text_x = splash_x + 260;
190 int const text_y = splash_y + 255;
192 fl_add_text(FL_NORMAL_TEXT, text_x, text_y, 160, 16,
194 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
195 fl_mapcolor(FL_FREE_COL2, 0x2b, 0x47, 0x82);
196 fl_mapcolor(FL_FREE_COL3, 0xe1, 0xd2, 0x9b);
197 fl_set_object_color(obj, FL_FREE_COL2, FL_FREE_COL2);
198 fl_set_object_lcol(obj, FL_FREE_COL3);
199 fl_set_object_lalign(obj, FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
200 fl_set_object_lstyle(obj, FL_BOLD_STYLE);
203 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
206 fl_set_object_boxtype(obj, FL_UP_BOX);
207 fl_set_object_resize(obj, FL_RESIZE_ALL);
208 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
210 fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
211 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
212 fl_set_scrollbar_value(scrollbar, 0.0);
213 fl_set_scrollbar_size(scrollbar, scrollbar->h);
218 int const bw = int(abs(fl_get_border_width()));
220 // Create the workarea pixmap
221 createPixmap(w - 15 - 2 * bw, h - 2 * bw);
223 // We add this object as late as possible to avoit problems
225 if (lyxerr.debugging(Debug::WORKAREA))
226 lyxerr << "\tfree object: +"
227 << x + bw << '+' << y + bw << ' '
228 << w - 15 - 2 * bw << 'x'
229 << h - 2 * bw << endl;
231 work_area = obj = fl_add_free(FL_ALL_FREE,
233 w - 15 - 2 * bw, // scrollbarwidth
235 C_WorkArea_work_area_handler);
236 obj->wantkey = FL_KEY_ALL;
237 obj->u_vdata = this; /* This is how we pass the WorkArea
238 to the work_area_handler. */
239 fl_set_object_boxtype(obj,FL_DOWN_BOX);
240 fl_set_object_resize(obj, FL_RESIZE_ALL);
241 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
243 /// X selection hook - xforms gets it wrong
244 fl_current_form->u_vdata = this;
245 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_WorkAreaEventCB);
247 fl_unfreeze_all_forms();
251 WorkArea::~WorkArea()
254 XFreePixmap(fl_get_display(), workareapixmap);
258 void WorkArea::resize(int xpos, int ypos, int width, int height)
260 fl_freeze_all_forms();
262 int const bw = int(abs(fl_get_border_width()));
265 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
270 fl_set_object_geometry(scrollbar, xpos + width - 15,
273 // Create the workarea pixmap
274 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
277 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
281 fl_unfreeze_all_forms();
286 void destroy_object(FL_OBJECT * obj)
294 fl_delete_object(obj);
300 void WorkArea::createPixmap(int width, int height)
302 // Three calls to createPixmap are needed to draw the initial view
303 // of LyX. Any more and the splash is destroyed.
304 static int counter = 0;
305 if (++counter == 4) {
306 destroy_object(splash_);
308 destroy_object(splash_text_);
312 static int cur_width = -1;
313 static int cur_height = -1;
315 if (cur_width == width && cur_height == height && workareapixmap)
322 XFreePixmap(fl_get_display(), workareapixmap);
324 if (lyxerr.debugging(Debug::WORKAREA))
325 lyxerr << "Creating pixmap ("
326 << width << 'x' << height << ")" << endl;
328 workareapixmap = XCreatePixmap(fl_get_display(),
329 RootWindow(fl_get_display(), 0),
332 fl_get_visual_depth());
333 if (lyxerr.debugging(Debug::WORKAREA))
334 lyxerr << "\tpixmap=" << workareapixmap << endl;
338 void WorkArea::greyOut() const
341 fl_winset(FL_ObjWin(work_area));
342 fl_rectangle(1, work_area->x, work_area->y,
343 work_area->w, work_area->h, FL_GRAY63);
348 void WorkArea::setFocus() const
350 fl_set_focus_object(work_area->form, work_area);
354 void WorkArea::setScrollbarParams(int height, int pos, int line_height)
356 // we need to cache this for scroll_cb
357 doc_height_ = height;
360 fl_set_scrollbar_value(scrollbar, 0.0);
361 fl_set_scrollbar_size(scrollbar, scrollbar->h);
365 long const work_height = workHeight();
367 lyxerr[Debug::GUI] << "scroll: height now " << height << endl;
368 lyxerr[Debug::GUI] << "scroll: work_height " << work_height << endl;
370 /* If the text is smaller than the working area, the scrollbar
371 * maximum must be the working area height. No scrolling will
373 if (height <= work_height) {
374 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !" << endl;
375 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
376 fl_set_scrollbar_value(scrollbar, pos);
377 fl_set_scrollbar_size(scrollbar, scrollbar->h);
381 fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
382 fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
384 fl_set_scrollbar_value(scrollbar, pos);
386 double const slider_size =
387 (height == 0) ? 1.0 : 1.0 / double(height);
389 fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
393 // callback for scrollbar slider
394 void WorkArea::scroll_cb()
396 double const val = fl_get_scrollbar_value(scrollbar);
397 lyxerr[Debug::GUI] << "scroll: val: " << val << endl;
398 lyxerr[Debug::GUI] << "scroll: height: " << scrollbar->h << endl;
399 lyxerr[Debug::GUI] << "scroll: docheight: " << doc_height_ << endl;
400 scrollDocView(int(val));
405 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
409 static int x_old = -1;
410 static int y_old = -1;
411 static long scrollbar_value_old = -1;
413 XEvent * ev = static_cast<XEvent*>(xev);
414 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
420 if (!area->work_area ||
421 !area->work_area->form->visible)
423 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
424 area->createPixmap(area->workWidth(), area->workHeight());
425 area->workAreaExpose();
428 if (!ev || ev->xbutton.button == 0) break;
429 // Should really have used xbutton.state
430 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
431 area->workAreaButtonPress(ev->xbutton.x - ob->x,
432 ev->xbutton.y - ob->y,
433 x_button_state(ev->xbutton.button));
436 if (!ev || ev->xbutton.button == 0) break;
437 // Should really have used xbutton.state
438 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
439 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
440 ev->xbutton.y - ob->y,
441 x_button_state(ev->xbutton.button));
443 #if FL_VERSION < 1 && FL_REVISION < 89
448 if (!ev || ! area->scrollbar) break;
449 if (ev->xmotion.x != x_old ||
450 ev->xmotion.y != y_old ||
451 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
453 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
454 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
455 ev->xmotion.y - ob->y,
456 x_motion_state(ev->xbutton.state));
459 #if FL_VERSION < 1 && FL_REVISION < 89
465 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
469 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
470 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
471 // XForms < 0.89.5 does not have compose support
472 // so we are using our own compose support
473 LyXLookupString(ev, dummy, 1, &keysym);
475 XLookupString(xke, dummy, 1, &keysym, 0);
476 // int num_keys = XLookupString(xke, dummy, 10, &keysym, &xcs);
477 // lyxerr << "We have " << num_keys << " keys in the returned buffer" << endl;
478 // lyxerr << "Our dummy string is " << dummy << endl;
480 if (lyxerr.debugging(Debug::KEY)) {
481 char const * tmp = XKeysymToString(key);
482 char const * tmp2 = XKeysymToString(keysym);
483 string const stm = (tmp ? tmp : "");
484 string const stm2 = (tmp2 ? tmp2 : "");
486 lyxerr[Debug::KEY] << "WorkArea: Key is `" << stm << "' ["
487 << key << "]" << endl;
488 lyxerr[Debug::KEY] << "WorkArea: Keysym is `" << stm2 << "' ["
489 << keysym << "]" << endl;
492 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
493 if (keysym == NoSymbol) {
495 << "Empty kdb action (probably composing)"
499 KeySym ret_key = keysym;
501 // Note that we need this handling because of a bug
502 // in XForms 0.89, if this bug is resolved in the way I hope
503 // we can just use the keysym directly with out looking
504 // at key at all. (Lgb)
507 // We migth have to add more keysyms here also,
508 // we will do that as the issues arise. (Lgb)
509 if (keysym == XK_space) {
511 lyxerr[Debug::KEY] << "Using keysym [A]"
516 // It seems that this was a bit optimistic...
517 // With this hacking things seems to be better (Lgb)
518 //if (!iscntrl(key)) {
520 // lyxerr[Debug::KEY]
521 // << "Using key [B]\n"
523 // << static_cast<unsigned char>(key)
526 ret_key = (keysym ? keysym : key);
527 lyxerr[Debug::KEY] << "Using keysym [B]"
533 unsigned int const ret_state = xke->state;
535 // If you have a better way to handle "wild-output" of
536 // characters after the key has been released than the one
537 // below, please contact me. (Lgb)
538 static Time last_time_pressed;
539 static unsigned int last_key_pressed;
540 static unsigned int last_state_pressed;
541 lyxerr[Debug::KEY] << "Workarea Diff: "
542 << xke->time - last_time_pressed
544 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
545 && ret_state == last_state_pressed
546 && xke->keycode == last_key_pressed) {
548 << "Workarea: Purging X events." << endl;
549 //lyxerr << "Workarea Events: "
550 // << XEventsQueued(fl_get_display(), QueuedAlready)
552 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
553 XSync(fl_get_display(), 1);
554 // This purge make f.ex. scrolling stop immidiatly when
555 // releasing the PageDown button. The question is if
556 // this purging of XEvents can cause any harm...
557 // after some testing I can see no problems, but
558 // I'd like other reports too.
561 last_time_pressed = xke->time;
562 last_key_pressed = xke->keycode;
563 last_state_pressed = ret_state;
565 area->workAreaKeyPress(ret_key, x_key_state(ret_state));
569 #if FL_VERSION > 0 || FL_REVISION >= 89
571 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
576 lyxerr[Debug::WORKAREA] << "Workarea event: FOCUS" << endl;
577 area->workAreaFocus();
580 lyxerr[Debug::WORKAREA] << "Workarea event: UNFOCUS" << endl;
581 area->workAreaUnfocus();
584 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
587 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
591 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
592 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
593 ev->xbutton.y - ob->y,
594 x_button_state(ev->xbutton.button));
598 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
599 area->workAreaTripleClick(ev->xbutton.x - ob->x,
600 ev->xbutton.y - ob->y,
601 x_button_state(ev->xbutton.button));
605 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
615 string clipboard_selection;
616 bool clipboard_read = false;
621 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
622 void const * data, long size)
624 clipboard_selection.erase();
627 clipboard_selection.reserve(size);
628 for (int i = 0; i < size; ++i)
629 clipboard_selection +=
630 static_cast<char const *>(data)[i];
631 clipboard_read = true;
640 int WorkArea::event_cb(XEvent * xev)
644 case SelectionRequest:
645 lyxerr[Debug::GUI] << "X requested selection." << endl;
646 selectionRequested();
649 lyxerr[Debug::GUI] << "Lost selection." << endl;
657 void WorkArea::haveSelection(bool yes) const
660 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
664 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
668 string const WorkArea::getClipboard() const
670 clipboard_read = false;
672 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
677 while (!clipboard_read) {
678 if (fl_check_forms() == FL_EVENT) {
680 lyxerr << "Received unhandled X11 event" << endl;
681 lyxerr << "Type: 0x" << hex << ev.xany.type <<
682 " Target: 0x" << hex << ev.xany.window << endl;
685 return clipboard_selection;
689 void WorkArea::putClipboard(string const & s) const
694 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);