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 *);
42 FL_OBJECT * figinset_canvas;
49 XSync(fl_get_display(), 0);
56 // Just a bunch of C wrappers around static members of WorkArea
58 void C_WorkArea_scroll_cb(FL_OBJECT * ob, long buf)
60 WorkArea::scroll_cb(ob, buf);
65 int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
69 return WorkArea::work_area_handler(ob, event,
74 int C_WorkAreaEventCB(FL_FORM * form, void * xev) {
75 WorkArea * wa=static_cast<WorkArea*>(form->u_vdata);
76 wa->event_cb(static_cast<XEvent*>(xev));
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();
89 if (lyxerr.debugging(Debug::WORKAREA))
90 lyxerr << "Creating work area: +"
91 << xpos << '+' << ypos << ' '
92 << width << 'x' << height << endl;
95 int const bw = int(abs(fl_get_border_width()));
97 // We really want to get rid of figinset_canvas.
98 ::figinset_canvas = figinset_canvas = obj =
99 fl_add_canvas(FL_NORMAL_CANVAS,
100 xpos + 1, ypos + 1, 1, 1, "");
101 fl_set_object_boxtype(obj, FL_NO_BOX);
102 fl_set_object_resize(obj, FL_RESIZE_ALL);
103 fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
106 if (lyxerr.debugging(Debug::WORKAREA))
107 lyxerr << "\tbackground box: +"
108 << xpos << '+' << ypos << ' '
109 << width - 15 << 'x' << height << endl;
110 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
114 fl_set_object_resize(obj, FL_RESIZE_ALL);
115 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
117 // Add a splash screen to the centre of the work area
118 string const splash_file = (lyxrc.show_banner) ?
119 LibFileSearch("images", "banner", "xpm") : string();
121 if (!splash_file.empty()) {
122 int const splash_w = 425;
123 int const splash_h = 290;
124 int const splash_x = xpos + (width - 15 - splash_w) / 2;
125 int const splash_y = ypos + (height - splash_h) / 2;
127 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
129 splash_w, splash_h, "");
130 fl_set_pixmapbutton_file(obj, splash_file.c_str());
131 fl_set_pixmapbutton_focus_outline(obj, 3);
132 fl_set_object_boxtype(obj, FL_NO_BOX);
134 int const text_x = splash_x + 260;
135 int const text_y = splash_y + 255;
137 fl_add_text(FL_NORMAL_TEXT, text_x, text_y, 160, 16,
139 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
140 fl_mapcolor(FL_FREE_COL2, 0x2b, 0x47, 0x82);
141 fl_mapcolor(FL_FREE_COL3, 0xe1, 0xd2, 0x9b);
142 fl_set_object_color(obj, FL_FREE_COL2, FL_FREE_COL2);
143 fl_set_object_lcol(obj, FL_FREE_COL3);
144 fl_set_object_lalign(obj, FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
145 fl_set_object_lstyle(obj, FL_BOLD_STYLE);
152 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
154 ypos, 17, height, "");
155 fl_set_object_boxtype(obj, FL_UP_BOX);
156 fl_set_object_resize(obj, FL_RESIZE_ALL);
157 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
159 fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
160 setScrollbarBounds(0.0, 0.0);
165 // Create the workarea pixmap
166 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
168 // We add this object as late as possible to avoit problems
170 if (lyxerr.debugging(Debug::WORKAREA))
171 lyxerr << "\tfree object: +"
172 << xpos + bw << '+' << ypos + bw << ' '
173 << width - 15 - 2 * bw << 'x'
174 << height - 2 * bw << endl;
176 work_area = obj = fl_add_free(FL_ALL_FREE,
177 xpos + bw, ypos + bw,
178 width - 15 - 2 * bw, // scrollbarwidth
180 C_WorkArea_work_area_handler);
181 obj->wantkey = FL_KEY_ALL;
182 obj->u_vdata = this; /* This is how we pass the WorkArea
183 to the work_area_handler. */
184 fl_set_object_boxtype(obj,FL_DOWN_BOX);
185 fl_set_object_resize(obj, FL_RESIZE_ALL);
186 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
188 /// X selection hook - xforms gets it wrong
189 fl_current_form->u_vdata = this;
190 fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_WorkAreaEventCB);
192 fl_unfreeze_all_forms();
196 WorkArea::~WorkArea()
199 XFreePixmap(fl_get_display(), workareapixmap);
203 bool WorkArea::belowMouse() const
207 fl_get_mouse(&x, &y, &button);
208 FL_Coord ulx = work_area->form->x + work_area->x;
209 FL_Coord uly = work_area->form->y + work_area->y;
210 FL_Coord w = work_area->w;
211 FL_Coord h = work_area->h;
212 if (x > ulx && y > uly && x < ulx + h && y < uly + w)
218 void WorkArea::resize(int xpos, int ypos, int width, int height)
220 fl_freeze_all_forms();
222 int const bw = int(abs(fl_get_border_width()));
225 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
230 fl_set_object_geometry(scrollbar, xpos + width - 15,
233 // Create the workarea pixmap
234 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
237 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
241 fl_unfreeze_all_forms();
246 void destroy_object(FL_OBJECT * obj)
254 fl_delete_object(obj);
260 void WorkArea::createPixmap(int width, int height)
262 // Three calls to createPixmap are needed to draw the initial view
263 // of LyX. Any more and the splash is destroyed.
264 static int counter = 0;
265 if (++counter == 4) {
266 destroy_object(splash_);
268 destroy_object(splash_text_);
272 static int cur_width = -1;
273 static int cur_height = -1;
275 if (cur_width == width && cur_height == height && workareapixmap)
282 XFreePixmap(fl_get_display(), workareapixmap);
284 if (lyxerr.debugging(Debug::WORKAREA))
285 lyxerr << "Creating pixmap ("
286 << width << 'x' << height << ")" << endl;
288 workareapixmap = XCreatePixmap(fl_get_display(),
289 RootWindow(fl_get_display(), 0),
292 fl_get_visual_depth());
293 if (lyxerr.debugging(Debug::WORKAREA))
294 lyxerr << "\tpixmap=" << workareapixmap << endl;
298 void WorkArea::greyOut() const
301 fl_winset(FL_ObjWin(work_area));
302 fl_rectangle(1, work_area->x, work_area->y,
303 work_area->w, work_area->h, FL_GRAY63);
308 void WorkArea::setFocus() const
310 fl_set_focus_object(work_area->form, work_area);
314 void WorkArea::setScrollbar(double pos, double length_fraction) const
316 fl_set_scrollbar_value(scrollbar, pos);
317 fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
321 void WorkArea::setScrollbarBounds(double l1, double l2) const
323 fl_set_scrollbar_bounds(scrollbar, l1, l2);
327 void WorkArea::setScrollbarIncrements(double inc) const
329 fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
333 // Callback for scrollbar slider
334 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
336 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
337 // If we really want the accellerating scroll we can do that
338 // from here. IMHO that is a waste of effort since we already
339 // have other ways to move fast around in the document. (Lgb)
340 area->scrollCB(fl_get_scrollbar_value(ob));
345 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
349 static int x_old = -1;
350 static int y_old = -1;
351 static long scrollbar_value_old = -1;
353 XEvent * ev = static_cast<XEvent*>(xev);
354 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
360 if (!area->work_area ||
361 !area->work_area->form->visible)
363 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
364 area->createPixmap(area->workWidth(), area->height());
365 area->workAreaExpose();
368 if (!ev || ev->xbutton.button == 0) break;
369 // Should really have used xbutton.state
370 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
371 area->workAreaButtonPress(ev->xbutton.x - ob->x,
372 ev->xbutton.y - ob->y,
374 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
377 if (!ev || ev->xbutton.button == 0) break;
378 // Should really have used xbutton.state
379 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
380 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
381 ev->xbutton.y - ob->y,
389 if (!ev || ! area->scrollbar) break;
390 if (ev->xmotion.x != x_old ||
391 ev->xmotion.y != y_old ||
392 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
394 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
395 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
396 ev->xmotion.y - ob->y,
406 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
410 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
411 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
412 // XForms < 0.89.5 does not have compose support
413 // so we are using our own compose support
414 LyXLookupString(ev, dummy, 1, &keysym);
416 XLookupString(xke, dummy, 1, &keysym, 0);
418 if (lyxerr.debugging(Debug::KEY)) {
419 char const * tmp = XKeysymToString(key);
420 char const * tmp2 = XKeysymToString(keysym);
421 string const stm = (tmp ? tmp : "");
422 string const stm2 = (tmp2 ? tmp2 : "");
424 lyxerr[Debug::KEY] << "WorkArea: Key is `" << stm << "' ["
425 << key << "]" << endl;
426 lyxerr[Debug::KEY] << "WorkArea: Keysym is `" << stm2 << "' ["
427 << keysym << "]" << endl;
430 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
431 if (keysym == NoSymbol) {
433 << "Empty kdb action (probably composing)"
437 KeySym ret_key = keysym;
439 // Note that we need this handling because of a bug
440 // in XForms 0.89, if this bug is resolved in the way I hope
441 // we can just use the keysym directly with out looking
442 // at key at all. (Lgb)
445 // We migth have to add more keysyms here also,
446 // we will do that as the issues arise. (Lgb)
447 if (keysym == XK_space) {
449 lyxerr[Debug::KEY] << "Using keysym [A]"
454 // It seems that this was a bit optimistic...
455 // With this hacking things seems to be better (Lgb)
456 //if (!iscntrl(key)) {
458 // lyxerr[Debug::KEY]
459 // << "Using key [B]\n"
461 // << static_cast<unsigned char>(key)
464 ret_key = (keysym ? keysym : key);
465 lyxerr[Debug::KEY] << "Using keysym [B]"
471 unsigned int const ret_state = xke->state;
473 // If you have a better way to handle "wild-output" of
474 // characters after the key has been released than the one
475 // below, please contact me. (Lgb)
476 static Time last_time_pressed;
477 static unsigned int last_key_pressed;
478 static unsigned int last_state_pressed;
479 lyxerr[Debug::KEY] << "Workarea Diff: "
480 << xke->time - last_time_pressed
482 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
483 && ret_state == last_state_pressed
484 && xke->keycode == last_key_pressed) {
486 << "Workarea: Purging X events." << endl;
487 //lyxerr << "Workarea Events: "
488 // << XEventsQueued(fl_get_display(), QueuedAlready)
490 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
491 XSync(fl_get_display(), 1);
492 // This purge make f.ex. scrolling stop immidiatly when
493 // releasing the PageDown button. The question is if
494 // this purging of XEvents can cause any harm...
495 // after some testing I can see no problems, but
496 // I'd like other reports too.
499 last_time_pressed = xke->time;
500 last_key_pressed = xke->keycode;
501 last_state_pressed = ret_state;
503 area->workAreaKeyPress(ret_key, ret_state);
507 #if FL_REVISION >= 89
509 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
514 lyxerr[Debug::WORKAREA] << "Workarea event: FOCUS" << endl;
515 area->workAreaFocus();
518 lyxerr[Debug::WORKAREA] << "Workarea event: UNFOCUS" << endl;
519 area->workAreaUnfocus();
522 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
523 area->workAreaEnter();
526 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
527 area->workAreaLeave();
531 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
532 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
533 ev->xbutton.y - ob->y,
538 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
539 area->workAreaTripleClick(ev->xbutton.x - ob->x,
540 ev->xbutton.y - ob->y,
545 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
555 string clipboard_selection;
556 bool clipboard_read = false;
561 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
562 void const * data, long size)
564 clipboard_selection.erase();
567 clipboard_selection.reserve(size);
568 for (int i = 0; i < size; ++i)
569 clipboard_selection +=
570 static_cast<char const *>(data)[i];
571 clipboard_read = true;
579 void WorkArea::event_cb(XEvent * xev)
582 case SelectionRequest:
583 lyxerr[Debug::GUI] << "X requested selection." << endl;
584 selectionRequested.emit();
587 lyxerr[Debug::GUI] << "Lost selection." << endl;
588 selectionLost.emit();
594 void WorkArea::haveSelection(bool yes) const
597 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
601 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
605 string const WorkArea::getClipboard() const
607 clipboard_read = false;
609 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
614 while (!clipboard_read) {
615 if (fl_check_forms() == FL_EVENT) {
617 lyxerr << "Received unhandled X11 event" << endl;
618 lyxerr << "Type: 0x" << hex << ev.xany.type <<
619 " Target: 0x" << hex << ev.xany.window << endl;
622 return clipboard_selection;
626 void WorkArea::putClipboard(string const & s) const
631 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);