]> git.lyx.org Git - lyx.git/blob - src/WorkArea.C
2611fdaea4efbbb5ce6bd6c1f6b599e6e62bc4fc
[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         lyxerr << "Below mouse: " << work_area->belowmouse << endl;
215         return work_area->belowmouse;
216 }
217
218
219 void WorkArea::resize(int xpos, int ypos, int width, int height)
220 {
221         fl_freeze_all_forms();
222         
223         const int bw = int(abs(float(fl_get_border_width())));
224
225         // a box
226         fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
227         
228         //
229         // THE SCROLLBAR
230         //
231
232         // up - scrollbar button
233         fl_set_object_geometry(button_up, xpos + width - 15,
234                                ypos,
235                                15, 15);
236         // the scrollbar slider
237         fl_set_object_geometry(scrollbar, xpos + width - 15,
238                                ypos + 15,
239                                15, height - 30);
240         // down - scrollbar button
241         fl_set_object_geometry(button_down, xpos + width - 15,
242                                ypos + height - 15,
243                                15, 15);
244
245         // Create the workarea pixmap
246         createPixmap(width - 15 - 2 * bw, height - 2 * bw);
247
248         // the free object
249         fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
250                                width - 15 - 2 * bw,
251                                height - 2 * bw);
252
253         fl_unfreeze_all_forms();
254
255 }
256
257
258 void WorkArea::createPixmap(int width, int height)
259 {
260         static int cur_width = -1;
261         static int cur_height = -1;
262
263         if (cur_width == width && cur_height == height && workareapixmap)
264                 return;
265         
266         cur_width = width;
267         cur_height = height;
268
269         if (workareapixmap)
270                 XFreePixmap(fl_display, workareapixmap);
271
272         if (lyxerr.debugging())
273                 lyxerr << "Creating pixmap ("
274                        << width << 'x' << height << ")" << endl;
275         
276         workareapixmap = XCreatePixmap(fl_display,
277                                        RootWindow(fl_display, 0),
278                                        width,
279                                        height, 
280                                        fl_get_visual_depth());
281         if (lyxerr.debugging())
282                 lyxerr << "\tpixmap=" << workareapixmap << endl;
283 }
284
285
286 void WorkArea::greyOut() const
287 {
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);
291 }
292
293
294 void WorkArea::setFocus() const
295 {
296         fl_set_focus_object(work_area->form, work_area);
297 }
298
299
300 void WorkArea::setScrollbar(double pos, double length_fraction) const
301 {
302         fl_set_slider_value(scrollbar, pos);
303         fl_set_slider_size(scrollbar, scrollbar->h * length_fraction);
304 }
305
306
307 void WorkArea::setScrollbarBounds(double l1, double l2) const
308 {
309         fl_set_slider_bounds(scrollbar, l1, l2);
310 }
311
312
313 void WorkArea::setScrollbarIncrements(float inc) const
314 {
315         fl_set_slider_increment(scrollbar, work_area->h - inc, inc);
316 }
317
318
319 void WorkArea::up_cb(FL_OBJECT * ob, long)
320 {
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) 
326                 time = 0;
327         //area->up(time++, fl_get_button_numb(ob));
328         area->owner->upCB(time++, fl_get_button_numb(ob));
329 }
330
331
332 void WorkArea::down_cb(FL_OBJECT * ob, long)
333 {
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)
338                 time = 0;
339         //area->down(time++, fl_get_button_numb(ob));
340         area->owner->downCB(time++, fl_get_button_numb(ob));
341 }
342
343
344 // Callback for scrollbar slider
345 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
346 {
347         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
348
349         //area->scroll(fl_get_slider_value(ob));
350         area->owner->scrollCB(fl_get_slider_value(ob));
351         waitForX();
352 }
353
354 bool Lgb_bug_find_hack = false;
355
356 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
357                                 FL_Coord, FL_Coord ,
358                                 int /*key*/, void * xev)
359 {
360         static int x_old = -1;
361         static int y_old = -1;
362         static long scrollbar_value_old = -1;
363         
364         XEvent * ev = static_cast<XEvent*>(xev);
365         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
366
367         if (!area) return 1;
368         
369         switch (event){   
370         case FL_DRAW:
371                 if (!area->work_area ||
372                     !area->work_area->form->visible)
373                         return 1;
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;
379                 break;
380         case FL_PUSH:
381                 if (!ev) break;
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,
386                                            ev->xbutton.button);
387                 break; 
388         case FL_RELEASE:
389                 if (!ev) break;
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,
394                                              ev->xbutton.button);
395                 break;
396         case FL_MOUSE:
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,
404                                                     ev->xbutton.state);
405                 }
406                 break;
407         // Done by the raw callback:
408         //  case FL_KEYBOARD: WorkAreaKeyPress(ob, 0,0,0,ev,0); break;
409         case FL_FOCUS:
410                 lyxerr.debug() << "Workarea event: FOCUS" << endl;
411                 break;
412         case FL_UNFOCUS:
413                 lyxerr.debug() << "Workarea event: UNFOCUS" << endl;
414                 break;
415         case FL_ENTER:
416                 lyxerr.debug() << "Workarea event: ENTER" << endl;
417                 break;
418         case FL_LEAVE:
419                 lyxerr.debug() << "Workarea event: LEAVE" << endl;
420                 break;
421         case FL_DBLCLICK:
422                 if (!ev) break;
423                 lyxerr.debug() << "Workarea event: DBLCLICK" << endl;
424                 area->owner->doubleClick(ev->xbutton.x - ob->x,
425                                          ev->xbutton.y - ob->y,
426                                          ev->xbutton.button);
427                 break;
428         case FL_TRPLCLICK:
429                 if (!ev) break;
430                 lyxerr.debug() << "Workarea event: TRPLCLICK" << endl;
431                 area->owner->tripleClick(ev->xbutton.x - ob->x,
432                                          ev->xbutton.y - ob->y,
433                                          ev->xbutton.button);
434                 break;
435         case FL_OTHER:
436                 if (!ev) break;
437                 if (ev->type == SelectionNotify) {
438                         lyxerr.debug() << "Workarea event: SELECTION" << endl;
439                         area->owner->workAreaSelectionNotify(area->work_area->form->window, ev);
440                 } else
441                         lyxerr.debug() << "Workarea event: OTHER" << endl;
442
443                 break;
444         }
445   
446         return 1;
447 }