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
22 #include "support/lstrings.h"
23 #include "BufferView.h"
27 FL_OBJECT * figinset_canvas;
29 // need to make the c++ compiler fint the correct version of abs.
30 // This is at least true for g++.
36 XSync(fl_get_display(), 0);
40 // Just a bunch of C wrappers around static members of WorkArea
41 void C_WorkArea_up_cb(FL_OBJECT * ob, long buf)
43 WorkArea::up_cb(ob, buf);
46 void C_WorkArea_down_cb(FL_OBJECT * ob, long buf)
48 WorkArea::down_cb(ob, buf);
51 void C_WorkArea_scroll_cb(FL_OBJECT * ob, long buf)
53 WorkArea::scroll_cb(ob, buf);
56 int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
60 return WorkArea::work_area_handler(ob, event,
67 WorkArea::WorkArea(BufferView * o, int xpos, int ypos, int width, int height)
68 : owner(o), workareapixmap(0), painter_(*this)
70 fl_freeze_all_forms();
74 if (lyxerr.debugging())
75 lyxerr << "Creating work area: +"
76 << xpos << '+' << ypos << ' '
77 << width << 'x' << height << endl;
80 const int bw = int(abs(float(fl_get_border_width())));
82 // We really want to get rid of figinset_canvas.
83 ::figinset_canvas = figinset_canvas = obj =
84 fl_add_canvas(FL_NORMAL_CANVAS,
85 xpos + 1, ypos + 1, 1, 1, "");
86 fl_set_object_boxtype(obj, FL_NO_BOX);
87 fl_set_object_resize(obj, FL_RESIZE_ALL);
88 fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
91 if (lyxerr.debugging())
92 lyxerr << "\tbackground box: +"
93 << xpos << '+' << ypos << ' '
94 << width - 15 << 'x' << height << endl;
95 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
99 fl_set_object_resize(obj, FL_RESIZE_ALL);
100 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
106 // up - scrollbar button
107 fl_set_border_width(-1);
109 if (lyxerr.debugging())
110 lyxerr << "\tup button: +"
111 << xpos + width - 15 << '+' << ypos << ' '
112 << 15 << 'x' << 15 << endl;
113 button_up = obj = fl_add_pixmapbutton(FL_TOUCH_BUTTON,
117 fl_set_object_boxtype(obj,FL_UP_BOX);
118 fl_set_object_color(obj,FL_MCOL,FL_BLUE);
119 fl_set_object_resize(obj, FL_RESIZE_ALL);
120 fl_set_object_gravity(obj,NorthEastGravity, NorthEastGravity);
122 fl_set_object_callback(obj,C_WorkArea_up_cb, 0);
123 fl_set_pixmapbutton_data(obj, const_cast<char**>(up_xpm));
125 // Remove the blue feedback rectangle
126 fl_set_pixmapbutton_focus_outline(obj,0);
128 // the scrollbar slider
129 fl_set_border_width(-bw);
131 if (lyxerr.debugging())
132 lyxerr << "\tscrollbar slider: +"
133 << xpos + width - 15 << '+' << ypos + 15 << ' '
134 << 15 << 'x' << height - 30 << endl;
135 scrollbar = obj = fl_add_slider(FL_VERT_SLIDER,
138 15, height - 30, "");
139 fl_set_object_color(obj,FL_COL1,FL_MCOL);
140 fl_set_object_boxtype(obj, FL_UP_BOX);
141 fl_set_object_resize(obj, FL_RESIZE_ALL);
142 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
144 fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
145 fl_set_slider_precision(obj, 0);
147 // down - scrollbar button
148 fl_set_border_width(-1);
150 if (lyxerr.debugging())
151 lyxerr << "\tdown button: +"
152 << xpos + width - 15 << '+'
153 << ypos + height - 15 << ' '
154 << 15 << 'x' << 15 << endl;
155 button_down = obj = fl_add_pixmapbutton(FL_TOUCH_BUTTON,
159 fl_set_object_boxtype(obj,FL_UP_BOX);
160 fl_set_object_color(obj,FL_MCOL,FL_BLUE);
161 fl_set_object_resize(obj, FL_RESIZE_ALL);
162 fl_set_object_gravity(obj, SouthEastGravity, SouthEastGravity);
164 fl_set_object_callback(obj, C_WorkArea_down_cb, 0);
165 fl_set_pixmapbutton_data(obj, const_cast<char**>(down_xpm));
167 fl_set_border_width(-bw);
169 // Remove the blue feedback rectangle
170 fl_set_pixmapbutton_focus_outline(obj,0);
175 // Create the workarea pixmap
176 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
179 painter_.setDisplay(fl_display);
181 // We add this object as late as possible to avoit problems
183 if (lyxerr.debugging())
184 lyxerr << "\tfree object: +"
185 << xpos + bw << '+' << ypos + bw << ' '
186 << width - 15 - 2 * bw << 'x'
187 << height - 2 * bw << endl;
189 work_area = obj = fl_add_free(FL_INPUT_FREE,
190 xpos + bw, ypos + bw,
191 width - 15 - 2 * bw, // scrollbarwidth
193 C_WorkArea_work_area_handler);
194 obj->wantkey = FL_KEY_TAB;
195 obj->u_vdata = this; /* This is how we pass the WorkArea
196 to the work_area_handler. */
197 fl_set_object_boxtype(obj,FL_DOWN_BOX);
198 fl_set_object_resize(obj, FL_RESIZE_ALL);
199 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
201 fl_unfreeze_all_forms();
205 WorkArea::~WorkArea()
208 XFreePixmap(fl_display, workareapixmap);
212 bool WorkArea::belowMouse() const
214 lyxerr << "Below mouse: " << work_area->belowmouse << endl;
215 return work_area->belowmouse;
219 void WorkArea::resize(int xpos, int ypos, int width, int height)
221 fl_freeze_all_forms();
223 const int bw = int(abs(float(fl_get_border_width())));
226 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
232 // up - scrollbar button
233 fl_set_object_geometry(button_up, xpos + width - 15,
236 // the scrollbar slider
237 fl_set_object_geometry(scrollbar, xpos + width - 15,
240 // down - scrollbar button
241 fl_set_object_geometry(button_down, xpos + width - 15,
245 // Create the workarea pixmap
246 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
249 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
253 fl_unfreeze_all_forms();
258 void WorkArea::createPixmap(int width, int height)
260 static int cur_width = -1;
261 static int cur_height = -1;
263 if (cur_width == width && cur_height == height && workareapixmap)
270 XFreePixmap(fl_display, workareapixmap);
272 if (lyxerr.debugging())
273 lyxerr << "Creating pixmap ("
274 << width << 'x' << height << ")" << endl;
276 workareapixmap = XCreatePixmap(fl_display,
277 RootWindow(fl_display, 0),
280 fl_get_visual_depth());
281 if (lyxerr.debugging())
282 lyxerr << "\tpixmap=" << workareapixmap << endl;
286 void WorkArea::greyOut() const
288 fl_winset(FL_ObjWin(work_area));
289 fl_rectangle(1, work_area->x, work_area->y,
290 work_area->w, work_area->h, FL_GRAY63);
294 void WorkArea::setFocus() const
296 fl_set_focus_object(work_area->form, work_area);
300 void WorkArea::setScrollbar(double pos, double length_fraction) const
302 fl_set_slider_value(scrollbar, pos);
303 fl_set_slider_size(scrollbar, scrollbar->h * length_fraction);
307 void WorkArea::setScrollbarBounds(double l1, double l2) const
309 fl_set_slider_bounds(scrollbar, l1, l2);
313 void WorkArea::setScrollbarIncrements(float inc) const
315 fl_set_slider_increment(scrollbar, work_area->h - inc, inc);
319 void WorkArea::up_cb(FL_OBJECT * ob, long)
321 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
322 XEvent const * ev2 = fl_last_event();
323 static long time = 0;
324 ev2 = fl_last_event();
325 if (ev2->type == ButtonPress || ev2->type == ButtonRelease)
327 //area->up(time++, fl_get_button_numb(ob));
328 area->owner->upCB(time++, fl_get_button_numb(ob));
332 void WorkArea::down_cb(FL_OBJECT * ob, long)
334 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
335 XEvent const * ev2 = fl_last_event();
336 static long time = 0;
337 if (ev2->type == ButtonPress || ev2->type == ButtonRelease)
339 //area->down(time++, fl_get_button_numb(ob));
340 area->owner->downCB(time++, fl_get_button_numb(ob));
344 // Callback for scrollbar slider
345 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
347 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
349 //area->scroll(fl_get_slider_value(ob));
350 area->owner->scrollCB(fl_get_slider_value(ob));
354 bool Lgb_bug_find_hack = false;
356 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
358 int /*key*/, void * xev)
360 static int x_old = -1;
361 static int y_old = -1;
362 static long scrollbar_value_old = -1;
364 XEvent * ev = static_cast<XEvent*>(xev);
365 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
371 if (!area->work_area ||
372 !area->work_area->form->visible)
374 lyxerr.debug() << "Workarea event: DRAW" << endl;
375 area->createPixmap(area->workWidth(), area->height());
376 Lgb_bug_find_hack = true;
377 area->owner->workAreaExpose();
378 Lgb_bug_find_hack = false;
382 // Should really have used xbutton.state
383 lyxerr.debug() << "Workarea event: PUSH" << endl;
384 area->owner->workAreaButtonPress(ev->xbutton.x - ob->x,
385 ev->xbutton.y - ob->y,
390 // Should really have used xbutton.state
391 lyxerr.debug() << "Workarea event: RELEASE" << endl;
392 area->owner->workAreaButtonRelease(ev->xbutton.x - ob->x,
393 ev->xbutton.y - ob->y,
397 if (!ev || ! area->scrollbar) break;
398 if (ev->xmotion.x != x_old ||
399 ev->xmotion.y != y_old ||
400 fl_get_slider_value(area->scrollbar) != scrollbar_value_old) {
401 lyxerr.debug() << "Workarea event: MOUSE" << endl;
402 area->owner->workAreaMotionNotify(ev->xmotion.x - ob->x,
403 ev->xmotion.y - ob->y,
407 // Done by the raw callback:
408 // case FL_KEYBOARD: WorkAreaKeyPress(ob, 0,0,0,ev,0); break;
410 lyxerr.debug() << "Workarea event: FOCUS" << endl;
413 lyxerr.debug() << "Workarea event: UNFOCUS" << endl;
416 lyxerr.debug() << "Workarea event: ENTER" << endl;
419 lyxerr.debug() << "Workarea event: LEAVE" << endl;
423 lyxerr.debug() << "Workarea event: DBLCLICK" << endl;
424 area->owner->doubleClick(ev->xbutton.x - ob->x,
425 ev->xbutton.y - ob->y,
430 lyxerr.debug() << "Workarea event: TRPLCLICK" << endl;
431 area->owner->tripleClick(ev->xbutton.x - ob->x,
432 ev->xbutton.y - ob->y,
437 if (ev->type == SelectionNotify) {
438 lyxerr.debug() << "Workarea event: SELECTION" << endl;
439 area->owner->workAreaSelectionNotify(area->work_area->form->window, ev);
441 lyxerr.debug() << "Workarea event: OTHER" << endl;