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 *);
41 FL_OBJECT * figinset_canvas;
48 XSync(fl_get_display(), 0);
55 // Just a bunch of C wrappers around static members of WorkArea
57 void C_WorkArea_scroll_cb(FL_OBJECT * ob, long buf)
59 WorkArea::scroll_cb(ob, buf);
64 int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
68 return WorkArea::work_area_handler(ob, event,
73 int C_WorkAreaEventCB(FL_FORM * form, void * xev) {
74 WorkArea * wa=static_cast<WorkArea*>(form->u_vdata);
75 wa->event_cb(static_cast<XEvent*>(xev));
81 WorkArea::WorkArea(int xpos, int ypos, int width, int height)
82 : splash_(0), splash_text_(0), workareapixmap(0), painter_(*this)
84 fl_freeze_all_forms();
88 if (lyxerr.debugging(Debug::WORKAREA))
89 lyxerr << "Creating work area: +"
90 << xpos << '+' << ypos << ' '
91 << width << 'x' << height << endl;
94 int const bw = int(abs(fl_get_border_width()));
96 // We really want to get rid of figinset_canvas.
97 ::figinset_canvas = figinset_canvas = obj =
98 fl_add_canvas(FL_NORMAL_CANVAS,
99 xpos + 1, ypos + 1, 1, 1, "");
100 fl_set_object_boxtype(obj, FL_NO_BOX);
101 fl_set_object_resize(obj, FL_RESIZE_ALL);
102 fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
105 if (lyxerr.debugging(Debug::WORKAREA))
106 lyxerr << "\tbackground box: +"
107 << xpos << '+' << ypos << ' '
108 << width - 15 << 'x' << height << endl;
109 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
113 fl_set_object_resize(obj, FL_RESIZE_ALL);
114 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
116 // Add a splash screen to the centre of the work area
117 string const splash_file = (lyxrc.show_banner) ?
118 LibFileSearch("images", "banner", "xpm") : string();
120 if (!splash_file.empty()) {
121 int const splash_w = 425;
122 int const splash_h = 290;
123 int const splash_x = xpos + (width - 15 - splash_w) / 2;
124 int const splash_y = ypos + (height - splash_h) / 2;
126 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
128 splash_w, splash_h, "");
129 fl_set_pixmapbutton_file(obj, splash_file.c_str());
130 fl_set_pixmapbutton_focus_outline(obj, 3);
131 fl_set_object_boxtype(obj, FL_NO_BOX);
133 int const text_x = splash_x + 260;
134 int const text_y = splash_y + 255;
136 fl_add_text(FL_NORMAL_TEXT, text_x, text_y, 160, 16,
138 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
139 fl_mapcolor(FL_FREE_COL2, 0x2b, 0x47, 0x82);
140 fl_mapcolor(FL_FREE_COL3, 0xe1, 0xd2, 0x9b);
141 fl_set_object_color(obj, FL_FREE_COL2, FL_FREE_COL2);
142 fl_set_object_lcol(obj, FL_FREE_COL3);
143 fl_set_object_lalign(obj, FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
144 fl_set_object_lstyle(obj, FL_BOLD_STYLE);
151 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
153 ypos, 17, height, "");
154 fl_set_object_boxtype(obj, FL_UP_BOX);
155 fl_set_object_resize(obj, FL_RESIZE_ALL);
156 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
158 fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
163 // Create the workarea pixmap
164 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
166 // We add this object as late as possible to avoit problems
168 if (lyxerr.debugging(Debug::WORKAREA))
169 lyxerr << "\tfree object: +"
170 << xpos + bw << '+' << ypos + bw << ' '
171 << width - 15 - 2 * bw << 'x'
172 << height - 2 * bw << endl;
174 work_area = obj = fl_add_free(FL_ALL_FREE,
175 xpos + bw, ypos + bw,
176 width - 15 - 2 * bw, // scrollbarwidth
178 C_WorkArea_work_area_handler);
179 obj->wantkey = FL_KEY_ALL;
180 obj->u_vdata = this; /* This is how we pass the WorkArea
181 to the work_area_handler. */
182 fl_set_object_boxtype(obj,FL_DOWN_BOX);
183 fl_set_object_resize(obj, FL_RESIZE_ALL);
184 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
186 /// X selection hook - xforms gets it wrong
187 fl_current_form->u_vdata = this;
188 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_WorkAreaEventCB);
190 fl_unfreeze_all_forms();
194 WorkArea::~WorkArea()
197 XFreePixmap(fl_get_display(), workareapixmap);
201 bool WorkArea::belowMouse() const
205 fl_get_mouse(&x, &y, &button);
206 FL_Coord ulx = work_area->form->x + work_area->x;
207 FL_Coord uly = work_area->form->y + work_area->y;
208 FL_Coord w = work_area->w;
209 FL_Coord h = work_area->h;
210 if (x > ulx && y > uly && x < ulx + h && y < uly + w)
216 void WorkArea::resize(int xpos, int ypos, int width, int height)
218 fl_freeze_all_forms();
220 int const bw = int(abs(fl_get_border_width()));
223 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
228 fl_set_object_geometry(scrollbar, xpos + width - 15,
231 // Create the workarea pixmap
232 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
235 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
239 fl_unfreeze_all_forms();
244 void destroy_object(FL_OBJECT * obj)
252 fl_delete_object(obj);
258 void WorkArea::createPixmap(int width, int height)
260 // Three calls to createPixmap are needed to draw the initial view
261 // of LyX. Any more and the splash is destroyed.
262 static int counter = 0;
263 if (++counter == 4) {
264 destroy_object(splash_);
266 destroy_object(splash_text_);
270 static int cur_width = -1;
271 static int cur_height = -1;
273 if (cur_width == width && cur_height == height && workareapixmap)
280 XFreePixmap(fl_get_display(), workareapixmap);
282 if (lyxerr.debugging(Debug::WORKAREA))
283 lyxerr << "Creating pixmap ("
284 << width << 'x' << height << ")" << endl;
286 workareapixmap = XCreatePixmap(fl_get_display(),
287 RootWindow(fl_get_display(), 0),
290 fl_get_visual_depth());
291 if (lyxerr.debugging(Debug::WORKAREA))
292 lyxerr << "\tpixmap=" << workareapixmap << endl;
296 void WorkArea::greyOut() const
299 fl_winset(FL_ObjWin(work_area));
300 fl_rectangle(1, work_area->x, work_area->y,
301 work_area->w, work_area->h, FL_GRAY63);
306 void WorkArea::setFocus() const
308 fl_set_focus_object(work_area->form, work_area);
312 void WorkArea::setScrollbar(double pos, double length_fraction) const
314 fl_set_scrollbar_value(scrollbar, pos);
315 fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
319 void WorkArea::setScrollbarBounds(double l1, double l2) const
321 fl_set_scrollbar_bounds(scrollbar, l1, l2);
325 void WorkArea::setScrollbarIncrements(double inc) const
327 fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
331 // Callback for scrollbar slider
332 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
334 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
335 // If we really want the accellerating scroll we can do that
336 // from here. IMHO that is a waste of effort since we already
337 // have other ways to move fast around in the document. (Lgb)
338 area->scrollCB(fl_get_scrollbar_value(ob));
343 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
347 static int x_old = -1;
348 static int y_old = -1;
349 static long scrollbar_value_old = -1;
351 XEvent * ev = static_cast<XEvent*>(xev);
352 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
358 if (!area->work_area ||
359 !area->work_area->form->visible)
361 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
362 area->createPixmap(area->workWidth(), area->height());
363 area->workAreaExpose();
366 if (!ev || ev->xbutton.button == 0) break;
367 // Should really have used xbutton.state
368 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
369 area->workAreaButtonPress(ev->xbutton.x - ob->x,
370 ev->xbutton.y - ob->y,
372 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
375 if (!ev || ev->xbutton.button == 0) break;
376 // Should really have used xbutton.state
377 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
378 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
379 ev->xbutton.y - ob->y,
387 if (!ev || ! area->scrollbar) break;
388 if (ev->xmotion.x != x_old ||
389 ev->xmotion.y != y_old ||
390 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
392 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
393 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
394 ev->xmotion.y - ob->y,
404 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
408 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
409 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
410 // XForms < 0.89.5 does not have compose support
411 // so we are using our own compose support
412 LyXLookupString(ev, dummy, 1, &keysym);
414 XLookupString(xke, dummy, 1, &keysym, 0);
416 if (lyxerr.debugging(Debug::KEY)) {
417 char const * tmp = XKeysymToString(key);
418 char const * tmp2 = XKeysymToString(keysym);
419 string const stm = (tmp ? tmp : "");
420 string const stm2 = (tmp2 ? tmp2 : "");
422 lyxerr[Debug::KEY] << "WorkArea: Key is `" << stm << "' ["
423 << key << "]" << endl;
424 lyxerr[Debug::KEY] << "WorkArea: Keysym is `" << stm2 << "' ["
425 << keysym << "]" << endl;
428 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
429 if (keysym == NoSymbol) {
431 << "Empty kdb action (probably composing)"
435 KeySym ret_key = keysym;
437 // Note that we need this handling because of a bug
438 // in XForms 0.89, if this bug is resolved in the way I hope
439 // we can just use the keysym directly with out looking
440 // at key at all. (Lgb)
443 // We migth have to add more keysyms here also,
444 // we will do that as the issues arise. (Lgb)
445 if (keysym == XK_space) {
447 lyxerr[Debug::KEY] << "Using keysym [A]"
452 // It seems that this was a bit optimistic...
453 // With this hacking things seems to be better (Lgb)
454 //if (!iscntrl(key)) {
456 // lyxerr[Debug::KEY]
457 // << "Using key [B]\n"
459 // << static_cast<unsigned char>(key)
462 ret_key = (keysym ? keysym : key);
463 lyxerr[Debug::KEY] << "Using keysym [B]"
469 unsigned int const ret_state = xke->state;
471 // If you have a better way to handle "wild-output" of
472 // characters after the key has been released than the one
473 // below, please contact me. (Lgb)
474 static Time last_time_pressed;
475 static unsigned int last_key_pressed;
476 static unsigned int last_state_pressed;
477 lyxerr[Debug::KEY] << "Workarea Diff: "
478 << xke->time - last_time_pressed
480 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
481 && ret_state == last_state_pressed
482 && xke->keycode == last_key_pressed) {
484 << "Workarea: Purging X events." << endl;
485 //lyxerr << "Workarea Events: "
486 // << XEventsQueued(fl_get_display(), QueuedAlready)
488 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
489 XSync(fl_get_display(), 1);
490 // This purge make f.ex. scrolling stop immidiatly when
491 // releasing the PageDown button. The question is if
492 // this purging of XEvents can cause any harm...
493 // after some testing I can see no problems, but
494 // I'd like other reports too.
497 last_time_pressed = xke->time;
498 last_key_pressed = xke->keycode;
499 last_state_pressed = ret_state;
501 area->workAreaKeyPress(ret_key, ret_state);
505 #if FL_REVISION >= 89
507 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
512 lyxerr[Debug::WORKAREA] << "Workarea event: FOCUS" << endl;
513 area->workAreaFocus();
516 lyxerr[Debug::WORKAREA] << "Workarea event: UNFOCUS" << endl;
517 area->workAreaUnfocus();
520 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
521 area->workAreaEnter();
524 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
525 area->workAreaLeave();
529 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
530 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
531 ev->xbutton.y - ob->y,
536 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
537 area->workAreaTripleClick(ev->xbutton.x - ob->x,
538 ev->xbutton.y - ob->y,
543 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
553 string clipboard_selection;
554 bool clipboard_read = false;
559 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
560 void const * data, long size)
562 clipboard_selection.erase();
565 clipboard_selection.reserve(size);
566 for (int i = 0; i < size; ++i)
567 clipboard_selection +=
568 static_cast<char const *>(data)[i];
569 clipboard_read = true;
577 void WorkArea::event_cb(XEvent * xev)
579 if (xev->type != SelectionRequest)
582 selectionRequested.emit();
587 void WorkArea::haveSelection(bool yes) const
590 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
594 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
598 string const WorkArea::getClipboard() const
600 clipboard_read = false;
602 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
607 while (!clipboard_read) {
608 if (fl_check_forms() == FL_EVENT) {
610 lyxerr << "Received unhandled X11 event" << endl;
611 lyxerr << "Type: 0x" << std::hex << ev.xany.type <<
612 "Target: 0x" << std::hex << ev.xany.window << endl;
615 return clipboard_selection;
619 void WorkArea::putClipboard(string const & s) const
624 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);