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"
25 FL_OBJECT * figinset_canvas;
27 // need to make the c++ compiler fint the correct version of abs.
28 // This is at least true for g++.
34 XSync(fl_get_display(), 0);
38 // Just a bunch of C wrappers around static members of WorkArea
39 void C_WorkArea_up_cb(FL_OBJECT * ob, long buf)
41 WorkArea::up_cb(ob, buf);
44 void C_WorkArea_down_cb(FL_OBJECT * ob, long buf)
46 WorkArea::down_cb(ob, buf);
49 void C_WorkArea_scroll_cb(FL_OBJECT * ob, long buf)
51 WorkArea::scroll_cb(ob, buf);
54 int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
58 return WorkArea::work_area_handler(ob, event,
65 WorkArea::WorkArea(BufferView * o, int xpos, int ypos, int width, int height)
66 : owner(o), workareapixmap(0), painter_(*this)
68 fl_freeze_all_forms();
72 if (lyxerr.debugging())
73 lyxerr << "Creating work area: +"
74 << xpos << '+' << ypos << ' '
75 << width << 'x' << height << endl;
78 const int bw = int(abs(float(fl_get_border_width())));
80 // We really want to get rid of figinset_canvas.
81 ::figinset_canvas = figinset_canvas = obj =
82 fl_add_canvas(FL_NORMAL_CANVAS,
83 xpos + 1, ypos + 1, 1, 1, "");
84 fl_set_object_boxtype(obj, FL_NO_BOX);
85 fl_set_object_resize(obj, FL_RESIZE_ALL);
86 fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
89 if (lyxerr.debugging())
90 lyxerr << "\tbackground box: +"
91 << xpos << '+' << ypos << ' '
92 << width - 15 << 'x' << height << endl;
93 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
97 fl_set_object_resize(obj, FL_RESIZE_ALL);
98 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
104 // up - scrollbar button
105 fl_set_border_width(-1);
107 if (lyxerr.debugging())
108 lyxerr << "\tup button: +"
109 << xpos + width - 15 << '+' << ypos << ' '
110 << 15 << 'x' << 15 << endl;
111 button_up = obj = fl_add_pixmapbutton(FL_TOUCH_BUTTON,
115 fl_set_object_boxtype(obj,FL_UP_BOX);
116 fl_set_object_color(obj,FL_MCOL,FL_BLUE);
117 fl_set_object_resize(obj, FL_RESIZE_ALL);
118 fl_set_object_gravity(obj,NorthEastGravity, NorthEastGravity);
120 fl_set_object_callback(obj,C_WorkArea_up_cb, 0);
121 fl_set_pixmapbutton_data(obj, const_cast<char**>(up_xpm));
123 // Remove the blue feedback rectangle
124 fl_set_pixmapbutton_focus_outline(obj,0);
126 // the scrollbar slider
127 fl_set_border_width(-bw);
129 if (lyxerr.debugging())
130 lyxerr << "\tscrollbar slider: +"
131 << xpos + width - 15 << '+' << ypos + 15 << ' '
132 << 15 << 'x' << height - 30 << endl;
133 scrollbar = obj = fl_add_slider(FL_VERT_SLIDER,
136 15, height - 30, "");
137 fl_set_object_color(obj,FL_COL1,FL_MCOL);
138 fl_set_object_boxtype(obj, FL_UP_BOX);
139 fl_set_object_resize(obj, FL_RESIZE_ALL);
140 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
142 fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
143 fl_set_slider_precision(obj, 0);
145 // down - scrollbar button
146 fl_set_border_width(-1);
148 if (lyxerr.debugging())
149 lyxerr << "\tdown button: +"
150 << xpos + width - 15 << '+'
151 << ypos + height - 15 << ' '
152 << 15 << 'x' << 15 << endl;
153 button_down = obj = fl_add_pixmapbutton(FL_TOUCH_BUTTON,
157 fl_set_object_boxtype(obj,FL_UP_BOX);
158 fl_set_object_color(obj,FL_MCOL,FL_BLUE);
159 fl_set_object_resize(obj, FL_RESIZE_ALL);
160 fl_set_object_gravity(obj, SouthEastGravity, SouthEastGravity);
162 fl_set_object_callback(obj, C_WorkArea_down_cb, 0);
163 fl_set_pixmapbutton_data(obj, const_cast<char**>(down_xpm));
165 fl_set_border_width(-bw);
167 // Remove the blue feedback rectangle
168 fl_set_pixmapbutton_focus_outline(obj,0);
173 // Create the workarea pixmap
174 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
177 painter_.setDisplay(fl_display);
179 // We add this object as late as possible to avoit problems
181 if (lyxerr.debugging())
182 lyxerr << "\tfree object: +"
183 << xpos + bw << '+' << ypos + bw << ' '
184 << width - 15 - 2 * bw << 'x'
185 << height - 2 * bw << endl;
187 work_area = obj = fl_add_free(FL_INPUT_FREE,
188 xpos + bw, ypos + bw,
189 width - 15 - 2 * bw, // scrollbarwidth
191 C_WorkArea_work_area_handler);
192 obj->wantkey = FL_KEY_TAB;
193 obj->u_vdata = this; /* This is how we pass the WorkArea
194 to the work_area_handler. */
195 fl_set_object_boxtype(obj,FL_DOWN_BOX);
196 fl_set_object_resize(obj, FL_RESIZE_ALL);
197 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
199 fl_unfreeze_all_forms();
203 WorkArea::~WorkArea()
206 XFreePixmap(fl_display, workareapixmap);
210 bool WorkArea::belowMouse() const
212 lyxerr << "Below mouse: " << work_area->belowmouse << endl;
213 return work_area->belowmouse;
217 void WorkArea::resize(int xpos, int ypos, int width, int height)
219 fl_freeze_all_forms();
221 const int bw = int(abs(float(fl_get_border_width())));
224 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
230 // up - scrollbar button
231 fl_set_object_geometry(button_up, xpos + width - 15,
234 // the scrollbar slider
235 fl_set_object_geometry(scrollbar, xpos + width - 15,
238 // down - scrollbar button
239 fl_set_object_geometry(button_down, xpos + width - 15,
243 // Create the workarea pixmap
244 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
247 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
251 fl_unfreeze_all_forms();
256 void WorkArea::createPixmap(int width, int height)
258 static int cur_width = -1;
259 static int cur_height = -1;
261 if (cur_width == width && cur_height == height && workareapixmap)
268 XFreePixmap(fl_display, workareapixmap);
270 if (lyxerr.debugging())
271 lyxerr << "Creating pixmap ("
272 << width << 'x' << height << ")" << endl;
274 workareapixmap = XCreatePixmap(fl_display,
275 RootWindow(fl_display, 0),
278 fl_get_visual_depth());
279 if (lyxerr.debugging())
280 lyxerr << "\tpixmap=" << workareapixmap << endl;
284 void WorkArea::greyOut() const
286 fl_winset(FL_ObjWin(work_area));
287 fl_rectangle(1, work_area->x, work_area->y,
288 work_area->w, work_area->h, FL_GRAY63);
292 void WorkArea::setFocus() const
294 fl_set_focus_object(work_area->form, work_area);
298 void WorkArea::setScrollbar(double pos, double length_fraction) const
300 fl_set_slider_value(scrollbar, pos);
301 fl_set_slider_size(scrollbar, scrollbar->h * length_fraction);
305 void WorkArea::setScrollbarBounds(double l1, double l2) const
307 fl_set_slider_bounds(scrollbar, l1, l2);
311 void WorkArea::setScrollbarIncrements(float inc) const
313 fl_set_slider_increment(scrollbar, work_area->h - inc, inc);
317 void WorkArea::up_cb(FL_OBJECT * ob, long)
319 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
320 XEvent const * ev2 = fl_last_event();
321 static long time = 0;
322 ev2 = fl_last_event();
323 if (ev2->type == ButtonPress || ev2->type == ButtonRelease)
325 //area->up(time++, fl_get_button_numb(ob));
326 area->owner->upCB(time++, fl_get_button_numb(ob));
330 void WorkArea::down_cb(FL_OBJECT * ob, long)
332 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
333 XEvent const * ev2 = fl_last_event();
334 static long time = 0;
335 if (ev2->type == ButtonPress || ev2->type == ButtonRelease)
337 //area->down(time++, fl_get_button_numb(ob));
338 area->owner->downCB(time++, fl_get_button_numb(ob));
342 // Callback for scrollbar slider
343 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
345 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
347 //area->scroll(fl_get_slider_value(ob));
348 area->owner->scrollCB(fl_get_slider_value(ob));
352 bool Lgb_bug_find_hack = false;
354 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
356 int /*key*/, void * xev)
358 static int x_old = -1;
359 static int y_old = -1;
360 static long scrollbar_value_old = -1;
362 XEvent * ev = static_cast<XEvent*>(xev);
363 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
369 if (!area->work_area ||
370 !area->work_area->form->visible)
372 lyxerr.debug() << "Workarea event: DRAW" << endl;
373 area->createPixmap(area->workWidth(), area->height());
374 Lgb_bug_find_hack = true;
375 area->owner->workAreaExpose();
376 Lgb_bug_find_hack = false;
380 // Should really have used xbutton.state
381 lyxerr.debug() << "Workarea event: PUSH" << endl;
382 area->owner->workAreaButtonPress(ev->xbutton.x - ob->x,
383 ev->xbutton.y - ob->y,
388 // Should really have used xbutton.state
389 lyxerr.debug() << "Workarea event: RELEASE" << endl;
390 area->owner->workAreaButtonRelease(ev->xbutton.x - ob->x,
391 ev->xbutton.y - ob->y,
395 if (!ev || ! area->scrollbar) break;
396 if (ev->xmotion.x != x_old ||
397 ev->xmotion.y != y_old ||
398 fl_get_slider_value(area->scrollbar) != scrollbar_value_old) {
399 lyxerr.debug() << "Workarea event: MOUSE" << endl;
400 area->owner->workAreaMotionNotify(ev->xmotion.x - ob->x,
401 ev->xmotion.y - ob->y,
405 // Done by the raw callback:
406 // case FL_KEYBOARD: WorkAreaKeyPress(ob, 0,0,0,ev,0); break;
408 lyxerr.debug() << "Workarea event: FOCUS" << endl;
411 lyxerr.debug() << "Workarea event: UNFOCUS" << endl;
414 lyxerr.debug() << "Workarea event: ENTER" << endl;
417 lyxerr.debug() << "Workarea event: LEAVE" << endl;
421 lyxerr.debug() << "Workarea event: DBLCLICK" << endl;
422 area->owner->doubleClick(ev->xbutton.x - ob->x,
423 ev->xbutton.y - ob->y,
428 lyxerr.debug() << "Workarea event: TRPLCLICK" << endl;
429 area->owner->tripleClick(ev->xbutton.x - ob->x,
430 ev->xbutton.y - ob->y,
435 if (ev->type == SelectionNotify) {
436 lyxerr.debug() << "Workarea event: SELECTION" << endl;
437 area->owner->workAreaSelectionNotify(area->work_area->form->window, ev);
439 lyxerr.debug() << "Workarea event: OTHER" << endl;