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 wa->event_cb(static_cast<XEvent*>(xev));
80 WorkArea::WorkArea(int xpos, int ypos, int width, int height)
81 : splash_(0), splash_text_(0), workareapixmap(0), painter_(*this)
83 fl_freeze_all_forms();
85 if (lyxerr.debugging(Debug::WORKAREA))
86 lyxerr << "Creating work area: +"
87 << xpos << '+' << ypos << ' '
88 << width << 'x' << height << endl;
91 int const bw = int(abs(fl_get_border_width()));
94 if (lyxerr.debugging(Debug::WORKAREA))
95 lyxerr << "\tbackground box: +"
96 << xpos << '+' << ypos << ' '
97 << width - 15 << 'x' << height << endl;
98 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
102 fl_set_object_resize(obj, FL_RESIZE_ALL);
103 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
105 // Add a splash screen to the centre of the work area
106 string const splash_file = (lyxrc.show_banner) ?
107 LibFileSearch("images", "banner", "xpm") : string();
109 if (!splash_file.empty()) {
110 int const splash_w = 425;
111 int const splash_h = 290;
112 int const splash_x = xpos + (width - 15 - splash_w) / 2;
113 int const splash_y = ypos + (height - splash_h) / 2;
115 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
117 splash_w, splash_h, "");
118 fl_set_pixmapbutton_file(obj, splash_file.c_str());
119 fl_set_pixmapbutton_focus_outline(obj, 3);
120 fl_set_object_boxtype(obj, FL_NO_BOX);
122 int const text_x = splash_x + 260;
123 int const text_y = splash_y + 255;
125 fl_add_text(FL_NORMAL_TEXT, text_x, text_y, 160, 16,
127 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
128 fl_mapcolor(FL_FREE_COL2, 0x2b, 0x47, 0x82);
129 fl_mapcolor(FL_FREE_COL3, 0xe1, 0xd2, 0x9b);
130 fl_set_object_color(obj, FL_FREE_COL2, FL_FREE_COL2);
131 fl_set_object_lcol(obj, FL_FREE_COL3);
132 fl_set_object_lalign(obj, FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
133 fl_set_object_lstyle(obj, FL_BOLD_STYLE);
140 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
142 ypos, 17, height, "");
143 fl_set_object_boxtype(obj, FL_UP_BOX);
144 fl_set_object_resize(obj, FL_RESIZE_ALL);
145 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
147 fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
148 setScrollbarBounds(0.0, 0.0);
153 // Create the workarea pixmap
154 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
156 // We add this object as late as possible to avoit problems
158 if (lyxerr.debugging(Debug::WORKAREA))
159 lyxerr << "\tfree object: +"
160 << xpos + bw << '+' << ypos + bw << ' '
161 << width - 15 - 2 * bw << 'x'
162 << height - 2 * bw << endl;
164 work_area = obj = fl_add_free(FL_ALL_FREE,
165 xpos + bw, ypos + bw,
166 width - 15 - 2 * bw, // scrollbarwidth
168 C_WorkArea_work_area_handler);
169 obj->wantkey = FL_KEY_ALL;
170 obj->u_vdata = this; /* This is how we pass the WorkArea
171 to the work_area_handler. */
172 fl_set_object_boxtype(obj,FL_DOWN_BOX);
173 fl_set_object_resize(obj, FL_RESIZE_ALL);
174 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
176 /// X selection hook - xforms gets it wrong
177 fl_current_form->u_vdata = this;
178 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_WorkAreaEventCB);
180 fl_unfreeze_all_forms();
184 WorkArea::~WorkArea()
187 XFreePixmap(fl_get_display(), workareapixmap);
191 bool WorkArea::belowMouse() const
195 fl_get_mouse(&x, &y, &button);
196 FL_Coord ulx = work_area->form->x + work_area->x;
197 FL_Coord uly = work_area->form->y + work_area->y;
198 FL_Coord w = work_area->w;
199 FL_Coord h = work_area->h;
200 if (x > ulx && y > uly && x < ulx + h && y < uly + w)
206 void WorkArea::resize(int xpos, int ypos, int width, int height)
208 fl_freeze_all_forms();
210 int const bw = int(abs(fl_get_border_width()));
213 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
218 fl_set_object_geometry(scrollbar, xpos + width - 15,
221 // Create the workarea pixmap
222 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
225 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
229 fl_unfreeze_all_forms();
234 void destroy_object(FL_OBJECT * obj)
242 fl_delete_object(obj);
248 void WorkArea::createPixmap(int width, int height)
250 // Three calls to createPixmap are needed to draw the initial view
251 // of LyX. Any more and the splash is destroyed.
252 static int counter = 0;
253 if (++counter == 4) {
254 destroy_object(splash_);
256 destroy_object(splash_text_);
260 static int cur_width = -1;
261 static int cur_height = -1;
263 if (cur_width == width && cur_height == height && workareapixmap)
270 XFreePixmap(fl_get_display(), workareapixmap);
272 if (lyxerr.debugging(Debug::WORKAREA))
273 lyxerr << "Creating pixmap ("
274 << width << 'x' << height << ")" << endl;
276 workareapixmap = XCreatePixmap(fl_get_display(),
277 RootWindow(fl_get_display(), 0),
280 fl_get_visual_depth());
281 if (lyxerr.debugging(Debug::WORKAREA))
282 lyxerr << "\tpixmap=" << workareapixmap << endl;
286 void WorkArea::greyOut() const
289 fl_winset(FL_ObjWin(work_area));
290 fl_rectangle(1, work_area->x, work_area->y,
291 work_area->w, work_area->h, FL_GRAY63);
296 void WorkArea::setFocus() const
298 fl_set_focus_object(work_area->form, work_area);
302 void WorkArea::setScrollbar(double pos, double length_fraction) const
304 fl_set_scrollbar_value(scrollbar, pos);
305 fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
309 void WorkArea::setScrollbarBounds(double l1, double l2) const
311 fl_set_scrollbar_bounds(scrollbar, l1, l2);
315 void WorkArea::setScrollbarIncrements(double inc) const
317 fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
321 // Callback for scrollbar slider
322 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
324 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
325 // If we really want the accellerating scroll we can do that
326 // from here. IMHO that is a waste of effort since we already
327 // have other ways to move fast around in the document. (Lgb)
328 area->scrollCB(fl_get_scrollbar_value(ob));
333 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
337 static int x_old = -1;
338 static int y_old = -1;
339 static long scrollbar_value_old = -1;
341 XEvent * ev = static_cast<XEvent*>(xev);
342 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
348 if (!area->work_area ||
349 !area->work_area->form->visible)
351 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
352 area->createPixmap(area->workWidth(), area->height());
353 area->workAreaExpose();
356 if (!ev || ev->xbutton.button == 0) break;
357 // Should really have used xbutton.state
358 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
359 area->workAreaButtonPress(ev->xbutton.x - ob->x,
360 ev->xbutton.y - ob->y,
362 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
365 if (!ev || ev->xbutton.button == 0) break;
366 // Should really have used xbutton.state
367 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
368 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
369 ev->xbutton.y - ob->y,
377 if (!ev || ! area->scrollbar) break;
378 if (ev->xmotion.x != x_old ||
379 ev->xmotion.y != y_old ||
380 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
382 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
383 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
384 ev->xmotion.y - ob->y,
394 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
398 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
399 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
400 // XForms < 0.89.5 does not have compose support
401 // so we are using our own compose support
402 LyXLookupString(ev, dummy, 1, &keysym);
404 XLookupString(xke, dummy, 1, &keysym, 0);
406 if (lyxerr.debugging(Debug::KEY)) {
407 char const * tmp = XKeysymToString(key);
408 char const * tmp2 = XKeysymToString(keysym);
409 string const stm = (tmp ? tmp : "");
410 string const stm2 = (tmp2 ? tmp2 : "");
412 lyxerr[Debug::KEY] << "WorkArea: Key is `" << stm << "' ["
413 << key << "]" << endl;
414 lyxerr[Debug::KEY] << "WorkArea: Keysym is `" << stm2 << "' ["
415 << keysym << "]" << endl;
418 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
419 if (keysym == NoSymbol) {
421 << "Empty kdb action (probably composing)"
425 KeySym ret_key = keysym;
427 // Note that we need this handling because of a bug
428 // in XForms 0.89, if this bug is resolved in the way I hope
429 // we can just use the keysym directly with out looking
430 // at key at all. (Lgb)
433 // We migth have to add more keysyms here also,
434 // we will do that as the issues arise. (Lgb)
435 if (keysym == XK_space) {
437 lyxerr[Debug::KEY] << "Using keysym [A]"
442 // It seems that this was a bit optimistic...
443 // With this hacking things seems to be better (Lgb)
444 //if (!iscntrl(key)) {
446 // lyxerr[Debug::KEY]
447 // << "Using key [B]\n"
449 // << static_cast<unsigned char>(key)
452 ret_key = (keysym ? keysym : key);
453 lyxerr[Debug::KEY] << "Using keysym [B]"
459 unsigned int const ret_state = xke->state;
461 // If you have a better way to handle "wild-output" of
462 // characters after the key has been released than the one
463 // below, please contact me. (Lgb)
464 static Time last_time_pressed;
465 static unsigned int last_key_pressed;
466 static unsigned int last_state_pressed;
467 lyxerr[Debug::KEY] << "Workarea Diff: "
468 << xke->time - last_time_pressed
470 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
471 && ret_state == last_state_pressed
472 && xke->keycode == last_key_pressed) {
474 << "Workarea: Purging X events." << endl;
475 //lyxerr << "Workarea Events: "
476 // << XEventsQueued(fl_get_display(), QueuedAlready)
478 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
479 XSync(fl_get_display(), 1);
480 // This purge make f.ex. scrolling stop immidiatly when
481 // releasing the PageDown button. The question is if
482 // this purging of XEvents can cause any harm...
483 // after some testing I can see no problems, but
484 // I'd like other reports too.
487 last_time_pressed = xke->time;
488 last_key_pressed = xke->keycode;
489 last_state_pressed = ret_state;
491 area->workAreaKeyPress(ret_key, ret_state);
495 #if FL_REVISION >= 89
497 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
502 lyxerr[Debug::WORKAREA] << "Workarea event: FOCUS" << endl;
503 area->workAreaFocus();
506 lyxerr[Debug::WORKAREA] << "Workarea event: UNFOCUS" << endl;
507 area->workAreaUnfocus();
510 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
511 area->workAreaEnter();
514 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
515 area->workAreaLeave();
519 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
520 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
521 ev->xbutton.y - ob->y,
526 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
527 area->workAreaTripleClick(ev->xbutton.x - ob->x,
528 ev->xbutton.y - ob->y,
533 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
543 string clipboard_selection;
544 bool clipboard_read = false;
549 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
550 void const * data, long size)
552 clipboard_selection.erase();
555 clipboard_selection.reserve(size);
556 for (int i = 0; i < size; ++i)
557 clipboard_selection +=
558 static_cast<char const *>(data)[i];
559 clipboard_read = true;
567 void WorkArea::event_cb(XEvent * xev)
570 case SelectionRequest:
571 lyxerr[Debug::GUI] << "X requested selection." << endl;
572 selectionRequested.emit();
575 lyxerr[Debug::GUI] << "Lost selection." << endl;
576 selectionLost.emit();
582 void WorkArea::haveSelection(bool yes) const
585 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
589 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
593 string const WorkArea::getClipboard() const
595 clipboard_read = false;
597 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
602 while (!clipboard_read) {
603 if (fl_check_forms() == FL_EVENT) {
605 lyxerr << "Received unhandled X11 event" << endl;
606 lyxerr << "Type: 0x" << hex << ev.xany.type <<
607 " Target: 0x" << hex << ev.xany.window << endl;
610 return clipboard_selection;
614 void WorkArea::putClipboard(string const & s) const
619 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);