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
20 #include "lyxrc.h" // lyxrc.show_banner
21 #include "version.h" // lyx_version
23 #if 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);
54 // Just a bunch of C wrappers around static members of WorkArea
56 void C_WorkArea_scroll_cb(FL_OBJECT * ob, long buf)
58 WorkArea::scroll_cb(ob, buf);
63 int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
67 return WorkArea::work_area_handler(ob, event,
72 int C_WorkAreaEventCB(FL_FORM * form, void * xev) {
73 WorkArea * wa = static_cast<WorkArea*>(form->u_vdata);
74 int ret = wa->event_cb(static_cast<XEvent*>(xev));
75 lyxerr << "Pending: " << XPending(fl_get_display()) << endl;
82 WorkArea::WorkArea(int xpos, int ypos, int width, int height)
83 : splash_(0), splash_text_(0), workareapixmap(0), painter_(*this)
85 fl_freeze_all_forms();
87 if (lyxerr.debugging(Debug::WORKAREA))
88 lyxerr << "Creating work area: +"
89 << xpos << '+' << ypos << ' '
90 << width << 'x' << height << endl;
93 int const bw = int(abs(fl_get_border_width()));
96 if (lyxerr.debugging(Debug::WORKAREA))
97 lyxerr << "\tbackground box: +"
98 << xpos << '+' << ypos << ' '
99 << width - 15 << 'x' << height << endl;
100 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
104 fl_set_object_resize(obj, FL_RESIZE_ALL);
105 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
107 // Add a splash screen to the centre of the work area
108 string const splash_file = (lyxrc.show_banner) ?
109 LibFileSearch("images", "banner", "xpm") : string();
111 if (!splash_file.empty()) {
112 int const splash_w = 425;
113 int const splash_h = 290;
114 int const splash_x = xpos + (width - 15 - splash_w) / 2;
115 int const splash_y = ypos + (height - splash_h) / 2;
117 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
119 splash_w, splash_h, "");
120 fl_set_pixmapbutton_file(obj, splash_file.c_str());
121 fl_set_pixmapbutton_focus_outline(obj, 3);
122 fl_set_object_boxtype(obj, FL_NO_BOX);
124 int const text_x = splash_x + 260;
125 int const text_y = splash_y + 255;
127 fl_add_text(FL_NORMAL_TEXT, text_x, text_y, 160, 16,
129 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
130 fl_mapcolor(FL_FREE_COL2, 0x2b, 0x47, 0x82);
131 fl_mapcolor(FL_FREE_COL3, 0xe1, 0xd2, 0x9b);
132 fl_set_object_color(obj, FL_FREE_COL2, FL_FREE_COL2);
133 fl_set_object_lcol(obj, FL_FREE_COL3);
134 fl_set_object_lalign(obj, FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
135 fl_set_object_lstyle(obj, FL_BOLD_STYLE);
142 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
144 ypos, 17, height, "");
145 fl_set_object_boxtype(obj, FL_UP_BOX);
146 fl_set_object_resize(obj, FL_RESIZE_ALL);
147 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
149 fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
150 setScrollbarBounds(0.0, 0.0);
155 // Create the workarea pixmap
156 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
158 // We add this object as late as possible to avoit problems
160 if (lyxerr.debugging(Debug::WORKAREA))
161 lyxerr << "\tfree object: +"
162 << xpos + bw << '+' << ypos + bw << ' '
163 << width - 15 - 2 * bw << 'x'
164 << height - 2 * bw << endl;
166 work_area = obj = fl_add_free(FL_ALL_FREE,
167 xpos + bw, ypos + bw,
168 width - 15 - 2 * bw, // scrollbarwidth
170 C_WorkArea_work_area_handler);
171 obj->wantkey = FL_KEY_ALL;
172 obj->u_vdata = this; /* This is how we pass the WorkArea
173 to the work_area_handler. */
174 fl_set_object_boxtype(obj,FL_DOWN_BOX);
175 fl_set_object_resize(obj, FL_RESIZE_ALL);
176 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
178 /// X selection hook - xforms gets it wrong
179 fl_current_form->u_vdata = this;
180 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_WorkAreaEventCB);
182 fl_unfreeze_all_forms();
186 WorkArea::~WorkArea()
189 XFreePixmap(fl_get_display(), workareapixmap);
193 bool WorkArea::belowMouse() const
197 fl_get_mouse(&x, &y, &button);
198 FL_Coord ulx = work_area->form->x + work_area->x;
199 FL_Coord uly = work_area->form->y + work_area->y;
200 FL_Coord w = work_area->w;
201 FL_Coord h = work_area->h;
202 if (x > ulx && y > uly && x < ulx + h && y < uly + w)
208 void WorkArea::resize(int xpos, int ypos, int width, int height)
210 fl_freeze_all_forms();
212 int const bw = int(abs(fl_get_border_width()));
215 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
220 fl_set_object_geometry(scrollbar, xpos + width - 15,
223 // Create the workarea pixmap
224 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
227 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
231 fl_unfreeze_all_forms();
236 void destroy_object(FL_OBJECT * obj)
244 fl_delete_object(obj);
250 void WorkArea::createPixmap(int width, int height)
252 // Three calls to createPixmap are needed to draw the initial view
253 // of LyX. Any more and the splash is destroyed.
254 static int counter = 0;
255 if (++counter == 4) {
256 destroy_object(splash_);
258 destroy_object(splash_text_);
262 static int cur_width = -1;
263 static int cur_height = -1;
265 if (cur_width == width && cur_height == height && workareapixmap)
272 XFreePixmap(fl_get_display(), workareapixmap);
274 if (lyxerr.debugging(Debug::WORKAREA))
275 lyxerr << "Creating pixmap ("
276 << width << 'x' << height << ")" << endl;
278 workareapixmap = XCreatePixmap(fl_get_display(),
279 RootWindow(fl_get_display(), 0),
282 fl_get_visual_depth());
283 if (lyxerr.debugging(Debug::WORKAREA))
284 lyxerr << "\tpixmap=" << workareapixmap << endl;
288 void WorkArea::greyOut() const
291 fl_winset(FL_ObjWin(work_area));
292 fl_rectangle(1, work_area->x, work_area->y,
293 work_area->w, work_area->h, FL_GRAY63);
298 void WorkArea::setFocus() const
300 fl_set_focus_object(work_area->form, work_area);
304 void WorkArea::setScrollbar(double pos, double length_fraction) const
306 fl_set_scrollbar_value(scrollbar, pos);
307 fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
311 void WorkArea::setScrollbarBounds(double l1, double l2) const
313 fl_set_scrollbar_bounds(scrollbar, l1, l2);
317 void WorkArea::setScrollbarIncrements(double inc) const
319 fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
323 // Callback for scrollbar slider
324 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
326 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
327 // If we really want the accellerating scroll we can do that
328 // from here. IMHO that is a waste of effort since we already
329 // have other ways to move fast around in the document. (Lgb)
330 area->scrollCB(fl_get_scrollbar_value(ob));
335 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
339 static int x_old = -1;
340 static int y_old = -1;
341 static long scrollbar_value_old = -1;
343 XEvent * ev = static_cast<XEvent*>(xev);
344 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
350 if (!area->work_area ||
351 !area->work_area->form->visible)
353 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
354 area->createPixmap(area->workWidth(), area->height());
355 area->workAreaExpose();
358 if (!ev || ev->xbutton.button == 0) break;
359 // Should really have used xbutton.state
360 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
361 area->workAreaButtonPress(ev->xbutton.x - ob->x,
362 ev->xbutton.y - ob->y,
364 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
367 if (!ev || ev->xbutton.button == 0) break;
368 // Should really have used xbutton.state
369 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
370 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
371 ev->xbutton.y - ob->y,
379 if (!ev || ! area->scrollbar) break;
380 if (ev->xmotion.x != x_old ||
381 ev->xmotion.y != y_old ||
382 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
384 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
385 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
386 ev->xmotion.y - ob->y,
396 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
400 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
401 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
402 // XForms < 0.89.5 does not have compose support
403 // so we are using our own compose support
404 LyXLookupString(ev, dummy, 1, &keysym);
406 XLookupString(xke, dummy, 1, &keysym, 0);
408 if (lyxerr.debugging(Debug::KEY)) {
409 char const * tmp = XKeysymToString(key);
410 char const * tmp2 = XKeysymToString(keysym);
411 string const stm = (tmp ? tmp : "");
412 string const stm2 = (tmp2 ? tmp2 : "");
414 lyxerr[Debug::KEY] << "WorkArea: Key is `" << stm << "' ["
415 << key << "]" << endl;
416 lyxerr[Debug::KEY] << "WorkArea: Keysym is `" << stm2 << "' ["
417 << keysym << "]" << endl;
420 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
421 if (keysym == NoSymbol) {
423 << "Empty kdb action (probably composing)"
427 KeySym ret_key = keysym;
429 // Note that we need this handling because of a bug
430 // in XForms 0.89, if this bug is resolved in the way I hope
431 // we can just use the keysym directly with out looking
432 // at key at all. (Lgb)
435 // We migth have to add more keysyms here also,
436 // we will do that as the issues arise. (Lgb)
437 if (keysym == XK_space) {
439 lyxerr[Debug::KEY] << "Using keysym [A]"
444 // It seems that this was a bit optimistic...
445 // With this hacking things seems to be better (Lgb)
446 //if (!iscntrl(key)) {
448 // lyxerr[Debug::KEY]
449 // << "Using key [B]\n"
451 // << static_cast<unsigned char>(key)
454 ret_key = (keysym ? keysym : key);
455 lyxerr[Debug::KEY] << "Using keysym [B]"
461 unsigned int const ret_state = xke->state;
463 // If you have a better way to handle "wild-output" of
464 // characters after the key has been released than the one
465 // below, please contact me. (Lgb)
466 static Time last_time_pressed;
467 static unsigned int last_key_pressed;
468 static unsigned int last_state_pressed;
469 lyxerr[Debug::KEY] << "Workarea Diff: "
470 << xke->time - last_time_pressed
472 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
473 && ret_state == last_state_pressed
474 && xke->keycode == last_key_pressed) {
476 << "Workarea: Purging X events." << endl;
477 //lyxerr << "Workarea Events: "
478 // << XEventsQueued(fl_get_display(), QueuedAlready)
480 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
481 XSync(fl_get_display(), 1);
482 // This purge make f.ex. scrolling stop immidiatly when
483 // releasing the PageDown button. The question is if
484 // this purging of XEvents can cause any harm...
485 // after some testing I can see no problems, but
486 // I'd like other reports too.
489 last_time_pressed = xke->time;
490 last_key_pressed = xke->keycode;
491 last_state_pressed = ret_state;
493 area->workAreaKeyPress(ret_key, ret_state);
497 #if FL_REVISION >= 89
499 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
504 lyxerr[Debug::WORKAREA] << "Workarea event: FOCUS" << endl;
505 area->workAreaFocus();
508 lyxerr[Debug::WORKAREA] << "Workarea event: UNFOCUS" << endl;
509 area->workAreaUnfocus();
512 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
513 area->workAreaEnter();
516 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
517 area->workAreaLeave();
521 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
522 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
523 ev->xbutton.y - ob->y,
528 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
529 area->workAreaTripleClick(ev->xbutton.x - ob->x,
530 ev->xbutton.y - ob->y,
535 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
545 string clipboard_selection;
546 bool clipboard_read = false;
551 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
552 void const * data, long size)
554 clipboard_selection.erase();
557 clipboard_selection.reserve(size);
558 for (int i = 0; i < size; ++i)
559 clipboard_selection +=
560 static_cast<char const *>(data)[i];
561 clipboard_read = true;
570 int WorkArea::event_cb(XEvent * xev)
574 case SelectionRequest:
575 lyxerr[Debug::GUI] << "X requested selection." << endl;
576 selectionRequested.emit();
580 lyxerr[Debug::GUI] << "Lost selection." << endl;
581 selectionLost.emit();
589 void WorkArea::haveSelection(bool yes) const
592 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
596 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
600 string const WorkArea::getClipboard() const
602 clipboard_read = false;
604 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
609 while (!clipboard_read) {
610 if (fl_check_forms() == FL_EVENT) {
612 lyxerr << "Received unhandled X11 event" << endl;
613 lyxerr << "Type: 0x" << hex << ev.xany.type <<
614 " Target: 0x" << hex << ev.xany.window << endl;
617 return clipboard_selection;
621 void WorkArea::putClipboard(string const & s) const
626 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);