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++.
41 XSync(fl_get_display(), 0);
48 // 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);
54 int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
58 return WorkArea::work_area_handler(ob, event,
64 WorkArea::WorkArea(int xpos, int ypos, int width, int height)
65 : workareapixmap(0), painter_(*this)
67 fl_freeze_all_forms();
71 if (lyxerr.debugging(Debug::GUI))
72 lyxerr << "Creating work area: +"
73 << xpos << '+' << ypos << ' '
74 << width << 'x' << height << endl;
77 int const bw = int(std::abs(float(fl_get_border_width())));
79 // We really want to get rid of figinset_canvas.
80 ::figinset_canvas = figinset_canvas = obj =
81 fl_add_canvas(FL_NORMAL_CANVAS,
82 xpos + 1, ypos + 1, 1, 1, "");
83 fl_set_object_boxtype(obj, FL_NO_BOX);
84 fl_set_object_resize(obj, FL_RESIZE_ALL);
85 fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
88 if (lyxerr.debugging(Debug::GUI))
89 lyxerr << "\tbackground box: +"
90 << xpos << '+' << ypos << ' '
91 << width - 15 << 'x' << height << endl;
92 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
96 fl_set_object_resize(obj, FL_RESIZE_ALL);
97 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
103 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
105 ypos, 17, height, "");
106 fl_set_object_boxtype(obj, FL_UP_BOX);
107 fl_set_object_resize(obj, FL_RESIZE_ALL);
108 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
110 fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
115 // Create the workarea pixmap
116 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
118 // We add this object as late as possible to avoit problems
120 if (lyxerr.debugging(Debug::GUI))
121 lyxerr << "\tfree object: +"
122 << xpos + bw << '+' << ypos + bw << ' '
123 << width - 15 - 2 * bw << 'x'
124 << height - 2 * bw << endl;
126 work_area = obj = fl_add_free(FL_ALL_FREE,
127 xpos + bw, ypos + bw,
128 width - 15 - 2 * bw, // scrollbarwidth
130 C_WorkArea_work_area_handler);
131 obj->wantkey = FL_KEY_ALL;
132 obj->u_vdata = this; /* This is how we pass the WorkArea
133 to the work_area_handler. */
134 fl_set_object_boxtype(obj,FL_DOWN_BOX);
135 fl_set_object_resize(obj, FL_RESIZE_ALL);
136 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
138 fl_unfreeze_all_forms();
142 WorkArea::~WorkArea()
145 XFreePixmap(fl_get_display(), workareapixmap);
149 bool WorkArea::belowMouse() const
153 fl_get_mouse(&x, &y, &button);
154 FL_Coord ulx = work_area->form->x + work_area->x;
155 FL_Coord uly = work_area->form->y + work_area->y;
156 FL_Coord w = work_area->w;
157 FL_Coord h = work_area->h;
158 if (x > ulx && y > uly && x < ulx + h && y < uly + w)
163 //lyxerr << "Mouse: (" << x << ", " << y <<") button = " << button << endl;
164 //lyxerr << "Workarea: (" << work_area->x + work_area->form->x << ", " << work_area->y + work_area->form->y << ", " << work_area->w << ", " << work_area->h << ")" << endl;
165 //lyxerr << "Below mouse: " << work_area->belowmouse << endl;
166 //return work_area->belowmouse;
170 void WorkArea::resize(int xpos, int ypos, int width, int height)
172 fl_freeze_all_forms();
174 int const bw = int(std::abs(float(fl_get_border_width())));
177 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
182 fl_set_object_geometry(scrollbar, xpos + width - 15,
185 // Create the workarea pixmap
186 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
189 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
193 fl_unfreeze_all_forms();
198 void WorkArea::createPixmap(int width, int height)
200 static int cur_width = -1;
201 static int cur_height = -1;
203 if (cur_width == width && cur_height == height && workareapixmap)
210 XFreePixmap(fl_get_display(), workareapixmap);
212 if (lyxerr.debugging(Debug::GUI))
213 lyxerr << "Creating pixmap ("
214 << width << 'x' << height << ")" << endl;
216 workareapixmap = XCreatePixmap(fl_get_display(),
217 RootWindow(fl_get_display(), 0),
220 fl_get_visual_depth());
221 if (lyxerr.debugging(Debug::GUI))
222 lyxerr << "\tpixmap=" << workareapixmap << endl;
226 void WorkArea::greyOut() const
228 fl_winset(FL_ObjWin(work_area));
229 fl_rectangle(1, work_area->x, work_area->y,
230 work_area->w, work_area->h, FL_GRAY63);
234 void WorkArea::setFocus() const
236 fl_set_focus_object(work_area->form, work_area);
240 void WorkArea::setScrollbar(double pos, double length_fraction) const
242 fl_set_scrollbar_value(scrollbar, pos);
243 fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
247 void WorkArea::setScrollbarBounds(double l1, double l2) const
249 fl_set_scrollbar_bounds(scrollbar, l1, l2);
253 void WorkArea::setScrollbarIncrements(double inc) const
255 fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
259 // Callback for scrollbar slider
260 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
262 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
263 // If we really want the accellerating scroll we can do that
264 // from here. IMHO that is a waste of effort since we already
265 // have other ways to move fast around in the document. (Lgb)
266 area->scrollCB(fl_get_scrollbar_value(ob));
271 bool Lgb_bug_find_hack = false;
273 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
277 static int x_old = -1;
278 static int y_old = -1;
279 static long scrollbar_value_old = -1;
281 XEvent * ev = static_cast<XEvent*>(xev);
282 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
288 if (!area->work_area ||
289 !area->work_area->form->visible)
291 lyxerr[Debug::GUI] << "Workarea event: DRAW" << endl;
292 area->createPixmap(area->workWidth(), area->height());
293 Lgb_bug_find_hack = true;
294 area->workAreaExpose();
295 Lgb_bug_find_hack = false;
298 if (!ev || ev->xbutton.button == 0) break;
299 // Should really have used xbutton.state
300 lyxerr[Debug::GUI] << "Workarea event: PUSH" << endl;
301 area->workAreaButtonPress(ev->xbutton.x - ob->x,
302 ev->xbutton.y - ob->y,
304 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
307 if (!ev || ev->xbutton.button == 0) break;
308 // Should really have used xbutton.state
309 lyxerr[Debug::GUI] << "Workarea event: RELEASE" << endl;
310 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
311 ev->xbutton.y - ob->y,
319 if (!ev || ! area->scrollbar) break;
320 if (ev->xmotion.x != x_old ||
321 ev->xmotion.y != y_old ||
322 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
324 lyxerr[Debug::GUI] << "Workarea event: MOUSE" << endl;
325 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
326 ev->xmotion.y - ob->y,
336 lyxerr[Debug::KEY] << "Workarea event: KEYBOARD" << endl;
340 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
341 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
342 // XForms < 0.89.5 does not have compose support
343 // so we are using our own compose support
344 LyXLookupString(ev, dummy, 1, &keysym);
346 XLookupString(xke, dummy, 1, &keysym, 0);
348 if (lyxerr.debugging(Debug::KEY)) {
349 char const * tmp = XKeysymToString(key);
350 char const * tmp2 = XKeysymToString(keysym);
351 string const stm = (tmp ? tmp : "");
352 string const stm2 = (tmp2 ? tmp2 : "");
354 lyxerr << "WorkArea: Key is `" << stm << "' ["
355 << key << "]" << endl;
356 lyxerr << "WorkArea: Keysym is `" << stm2 << "' ["
357 << keysym << "]" << endl;
361 if (keysym == NoSymbol) {
363 << "Empty kdb action (probably composing)"
367 KeySym ret_key = keysym;
369 // Note that we need this handling because of a bug
370 // in XForms 0.89, if this bug is resolved in the way I hope
371 // we can just use the keysym directly with out looking
372 // at key at all. (Lgb)
375 // We migth have to add more keysyms here also,
376 // we will do that as the issues arise. (Lgb)
377 if (keysym == XK_space) {
379 lyxerr[Debug::KEY] << "Using keysym [A]"
384 // It seems that this was a bit optimistic...
385 // With this hacking things seems to be better (Lgb)
386 //if (!iscntrl(key)) {
388 // lyxerr[Debug::KEY]
389 // << "Using key [B]\n"
391 // << static_cast<unsigned char>(key)
394 ret_key = (keysym ? keysym : key);
395 lyxerr[Debug::KEY] << "Using keysym [B]"
401 unsigned int const ret_state = xke->state;
403 // If you have a better way to handle "wild-output" of
404 // characters after the key has been released than the one
405 // below, please contact me. (Lgb)
406 static Time last_time_pressed = 0;
407 static unsigned int last_key_pressed = 0;
408 static unsigned int last_state_pressed = 0;
409 lyxerr[Debug::KEY] << "Workarea Diff: "
410 << xke->time - last_time_pressed
412 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
413 && ret_state == last_state_pressed
414 && xke->keycode == last_key_pressed) {
416 << "Workarea: Purging X events." << endl;
417 //lyxerr << "Workarea Events: "
418 // << XEventsQueued(fl_get_display(), QueuedAlready)
420 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
421 XSync(fl_get_display(), 1);
422 // This purge make f.ex. scrolling stop immidiatly when
423 // releasing the PageDown button. The question is if
424 // this purging of XEvents can cause any harm...
425 // after some testing I can see no problems, but
426 // I'd like other reports too.
429 last_time_pressed = xke->time;
430 last_key_pressed = xke->keycode;
431 last_state_pressed = ret_state;
433 area->workAreaKeyPress(ret_key, ret_state);
437 #if FL_REVISION >= 89
439 lyxerr << "Workarea event: KEYRELEASE" << endl;
444 lyxerr[Debug::GUI] << "Workarea event: FOCUS" << endl;
445 area->workAreaFocus();
448 lyxerr[Debug::GUI] << "Workarea event: UNFOCUS" << endl;
449 area->workAreaUnfocus();
452 lyxerr[Debug::GUI] << "Workarea event: ENTER" << endl;
453 area->workAreaEnter();
456 lyxerr[Debug::GUI] << "Workarea event: LEAVE" << endl;
457 area->workAreaLeave();
461 lyxerr[Debug::GUI] << "Workarea event: DBLCLICK" << endl;
462 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
463 ev->xbutton.y - ob->y,
468 lyxerr[Debug::GUI] << "Workarea event: TRPLCLICK" << endl;
469 area->workAreaTripleClick(ev->xbutton.x - ob->x,
470 ev->xbutton.y - ob->y,
475 lyxerr[Debug::GUI] << "Workarea event: OTHER" << endl;
486 string clipboard_selection;
487 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);