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);
159 setScrollbarBounds(0.0, 0.0);
164 // Create the workarea pixmap
165 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
167 // We add this object as late as possible to avoit problems
169 if (lyxerr.debugging(Debug::WORKAREA))
170 lyxerr << "\tfree object: +"
171 << xpos + bw << '+' << ypos + bw << ' '
172 << width - 15 - 2 * bw << 'x'
173 << height - 2 * bw << endl;
175 work_area = obj = fl_add_free(FL_ALL_FREE,
176 xpos + bw, ypos + bw,
177 width - 15 - 2 * bw, // scrollbarwidth
179 C_WorkArea_work_area_handler);
180 obj->wantkey = FL_KEY_ALL;
181 obj->u_vdata = this; /* This is how we pass the WorkArea
182 to the work_area_handler. */
183 fl_set_object_boxtype(obj,FL_DOWN_BOX);
184 fl_set_object_resize(obj, FL_RESIZE_ALL);
185 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
187 /// X selection hook - xforms gets it wrong
188 fl_current_form->u_vdata = this;
189 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_WorkAreaEventCB);
191 fl_unfreeze_all_forms();
195 WorkArea::~WorkArea()
198 XFreePixmap(fl_get_display(), workareapixmap);
202 bool WorkArea::belowMouse() const
206 fl_get_mouse(&x, &y, &button);
207 FL_Coord ulx = work_area->form->x + work_area->x;
208 FL_Coord uly = work_area->form->y + work_area->y;
209 FL_Coord w = work_area->w;
210 FL_Coord h = work_area->h;
211 if (x > ulx && y > uly && x < ulx + h && y < uly + w)
217 void WorkArea::resize(int xpos, int ypos, int width, int height)
219 fl_freeze_all_forms();
221 int const bw = int(abs(fl_get_border_width()));
224 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
229 fl_set_object_geometry(scrollbar, xpos + width - 15,
232 // Create the workarea pixmap
233 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
236 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
240 fl_unfreeze_all_forms();
245 void destroy_object(FL_OBJECT * obj)
253 fl_delete_object(obj);
259 void WorkArea::createPixmap(int width, int height)
261 // Three calls to createPixmap are needed to draw the initial view
262 // of LyX. Any more and the splash is destroyed.
263 static int counter = 0;
264 if (++counter == 4) {
265 destroy_object(splash_);
267 destroy_object(splash_text_);
271 static int cur_width = -1;
272 static int cur_height = -1;
274 if (cur_width == width && cur_height == height && workareapixmap)
281 XFreePixmap(fl_get_display(), workareapixmap);
283 if (lyxerr.debugging(Debug::WORKAREA))
284 lyxerr << "Creating pixmap ("
285 << width << 'x' << height << ")" << endl;
287 workareapixmap = XCreatePixmap(fl_get_display(),
288 RootWindow(fl_get_display(), 0),
291 fl_get_visual_depth());
292 if (lyxerr.debugging(Debug::WORKAREA))
293 lyxerr << "\tpixmap=" << workareapixmap << endl;
297 void WorkArea::greyOut() const
300 fl_winset(FL_ObjWin(work_area));
301 fl_rectangle(1, work_area->x, work_area->y,
302 work_area->w, work_area->h, FL_GRAY63);
307 void WorkArea::setFocus() const
309 fl_set_focus_object(work_area->form, work_area);
313 void WorkArea::setScrollbar(double pos, double length_fraction) const
315 fl_set_scrollbar_value(scrollbar, pos);
316 fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
320 void WorkArea::setScrollbarBounds(double l1, double l2) const
322 fl_set_scrollbar_bounds(scrollbar, l1, l2);
326 void WorkArea::setScrollbarIncrements(double inc) const
328 fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
332 // Callback for scrollbar slider
333 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
335 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
336 // If we really want the accellerating scroll we can do that
337 // from here. IMHO that is a waste of effort since we already
338 // have other ways to move fast around in the document. (Lgb)
339 area->scrollCB(fl_get_scrollbar_value(ob));
344 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
348 static int x_old = -1;
349 static int y_old = -1;
350 static long scrollbar_value_old = -1;
352 XEvent * ev = static_cast<XEvent*>(xev);
353 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
359 if (!area->work_area ||
360 !area->work_area->form->visible)
362 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
363 area->createPixmap(area->workWidth(), area->height());
364 area->workAreaExpose();
367 if (!ev || ev->xbutton.button == 0) break;
368 // Should really have used xbutton.state
369 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
370 area->workAreaButtonPress(ev->xbutton.x - ob->x,
371 ev->xbutton.y - ob->y,
373 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
376 if (!ev || ev->xbutton.button == 0) break;
377 // Should really have used xbutton.state
378 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
379 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
380 ev->xbutton.y - ob->y,
388 if (!ev || ! area->scrollbar) break;
389 if (ev->xmotion.x != x_old ||
390 ev->xmotion.y != y_old ||
391 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
393 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
394 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
395 ev->xmotion.y - ob->y,
405 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
409 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
410 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
411 // XForms < 0.89.5 does not have compose support
412 // so we are using our own compose support
413 LyXLookupString(ev, dummy, 1, &keysym);
415 XLookupString(xke, dummy, 1, &keysym, 0);
417 if (lyxerr.debugging(Debug::KEY)) {
418 char const * tmp = XKeysymToString(key);
419 char const * tmp2 = XKeysymToString(keysym);
420 string const stm = (tmp ? tmp : "");
421 string const stm2 = (tmp2 ? tmp2 : "");
423 lyxerr[Debug::KEY] << "WorkArea: Key is `" << stm << "' ["
424 << key << "]" << endl;
425 lyxerr[Debug::KEY] << "WorkArea: Keysym is `" << stm2 << "' ["
426 << keysym << "]" << endl;
429 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
430 if (keysym == NoSymbol) {
432 << "Empty kdb action (probably composing)"
436 KeySym ret_key = keysym;
438 // Note that we need this handling because of a bug
439 // in XForms 0.89, if this bug is resolved in the way I hope
440 // we can just use the keysym directly with out looking
441 // at key at all. (Lgb)
444 // We migth have to add more keysyms here also,
445 // we will do that as the issues arise. (Lgb)
446 if (keysym == XK_space) {
448 lyxerr[Debug::KEY] << "Using keysym [A]"
453 // It seems that this was a bit optimistic...
454 // With this hacking things seems to be better (Lgb)
455 //if (!iscntrl(key)) {
457 // lyxerr[Debug::KEY]
458 // << "Using key [B]\n"
460 // << static_cast<unsigned char>(key)
463 ret_key = (keysym ? keysym : key);
464 lyxerr[Debug::KEY] << "Using keysym [B]"
470 unsigned int const ret_state = xke->state;
472 // If you have a better way to handle "wild-output" of
473 // characters after the key has been released than the one
474 // below, please contact me. (Lgb)
475 static Time last_time_pressed;
476 static unsigned int last_key_pressed;
477 static unsigned int last_state_pressed;
478 lyxerr[Debug::KEY] << "Workarea Diff: "
479 << xke->time - last_time_pressed
481 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
482 && ret_state == last_state_pressed
483 && xke->keycode == last_key_pressed) {
485 << "Workarea: Purging X events." << endl;
486 //lyxerr << "Workarea Events: "
487 // << XEventsQueued(fl_get_display(), QueuedAlready)
489 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
490 XSync(fl_get_display(), 1);
491 // This purge make f.ex. scrolling stop immidiatly when
492 // releasing the PageDown button. The question is if
493 // this purging of XEvents can cause any harm...
494 // after some testing I can see no problems, but
495 // I'd like other reports too.
498 last_time_pressed = xke->time;
499 last_key_pressed = xke->keycode;
500 last_state_pressed = ret_state;
502 area->workAreaKeyPress(ret_key, ret_state);
506 #if FL_REVISION >= 89
508 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
513 lyxerr[Debug::WORKAREA] << "Workarea event: FOCUS" << endl;
514 area->workAreaFocus();
517 lyxerr[Debug::WORKAREA] << "Workarea event: UNFOCUS" << endl;
518 area->workAreaUnfocus();
521 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
522 area->workAreaEnter();
525 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
526 area->workAreaLeave();
530 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
531 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
532 ev->xbutton.y - ob->y,
537 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
538 area->workAreaTripleClick(ev->xbutton.x - ob->x,
539 ev->xbutton.y - ob->y,
544 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
554 string clipboard_selection;
555 bool clipboard_read = false;
560 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
561 void const * data, long size)
563 clipboard_selection.erase();
566 clipboard_selection.reserve(size);
567 for (int i = 0; i < size; ++i)
568 clipboard_selection +=
569 static_cast<char const *>(data)[i];
570 clipboard_read = true;
578 void WorkArea::event_cb(XEvent * xev)
580 if (xev->type != SelectionRequest)
583 selectionRequested.emit();
588 void WorkArea::haveSelection(bool yes) const
591 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
595 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
599 string const WorkArea::getClipboard() const
601 clipboard_read = false;
603 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
608 while (!clipboard_read) {
609 if (fl_check_forms() == FL_EVENT) {
611 lyxerr << "Received unhandled X11 event" << endl;
612 lyxerr << "Type: 0x" << std::hex << ev.xany.type <<
613 "Target: 0x" << std::hex << ev.xany.window << endl;
616 return clipboard_selection;
620 void WorkArea::putClipboard(string const & s) const
625 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);