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++.
39 XSync(fl_get_display(), 0);
44 // Just a bunch of C wrappers around static members of WorkArea
45 void C_WorkArea_scroll_cb(FL_OBJECT * ob, long buf)
47 WorkArea::scroll_cb(ob, buf);
50 int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
54 return WorkArea::work_area_handler(ob, event,
61 WorkArea::WorkArea(int xpos, int ypos, int width, int height)
62 : workareapixmap(0), painter_(*this)
64 fl_freeze_all_forms();
68 if (lyxerr.debugging(Debug::GUI))
69 lyxerr << "Creating work area: +"
70 << xpos << '+' << ypos << ' '
71 << width << 'x' << height << endl;
74 int const bw = int(std::abs(float(fl_get_border_width())));
76 // We really want to get rid of figinset_canvas.
77 ::figinset_canvas = figinset_canvas = obj =
78 fl_add_canvas(FL_NORMAL_CANVAS,
79 xpos + 1, ypos + 1, 1, 1, "");
80 fl_set_object_boxtype(obj, FL_NO_BOX);
81 fl_set_object_resize(obj, FL_RESIZE_ALL);
82 fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
85 if (lyxerr.debugging(Debug::GUI))
86 lyxerr << "\tbackground box: +"
87 << xpos << '+' << ypos << ' '
88 << width - 15 << 'x' << height << endl;
89 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
93 fl_set_object_resize(obj, FL_RESIZE_ALL);
94 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
100 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
102 ypos, 17, height, "");
103 fl_set_object_boxtype(obj, FL_UP_BOX);
104 fl_set_object_resize(obj, FL_RESIZE_ALL);
105 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
107 fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
112 // Create the workarea pixmap
113 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
115 // We add this object as late as possible to avoit problems
117 if (lyxerr.debugging(Debug::GUI))
118 lyxerr << "\tfree object: +"
119 << xpos + bw << '+' << ypos + bw << ' '
120 << width - 15 - 2 * bw << 'x'
121 << height - 2 * bw << endl;
123 work_area = obj = fl_add_free(FL_ALL_FREE,
124 xpos + bw, ypos + bw,
125 width - 15 - 2 * bw, // scrollbarwidth
127 C_WorkArea_work_area_handler);
128 obj->wantkey = FL_KEY_ALL;
129 obj->u_vdata = this; /* This is how we pass the WorkArea
130 to the work_area_handler. */
131 fl_set_object_boxtype(obj,FL_DOWN_BOX);
132 fl_set_object_resize(obj, FL_RESIZE_ALL);
133 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
135 fl_unfreeze_all_forms();
139 WorkArea::~WorkArea()
142 XFreePixmap(fl_get_display(), workareapixmap);
146 bool WorkArea::belowMouse() const
150 fl_get_mouse(&x, &y, &button);
151 FL_Coord ulx = work_area->form->x + work_area->x;
152 FL_Coord uly = work_area->form->y + work_area->y;
153 FL_Coord w = work_area->w;
154 FL_Coord h = work_area->h;
155 if (x > ulx && y > uly && x < ulx + h && y < uly + w)
160 //lyxerr << "Mouse: (" << x << ", " << y <<") button = " << button << endl;
161 //lyxerr << "Workarea: (" << work_area->x + work_area->form->x << ", " << work_area->y + work_area->form->y << ", " << work_area->w << ", " << work_area->h << ")" << endl;
162 //lyxerr << "Below mouse: " << work_area->belowmouse << endl;
163 //return work_area->belowmouse;
167 void WorkArea::resize(int xpos, int ypos, int width, int height)
169 fl_freeze_all_forms();
171 int const bw = int(std::abs(float(fl_get_border_width())));
174 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
179 fl_set_object_geometry(scrollbar, xpos + width - 15,
182 // Create the workarea pixmap
183 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
186 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
190 fl_unfreeze_all_forms();
195 void WorkArea::createPixmap(int width, int height)
197 static int cur_width = -1;
198 static int cur_height = -1;
200 if (cur_width == width && cur_height == height && workareapixmap)
207 XFreePixmap(fl_get_display(), workareapixmap);
209 if (lyxerr.debugging(Debug::GUI))
210 lyxerr << "Creating pixmap ("
211 << width << 'x' << height << ")" << endl;
213 workareapixmap = XCreatePixmap(fl_get_display(),
214 RootWindow(fl_get_display(), 0),
217 fl_get_visual_depth());
218 if (lyxerr.debugging(Debug::GUI))
219 lyxerr << "\tpixmap=" << workareapixmap << endl;
223 void WorkArea::greyOut() const
225 fl_winset(FL_ObjWin(work_area));
226 fl_rectangle(1, work_area->x, work_area->y,
227 work_area->w, work_area->h, FL_GRAY63);
231 void WorkArea::setFocus() const
233 fl_set_focus_object(work_area->form, work_area);
237 void WorkArea::setScrollbar(double pos, double length_fraction) const
239 fl_set_scrollbar_value(scrollbar, pos);
240 fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
244 void WorkArea::setScrollbarBounds(double l1, double l2) const
246 fl_set_scrollbar_bounds(scrollbar, l1, l2);
250 void WorkArea::setScrollbarIncrements(double inc) const
252 fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
256 // Callback for scrollbar slider
257 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
259 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
260 // If we really want the accellerating scroll we can do that
261 // from here. IMHO that is a waste of effort since we already
262 // have other ways to move fast around in the document. (Lgb)
263 area->scrollCB(fl_get_scrollbar_value(ob));
268 bool Lgb_bug_find_hack = false;
270 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
274 static int x_old = -1;
275 static int y_old = -1;
276 static long scrollbar_value_old = -1;
278 XEvent * ev = static_cast<XEvent*>(xev);
279 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
285 if (!area->work_area ||
286 !area->work_area->form->visible)
288 lyxerr[Debug::GUI] << "Workarea event: DRAW" << endl;
289 area->createPixmap(area->workWidth(), area->height());
290 Lgb_bug_find_hack = true;
291 area->workAreaExpose();
292 Lgb_bug_find_hack = false;
295 if (!ev || ev->xbutton.button == 0) break;
296 // Should really have used xbutton.state
297 lyxerr[Debug::GUI] << "Workarea event: PUSH" << endl;
298 area->workAreaButtonPress(ev->xbutton.x - ob->x,
299 ev->xbutton.y - ob->y,
301 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
304 if (!ev || ev->xbutton.button == 0) break;
305 // Should really have used xbutton.state
306 lyxerr[Debug::GUI] << "Workarea event: RELEASE" << endl;
307 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
308 ev->xbutton.y - ob->y,
316 if (!ev || ! area->scrollbar) break;
317 if (ev->xmotion.x != x_old ||
318 ev->xmotion.y != y_old ||
319 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
321 lyxerr[Debug::GUI] << "Workarea event: MOUSE" << endl;
322 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
323 ev->xmotion.y - ob->y,
333 lyxerr[Debug::KEY] << "Workarea event: KEYBOARD" << endl;
337 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
338 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
339 // XForms < 0.89.5 does not have compose support
340 // so we are using our own compose support
341 LyXLookupString(ev, dummy, 1, &keysym);
343 XLookupString(xke, dummy, 1, &keysym, 0);
345 if (lyxerr.debugging(Debug::KEY)) {
346 char const * tmp = XKeysymToString(key);
347 char const * tmp2 = XKeysymToString(keysym);
348 string const stm = (tmp ? tmp : "");
349 string const stm2 = (tmp2 ? tmp2 : "");
351 lyxerr << "WorkArea: Key is `" << stm << "' ["
352 << key << "]" << endl;
353 lyxerr << "WorkArea: Keysym is `" << stm2 << "' ["
354 << keysym << "]" << endl;
358 if (keysym == NoSymbol) {
360 << "Empty kdb action (probably composing)"
364 KeySym ret_key = keysym;
366 // Note that we need this handling because of a bug
367 // in XForms 0.89, if this bug is resolved in the way I hope
368 // we can just use the keysym directly with out looking
369 // at key at all. (Lgb)
372 // We migth have to add more keysyms here also,
373 // we will do that as the issues arise. (Lgb)
374 if (keysym == XK_space) {
376 lyxerr[Debug::KEY] << "Using keysym [A]"
381 // It seems that this was a bit optimistic...
382 // With this hacking things seems to be better (Lgb)
383 //if (!iscntrl(key)) {
385 // lyxerr[Debug::KEY]
386 // << "Using key [B]\n"
388 // << static_cast<unsigned char>(key)
391 ret_key = (keysym ? keysym : key);
392 lyxerr[Debug::KEY] << "Using keysym [B]"
398 unsigned int const ret_state = xke->state;
400 // If you have a better way to handle "wild-output" of
401 // characters after the key has been released than the one
402 // below, please contact me. (Lgb)
403 static Time last_time_pressed = 0;
404 static unsigned int last_key_pressed = 0;
405 static unsigned int last_state_pressed = 0;
406 lyxerr[Debug::KEY] << "Workarea Diff: "
407 << xke->time - last_time_pressed
409 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
410 && ret_state == last_state_pressed
411 && xke->keycode == last_key_pressed) {
413 << "Workarea: Purging X events." << endl;
414 //lyxerr << "Workarea Events: "
415 // << XEventsQueued(fl_get_display(), QueuedAlready)
417 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
418 XSync(fl_get_display(), 1);
419 // This purge make f.ex. scrolling stop immidiatly when
420 // releasing the PageDown button. The question is if
421 // this purging of XEvents can cause any harm...
422 // after some testing I can see no problems, but
423 // I'd like other reports too.
426 last_time_pressed = xke->time;
427 last_key_pressed = xke->keycode;
428 last_state_pressed = ret_state;
430 area->workAreaKeyPress(ret_key, ret_state);
434 #if FL_REVISION >= 89
436 lyxerr << "Workarea event: KEYRELEASE" << endl;
441 lyxerr[Debug::GUI] << "Workarea event: FOCUS" << endl;
442 area->workAreaFocus();
445 lyxerr[Debug::GUI] << "Workarea event: UNFOCUS" << endl;
446 area->workAreaUnfocus();
449 lyxerr[Debug::GUI] << "Workarea event: ENTER" << endl;
450 area->workAreaEnter();
453 lyxerr[Debug::GUI] << "Workarea event: LEAVE" << endl;
454 area->workAreaLeave();
458 lyxerr[Debug::GUI] << "Workarea event: DBLCLICK" << endl;
459 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
460 ev->xbutton.y - ob->y,
465 lyxerr[Debug::GUI] << "Workarea event: TRPLCLICK" << endl;
466 area->workAreaTripleClick(ev->xbutton.x - ob->x,
467 ev->xbutton.y - ob->y,
472 lyxerr[Debug::GUI] << "Workarea event: OTHER" << endl;
481 static string clipboard_selection;
482 static bool clipboard_read = false;
486 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
487 void const * data, long size)
489 clipboard_selection.erase();
492 clipboard_selection.reserve(size);
493 for (int i = 0; i < size; ++i)
494 clipboard_selection += static_cast<char const *>(data)[i];
495 clipboard_read = true;
501 string const WorkArea::getClipboard() const
503 clipboard_read = false;
505 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
510 while (!clipboard_read) {
511 if (fl_check_forms() == FL_EVENT) {
512 lyxerr << "LyX: This shouldn't happen..." << endl;
516 return clipboard_selection;
520 void WorkArea::putClipboard(string const & s) const
525 fl_stuff_clipboard(work_area, 0, hold.c_str(), hold.size(), 0);