]> git.lyx.org Git - lyx.git/blob - src/WorkArea.C
use more explicit on constructors use the pimpl idom to reduce size with about 500k
[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 using std::endl;
26
27 FL_OBJECT * figinset_canvas;
28
29 // need to make the c++ compiler fint the correct version of abs.
30 // This is at least true for g++.
31 using std::abs;
32
33 static inline
34 void waitForX()
35 {
36         XSync(fl_get_display(), 0);
37 }
38
39 extern "C" {
40 // Just a bunch of C wrappers around static members of WorkArea
41         void C_WorkArea_up_cb(FL_OBJECT * ob, long buf)
42         {
43                 WorkArea::up_cb(ob, buf);
44         }
45
46         void C_WorkArea_down_cb(FL_OBJECT * ob, long buf)
47         {
48                 WorkArea::down_cb(ob, buf);
49         }
50
51         void C_WorkArea_scroll_cb(FL_OBJECT * ob, long buf)
52         {
53                 WorkArea::scroll_cb(ob, buf);
54         }
55
56         int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
57                                            FL_Coord, FL_Coord, 
58                                            int key, void * xev)
59         {
60                 return WorkArea::work_area_handler(ob, event,
61                                                    0, 0, key, xev);
62         }
63 }
64
65
66
67 WorkArea::WorkArea(BufferView * o, int xpos, int ypos, int width, int height)
68         : owner(o), workareapixmap(0), painter_(*this)
69 {
70         fl_freeze_all_forms();
71
72         figinset_canvas = 0;
73
74         if (lyxerr.debugging())
75                 lyxerr << "Creating work area: +"
76                        << xpos << '+' << ypos << ' '
77                        << width << 'x' << height << endl;
78         //
79         FL_OBJECT * obj;
80         const int bw = int(abs(float(fl_get_border_width())));
81
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);
89         
90         // a box
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,
96                                          xpos, ypos,
97                                          width - 15,
98                                          height,"");
99         fl_set_object_resize(obj, FL_RESIZE_ALL);
100         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
101
102         //
103         // THE SCROLLBAR
104         //
105
106         // up - scrollbar button
107         fl_set_border_width(-1);
108
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,
114                                               xpos + width - 15,
115                                               ypos,
116                                               15, 15, "");
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);
121         obj->u_vdata = this;
122         fl_set_object_callback(obj,C_WorkArea_up_cb, 0);
123         fl_set_pixmapbutton_data(obj, const_cast<char**>(up_xpm));
124
125         // Remove the blue feedback rectangle
126         fl_set_pixmapbutton_focus_outline(obj,0);
127
128         // the scrollbar slider
129         fl_set_border_width(-bw);
130
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,
136                                         xpos + width - 15,
137                                         ypos + 15,
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);
143         obj->u_vdata = this;
144         fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
145         fl_set_slider_precision(obj, 0);
146         
147         // down - scrollbar button
148         fl_set_border_width(-1);
149
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,
156                                                 xpos + width - 15,
157                                                 ypos + height - 15,
158                                                 15, 15, "");
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);
163         obj->u_vdata = this;
164         fl_set_object_callback(obj, C_WorkArea_down_cb, 0);
165         fl_set_pixmapbutton_data(obj, const_cast<char**>(down_xpm));
166
167         fl_set_border_width(-bw);
168
169         // Remove the blue feedback rectangle
170         fl_set_pixmapbutton_focus_outline(obj,0);
171
172         ///
173         /// The free object
174
175         // Create the workarea pixmap
176         createPixmap(width - 15 - 2 * bw, height - 2 * bw);
177
178         // setup the painter
179         painter_.setDisplay(fl_display);
180         
181         // We add this object as late as possible to avoit problems
182         // with drawing.
183         if (lyxerr.debugging())
184                 lyxerr << "\tfree object: +"
185                        << xpos + bw << '+' << ypos + bw << ' '
186                        << width - 15 - 2 * bw << 'x'
187                        << height - 2 * bw << endl;
188         
189         work_area = obj = fl_add_free(FL_INPUT_FREE,
190                                       xpos + bw, ypos + bw,
191                                       width - 15 - 2 * bw, // scrollbarwidth
192                                       height - 2 * bw, "",
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);
200
201         fl_unfreeze_all_forms();
202 }
203
204
205 WorkArea::~WorkArea()
206 {
207         if (workareapixmap)
208                 XFreePixmap(fl_display, workareapixmap);
209 }
210
211
212 bool WorkArea::belowMouse() const
213 {
214         FL_Coord x, y;
215         unsigned int button;
216         fl_get_mouse(&x, &y, &button);
217         FL_Coord ulx = work_area->form->x + work_area->x;
218         FL_Coord uly = work_area->form->y + work_area->y;
219         FL_Coord w = work_area->w;
220         FL_Coord h = work_area->h;
221         if (x > ulx && y > uly && x < ulx + h && y < uly + w)
222                 return true;
223         return false;
224         
225         
226         //lyxerr << "Mouse: (" << x << ", " << y <<") button = " << button << endl;
227         //lyxerr << "Workarea: (" << work_area->x + work_area->form->x << ", " << work_area->y + work_area->form->y << ", " << work_area->w << ", " << work_area->h << ")" << endl;
228         //lyxerr << "Below mouse: " << work_area->belowmouse << endl;
229         //return work_area->belowmouse;
230 }
231
232
233 void WorkArea::resize(int xpos, int ypos, int width, int height)
234 {
235         fl_freeze_all_forms();
236         
237         const int bw = int(abs(float(fl_get_border_width())));
238
239         // a box
240         fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
241         
242         //
243         // THE SCROLLBAR
244         //
245
246         // up - scrollbar button
247         fl_set_object_geometry(button_up, xpos + width - 15,
248                                ypos,
249                                15, 15);
250         // the scrollbar slider
251         fl_set_object_geometry(scrollbar, xpos + width - 15,
252                                ypos + 15,
253                                15, height - 30);
254         // down - scrollbar button
255         fl_set_object_geometry(button_down, xpos + width - 15,
256                                ypos + height - 15,
257                                15, 15);
258
259         // Create the workarea pixmap
260         createPixmap(width - 15 - 2 * bw, height - 2 * bw);
261
262         // the free object
263         fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
264                                width - 15 - 2 * bw,
265                                height - 2 * bw);
266
267         fl_unfreeze_all_forms();
268
269 }
270
271
272 void WorkArea::createPixmap(int width, int height)
273 {
274         static int cur_width = -1;
275         static int cur_height = -1;
276
277         if (cur_width == width && cur_height == height && workareapixmap)
278                 return;
279         
280         cur_width = width;
281         cur_height = height;
282
283         if (workareapixmap)
284                 XFreePixmap(fl_display, workareapixmap);
285
286         if (lyxerr.debugging())
287                 lyxerr << "Creating pixmap ("
288                        << width << 'x' << height << ")" << endl;
289         
290         workareapixmap = XCreatePixmap(fl_display,
291                                        RootWindow(fl_display, 0),
292                                        width,
293                                        height, 
294                                        fl_get_visual_depth());
295         if (lyxerr.debugging())
296                 lyxerr << "\tpixmap=" << workareapixmap << endl;
297 }
298
299
300 void WorkArea::greyOut() const
301 {
302         fl_winset(FL_ObjWin(work_area));
303         fl_rectangle(1, work_area->x, work_area->y,
304                      work_area->w, work_area->h, FL_GRAY63);
305 }
306
307
308 void WorkArea::setFocus() const
309 {
310         fl_set_focus_object(work_area->form, work_area);
311 }
312
313
314 void WorkArea::setScrollbar(double pos, double length_fraction) const
315 {
316         fl_set_slider_value(scrollbar, pos);
317         fl_set_slider_size(scrollbar, scrollbar->h * length_fraction);
318 }
319
320
321 void WorkArea::setScrollbarBounds(double l1, double l2) const
322 {
323         fl_set_slider_bounds(scrollbar, l1, l2);
324 }
325
326
327 void WorkArea::setScrollbarIncrements(float inc) const
328 {
329         fl_set_slider_increment(scrollbar, work_area->h - inc, inc);
330 }
331
332
333 void WorkArea::up_cb(FL_OBJECT * ob, long)
334 {
335         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
336         XEvent const * ev2 = fl_last_event();
337         static long time = 0;
338         ev2 = fl_last_event();
339         if (ev2->type == ButtonPress || ev2->type == ButtonRelease) 
340                 time = 0;
341         //area->up(time++, fl_get_button_numb(ob));
342         area->owner->upCB(time++, fl_get_button_numb(ob));
343 }
344
345
346 void WorkArea::down_cb(FL_OBJECT * ob, long)
347 {
348         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
349         XEvent const * ev2 = fl_last_event();
350         static long time = 0;
351         if (ev2->type == ButtonPress || ev2->type == ButtonRelease)
352                 time = 0;
353         //area->down(time++, fl_get_button_numb(ob));
354         area->owner->downCB(time++, fl_get_button_numb(ob));
355 }
356
357
358 // Callback for scrollbar slider
359 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
360 {
361         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
362
363         //area->scroll(fl_get_slider_value(ob));
364         area->owner->scrollCB(fl_get_slider_value(ob));
365         waitForX();
366 }
367
368 bool Lgb_bug_find_hack = false;
369
370 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
371                                 FL_Coord, FL_Coord ,
372                                 int /*key*/, void * xev)
373 {
374         static int x_old = -1;
375         static int y_old = -1;
376         static long scrollbar_value_old = -1;
377         
378         XEvent * ev = static_cast<XEvent*>(xev);
379         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
380
381         if (!area) return 1;
382         
383         switch (event){   
384         case FL_DRAW:
385                 if (!area->work_area ||
386                     !area->work_area->form->visible)
387                         return 1;
388                 lyxerr.debug() << "Workarea event: DRAW" << endl;
389                 area->createPixmap(area->workWidth(), area->height());
390                 Lgb_bug_find_hack = true;
391                 area->owner->workAreaExpose();
392                 Lgb_bug_find_hack = false;
393                 break;
394         case FL_PUSH:
395                 if (!ev) break;
396                 // Should really have used xbutton.state
397                 lyxerr.debug() << "Workarea event: PUSH" << endl;
398                 area->owner->workAreaButtonPress(ev->xbutton.x - ob->x,
399                                            ev->xbutton.y - ob->y,
400                                            ev->xbutton.button);
401                 break; 
402         case FL_RELEASE:
403                 if (!ev) break;
404                 // Should really have used xbutton.state
405                 lyxerr.debug() << "Workarea event: RELEASE" << endl;
406                 area->owner->workAreaButtonRelease(ev->xbutton.x - ob->x,
407                                              ev->xbutton.y - ob->y,
408                                              ev->xbutton.button);
409                 break;
410         case FL_MOUSE:
411                 if (!ev || ! area->scrollbar) break;
412                 if (ev->xmotion.x != x_old ||
413                     ev->xmotion.y != y_old ||
414                     fl_get_slider_value(area->scrollbar) != scrollbar_value_old) {
415                         lyxerr.debug() << "Workarea event: MOUSE" << endl;
416                         area->owner->workAreaMotionNotify(ev->xmotion.x - ob->x,
417                                                     ev->xmotion.y - ob->y,
418                                                     ev->xbutton.state);
419                 }
420                 break;
421         // Done by the raw callback:
422         //  case FL_KEYBOARD: WorkAreaKeyPress(ob, 0,0,0,ev,0); break;
423         case FL_FOCUS:
424                 lyxerr.debug() << "Workarea event: FOCUS" << endl;
425                 break;
426         case FL_UNFOCUS:
427                 lyxerr.debug() << "Workarea event: UNFOCUS" << endl;
428                 break;
429         case FL_ENTER:
430                 lyxerr.debug() << "Workarea event: ENTER" << endl;
431                 break;
432         case FL_LEAVE:
433                 lyxerr.debug() << "Workarea event: LEAVE" << endl;
434                 break;
435         case FL_DBLCLICK:
436                 if (!ev) break;
437                 lyxerr.debug() << "Workarea event: DBLCLICK" << endl;
438                 area->owner->doubleClick(ev->xbutton.x - ob->x,
439                                          ev->xbutton.y - ob->y,
440                                          ev->xbutton.button);
441                 break;
442         case FL_TRPLCLICK:
443                 if (!ev) break;
444                 lyxerr.debug() << "Workarea event: TRPLCLICK" << endl;
445                 area->owner->tripleClick(ev->xbutton.x - ob->x,
446                                          ev->xbutton.y - ob->y,
447                                          ev->xbutton.button);
448                 break;
449         case FL_OTHER:
450                 if (!ev) break;
451                 if (ev->type == SelectionNotify) {
452                         lyxerr.debug() << "Workarea event: SELECTION" << endl;
453                         area->owner->workAreaSelectionNotify(area->work_area->form->window, ev);
454                 } else
455                         lyxerr.debug() << "Workarea event: OTHER" << endl;
456
457                 break;
458         }
459   
460         return 1;
461 }