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 return wa->event_cb(static_cast<XEvent*>(xev));
79 WorkArea::WorkArea(int xpos, int ypos, int width, int height)
80 : splash_(0), splash_text_(0), workareapixmap(0), painter_(*this)
82 fl_freeze_all_forms();
84 if (lyxerr.debugging(Debug::WORKAREA))
85 lyxerr << "Creating work area: +"
86 << xpos << '+' << ypos << ' '
87 << width << 'x' << height << endl;
90 int const bw = int(abs(fl_get_border_width()));
93 if (lyxerr.debugging(Debug::WORKAREA))
94 lyxerr << "\tbackground box: +"
95 << xpos << '+' << ypos << ' '
96 << width - 15 << 'x' << height << endl;
97 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
101 fl_set_object_resize(obj, FL_RESIZE_ALL);
102 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
104 // Add a splash screen to the centre of the work area
105 string const splash_file = (lyxrc.show_banner) ?
106 LibFileSearch("images", "banner", "xpm") : string();
108 if (!splash_file.empty()) {
109 int const splash_w = 425;
110 int const splash_h = 290;
111 int const splash_x = xpos + (width - 15 - splash_w) / 2;
112 int const splash_y = ypos + (height - splash_h) / 2;
114 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
116 splash_w, splash_h, "");
117 fl_set_pixmapbutton_file(obj, splash_file.c_str());
118 fl_set_pixmapbutton_focus_outline(obj, 3);
119 fl_set_object_boxtype(obj, FL_NO_BOX);
121 int const text_x = splash_x + 260;
122 int const text_y = splash_y + 255;
124 fl_add_text(FL_NORMAL_TEXT, text_x, text_y, 160, 16,
126 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
127 fl_mapcolor(FL_FREE_COL2, 0x2b, 0x47, 0x82);
128 fl_mapcolor(FL_FREE_COL3, 0xe1, 0xd2, 0x9b);
129 fl_set_object_color(obj, FL_FREE_COL2, FL_FREE_COL2);
130 fl_set_object_lcol(obj, FL_FREE_COL3);
131 fl_set_object_lalign(obj, FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
132 fl_set_object_lstyle(obj, FL_BOLD_STYLE);
139 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
141 ypos, 17, height, "");
142 fl_set_object_boxtype(obj, FL_UP_BOX);
143 fl_set_object_resize(obj, FL_RESIZE_ALL);
144 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
146 fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
147 setScrollbarBounds(0.0, 0.0);
152 // Create the workarea pixmap
153 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
155 // We add this object as late as possible to avoit problems
157 if (lyxerr.debugging(Debug::WORKAREA))
158 lyxerr << "\tfree object: +"
159 << xpos + bw << '+' << ypos + bw << ' '
160 << width - 15 - 2 * bw << 'x'
161 << height - 2 * bw << endl;
163 work_area = obj = fl_add_free(FL_ALL_FREE,
164 xpos + bw, ypos + bw,
165 width - 15 - 2 * bw, // scrollbarwidth
167 C_WorkArea_work_area_handler);
168 obj->wantkey = FL_KEY_ALL;
169 obj->u_vdata = this; /* This is how we pass the WorkArea
170 to the work_area_handler. */
171 fl_set_object_boxtype(obj,FL_DOWN_BOX);
172 fl_set_object_resize(obj, FL_RESIZE_ALL);
173 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
175 /// X selection hook - xforms gets it wrong
176 fl_current_form->u_vdata = this;
177 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_WorkAreaEventCB);
179 fl_unfreeze_all_forms();
183 WorkArea::~WorkArea()
186 XFreePixmap(fl_get_display(), workareapixmap);
190 bool WorkArea::belowMouse() const
194 fl_get_mouse(&x, &y, &button);
195 FL_Coord ulx = work_area->form->x + work_area->x;
196 FL_Coord uly = work_area->form->y + work_area->y;
197 FL_Coord w = work_area->w;
198 FL_Coord h = work_area->h;
199 if (x > ulx && y > uly && x < ulx + h && y < uly + w)
205 void WorkArea::resize(int xpos, int ypos, int width, int height)
207 fl_freeze_all_forms();
209 int const bw = int(abs(fl_get_border_width()));
212 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
217 fl_set_object_geometry(scrollbar, xpos + width - 15,
220 // Create the workarea pixmap
221 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
224 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
228 fl_unfreeze_all_forms();
233 void destroy_object(FL_OBJECT * obj)
241 fl_delete_object(obj);
247 void WorkArea::createPixmap(int width, int height)
249 // Three calls to createPixmap are needed to draw the initial view
250 // of LyX. Any more and the splash is destroyed.
251 static int counter = 0;
252 if (++counter == 4) {
253 destroy_object(splash_);
255 destroy_object(splash_text_);
259 static int cur_width = -1;
260 static int cur_height = -1;
262 if (cur_width == width && cur_height == height && workareapixmap)
269 XFreePixmap(fl_get_display(), workareapixmap);
271 if (lyxerr.debugging(Debug::WORKAREA))
272 lyxerr << "Creating pixmap ("
273 << width << 'x' << height << ")" << endl;
275 workareapixmap = XCreatePixmap(fl_get_display(),
276 RootWindow(fl_get_display(), 0),
279 fl_get_visual_depth());
280 if (lyxerr.debugging(Debug::WORKAREA))
281 lyxerr << "\tpixmap=" << workareapixmap << endl;
285 void WorkArea::greyOut() const
288 fl_winset(FL_ObjWin(work_area));
289 fl_rectangle(1, work_area->x, work_area->y,
290 work_area->w, work_area->h, FL_GRAY63);
295 void WorkArea::setFocus() const
297 fl_set_focus_object(work_area->form, work_area);
301 void WorkArea::setScrollbar(double pos, double length_fraction) const
303 fl_set_scrollbar_value(scrollbar, pos);
304 fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
308 void WorkArea::setScrollbarBounds(double l1, double l2) const
310 fl_set_scrollbar_bounds(scrollbar, l1, l2);
314 void WorkArea::setScrollbarIncrements(double inc) const
316 fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
320 // Callback for scrollbar slider
321 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
323 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
324 // If we really want the accellerating scroll we can do that
325 // from here. IMHO that is a waste of effort since we already
326 // have other ways to move fast around in the document. (Lgb)
327 area->scrollCB(fl_get_scrollbar_value(ob));
332 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
336 static int x_old = -1;
337 static int y_old = -1;
338 static long scrollbar_value_old = -1;
340 XEvent * ev = static_cast<XEvent*>(xev);
341 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
347 if (!area->work_area ||
348 !area->work_area->form->visible)
350 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
351 area->createPixmap(area->workWidth(), area->height());
352 area->workAreaExpose();
355 if (!ev || ev->xbutton.button == 0) break;
356 // Should really have used xbutton.state
357 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
358 area->workAreaButtonPress(ev->xbutton.x - ob->x,
359 ev->xbutton.y - ob->y,
361 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
364 if (!ev || ev->xbutton.button == 0) break;
365 // Should really have used xbutton.state
366 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
367 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
368 ev->xbutton.y - ob->y,
376 if (!ev || ! area->scrollbar) break;
377 if (ev->xmotion.x != x_old ||
378 ev->xmotion.y != y_old ||
379 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
381 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
382 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
383 ev->xmotion.y - ob->y,
393 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
397 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
398 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
399 // XForms < 0.89.5 does not have compose support
400 // so we are using our own compose support
401 LyXLookupString(ev, dummy, 1, &keysym);
403 XLookupString(xke, dummy, 1, &keysym, 0);
404 // int num_keys = XLookupString(xke, dummy, 10, &keysym, &xcs);
405 // lyxerr << "We have " << num_keys << " keys in the returned buffer" << endl;
406 // lyxerr << "Our dummy string is " << dummy << endl;
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);