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"
25 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
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 int const 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 int const 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);
339 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
340 // XForms < 0.89.5 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 // It seems that this was a bit optimistic...
381 // With this hacking things seems to be better (Lgb)
382 if (static_cast<unsigned char>(key) == key
386 ret_key = (keysym ? keysym : key);
390 unsigned int ret_state = xke->state;
392 // If you have a better way to handle "wild-output" of
393 // characters after the key has been released than the one
394 // below, please contact me. (Lgb)
395 static Time last_time_pressed = 0;
396 static unsigned int last_key_pressed = 0;
397 static unsigned int last_state_pressed = 0;
398 lyxerr[Debug::KEY] << "Workarea Diff: "
399 << xke->time - last_time_pressed
401 if (xke->time - last_time_pressed < 35 // should perhaps be tunable
402 && xke->state == last_state_pressed
403 && xke->keycode == last_key_pressed) {
405 << "Workarea: Purging X events." << endl;
406 //lyxerr << "Workarea Events: "
407 // << XEventsQueued(fl_get_display(), QueuedAlready)
409 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
410 XSync(fl_get_display(), 1);
411 // This purge make f.ex. scrolling stop immidiatly when
412 // releasing the PageDown button. The question is if
413 // this purging of XEvents can cause any harm...
414 // after some testing I can see no problems, but
415 // I'd like other reports too.
418 last_time_pressed = xke->time;
419 last_key_pressed = xke->keycode;
420 last_state_pressed = xke->state;
422 area->workAreaKeyPress(ret_key, ret_state);
426 #if FL_REVISION >= 89
428 lyxerr << "Workarea event: KEYRELEASE" << endl;
433 lyxerr[Debug::GUI] << "Workarea event: FOCUS" << endl;
434 area->workAreaFocus();
437 lyxerr[Debug::GUI] << "Workarea event: UNFOCUS" << endl;
438 area->workAreaUnfocus();
441 lyxerr[Debug::GUI] << "Workarea event: ENTER" << endl;
442 area->workAreaEnter();
445 lyxerr[Debug::GUI] << "Workarea event: LEAVE" << endl;
446 area->workAreaLeave();
450 lyxerr[Debug::GUI] << "Workarea event: DBLCLICK" << endl;
451 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
452 ev->xbutton.y - ob->y,
457 lyxerr[Debug::GUI] << "Workarea event: TRPLCLICK" << endl;
458 area->workAreaTripleClick(ev->xbutton.x - ob->x,
459 ev->xbutton.y - ob->y,
464 lyxerr[Debug::GUI] << "Workarea event: OTHER" << endl;
473 static string clipboard_selection;
474 static bool clipboard_read = false;
478 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
479 void const * data, long size)
481 clipboard_selection.erase();
484 clipboard_selection.reserve(size);
485 for (int i = 0; i < size; ++i)
486 clipboard_selection += static_cast<char const *>(data)[i];
487 clipboard_read = true;
493 string const WorkArea::getClipboard() const
495 clipboard_read = false;
497 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
502 while (!clipboard_read) {
503 if (fl_check_forms() == FL_EVENT) {
504 lyxerr << "LyX: This shouldn't happen..." << endl;
508 return clipboard_selection;
512 void WorkArea::putClipboard(string const & s) const
517 fl_stuff_clipboard(work_area, 0, hold.c_str(), hold.size(), 0);