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
49 void C_WorkArea_scroll_cb(FL_OBJECT * ob, long buf)
51 WorkArea::scroll_cb(ob, buf);
56 int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
60 return WorkArea::work_area_handler(ob, event,
66 WorkArea::WorkArea(int xpos, int ypos, int width, int height)
67 : workareapixmap(0), painter_(*this)
69 fl_freeze_all_forms();
73 if (lyxerr.debugging(Debug::GUI))
74 lyxerr << "Creating work area: +"
75 << xpos << '+' << ypos << ' '
76 << width << 'x' << height << endl;
79 int const bw = int(std::abs(float(fl_get_border_width())));
81 // We really want to get rid of figinset_canvas.
82 ::figinset_canvas = figinset_canvas = obj =
83 fl_add_canvas(FL_NORMAL_CANVAS,
84 xpos + 1, ypos + 1, 1, 1, "");
85 fl_set_object_boxtype(obj, FL_NO_BOX);
86 fl_set_object_resize(obj, FL_RESIZE_ALL);
87 fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
90 if (lyxerr.debugging(Debug::GUI))
91 lyxerr << "\tbackground box: +"
92 << xpos << '+' << ypos << ' '
93 << width - 15 << 'x' << height << endl;
94 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
98 fl_set_object_resize(obj, FL_RESIZE_ALL);
99 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
105 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
107 ypos, 17, height, "");
108 fl_set_object_boxtype(obj, FL_UP_BOX);
109 fl_set_object_resize(obj, FL_RESIZE_ALL);
110 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
112 fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
117 // Create the workarea pixmap
118 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
120 // We add this object as late as possible to avoit problems
122 if (lyxerr.debugging(Debug::GUI))
123 lyxerr << "\tfree object: +"
124 << xpos + bw << '+' << ypos + bw << ' '
125 << width - 15 - 2 * bw << 'x'
126 << height - 2 * bw << endl;
128 work_area = obj = fl_add_free(FL_ALL_FREE,
129 xpos + bw, ypos + bw,
130 width - 15 - 2 * bw, // scrollbarwidth
132 C_WorkArea_work_area_handler);
133 obj->wantkey = FL_KEY_ALL;
134 obj->u_vdata = this; /* This is how we pass the WorkArea
135 to the work_area_handler. */
136 fl_set_object_boxtype(obj,FL_DOWN_BOX);
137 fl_set_object_resize(obj, FL_RESIZE_ALL);
138 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
140 fl_unfreeze_all_forms();
144 WorkArea::~WorkArea()
147 XFreePixmap(fl_get_display(), workareapixmap);
151 bool WorkArea::belowMouse() const
155 fl_get_mouse(&x, &y, &button);
156 FL_Coord ulx = work_area->form->x + work_area->x;
157 FL_Coord uly = work_area->form->y + work_area->y;
158 FL_Coord w = work_area->w;
159 FL_Coord h = work_area->h;
160 if (x > ulx && y > uly && x < ulx + h && y < uly + w)
165 //lyxerr << "Mouse: (" << x << ", " << y <<") button = " << button << endl;
166 //lyxerr << "Workarea: (" << work_area->x + work_area->form->x << ", " << work_area->y + work_area->form->y << ", " << work_area->w << ", " << work_area->h << ")" << endl;
167 //lyxerr << "Below mouse: " << work_area->belowmouse << endl;
168 //return work_area->belowmouse;
172 void WorkArea::resize(int xpos, int ypos, int width, int height)
174 fl_freeze_all_forms();
176 int const bw = int(std::abs(float(fl_get_border_width())));
179 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
184 fl_set_object_geometry(scrollbar, xpos + width - 15,
187 // Create the workarea pixmap
188 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
191 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
195 fl_unfreeze_all_forms();
200 void WorkArea::createPixmap(int width, int height)
202 static int cur_width = -1;
203 static int cur_height = -1;
205 if (cur_width == width && cur_height == height && workareapixmap)
212 XFreePixmap(fl_get_display(), workareapixmap);
214 if (lyxerr.debugging(Debug::GUI))
215 lyxerr << "Creating pixmap ("
216 << width << 'x' << height << ")" << endl;
218 workareapixmap = XCreatePixmap(fl_get_display(),
219 RootWindow(fl_get_display(), 0),
222 fl_get_visual_depth());
223 if (lyxerr.debugging(Debug::GUI))
224 lyxerr << "\tpixmap=" << workareapixmap << endl;
228 void WorkArea::greyOut() const
230 fl_winset(FL_ObjWin(work_area));
231 fl_rectangle(1, work_area->x, work_area->y,
232 work_area->w, work_area->h, FL_GRAY63);
236 void WorkArea::setFocus() const
238 fl_set_focus_object(work_area->form, work_area);
242 void WorkArea::setScrollbar(double pos, double length_fraction) const
244 fl_set_scrollbar_value(scrollbar, pos);
245 fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
249 void WorkArea::setScrollbarBounds(double l1, double l2) const
251 fl_set_scrollbar_bounds(scrollbar, l1, l2);
255 void WorkArea::setScrollbarIncrements(double inc) const
257 fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
261 // Callback for scrollbar slider
262 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
264 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
265 // If we really want the accellerating scroll we can do that
266 // from here. IMHO that is a waste of effort since we already
267 // have other ways to move fast around in the document. (Lgb)
268 area->scrollCB(fl_get_scrollbar_value(ob));
273 bool Lgb_bug_find_hack = false;
275 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
279 static int x_old = -1;
280 static int y_old = -1;
281 static long scrollbar_value_old = -1;
283 XEvent * ev = static_cast<XEvent*>(xev);
284 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
290 if (!area->work_area ||
291 !area->work_area->form->visible)
293 lyxerr[Debug::GUI] << "Workarea event: DRAW" << endl;
294 area->createPixmap(area->workWidth(), area->height());
295 Lgb_bug_find_hack = true;
296 area->workAreaExpose();
297 Lgb_bug_find_hack = false;
300 if (!ev || ev->xbutton.button == 0) break;
301 // Should really have used xbutton.state
302 lyxerr[Debug::GUI] << "Workarea event: PUSH" << endl;
303 area->workAreaButtonPress(ev->xbutton.x - ob->x,
304 ev->xbutton.y - ob->y,
306 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
309 if (!ev || ev->xbutton.button == 0) break;
310 // Should really have used xbutton.state
311 lyxerr[Debug::GUI] << "Workarea event: RELEASE" << endl;
312 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
313 ev->xbutton.y - ob->y,
321 if (!ev || ! area->scrollbar) break;
322 if (ev->xmotion.x != x_old ||
323 ev->xmotion.y != y_old ||
324 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
326 lyxerr[Debug::GUI] << "Workarea event: MOUSE" << endl;
327 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
328 ev->xmotion.y - ob->y,
338 lyxerr[Debug::KEY] << "Workarea event: KEYBOARD" << endl;
342 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
343 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
344 // XForms < 0.89.5 does not have compose support
345 // so we are using our own compose support
346 LyXLookupString(ev, dummy, 1, &keysym);
348 XLookupString(xke, dummy, 1, &keysym, 0);
350 if (lyxerr.debugging(Debug::KEY)) {
351 char const * tmp = XKeysymToString(key);
352 char const * tmp2 = XKeysymToString(keysym);
353 string const stm = (tmp ? tmp : "");
354 string const stm2 = (tmp2 ? tmp2 : "");
356 lyxerr << "WorkArea: Key is `" << stm << "' ["
357 << key << "]" << endl;
358 lyxerr << "WorkArea: Keysym is `" << stm2 << "' ["
359 << keysym << "]" << endl;
362 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
363 if (keysym == NoSymbol) {
365 << "Empty kdb action (probably composing)"
369 KeySym ret_key = keysym;
371 // Note that we need this handling because of a bug
372 // in XForms 0.89, if this bug is resolved in the way I hope
373 // we can just use the keysym directly with out looking
374 // at key at all. (Lgb)
377 // We migth have to add more keysyms here also,
378 // we will do that as the issues arise. (Lgb)
379 if (keysym == XK_space) {
381 lyxerr[Debug::KEY] << "Using keysym [A]"
386 // It seems that this was a bit optimistic...
387 // With this hacking things seems to be better (Lgb)
388 //if (!iscntrl(key)) {
390 // lyxerr[Debug::KEY]
391 // << "Using key [B]\n"
393 // << static_cast<unsigned char>(key)
396 ret_key = (keysym ? keysym : key);
397 lyxerr[Debug::KEY] << "Using keysym [B]"
403 unsigned int const ret_state = xke->state;
405 // If you have a better way to handle "wild-output" of
406 // characters after the key has been released than the one
407 // below, please contact me. (Lgb)
408 static Time last_time_pressed;
409 static unsigned int last_key_pressed;
410 static unsigned int last_state_pressed;
411 lyxerr[Debug::KEY] << "Workarea Diff: "
412 << xke->time - last_time_pressed
414 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
415 && ret_state == last_state_pressed
416 && xke->keycode == last_key_pressed) {
418 << "Workarea: Purging X events." << endl;
419 //lyxerr << "Workarea Events: "
420 // << XEventsQueued(fl_get_display(), QueuedAlready)
422 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
423 XSync(fl_get_display(), 1);
424 // This purge make f.ex. scrolling stop immidiatly when
425 // releasing the PageDown button. The question is if
426 // this purging of XEvents can cause any harm...
427 // after some testing I can see no problems, but
428 // I'd like other reports too.
431 last_time_pressed = xke->time;
432 last_key_pressed = xke->keycode;
433 last_state_pressed = ret_state;
435 area->workAreaKeyPress(ret_key, ret_state);
439 #if FL_REVISION >= 89
441 lyxerr << "Workarea event: KEYRELEASE" << endl;
446 lyxerr[Debug::GUI] << "Workarea event: FOCUS" << endl;
447 area->workAreaFocus();
450 lyxerr[Debug::GUI] << "Workarea event: UNFOCUS" << endl;
451 area->workAreaUnfocus();
454 lyxerr[Debug::GUI] << "Workarea event: ENTER" << endl;
455 area->workAreaEnter();
458 lyxerr[Debug::GUI] << "Workarea event: LEAVE" << endl;
459 area->workAreaLeave();
463 lyxerr[Debug::GUI] << "Workarea event: DBLCLICK" << endl;
464 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
465 ev->xbutton.y - ob->y,
470 lyxerr[Debug::GUI] << "Workarea event: TRPLCLICK" << endl;
471 area->workAreaTripleClick(ev->xbutton.x - ob->x,
472 ev->xbutton.y - ob->y,
477 lyxerr[Debug::GUI] << "Workarea event: OTHER" << endl;
488 string clipboard_selection;
489 bool clipboard_read = false;
494 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
495 void const * data, long size)
497 clipboard_selection.erase();
500 clipboard_selection.reserve(size);
501 for (int i = 0; i < size; ++i)
502 clipboard_selection +=
503 static_cast<char const *>(data)[i];
504 clipboard_read = true;
512 string const WorkArea::getClipboard() const
514 clipboard_read = false;
516 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
521 while (!clipboard_read) {
522 if (fl_check_forms() == FL_EVENT) {
523 lyxerr << "LyX: This shouldn't happen..." << endl;
527 return clipboard_selection;
531 void WorkArea::putClipboard(string const & s) const
536 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);