]> git.lyx.org Git - lyx.git/blob - src/WorkArea.C
read changelog
[lyx.git] / src / WorkArea.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *        
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12 #include <cmath>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "WorkArea.h"
19 #include "up.xpm"
20 #include "down.xpm"
21 #include "debug.h"
22 #include "support/lstrings.h"
23 #include "BufferView.h"
24
25 FL_OBJECT * figinset_canvas;
26
27 static inline
28 void waitForX()
29 {
30         XSync(fl_get_display(), 0);
31 }
32
33 extern "C" {
34 // Just a bunch of C wrappers around static members of WorkArea
35         void C_WorkArea_up_cb(FL_OBJECT * ob, long buf)
36         {
37                 WorkArea::up_cb(ob, buf);
38         }
39
40         void C_WorkArea_down_cb(FL_OBJECT * ob, long buf)
41         {
42                 WorkArea::down_cb(ob, buf);
43         }
44
45         void C_WorkArea_scroll_cb(FL_OBJECT * ob, long buf)
46         {
47                 WorkArea::scroll_cb(ob, buf);
48         }
49
50         int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
51                                            FL_Coord, FL_Coord, 
52                                            int key, void * xev)
53         {
54                 return WorkArea::work_area_handler(ob, event,
55                                                    0, 0, key, xev);
56         }
57 }
58
59
60
61 WorkArea::WorkArea(BufferView * o, int xpos, int ypos, int width, int height)
62         : owner(o), workareapixmap(0)
63 #ifdef USE_PAINTER
64         , painter_(*this)
65 #endif
66 {
67         fl_freeze_all_forms();
68
69         figinset_canvas = 0;
70         
71         lyxerr << "Creating work area: +"
72                << xpos << '+' << ypos << ' '
73                << width << 'x' << height << endl;
74         //
75         FL_OBJECT * obj;
76         const int bw = int(abs(float(fl_get_border_width())));
77
78         // We really want to get rid of figinset_canvas.
79         ::figinset_canvas = figinset_canvas = obj =
80                   fl_add_canvas(FL_NORMAL_CANVAS,
81                                 xpos + 1, ypos + 1, 1, 1, "");
82         fl_set_object_boxtype(obj, FL_NO_BOX);
83         fl_set_object_resize(obj, FL_RESIZE_ALL);
84         fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
85         
86         // a box
87         lyxerr << "\tbackground box: +"
88                << xpos << '+' << ypos << ' '
89                << width - 15 << 'x' << height << endl;
90         backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
91                                          xpos, ypos,
92                                          width - 15,
93                                          height,"");
94         fl_set_object_resize(obj, FL_RESIZE_ALL);
95         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
96
97         //
98         // THE SCROLLBAR
99         //
100
101         // up - scrollbar button
102         fl_set_border_width(-1);
103
104         lyxerr << "\tup button: +"
105                << xpos + width - 15 << '+' << ypos << ' '
106                << 15 << 'x' << 15 << endl;
107         button_up = obj = fl_add_pixmapbutton(FL_TOUCH_BUTTON,
108                                               xpos + width - 15,
109                                               ypos,
110                                               15, 15, "");
111         fl_set_object_boxtype(obj,FL_UP_BOX);
112         fl_set_object_color(obj,FL_MCOL,FL_BLUE);
113         fl_set_object_resize(obj, FL_RESIZE_ALL);
114         fl_set_object_gravity(obj,NorthEastGravity, NorthEastGravity);
115         obj->u_vdata = this;
116         fl_set_object_callback(obj,C_WorkArea_up_cb, 0);
117         fl_set_pixmapbutton_data(obj, const_cast<char**>(up_xpm));
118
119         // Remove the blue feedback rectangle
120         fl_set_pixmapbutton_focus_outline(obj,0);
121
122         // the scrollbar slider
123         fl_set_border_width(-bw);
124         lyxerr << "\tscrollbar slider: +"
125                << xpos + width - 15 << '+' << ypos + 15 << ' '
126                << 15 << 'x' << height - 30 << endl;
127         scrollbar = obj = fl_add_slider(FL_VERT_SLIDER,
128                                         xpos + width - 15,
129                                         ypos + 15,
130                                         15, height - 30, "");
131         fl_set_object_color(obj,FL_COL1,FL_MCOL);
132         fl_set_object_boxtype(obj, FL_UP_BOX);
133         fl_set_object_resize(obj, FL_RESIZE_ALL);
134         fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
135         obj->u_vdata = this;
136         fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
137         fl_set_slider_precision(obj, 0);
138         
139         // down - scrollbar button
140         fl_set_border_width(-1);
141
142         lyxerr << "\tdown button: +"
143                << xpos + width - 15 << '+' << ypos + height - 15 << ' '
144                << 15 << 'x' << 15 << endl;
145         button_down = obj = fl_add_pixmapbutton(FL_TOUCH_BUTTON,
146                                                 xpos + width - 15,
147                                                 ypos + height - 15,
148                                                 15, 15, "");
149         fl_set_object_boxtype(obj,FL_UP_BOX);
150         fl_set_object_color(obj,FL_MCOL,FL_BLUE);
151         fl_set_object_resize(obj, FL_RESIZE_ALL);
152         fl_set_object_gravity(obj, SouthEastGravity, SouthEastGravity);
153         obj->u_vdata = this;
154         fl_set_object_callback(obj, C_WorkArea_down_cb, 0);
155         fl_set_pixmapbutton_data(obj, const_cast<char**>(down_xpm));
156
157         fl_set_border_width(-bw);
158
159         // Remove the blue feedback rectangle
160         fl_set_pixmapbutton_focus_outline(obj,0);
161
162         ///
163         /// The free object
164
165         // Create the workarea pixmap
166         createPixmap(width - 15 - 2 * bw, height - 2 * bw);
167 #ifdef USE_PAINTER
168         // setup the painter
169         painter_.setDisplay(fl_display);
170         painter_.setDrawable(workareapixmap);
171 #endif
172         
173         // We add this object as late as possible to avoit problems
174         // with drawing.
175         lyxerr << "\tfree object: +"
176                << xpos + bw << '+' << ypos + bw << ' '
177                << width - 15 - 2 * bw << 'x' << height - 2 * bw << endl;
178         work_area = obj = fl_add_free(FL_INPUT_FREE,
179                                       xpos + bw, ypos + bw,
180                                       width - 15 - 2 * bw, // scrollbarwidth
181                                       height - 2 * bw, "",
182                                       C_WorkArea_work_area_handler);
183         obj->wantkey = FL_KEY_TAB;
184         obj->u_vdata = this; /* This is how we pass the WorkArea
185                                        to the work_area_handler. */
186         fl_set_object_boxtype(obj,FL_DOWN_BOX);
187         fl_set_object_resize(obj, FL_RESIZE_ALL);
188         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
189
190         fl_unfreeze_all_forms();
191 }
192
193
194 WorkArea::~WorkArea()
195 {
196         if (workareapixmap)
197                 XFreePixmap(fl_display, workareapixmap);
198 }
199
200
201 void WorkArea::resize(int xpos, int ypos, int width, int height)
202 {
203         fl_freeze_all_forms();
204         
205         const int bw = int(abs(float(fl_get_border_width())));
206
207         // a box
208         fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
209         
210         //
211         // THE SCROLLBAR
212         //
213
214         // up - scrollbar button
215         fl_set_object_geometry(button_up, xpos + width - 15,
216                                ypos,
217                                15, 15);
218         // the scrollbar slider
219         fl_set_object_geometry(scrollbar, xpos + width - 15,
220                                ypos + 15,
221                                15, height - 30);
222         // down - scrollbar button
223         fl_set_object_geometry(button_down, xpos + width - 15,
224                                ypos + height - 15,
225                                15, 15);
226
227         // Create the workarea pixmap
228         createPixmap(width - 15 - 2 * bw, height - 2 * bw);
229
230 #ifdef USE_PAINTER
231         // reset the painter
232         painter_.setDrawable(workareapixmap);
233 #endif
234         
235         // the free object
236         fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
237                                width - 15 - 2 * bw,
238                                height - 2 * bw);
239
240         fl_unfreeze_all_forms();
241
242 }
243
244
245 void WorkArea::createPixmap(int width, int height)
246 {
247         static int cur_width = -1;
248         static int cur_height = -1;
249
250         if (cur_width == width && cur_height == height && workareapixmap)
251                 return;
252         
253         cur_width = width;
254         cur_height = height;
255
256         if (workareapixmap)
257                 XFreePixmap(fl_display, workareapixmap);
258         
259         lyxerr << "Creating pixmap (" << width << 'x' << height << ")" << endl;
260         
261         workareapixmap = XCreatePixmap(fl_display,
262                                        RootWindow(fl_display, 0),
263                                        width,
264                                        height, 
265                                        fl_get_visual_depth());
266         lyxerr << "\tpixmap=" << workareapixmap << endl;
267 }
268
269
270 void WorkArea::greyOut() const
271 {
272         fl_winset(FL_ObjWin(work_area));
273         fl_rectangle(1, work_area->x, work_area->y,
274                      work_area->w, work_area->h, FL_GRAY63);
275 }
276
277
278 void WorkArea::setFocus() const
279 {
280         fl_set_focus_object(work_area->form, work_area);
281 }
282
283
284 void WorkArea::setScrollbar(double pos, double length_fraction) const
285 {
286         fl_set_slider_value(scrollbar, pos);
287         fl_set_slider_size(scrollbar, scrollbar->h * length_fraction);
288 }
289
290
291 void WorkArea::setScrollbarBounds(double l1, double l2) const
292 {
293         fl_set_slider_bounds(scrollbar, l1, l2);
294 }
295
296
297 void WorkArea::setScrollbarIncrements(float inc) const
298 {
299         fl_set_slider_increment(scrollbar, work_area->h - inc, inc);
300 }
301
302
303 void WorkArea::up_cb(FL_OBJECT * ob, long)
304 {
305         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
306         XEvent const * ev2 = fl_last_event();
307         static long time = 0;
308         ev2 = fl_last_event();
309         if (ev2->type == ButtonPress || ev2->type == ButtonRelease) 
310                 time = 0;
311         //area->up(time++, fl_get_button_numb(ob));
312         area->owner->UpCB(time++, fl_get_button_numb(ob));
313 }
314
315
316 void WorkArea::down_cb(FL_OBJECT * ob, long)
317 {
318         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
319         XEvent const * ev2 = fl_last_event();
320         static long time = 0;
321         if (ev2->type == ButtonPress || ev2->type == ButtonRelease)
322                 time = 0;
323         //area->down(time++, fl_get_button_numb(ob));
324         area->owner->DownCB(time++, fl_get_button_numb(ob));
325 }
326
327
328 // Callback for scrollbar slider
329 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
330 {
331         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
332
333         //area->scroll(fl_get_slider_value(ob));
334         area->owner->ScrollCB(fl_get_slider_value(ob));
335         waitForX();
336 }
337
338 bool Lgb_bug_find_hack = false;
339
340 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
341                                   FL_Coord, FL_Coord ,
342                                   int /*key*/, void * xev)
343 {
344         static int x_old = -1;
345         static int y_old = -1;
346         static long scrollbar_value_old = -1;
347         
348         XEvent * ev = static_cast<XEvent*>(xev);
349         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
350
351         if (!area) return 1;
352         
353         switch (event){   
354         case FL_DRAW:
355                 if (!area->work_area ||
356                     !area->work_area->form->visible)
357                         return 1;
358                 lyxerr.debug() << "Workarea event: DRAW" << endl;
359                 area->createPixmap(area->workWidth(), area->height());
360                 Lgb_bug_find_hack = true;
361                 //area->expose();
362                 area->owner->workAreaExpose();
363                 Lgb_bug_find_hack = false;
364                 break;
365         case FL_PUSH:
366                 if (!ev) break;
367                 // Should really have used xbutton.state
368                 lyxerr.debug() << "Workarea event: PUSH" << endl;
369                 //area->buttonPress(ev->xbutton.x - ob->x,
370                 //                ev->xbutton.y - ob->y,
371                 //                ev->xbutton.button);
372                 area->owner->WorkAreaButtonPress(ev->xbutton.x - ob->x,
373                                            ev->xbutton.y - ob->y,
374                                            ev->xbutton.button);
375                 break; 
376         case FL_RELEASE:
377                 if (!ev) break;
378                 // Should really have used xbutton.state
379                 lyxerr.debug() << "Workarea event: RELEASE" << endl;
380                 //area->buttonRelease(ev->xbutton.x - ob->x,
381                 //                  ev->xbutton.y - ob->y,
382                 //                  ev->xbutton.button);
383                 area->owner->WorkAreaButtonRelease(ev->xbutton.x - ob->x,
384                                              ev->xbutton.y - ob->y,
385                                              ev->xbutton.button);
386                 break;
387         case FL_MOUSE:
388                 if (!ev || ! area->scrollbar) break;
389                 if (ev->xmotion.x != x_old ||
390                     ev->xmotion.y != y_old ||
391                     fl_get_slider_value(area->scrollbar) != scrollbar_value_old) {
392                         lyxerr.debug() << "Workarea event: MOUSE" << endl;
393                         //area->motion(ev->xmotion.x - ob->x,
394                         //           ev->xmotion.y - ob->y,
395                         //           ev->xbutton.state);
396                         area->owner->WorkAreaMotionNotify(ev->xmotion.x - ob->x,
397                                                     ev->xmotion.y - ob->y,
398                                                     ev->xbutton.state);
399                 }
400                 break;
401         // Done by the raw callback:
402         //  case FL_KEYBOARD: WorkAreaKeyPress(ob, 0,0,0,ev,0); break;
403         case FL_FOCUS:
404                 lyxerr.debug() << "Workarea event: FOCUS" << endl;
405                 //area->focus();
406                 break;
407         case FL_UNFOCUS:
408                 lyxerr.debug() << "Workarea event: UNFOCUS" << endl;
409                 //area->unfocus();
410                 break;
411         case FL_ENTER:
412                 lyxerr.debug() << "Workarea event: ENTER" << endl;
413                 //area->enter();
414                 break;
415         case FL_LEAVE:
416                 lyxerr.debug() << "Workarea event: LEAVE" << endl;
417                 //area->leave();
418                 break;
419         case FL_DBLCLICK:
420                 if (!ev) break;
421                 lyxerr.debug() << "Workarea event: DBLCLICK" << endl;;
422                 //area->doubleclick(ev->xbutton.x,
423                 //                ev->xbutton.y,
424                 //                ev->xbutton.button);
425                 break;
426         case FL_TRPLCLICK:
427                 if (!ev) break;
428                 lyxerr.debug() << "Workarea event: TRPLCLICK" << endl;
429                 //area->trippleclick(ev->xbutton.x,
430                 //                 ev->xbutton.y,
431                 //                 ev->xbutton.button);
432                 break;
433         case FL_OTHER:
434                 if (!ev) break;
435                 if (ev->type == SelectionNotify) {
436                         lyxerr.debug() << "Workarea event: SELECTION" << endl;
437                         //area->selection(area->work_area->form->window, ev);
438                         area->owner->WorkAreaSelectionNotify(area->work_area->form->window, ev);
439                 } else
440                         lyxerr.debug() << "Workarea event: OTHER" << endl;
441
442                 break;
443         }
444   
445         return 1;
446 }