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);
45 // Just a bunch of C wrappers around static members of WorkArea
46 void C_WorkArea_scroll_cb(FL_OBJECT * ob, long buf)
48 WorkArea::scroll_cb(ob, buf);
51 int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
55 return WorkArea::work_area_handler(ob, event,
62 WorkArea::WorkArea(BufferView * o, int xpos, int ypos, int width, int height)
63 : owner_(o), workareapixmap(0), painter_(*this)
65 fl_freeze_all_forms();
69 if (lyxerr.debugging(Debug::GUI))
70 lyxerr << "Creating work area: +"
71 << xpos << '+' << ypos << ' '
72 << width << 'x' << height << endl;
75 const int bw = int(std::abs(float(fl_get_border_width())));
77 // We really want to get rid of figinset_canvas.
78 ::figinset_canvas = figinset_canvas = obj =
79 fl_add_canvas(FL_NORMAL_CANVAS,
80 xpos + 1, ypos + 1, 1, 1, "");
81 fl_set_object_boxtype(obj, FL_NO_BOX);
82 fl_set_object_resize(obj, FL_RESIZE_ALL);
83 fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
86 if (lyxerr.debugging(Debug::GUI))
87 lyxerr << "\tbackground box: +"
88 << xpos << '+' << ypos << ' '
89 << width - 15 << 'x' << height << endl;
90 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
94 fl_set_object_resize(obj, FL_RESIZE_ALL);
95 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
101 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
103 ypos, 17, height, "");
104 fl_set_object_boxtype(obj, FL_UP_BOX);
105 fl_set_object_resize(obj, FL_RESIZE_ALL);
106 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
108 fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
113 // Create the workarea pixmap
114 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
116 // We add this object as late as possible to avoit problems
118 if (lyxerr.debugging(Debug::GUI))
119 lyxerr << "\tfree object: +"
120 << xpos + bw << '+' << ypos + bw << ' '
121 << width - 15 - 2 * bw << 'x'
122 << height - 2 * bw << endl;
124 work_area = obj = fl_add_free(FL_ALL_FREE,
125 xpos + bw, ypos + bw,
126 width - 15 - 2 * bw, // scrollbarwidth
128 C_WorkArea_work_area_handler);
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_get_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_get_display(), workareapixmap);
210 if (lyxerr.debugging(Debug::GUI))
211 lyxerr << "Creating pixmap ("
212 << width << 'x' << height << ")" << endl;
214 workareapixmap = XCreatePixmap(fl_get_display(),
215 RootWindow(fl_get_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));
269 bool Lgb_bug_find_hack = false;
271 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
275 static int x_old = -1;
276 static int y_old = -1;
277 static long scrollbar_value_old = -1;
279 XEvent * ev = static_cast<XEvent*>(xev);
280 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
286 if (!area->work_area ||
287 !area->work_area->form->visible)
289 lyxerr[Debug::GUI] << "Workarea event: DRAW" << endl;
290 area->createPixmap(area->workWidth(), area->height());
291 Lgb_bug_find_hack = true;
292 area->workAreaExpose();
293 Lgb_bug_find_hack = false;
297 // Should really have used xbutton.state
298 lyxerr[Debug::GUI] << "Workarea event: PUSH" << endl;
299 area->workAreaButtonPress(ev->xbutton.x - ob->x,
300 ev->xbutton.y - ob->y,
302 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
306 // Should really have used xbutton.state
307 lyxerr[Debug::GUI] << "Workarea event: RELEASE" << endl;
308 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
309 ev->xbutton.y - ob->y,
317 if (!ev || ! area->scrollbar) break;
318 if (ev->xmotion.x != x_old ||
319 ev->xmotion.y != y_old ||
320 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
322 lyxerr[Debug::GUI] << "Workarea event: MOUSE" << endl;
323 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
324 ev->xmotion.y - ob->y,
334 lyxerr[Debug::KEY] << "Workarea event: KEYBOARD" << endl;
338 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
340 // XForms < 0.89 does not have compose support
341 // so we are using our own compose support
342 LyXLookupString(ev, dummy, 1, &keysym);
344 XLookupString(xke, dummy, 1, &keysym, 0);
346 if (lyxerr.debugging(Debug::KEY)) {
347 char const * tmp = XKeysymToString(key);
348 char const * tmp2 = XKeysymToString(keysym);
349 string stm = (tmp ? tmp : "");
350 string stm2 = (tmp2 ? tmp2 : "");
352 lyxerr << "WorkArea: Key is `" << stm << "' ["
353 << key << "]" << endl;
354 lyxerr << "WorkArea: Keysym is `" << stm2 << "' ["
355 << keysym << "]" << endl;
359 if (keysym == NoSymbol) {
361 << "Empty kdb action (probably composing)"
365 KeySym ret_key = keysym;
367 // Note that we need this handling because of a bug
368 // in XForms 0.89, if this bug is resolved in the way I hope
369 // we can just use the keysym directly with out looking
370 // at key at all. (Lgb)
373 // We migth have to add more keysyms here also,
374 // we will do that as the issues arise. (Lgb)
375 if (keysym == XK_space)
380 ret_key = (keysym ? keysym : key);
384 unsigned int ret_state = xke->state;
386 // If you have a better way to handle "wild-output" of
387 // characters after the key has been released than the one
388 // below, please contact me. (Lgb)
389 static Time last_time_pressed = 0;
390 static unsigned int last_key_pressed = 0;
391 static unsigned int last_state_pressed = 0;
392 lyxerr[Debug::KEY] << "Workarea Diff: "
393 << xke->time - last_time_pressed
395 if (xke->time - last_time_pressed < 35 // should perhaps be tunable
396 && xke->state == last_state_pressed
397 && xke->keycode == last_key_pressed) {
399 << "Workarea: Purging X events." << endl;
400 //lyxerr << "Workarea Events: "
401 // << XEventsQueued(fl_get_display(), QueuedAlready)
403 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
404 XSync(fl_get_display(), 1);
405 // This purge make f.ex. scrolling stop immidiatly when
406 // releasing the PageDown button. The question is if
407 // this purging of XEvents can cause any harm...
408 // after some testing I can see no problems, but
409 // I'd like other reports too.
412 last_time_pressed = xke->time;
413 last_key_pressed = xke->keycode;
414 last_state_pressed = xke->state;
416 area->workAreaKeyPress(ret_key, ret_state);
420 #if FL_REVISION >= 89
422 lyxerr << "Workarea event: KEYRELEASE" << endl;
427 lyxerr[Debug::GUI] << "Workarea event: FOCUS" << endl;
428 area->workAreaFocus();
431 lyxerr[Debug::GUI] << "Workarea event: UNFOCUS" << endl;
432 area->workAreaUnfocus();
435 lyxerr[Debug::GUI] << "Workarea event: ENTER" << endl;
436 area->workAreaEnter();
439 lyxerr[Debug::GUI] << "Workarea event: LEAVE" << endl;
440 area->workAreaLeave();
444 lyxerr[Debug::GUI] << "Workarea event: DBLCLICK" << endl;
445 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
446 ev->xbutton.y - ob->y,
451 lyxerr[Debug::GUI] << "Workarea event: TRPLCLICK" << endl;
452 area->workAreaTripleClick(ev->xbutton.x - ob->x,
453 ev->xbutton.y - ob->y,
458 lyxerr[Debug::GUI] << "Workarea event: OTHER" << endl;
467 static string clipboard_selection;
468 static bool clipboard_read = false;
472 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
473 void const * data, long size)
475 clipboard_selection.erase();
478 clipboard_selection.reserve(size);
479 for (int i = 0; i < size; ++i)
480 clipboard_selection += static_cast<char const *>(data)[i];
481 clipboard_read = true;
487 string const WorkArea::getClipboard() const
489 clipboard_read = false;
491 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
496 while (!clipboard_read) {
497 if (fl_check_forms() == FL_EVENT) {
498 lyxerr << "LyX: This shouldn't happen..." << endl;
502 return clipboard_selection;
506 void WorkArea::putClipboard(string const & s) const
511 fl_stuff_clipboard(work_area, 0, hold.c_str(), hold.size(), 0);