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"
21 #include "BufferView.h"
26 #include "lyxlookup.h"
31 FL_OBJECT * figinset_canvas;
33 // needed to make the c++ compiler find the correct version of abs.
34 // This is at least true for g++.
40 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(BufferView * o, int xpos, int ypos, int width, int height)
62 : owner_(o), 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 const int 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_INPUT_FREE,
124 xpos + bw, ypos + bw,
125 width - 15 - 2 * bw, // scrollbarwidth
127 C_WorkArea_work_area_handler);
128 //obj->wantkey = FL_KEY_TAB;
129 obj->wantkey = FL_KEY_ALL;
130 obj->u_vdata = this; /* This is how we pass the WorkArea
131 to the work_area_handler. */
132 fl_set_object_boxtype(obj,FL_DOWN_BOX);
133 fl_set_object_resize(obj, FL_RESIZE_ALL);
134 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
136 fl_unfreeze_all_forms();
140 WorkArea::~WorkArea()
143 XFreePixmap(fl_display, workareapixmap);
147 bool WorkArea::belowMouse() const
151 fl_get_mouse(&x, &y, &button);
152 FL_Coord ulx = work_area->form->x + work_area->x;
153 FL_Coord uly = work_area->form->y + work_area->y;
154 FL_Coord w = work_area->w;
155 FL_Coord h = work_area->h;
156 if (x > ulx && y > uly && x < ulx + h && y < uly + w)
161 //lyxerr << "Mouse: (" << x << ", " << y <<") button = " << button << endl;
162 //lyxerr << "Workarea: (" << work_area->x + work_area->form->x << ", " << work_area->y + work_area->form->y << ", " << work_area->w << ", " << work_area->h << ")" << endl;
163 //lyxerr << "Below mouse: " << work_area->belowmouse << endl;
164 //return work_area->belowmouse;
168 void WorkArea::resize(int xpos, int ypos, int width, int height)
170 fl_freeze_all_forms();
172 const int bw = int(std::abs(float(fl_get_border_width())));
175 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
180 fl_set_object_geometry(scrollbar, xpos + width - 15,
183 // Create the workarea pixmap
184 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
187 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
191 fl_unfreeze_all_forms();
196 void WorkArea::createPixmap(int width, int height)
198 static int cur_width = -1;
199 static int cur_height = -1;
201 if (cur_width == width && cur_height == height && workareapixmap)
208 XFreePixmap(fl_display, workareapixmap);
210 if (lyxerr.debugging(Debug::GUI))
211 lyxerr << "Creating pixmap ("
212 << width << 'x' << height << ")" << endl;
214 workareapixmap = XCreatePixmap(fl_display,
215 RootWindow(fl_display, 0),
218 fl_get_visual_depth());
219 if (lyxerr.debugging(Debug::GUI))
220 lyxerr << "\tpixmap=" << workareapixmap << endl;
224 void WorkArea::greyOut() const
226 fl_winset(FL_ObjWin(work_area));
227 fl_rectangle(1, work_area->x, work_area->y,
228 work_area->w, work_area->h, FL_GRAY63);
232 void WorkArea::setFocus() const
234 fl_set_focus_object(work_area->form, work_area);
238 void WorkArea::setScrollbar(double pos, double length_fraction) const
240 fl_set_scrollbar_value(scrollbar, pos);
241 fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
245 void WorkArea::setScrollbarBounds(double l1, double l2) const
247 fl_set_scrollbar_bounds(scrollbar, l1, l2);
251 void WorkArea::setScrollbarIncrements(double inc) const
253 fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
257 // Callback for scrollbar slider
258 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
260 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
261 // If we really want the accellerating scroll we can do that
262 // from here. IMHO that is a waste of effort since we already
263 // have other ways to move fast around in the document. (Lgb)
264 area->owner_->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;
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);
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,
312 if (!ev || ! area->scrollbar) break;
313 if (ev->xmotion.x != x_old ||
314 ev->xmotion.y != y_old ||
315 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
317 lyxerr[Debug::GUI] << "Workarea event: MOUSE" << endl;
318 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
319 ev->xmotion.y - ob->y,
325 lyxerr[Debug::KEY] << "Workarea event: KEYBOARD" << endl;
329 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
331 // XForms < 0.89 does not have compose support
332 // so we are using our own compose support
333 LyXLookupString(ev, dummy, 1, &keysym);
335 XLookupString(xke, dummy, 1, &keysym, 0);
337 if (lyxerr.debugging(Debug::KEY)) {
338 char const * tmp = XKeysymToString(key);
339 char const * tmp2 = XKeysymToString(keysym);
340 string stm = (tmp ? tmp : "");
341 string stm2 = (tmp2 ? tmp2 : "");
343 lyxerr << "WorkArea: Key is `" << stm << "' ["
344 << key << "]" << endl;
345 lyxerr << "WorkArea: Keysym is `" << stm2 << "' ["
346 << keysym << "]" << endl;
350 if (keysym == NoSymbol) {
352 << "Empty kdb action (probably composing)"
356 KeySym ret_key = keysym;
358 // Ok, this is a bit simplistic...seems that the rules
359 // need to be a bit more...
361 KeySym ret_key = (keysym ? keysym : key);
363 unsigned int ret_state = xke->state;
365 static Time last_time_pressed = 0;
366 static unsigned int last_key_pressed = 0;
367 static unsigned int last_state_pressed = 0;
368 //lyxerr << "Workarea Diff: " << xke->time - last_time_pressed
370 if (xke->time - last_time_pressed < 40 // should perhaps be tunable
371 && xke->state == last_state_pressed
372 && xke->keycode == last_key_pressed) {
374 << "Workarea: Purging X events." << endl;
375 //lyxerr << "Workarea Events: "
376 // << XEventsQueued(fl_get_display(), QueuedAlready)
378 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
379 XSync(fl_get_display(), 1);
380 // This purge make f.ex. scrolling stop immidiatly when
381 // releasing the PageDown button. The question is if
382 // this purging of XEvents can cause any harm...
383 // after some testing I can see no problems, but
384 // I'd like other reports too.
387 last_time_pressed = xke->time;
388 last_key_pressed = xke->keycode;
389 last_state_pressed = xke->state;
391 area->workAreaKeyPress(ret_key, ret_state);
395 lyxerr[Debug::GUI] << "Workarea event: FOCUS" << endl;
396 area->workAreaFocus();
399 lyxerr[Debug::GUI] << "Workarea event: UNFOCUS" << endl;
400 area->workAreaUnfocus();
403 lyxerr[Debug::GUI] << "Workarea event: ENTER" << endl;
404 area->workAreaEnter();
407 lyxerr[Debug::GUI] << "Workarea event: LEAVE" << endl;
408 area->workAreaLeave();
412 lyxerr[Debug::GUI] << "Workarea event: DBLCLICK" << endl;
413 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
414 ev->xbutton.y - ob->y,
419 lyxerr[Debug::GUI] << "Workarea event: TRPLCLICK" << endl;
420 area->workAreaTripleClick(ev->xbutton.x - ob->x,
421 ev->xbutton.y - ob->y,
426 lyxerr[Debug::GUI] << "Workarea event: OTHER" << endl;
435 static string clipboard_selection;
436 static bool clipboard_read = false;
440 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
441 void const * data, long size)
443 clipboard_selection.erase();
446 clipboard_selection.reserve(size);
447 for (int i = 0; i < size; ++i)
448 clipboard_selection += static_cast<char const *>(data)[i];
449 clipboard_read = true;
454 string WorkArea::getClipboard() const
456 clipboard_read = false;
458 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
463 while (!clipboard_read) {
464 if (fl_check_forms() == FL_EVENT) {
465 lyxerr << "LyX: This shouldn't happen..." << endl;
469 return clipboard_selection;
473 void WorkArea::putClipboard(string const & s) const
478 fl_stuff_clipboard(work_area, 0, hold.c_str(), hold.size(), 0);