1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
15 #pragma implementation
20 #include "support/lstrings.h"
24 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
25 #include "lyxlookup.h"
30 FL_OBJECT * figinset_canvas;
32 // needed to make the c++ compiler find the correct version of abs.
33 // This is at least true for g++.
42 XSync(fl_get_display(), 0);
49 // Just a bunch of C wrappers around static members of WorkArea
50 void C_WorkArea_scroll_cb(FL_OBJECT * ob, long buf)
52 WorkArea::scroll_cb(ob, buf);
55 int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
59 return WorkArea::work_area_handler(ob, event,
65 WorkArea::WorkArea(int xpos, int ypos, int width, int height)
66 : workareapixmap(0), painter_(*this)
68 fl_freeze_all_forms();
72 if (lyxerr.debugging(Debug::GUI))
73 lyxerr << "Creating work area: +"
74 << xpos << '+' << ypos << ' '
75 << width << 'x' << height << endl;
78 int const bw = int(std::abs(float(fl_get_border_width())));
80 // We really want to get rid of figinset_canvas.
81 ::figinset_canvas = figinset_canvas = obj =
82 fl_add_canvas(FL_NORMAL_CANVAS,
83 xpos + 1, ypos + 1, 1, 1, "");
84 fl_set_object_boxtype(obj, FL_NO_BOX);
85 fl_set_object_resize(obj, FL_RESIZE_ALL);
86 fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
89 if (lyxerr.debugging(Debug::GUI))
90 lyxerr << "\tbackground box: +"
91 << xpos << '+' << ypos << ' '
92 << width - 15 << 'x' << height << endl;
93 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
97 fl_set_object_resize(obj, FL_RESIZE_ALL);
98 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
104 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
106 ypos, 17, height, "");
107 fl_set_object_boxtype(obj, FL_UP_BOX);
108 fl_set_object_resize(obj, FL_RESIZE_ALL);
109 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
111 fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
116 // Create the workarea pixmap
117 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
119 // We add this object as late as possible to avoit problems
121 if (lyxerr.debugging(Debug::GUI))
122 lyxerr << "\tfree object: +"
123 << xpos + bw << '+' << ypos + bw << ' '
124 << width - 15 - 2 * bw << 'x'
125 << height - 2 * bw << endl;
127 work_area = obj = fl_add_free(FL_ALL_FREE,
128 xpos + bw, ypos + bw,
129 width - 15 - 2 * bw, // scrollbarwidth
131 C_WorkArea_work_area_handler);
132 obj->wantkey = FL_KEY_ALL;
133 obj->u_vdata = this; /* This is how we pass the WorkArea
134 to the work_area_handler. */
135 fl_set_object_boxtype(obj,FL_DOWN_BOX);
136 fl_set_object_resize(obj, FL_RESIZE_ALL);
137 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
139 fl_unfreeze_all_forms();
143 WorkArea::~WorkArea()
146 XFreePixmap(fl_get_display(), workareapixmap);
150 bool WorkArea::belowMouse() const
154 fl_get_mouse(&x, &y, &button);
155 FL_Coord ulx = work_area->form->x + work_area->x;
156 FL_Coord uly = work_area->form->y + work_area->y;
157 FL_Coord w = work_area->w;
158 FL_Coord h = work_area->h;
159 if (x > ulx && y > uly && x < ulx + h && y < uly + w)
164 //lyxerr << "Mouse: (" << x << ", " << y <<") button = " << button << endl;
165 //lyxerr << "Workarea: (" << work_area->x + work_area->form->x << ", " << work_area->y + work_area->form->y << ", " << work_area->w << ", " << work_area->h << ")" << endl;
166 //lyxerr << "Below mouse: " << work_area->belowmouse << endl;
167 //return work_area->belowmouse;
171 void WorkArea::resize(int xpos, int ypos, int width, int height)
173 fl_freeze_all_forms();
175 int const bw = int(std::abs(float(fl_get_border_width())));
178 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
183 fl_set_object_geometry(scrollbar, xpos + width - 15,
186 // Create the workarea pixmap
187 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
190 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
194 fl_unfreeze_all_forms();
199 void WorkArea::createPixmap(int width, int height)
201 static int cur_width = -1;
202 static int cur_height = -1;
204 if (cur_width == width && cur_height == height && workareapixmap)
211 XFreePixmap(fl_get_display(), workareapixmap);
213 if (lyxerr.debugging(Debug::GUI))
214 lyxerr << "Creating pixmap ("
215 << width << 'x' << height << ")" << endl;
217 workareapixmap = XCreatePixmap(fl_get_display(),
218 RootWindow(fl_get_display(), 0),
221 fl_get_visual_depth());
222 if (lyxerr.debugging(Debug::GUI))
223 lyxerr << "\tpixmap=" << workareapixmap << endl;
227 void WorkArea::greyOut() const
229 fl_winset(FL_ObjWin(work_area));
230 fl_rectangle(1, work_area->x, work_area->y,
231 work_area->w, work_area->h, FL_GRAY63);
235 void WorkArea::setFocus() const
237 fl_set_focus_object(work_area->form, work_area);
241 void WorkArea::setScrollbar(double pos, double length_fraction) const
243 fl_set_scrollbar_value(scrollbar, pos);
244 fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
248 void WorkArea::setScrollbarBounds(double l1, double l2) const
250 fl_set_scrollbar_bounds(scrollbar, l1, l2);
254 void WorkArea::setScrollbarIncrements(double inc) const
256 fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
260 // Callback for scrollbar slider
261 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
263 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
264 // If we really want the accellerating scroll we can do that
265 // from here. IMHO that is a waste of effort since we already
266 // have other ways to move fast around in the document. (Lgb)
267 area->scrollCB(fl_get_scrollbar_value(ob));
272 bool Lgb_bug_find_hack = false;
274 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
278 static int x_old = -1;
279 static int y_old = -1;
280 static long scrollbar_value_old = -1;
282 XEvent * ev = static_cast<XEvent*>(xev);
283 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
289 if (!area->work_area ||
290 !area->work_area->form->visible)
292 lyxerr[Debug::GUI] << "Workarea event: DRAW" << endl;
293 area->createPixmap(area->workWidth(), area->height());
294 Lgb_bug_find_hack = true;
295 area->workAreaExpose();
296 Lgb_bug_find_hack = false;
299 if (!ev || ev->xbutton.button == 0) break;
300 // Should really have used xbutton.state
301 lyxerr[Debug::GUI] << "Workarea event: PUSH" << endl;
302 area->workAreaButtonPress(ev->xbutton.x - ob->x,
303 ev->xbutton.y - ob->y,
305 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
308 if (!ev || ev->xbutton.button == 0) break;
309 // Should really have used xbutton.state
310 lyxerr[Debug::GUI] << "Workarea event: RELEASE" << endl;
311 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
312 ev->xbutton.y - ob->y,
320 if (!ev || ! area->scrollbar) break;
321 if (ev->xmotion.x != x_old ||
322 ev->xmotion.y != y_old ||
323 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
325 lyxerr[Debug::GUI] << "Workarea event: MOUSE" << endl;
326 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
327 ev->xmotion.y - ob->y,
337 lyxerr[Debug::KEY] << "Workarea event: KEYBOARD" << endl;
341 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
342 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
343 // XForms < 0.89.5 does not have compose support
344 // so we are using our own compose support
345 LyXLookupString(ev, dummy, 1, &keysym);
347 XLookupString(xke, dummy, 1, &keysym, 0);
349 if (lyxerr.debugging(Debug::KEY)) {
350 char const * tmp = XKeysymToString(key);
351 char const * tmp2 = XKeysymToString(keysym);
352 string const stm = (tmp ? tmp : "");
353 string const stm2 = (tmp2 ? tmp2 : "");
355 lyxerr << "WorkArea: Key is `" << stm << "' ["
356 << key << "]" << endl;
357 lyxerr << "WorkArea: Keysym is `" << stm2 << "' ["
358 << keysym << "]" << endl;
362 if (keysym == NoSymbol) {
364 << "Empty kdb action (probably composing)"
368 KeySym ret_key = keysym;
370 // Note that we need this handling because of a bug
371 // in XForms 0.89, if this bug is resolved in the way I hope
372 // we can just use the keysym directly with out looking
373 // at key at all. (Lgb)
376 // We migth have to add more keysyms here also,
377 // we will do that as the issues arise. (Lgb)
378 if (keysym == XK_space) {
380 lyxerr[Debug::KEY] << "Using keysym [A]"
385 // It seems that this was a bit optimistic...
386 // With this hacking things seems to be better (Lgb)
387 //if (!iscntrl(key)) {
389 // lyxerr[Debug::KEY]
390 // << "Using key [B]\n"
392 // << static_cast<unsigned char>(key)
395 ret_key = (keysym ? keysym : key);
396 lyxerr[Debug::KEY] << "Using keysym [B]"
402 unsigned int const ret_state = xke->state;
404 // If you have a better way to handle "wild-output" of
405 // characters after the key has been released than the one
406 // below, please contact me. (Lgb)
407 static Time last_time_pressed = 0;
408 static unsigned int last_key_pressed = 0;
409 static unsigned int last_state_pressed = 0;
410 lyxerr[Debug::KEY] << "Workarea Diff: "
411 << xke->time - last_time_pressed
413 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
414 && ret_state == last_state_pressed
415 && xke->keycode == last_key_pressed) {
417 << "Workarea: Purging X events." << endl;
418 //lyxerr << "Workarea Events: "
419 // << XEventsQueued(fl_get_display(), QueuedAlready)
421 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
422 XSync(fl_get_display(), 1);
423 // This purge make f.ex. scrolling stop immidiatly when
424 // releasing the PageDown button. The question is if
425 // this purging of XEvents can cause any harm...
426 // after some testing I can see no problems, but
427 // I'd like other reports too.
430 last_time_pressed = xke->time;
431 last_key_pressed = xke->keycode;
432 last_state_pressed = ret_state;
434 area->workAreaKeyPress(ret_key, ret_state);
438 #if FL_REVISION >= 89
440 lyxerr << "Workarea event: KEYRELEASE" << endl;
445 lyxerr[Debug::GUI] << "Workarea event: FOCUS" << endl;
446 area->workAreaFocus();
449 lyxerr[Debug::GUI] << "Workarea event: UNFOCUS" << endl;
450 area->workAreaUnfocus();
453 lyxerr[Debug::GUI] << "Workarea event: ENTER" << endl;
454 area->workAreaEnter();
457 lyxerr[Debug::GUI] << "Workarea event: LEAVE" << endl;
458 area->workAreaLeave();
462 lyxerr[Debug::GUI] << "Workarea event: DBLCLICK" << endl;
463 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
464 ev->xbutton.y - ob->y,
469 lyxerr[Debug::GUI] << "Workarea event: TRPLCLICK" << endl;
470 area->workAreaTripleClick(ev->xbutton.x - ob->x,
471 ev->xbutton.y - ob->y,
476 lyxerr[Debug::GUI] << "Workarea event: OTHER" << endl;
485 static string clipboard_selection;
486 static bool clipboard_read = false;
490 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
491 void const * data, long size)
493 clipboard_selection.erase();
496 clipboard_selection.reserve(size);
497 for (int i = 0; i < size; ++i)
498 clipboard_selection += static_cast<char const *>(data)[i];
499 clipboard_read = true;
505 string const WorkArea::getClipboard() const
507 clipboard_read = false;
509 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
514 while (!clipboard_read) {
515 if (fl_check_forms() == FL_EVENT) {
516 lyxerr << "LyX: This shouldn't happen..." << endl;
520 return clipboard_selection;
524 void WorkArea::putClipboard(string const & s) const
529 fl_stuff_clipboard(work_area, 0, hold.c_str(), hold.size(), 0);