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 * ====================================================== */
15 #pragma implementation
20 #include "support/lstrings.h"
23 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
24 #include "lyxlookup.h"
29 FL_OBJECT * figinset_canvas;
31 // needed to make the c++ compiler find the correct version of abs.
32 // This is at least true for g++.
40 XSync(fl_get_display(), 0);
47 // Just a bunch of C wrappers around static members of WorkArea
48 void C_WorkArea_scroll_cb(FL_OBJECT * ob, long buf)
50 WorkArea::scroll_cb(ob, buf);
53 int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
57 return WorkArea::work_area_handler(ob, event,
63 WorkArea::WorkArea(int xpos, int ypos, int width, int height)
64 : workareapixmap(0), painter_(*this)
66 fl_freeze_all_forms();
70 if (lyxerr.debugging(Debug::GUI))
71 lyxerr << "Creating work area: +"
72 << xpos << '+' << ypos << ' '
73 << width << 'x' << height << endl;
76 int const bw = int(std::abs(float(fl_get_border_width())));
78 // We really want to get rid of figinset_canvas.
79 ::figinset_canvas = figinset_canvas = obj =
80 fl_add_canvas(FL_NORMAL_CANVAS,
81 xpos + 1, ypos + 1, 1, 1, "");
82 fl_set_object_boxtype(obj, FL_NO_BOX);
83 fl_set_object_resize(obj, FL_RESIZE_ALL);
84 fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
87 if (lyxerr.debugging(Debug::GUI))
88 lyxerr << "\tbackground box: +"
89 << xpos << '+' << ypos << ' '
90 << width - 15 << 'x' << height << endl;
91 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
95 fl_set_object_resize(obj, FL_RESIZE_ALL);
96 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
102 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
104 ypos, 17, height, "");
105 fl_set_object_boxtype(obj, FL_UP_BOX);
106 fl_set_object_resize(obj, FL_RESIZE_ALL);
107 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
109 fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
114 // Create the workarea pixmap
115 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
117 // We add this object as late as possible to avoit problems
119 if (lyxerr.debugging(Debug::GUI))
120 lyxerr << "\tfree object: +"
121 << xpos + bw << '+' << ypos + bw << ' '
122 << width - 15 - 2 * bw << 'x'
123 << height - 2 * bw << endl;
125 work_area = obj = fl_add_free(FL_ALL_FREE,
126 xpos + bw, ypos + bw,
127 width - 15 - 2 * bw, // scrollbarwidth
129 C_WorkArea_work_area_handler);
130 obj->wantkey = FL_KEY_ALL;
131 obj->u_vdata = this; /* This is how we pass the WorkArea
132 to the work_area_handler. */
133 fl_set_object_boxtype(obj,FL_DOWN_BOX);
134 fl_set_object_resize(obj, FL_RESIZE_ALL);
135 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
137 fl_unfreeze_all_forms();
141 WorkArea::~WorkArea()
144 XFreePixmap(fl_get_display(), workareapixmap);
148 bool WorkArea::belowMouse() const
152 fl_get_mouse(&x, &y, &button);
153 FL_Coord ulx = work_area->form->x + work_area->x;
154 FL_Coord uly = work_area->form->y + work_area->y;
155 FL_Coord w = work_area->w;
156 FL_Coord h = work_area->h;
157 if (x > ulx && y > uly && x < ulx + h && y < uly + w)
162 //lyxerr << "Mouse: (" << x << ", " << y <<") button = " << button << endl;
163 //lyxerr << "Workarea: (" << work_area->x + work_area->form->x << ", " << work_area->y + work_area->form->y << ", " << work_area->w << ", " << work_area->h << ")" << endl;
164 //lyxerr << "Below mouse: " << work_area->belowmouse << endl;
165 //return work_area->belowmouse;
169 void WorkArea::resize(int xpos, int ypos, int width, int height)
171 fl_freeze_all_forms();
173 int const bw = int(std::abs(float(fl_get_border_width())));
176 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
181 fl_set_object_geometry(scrollbar, xpos + width - 15,
184 // Create the workarea pixmap
185 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
188 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
192 fl_unfreeze_all_forms();
197 void WorkArea::createPixmap(int width, int height)
199 static int cur_width = -1;
200 static int cur_height = -1;
202 if (cur_width == width && cur_height == height && workareapixmap)
209 XFreePixmap(fl_get_display(), workareapixmap);
211 if (lyxerr.debugging(Debug::GUI))
212 lyxerr << "Creating pixmap ("
213 << width << 'x' << height << ")" << endl;
215 workareapixmap = XCreatePixmap(fl_get_display(),
216 RootWindow(fl_get_display(), 0),
219 fl_get_visual_depth());
220 if (lyxerr.debugging(Debug::GUI))
221 lyxerr << "\tpixmap=" << workareapixmap << endl;
225 void WorkArea::greyOut() const
227 fl_winset(FL_ObjWin(work_area));
228 fl_rectangle(1, work_area->x, work_area->y,
229 work_area->w, work_area->h, FL_GRAY63);
233 void WorkArea::setFocus() const
235 fl_set_focus_object(work_area->form, work_area);
239 void WorkArea::setScrollbar(double pos, double length_fraction) const
241 fl_set_scrollbar_value(scrollbar, pos);
242 fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
246 void WorkArea::setScrollbarBounds(double l1, double l2) const
248 fl_set_scrollbar_bounds(scrollbar, l1, l2);
252 void WorkArea::setScrollbarIncrements(double inc) const
254 fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
258 // Callback for scrollbar slider
259 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
261 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
262 // If we really want the accellerating scroll we can do that
263 // from here. IMHO that is a waste of effort since we already
264 // have other ways to move fast around in the document. (Lgb)
265 area->scrollCB(fl_get_scrollbar_value(ob));
270 bool Lgb_bug_find_hack = false;
272 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
276 static int x_old = -1;
277 static int y_old = -1;
278 static long scrollbar_value_old = -1;
280 XEvent * ev = static_cast<XEvent*>(xev);
281 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
287 if (!area->work_area ||
288 !area->work_area->form->visible)
290 lyxerr[Debug::GUI] << "Workarea event: DRAW" << endl;
291 area->createPixmap(area->workWidth(), area->height());
292 Lgb_bug_find_hack = true;
293 area->workAreaExpose();
294 Lgb_bug_find_hack = false;
297 if (!ev || ev->xbutton.button == 0) break;
298 // Should really have used xbutton.state
299 lyxerr[Debug::GUI] << "Workarea event: PUSH" << endl;
300 area->workAreaButtonPress(ev->xbutton.x - ob->x,
301 ev->xbutton.y - ob->y,
303 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
306 if (!ev || ev->xbutton.button == 0) break;
307 // Should really have used xbutton.state
308 lyxerr[Debug::GUI] << "Workarea event: RELEASE" << endl;
309 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
310 ev->xbutton.y - ob->y,
318 if (!ev || ! area->scrollbar) break;
319 if (ev->xmotion.x != x_old ||
320 ev->xmotion.y != y_old ||
321 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
323 lyxerr[Debug::GUI] << "Workarea event: MOUSE" << endl;
324 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
325 ev->xmotion.y - ob->y,
335 lyxerr[Debug::KEY] << "Workarea event: KEYBOARD" << endl;
339 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
340 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
341 // XForms < 0.89.5 does not have compose support
342 // so we are using our own compose support
343 LyXLookupString(ev, dummy, 1, &keysym);
345 XLookupString(xke, dummy, 1, &keysym, 0);
347 if (lyxerr.debugging(Debug::KEY)) {
348 char const * tmp = XKeysymToString(key);
349 char const * tmp2 = XKeysymToString(keysym);
350 string const stm = (tmp ? tmp : "");
351 string const stm2 = (tmp2 ? tmp2 : "");
353 lyxerr << "WorkArea: Key is `" << stm << "' ["
354 << key << "]" << endl;
355 lyxerr << "WorkArea: Keysym is `" << stm2 << "' ["
356 << keysym << "]" << endl;
359 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
360 if (keysym == NoSymbol) {
362 << "Empty kdb action (probably composing)"
366 KeySym ret_key = keysym;
368 // Note that we need this handling because of a bug
369 // in XForms 0.89, if this bug is resolved in the way I hope
370 // we can just use the keysym directly with out looking
371 // at key at all. (Lgb)
374 // We migth have to add more keysyms here also,
375 // we will do that as the issues arise. (Lgb)
376 if (keysym == XK_space) {
378 lyxerr[Debug::KEY] << "Using keysym [A]"
383 // It seems that this was a bit optimistic...
384 // With this hacking things seems to be better (Lgb)
385 //if (!iscntrl(key)) {
387 // lyxerr[Debug::KEY]
388 // << "Using key [B]\n"
390 // << static_cast<unsigned char>(key)
393 ret_key = (keysym ? keysym : key);
394 lyxerr[Debug::KEY] << "Using keysym [B]"
400 unsigned int const ret_state = xke->state;
402 // If you have a better way to handle "wild-output" of
403 // characters after the key has been released than the one
404 // below, please contact me. (Lgb)
405 static Time last_time_pressed;
406 static unsigned int last_key_pressed;
407 static unsigned int last_state_pressed;
408 lyxerr[Debug::KEY] << "Workarea Diff: "
409 << xke->time - last_time_pressed
411 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
412 && ret_state == last_state_pressed
413 && xke->keycode == last_key_pressed) {
415 << "Workarea: Purging X events." << endl;
416 //lyxerr << "Workarea Events: "
417 // << XEventsQueued(fl_get_display(), QueuedAlready)
419 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
420 XSync(fl_get_display(), 1);
421 // This purge make f.ex. scrolling stop immidiatly when
422 // releasing the PageDown button. The question is if
423 // this purging of XEvents can cause any harm...
424 // after some testing I can see no problems, but
425 // I'd like other reports too.
428 last_time_pressed = xke->time;
429 last_key_pressed = xke->keycode;
430 last_state_pressed = ret_state;
432 area->workAreaKeyPress(ret_key, ret_state);
436 #if FL_REVISION >= 89
438 lyxerr << "Workarea event: KEYRELEASE" << endl;
443 lyxerr[Debug::GUI] << "Workarea event: FOCUS" << endl;
444 area->workAreaFocus();
447 lyxerr[Debug::GUI] << "Workarea event: UNFOCUS" << endl;
448 area->workAreaUnfocus();
451 lyxerr[Debug::GUI] << "Workarea event: ENTER" << endl;
452 area->workAreaEnter();
455 lyxerr[Debug::GUI] << "Workarea event: LEAVE" << endl;
456 area->workAreaLeave();
460 lyxerr[Debug::GUI] << "Workarea event: DBLCLICK" << endl;
461 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
462 ev->xbutton.y - ob->y,
467 lyxerr[Debug::GUI] << "Workarea event: TRPLCLICK" << endl;
468 area->workAreaTripleClick(ev->xbutton.x - ob->x,
469 ev->xbutton.y - ob->y,
474 lyxerr[Debug::GUI] << "Workarea event: OTHER" << endl;
485 string clipboard_selection;
486 bool clipboard_read = false;
489 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
490 void const * data, long size)
492 clipboard_selection.erase();
495 clipboard_selection.reserve(size);
496 for (int i = 0; i < size; ++i)
497 clipboard_selection += static_cast<char const *>(data)[i];
498 clipboard_read = true;
504 string const WorkArea::getClipboard() const
506 clipboard_read = false;
508 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
513 while (!clipboard_read) {
514 if (fl_check_forms() == FL_EVENT) {
515 lyxerr << "LyX: This shouldn't happen..." << endl;
519 return clipboard_selection;
523 void WorkArea::putClipboard(string const & s) const
528 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);