1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
17 #include "XWorkArea.h"
19 #include "frontends/LyXView.h"
20 #include "lyxrc.h" // lyxrc.show_banner
21 #include "version.h" // lyx_version
23 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
24 #include "frontends/xforms/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 mouse_button::state x_button_state(unsigned int button)
53 mouse_button::state b = mouse_button::none;
56 b = mouse_button::button1;
59 b = mouse_button::button2;
62 b = mouse_button::button3;
65 b = mouse_button::button4;
68 b = mouse_button::button5;
78 mouse_button::state x_motion_state(unsigned int state)
80 mouse_button::state b = mouse_button::none;
81 if (state & Button1MotionMask)
82 b |= mouse_button::button1;
83 if (state & Button2MotionMask)
84 b |= mouse_button::button2;
85 if (state & Button3MotionMask)
86 b |= mouse_button::button3;
87 if (state & Button4MotionMask)
88 b |= mouse_button::button4;
89 if (state & Button5MotionMask)
90 b |= mouse_button::button5;
95 key_modifier::state x_key_state(unsigned int state)
97 key_modifier::state k = key_modifier::none;
98 if (state & ControlMask)
99 k |= key_modifier::ctrl;
100 if (state & ShiftMask)
101 k |= key_modifier::shift;
102 if (state & Mod1Mask)
103 k |= key_modifier::alt;
112 // Just a bunch of C wrappers around static members of WorkArea
114 void C_WorkArea_scroll_cb(FL_OBJECT * ob, long buf)
116 WorkArea::scroll_cb(ob, buf);
121 int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
125 return WorkArea::work_area_handler(ob, event,
130 int C_WorkAreaEventCB(FL_FORM * form, void * xev) {
131 WorkArea * wa = static_cast<WorkArea*>(form->u_vdata);
132 return wa->event_cb(static_cast<XEvent*>(xev));
137 WorkArea::WorkArea(int xpos, int ypos, int width, int height)
138 : splash_(0), splash_text_(0), workareapixmap(0), painter_(*this)
140 fl_freeze_all_forms();
142 if (lyxerr.debugging(Debug::WORKAREA))
143 lyxerr << "Creating work area: +"
144 << xpos << '+' << ypos << ' '
145 << width << 'x' << height << endl;
148 int const bw = int(abs(fl_get_border_width()));
151 if (lyxerr.debugging(Debug::WORKAREA))
152 lyxerr << "\tbackground box: +"
153 << xpos << '+' << ypos << ' '
154 << width - 15 << 'x' << height << endl;
155 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
159 fl_set_object_resize(obj, FL_RESIZE_ALL);
160 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
162 // Add a splash screen to the centre of the work area
163 string const splash_file = (lyxrc.show_banner) ?
164 LibFileSearch("images", "banner", "xpm") : string();
166 if (!splash_file.empty()) {
167 int const splash_w = 425;
168 int const splash_h = 290;
169 int const splash_x = xpos + (width - 15 - splash_w) / 2;
170 int const splash_y = ypos + (height - splash_h) / 2;
172 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
174 splash_w, splash_h, "");
175 fl_set_pixmapbutton_file(obj, splash_file.c_str());
176 fl_set_pixmapbutton_focus_outline(obj, 3);
177 fl_set_object_boxtype(obj, FL_NO_BOX);
179 int const text_x = splash_x + 260;
180 int const text_y = splash_y + 255;
182 fl_add_text(FL_NORMAL_TEXT, text_x, text_y, 160, 16,
184 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
185 fl_mapcolor(FL_FREE_COL2, 0x2b, 0x47, 0x82);
186 fl_mapcolor(FL_FREE_COL3, 0xe1, 0xd2, 0x9b);
187 fl_set_object_color(obj, FL_FREE_COL2, FL_FREE_COL2);
188 fl_set_object_lcol(obj, FL_FREE_COL3);
189 fl_set_object_lalign(obj, FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
190 fl_set_object_lstyle(obj, FL_BOLD_STYLE);
197 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
199 ypos, 17, height, "");
200 fl_set_object_boxtype(obj, FL_UP_BOX);
201 fl_set_object_resize(obj, FL_RESIZE_ALL);
202 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
204 fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
205 setScrollbarBounds(0.0, 0.0);
210 // Create the workarea pixmap
211 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
213 // We add this object as late as possible to avoit problems
215 if (lyxerr.debugging(Debug::WORKAREA))
216 lyxerr << "\tfree object: +"
217 << xpos + bw << '+' << ypos + bw << ' '
218 << width - 15 - 2 * bw << 'x'
219 << height - 2 * bw << endl;
221 work_area = obj = fl_add_free(FL_ALL_FREE,
222 xpos + bw, ypos + bw,
223 width - 15 - 2 * bw, // scrollbarwidth
225 C_WorkArea_work_area_handler);
226 obj->wantkey = FL_KEY_ALL;
227 obj->u_vdata = this; /* This is how we pass the WorkArea
228 to the work_area_handler. */
229 fl_set_object_boxtype(obj,FL_DOWN_BOX);
230 fl_set_object_resize(obj, FL_RESIZE_ALL);
231 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
233 /// X selection hook - xforms gets it wrong
234 fl_current_form->u_vdata = this;
235 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_WorkAreaEventCB);
237 fl_unfreeze_all_forms();
241 WorkArea::~WorkArea()
244 XFreePixmap(fl_get_display(), workareapixmap);
248 void WorkArea::resize(int xpos, int ypos, int width, int height)
250 fl_freeze_all_forms();
252 int const bw = int(abs(fl_get_border_width()));
255 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
260 fl_set_object_geometry(scrollbar, xpos + width - 15,
263 // Create the workarea pixmap
264 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
267 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
271 fl_unfreeze_all_forms();
276 void destroy_object(FL_OBJECT * obj)
284 fl_delete_object(obj);
290 void WorkArea::createPixmap(int width, int height)
292 // Three calls to createPixmap are needed to draw the initial view
293 // of LyX. Any more and the splash is destroyed.
294 static int counter = 0;
295 if (++counter == 4) {
296 destroy_object(splash_);
298 destroy_object(splash_text_);
302 static int cur_width = -1;
303 static int cur_height = -1;
305 if (cur_width == width && cur_height == height && workareapixmap)
312 XFreePixmap(fl_get_display(), workareapixmap);
314 if (lyxerr.debugging(Debug::WORKAREA))
315 lyxerr << "Creating pixmap ("
316 << width << 'x' << height << ")" << endl;
318 workareapixmap = XCreatePixmap(fl_get_display(),
319 RootWindow(fl_get_display(), 0),
322 fl_get_visual_depth());
323 if (lyxerr.debugging(Debug::WORKAREA))
324 lyxerr << "\tpixmap=" << workareapixmap << endl;
328 void WorkArea::greyOut() const
331 fl_winset(FL_ObjWin(work_area));
332 fl_rectangle(1, work_area->x, work_area->y,
333 work_area->w, work_area->h, FL_GRAY63);
338 void WorkArea::setFocus() const
340 fl_set_focus_object(work_area->form, work_area);
344 void WorkArea::setScrollbar(double pos, double length_fraction) const
346 fl_set_scrollbar_value(scrollbar, pos);
347 fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
351 void WorkArea::setScrollbarBounds(double l1, double l2) const
353 fl_set_scrollbar_bounds(scrollbar, l1, l2);
357 void WorkArea::setScrollbarIncrements(double inc) const
359 fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
363 // Callback for scrollbar slider
364 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
366 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
367 // If we really want the accellerating scroll we can do that
368 // from here. IMHO that is a waste of effort since we already
369 // have other ways to move fast around in the document. (Lgb)
370 area->scrollCB(fl_get_scrollbar_value(ob));
375 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
379 static int x_old = -1;
380 static int y_old = -1;
381 static long scrollbar_value_old = -1;
383 XEvent * ev = static_cast<XEvent*>(xev);
384 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
390 if (!area->work_area ||
391 !area->work_area->form->visible)
393 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
394 area->createPixmap(area->workWidth(), area->workHeight());
395 area->workAreaExpose();
398 if (!ev || ev->xbutton.button == 0) break;
399 // Should really have used xbutton.state
400 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
401 area->workAreaButtonPress(ev->xbutton.x - ob->x,
402 ev->xbutton.y - ob->y,
403 x_button_state(ev->xbutton.button));
406 if (!ev || ev->xbutton.button == 0) break;
407 // Should really have used xbutton.state
408 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
409 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
410 ev->xbutton.y - ob->y,
411 x_button_state(ev->xbutton.button));
413 #if FL_VERSION < 1 && FL_REVISION < 89
418 if (!ev || ! area->scrollbar) break;
419 if (ev->xmotion.x != x_old ||
420 ev->xmotion.y != y_old ||
421 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
423 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
424 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
425 ev->xmotion.y - ob->y,
426 x_motion_state(ev->xbutton.state));
429 #if FL_VERSION < 1 && FL_REVISION < 89
435 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
439 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
440 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
441 // XForms < 0.89.5 does not have compose support
442 // so we are using our own compose support
443 LyXLookupString(ev, dummy, 1, &keysym);
445 XLookupString(xke, dummy, 1, &keysym, 0);
446 // int num_keys = XLookupString(xke, dummy, 10, &keysym, &xcs);
447 // lyxerr << "We have " << num_keys << " keys in the returned buffer" << endl;
448 // lyxerr << "Our dummy string is " << dummy << endl;
450 if (lyxerr.debugging(Debug::KEY)) {
451 char const * tmp = XKeysymToString(key);
452 char const * tmp2 = XKeysymToString(keysym);
453 string const stm = (tmp ? tmp : "");
454 string const stm2 = (tmp2 ? tmp2 : "");
456 lyxerr[Debug::KEY] << "WorkArea: Key is `" << stm << "' ["
457 << key << "]" << endl;
458 lyxerr[Debug::KEY] << "WorkArea: Keysym is `" << stm2 << "' ["
459 << keysym << "]" << endl;
462 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
463 if (keysym == NoSymbol) {
465 << "Empty kdb action (probably composing)"
469 KeySym ret_key = keysym;
471 // Note that we need this handling because of a bug
472 // in XForms 0.89, if this bug is resolved in the way I hope
473 // we can just use the keysym directly with out looking
474 // at key at all. (Lgb)
477 // We migth have to add more keysyms here also,
478 // we will do that as the issues arise. (Lgb)
479 if (keysym == XK_space) {
481 lyxerr[Debug::KEY] << "Using keysym [A]"
486 // It seems that this was a bit optimistic...
487 // With this hacking things seems to be better (Lgb)
488 //if (!iscntrl(key)) {
490 // lyxerr[Debug::KEY]
491 // << "Using key [B]\n"
493 // << static_cast<unsigned char>(key)
496 ret_key = (keysym ? keysym : key);
497 lyxerr[Debug::KEY] << "Using keysym [B]"
503 unsigned int const ret_state = xke->state;
505 // If you have a better way to handle "wild-output" of
506 // characters after the key has been released than the one
507 // below, please contact me. (Lgb)
508 static Time last_time_pressed;
509 static unsigned int last_key_pressed;
510 static unsigned int last_state_pressed;
511 lyxerr[Debug::KEY] << "Workarea Diff: "
512 << xke->time - last_time_pressed
514 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
515 && ret_state == last_state_pressed
516 && xke->keycode == last_key_pressed) {
518 << "Workarea: Purging X events." << endl;
519 //lyxerr << "Workarea Events: "
520 // << XEventsQueued(fl_get_display(), QueuedAlready)
522 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
523 XSync(fl_get_display(), 1);
524 // This purge make f.ex. scrolling stop immidiatly when
525 // releasing the PageDown button. The question is if
526 // this purging of XEvents can cause any harm...
527 // after some testing I can see no problems, but
528 // I'd like other reports too.
531 last_time_pressed = xke->time;
532 last_key_pressed = xke->keycode;
533 last_state_pressed = ret_state;
535 area->workAreaKeyPress(ret_key, x_key_state(ret_state));
539 #if FL_VERSION > 0 || FL_REVISION >= 89
541 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
546 lyxerr[Debug::WORKAREA] << "Workarea event: FOCUS" << endl;
547 area->workAreaFocus();
550 lyxerr[Debug::WORKAREA] << "Workarea event: UNFOCUS" << endl;
551 area->workAreaUnfocus();
554 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
557 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
561 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
562 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
563 ev->xbutton.y - ob->y,
564 x_button_state(ev->xbutton.button));
568 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
569 area->workAreaTripleClick(ev->xbutton.x - ob->x,
570 ev->xbutton.y - ob->y,
571 x_button_state(ev->xbutton.button));
575 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
585 string clipboard_selection;
586 bool clipboard_read = false;
591 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
592 void const * data, long size)
594 clipboard_selection.erase();
597 clipboard_selection.reserve(size);
598 for (int i = 0; i < size; ++i)
599 clipboard_selection +=
600 static_cast<char const *>(data)[i];
601 clipboard_read = true;
610 int WorkArea::event_cb(XEvent * xev)
614 case SelectionRequest:
615 lyxerr[Debug::GUI] << "X requested selection." << endl;
616 selectionRequested();
619 lyxerr[Debug::GUI] << "Lost selection." << endl;
627 void WorkArea::haveSelection(bool yes) const
630 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
634 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
638 string const WorkArea::getClipboard() const
640 clipboard_read = false;
642 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
647 while (!clipboard_read) {
648 if (fl_check_forms() == FL_EVENT) {
650 lyxerr << "Received unhandled X11 event" << endl;
651 lyxerr << "Type: 0x" << hex << ev.xany.type <<
652 " Target: 0x" << hex << ev.xany.window << endl;
655 return clipboard_selection;
659 void WorkArea::putClipboard(string const & s) const
664 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);