]> git.lyx.org Git - features.git/blob - src/WorkArea.C
5f95e0cada78a26bbfe15c90a711b138fc8f6626
[features.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), painter_(*this)
63 {
64         fl_freeze_all_forms();
65
66         figinset_canvas = 0;
67
68         if (lyxerr.debugging())
69                 lyxerr << "Creating work area: +"
70                        << xpos << '+' << ypos << ' '
71                        << width << 'x' << height << endl;
72         //
73         FL_OBJECT * obj;
74         const int bw = int(abs(float(fl_get_border_width())));
75
76         // We really want to get rid of figinset_canvas.
77         ::figinset_canvas = figinset_canvas = obj =
78                   fl_add_canvas(FL_NORMAL_CANVAS,
79                                 xpos + 1, ypos + 1, 1, 1, "");
80         fl_set_object_boxtype(obj, FL_NO_BOX);
81         fl_set_object_resize(obj, FL_RESIZE_ALL);
82         fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
83         
84         // a box
85         if (lyxerr.debugging())
86                 lyxerr << "\tbackground box: +"
87                        << xpos << '+' << ypos << ' '
88                        << width - 15 << 'x' << height << endl;
89         backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
90                                          xpos, ypos,
91                                          width - 15,
92                                          height,"");
93         fl_set_object_resize(obj, FL_RESIZE_ALL);
94         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
95
96         //
97         // THE SCROLLBAR
98         //
99
100         // up - scrollbar button
101         fl_set_border_width(-1);
102
103         if (lyxerr.debugging())
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
125         if (lyxerr.debugging())
126                 lyxerr << "\tscrollbar slider: +"
127                        << xpos + width - 15 << '+' << ypos + 15 << ' '
128                        << 15 << 'x' << height - 30 << endl;
129         scrollbar = obj = fl_add_slider(FL_VERT_SLIDER,
130                                         xpos + width - 15,
131                                         ypos + 15,
132                                         15, height - 30, "");
133         fl_set_object_color(obj,FL_COL1,FL_MCOL);
134         fl_set_object_boxtype(obj, FL_UP_BOX);
135         fl_set_object_resize(obj, FL_RESIZE_ALL);
136         fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
137         obj->u_vdata = this;
138         fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
139         fl_set_slider_precision(obj, 0);
140         
141         // down - scrollbar button
142         fl_set_border_width(-1);
143
144         if (lyxerr.debugging())
145                 lyxerr << "\tdown button: +"
146                        << xpos + width - 15 << '+'
147                        << ypos + height - 15 << ' '
148                        << 15 << 'x' << 15 << endl;
149         button_down = obj = fl_add_pixmapbutton(FL_TOUCH_BUTTON,
150                                                 xpos + width - 15,
151                                                 ypos + height - 15,
152                                                 15, 15, "");
153         fl_set_object_boxtype(obj,FL_UP_BOX);
154         fl_set_object_color(obj,FL_MCOL,FL_BLUE);
155         fl_set_object_resize(obj, FL_RESIZE_ALL);
156         fl_set_object_gravity(obj, SouthEastGravity, SouthEastGravity);
157         obj->u_vdata = this;
158         fl_set_object_callback(obj, C_WorkArea_down_cb, 0);
159         fl_set_pixmapbutton_data(obj, const_cast<char**>(down_xpm));
160
161         fl_set_border_width(-bw);
162
163         // Remove the blue feedback rectangle
164         fl_set_pixmapbutton_focus_outline(obj,0);
165
166         ///
167         /// The free object
168
169         // Create the workarea pixmap
170         createPixmap(width - 15 - 2 * bw, height - 2 * bw);
171
172         // setup the painter
173         painter_.setDisplay(fl_display);
174         
175         // We add this object as late as possible to avoit problems
176         // with drawing.
177         if (lyxerr.debugging())
178                 lyxerr << "\tfree object: +"
179                        << xpos + bw << '+' << ypos + bw << ' '
180                        << width - 15 - 2 * bw << 'x'
181                        << height - 2 * bw << endl;
182         
183         work_area = obj = fl_add_free(FL_INPUT_FREE,
184                                       xpos + bw, ypos + bw,
185                                       width - 15 - 2 * bw, // scrollbarwidth
186                                       height - 2 * bw, "",
187                                       C_WorkArea_work_area_handler);
188         obj->wantkey = FL_KEY_TAB;
189         obj->u_vdata = this; /* This is how we pass the WorkArea
190                                        to the work_area_handler. */
191         fl_set_object_boxtype(obj,FL_DOWN_BOX);
192         fl_set_object_resize(obj, FL_RESIZE_ALL);
193         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
194
195         fl_unfreeze_all_forms();
196 }
197
198
199 WorkArea::~WorkArea()
200 {
201         if (workareapixmap)
202                 XFreePixmap(fl_display, workareapixmap);
203 }
204
205
206 void WorkArea::resize(int xpos, int ypos, int width, int height)
207 {
208         fl_freeze_all_forms();
209         
210         const int bw = int(abs(float(fl_get_border_width())));
211
212         // a box
213         fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
214         
215         //
216         // THE SCROLLBAR
217         //
218
219         // up - scrollbar button
220         fl_set_object_geometry(button_up, xpos + width - 15,
221                                ypos,
222                                15, 15);
223         // the scrollbar slider
224         fl_set_object_geometry(scrollbar, xpos + width - 15,
225                                ypos + 15,
226                                15, height - 30);
227         // down - scrollbar button
228         fl_set_object_geometry(button_down, xpos + width - 15,
229                                ypos + height - 15,
230                                15, 15);
231
232         // Create the workarea pixmap
233         createPixmap(width - 15 - 2 * bw, height - 2 * bw);
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         if (lyxerr.debugging())
260                 lyxerr << "Creating pixmap ("
261                        << width << 'x' << height << ")" << endl;
262         
263         workareapixmap = XCreatePixmap(fl_display,
264                                        RootWindow(fl_display, 0),
265                                        width,
266                                        height, 
267                                        fl_get_visual_depth());
268         if (lyxerr.debugging())
269                 lyxerr << "\tpixmap=" << workareapixmap << endl;
270 }
271
272
273 void WorkArea::greyOut() const
274 {
275         fl_winset(FL_ObjWin(work_area));
276         fl_rectangle(1, work_area->x, work_area->y,
277                      work_area->w, work_area->h, FL_GRAY63);
278 }
279
280
281 void WorkArea::setFocus() const
282 {
283         fl_set_focus_object(work_area->form, work_area);
284 }
285
286
287 void WorkArea::setScrollbar(double pos, double length_fraction) const
288 {
289         fl_set_slider_value(scrollbar, pos);
290         fl_set_slider_size(scrollbar, scrollbar->h * length_fraction);
291 }
292
293
294 void WorkArea::setScrollbarBounds(double l1, double l2) const
295 {
296         fl_set_slider_bounds(scrollbar, l1, l2);
297 }
298
299
300 void WorkArea::setScrollbarIncrements(float inc) const
301 {
302         fl_set_slider_increment(scrollbar, work_area->h - inc, inc);
303 }
304
305
306 void WorkArea::up_cb(FL_OBJECT * ob, long)
307 {
308         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
309         XEvent const * ev2 = fl_last_event();
310         static long time = 0;
311         ev2 = fl_last_event();
312         if (ev2->type == ButtonPress || ev2->type == ButtonRelease) 
313                 time = 0;
314         //area->up(time++, fl_get_button_numb(ob));
315         area->owner->upCB(time++, fl_get_button_numb(ob));
316 }
317
318
319 void WorkArea::down_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         if (ev2->type == ButtonPress || ev2->type == ButtonRelease)
325                 time = 0;
326         //area->down(time++, fl_get_button_numb(ob));
327         area->owner->downCB(time++, fl_get_button_numb(ob));
328 }
329
330
331 // Callback for scrollbar slider
332 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
333 {
334         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
335
336         //area->scroll(fl_get_slider_value(ob));
337         area->owner->scrollCB(fl_get_slider_value(ob));
338         waitForX();
339 }
340
341 bool Lgb_bug_find_hack = false;
342
343 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
344                                 FL_Coord, FL_Coord ,
345                                 int /*key*/, void * xev)
346 {
347         static int x_old = -1;
348         static int y_old = -1;
349         static long scrollbar_value_old = -1;
350         
351         XEvent * ev = static_cast<XEvent*>(xev);
352         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
353
354         if (!area) return 1;
355         
356         switch (event){   
357         case FL_DRAW:
358                 if (!area->work_area ||
359                     !area->work_area->form->visible)
360                         return 1;
361                 lyxerr.debug() << "Workarea event: DRAW" << endl;
362                 area->createPixmap(area->workWidth(), area->height());
363                 Lgb_bug_find_hack = true;
364                 area->owner->workAreaExpose();
365                 Lgb_bug_find_hack = false;
366                 break;
367         case FL_PUSH:
368                 if (!ev) break;
369                 // Should really have used xbutton.state
370                 lyxerr.debug() << "Workarea event: PUSH" << endl;
371                 area->owner->workAreaButtonPress(ev->xbutton.x - ob->x,
372                                            ev->xbutton.y - ob->y,
373                                            ev->xbutton.button);
374                 break; 
375         case FL_RELEASE:
376                 if (!ev) break;
377                 // Should really have used xbutton.state
378                 lyxerr.debug() << "Workarea event: RELEASE" << endl;
379                 area->owner->workAreaButtonRelease(ev->xbutton.x - ob->x,
380                                              ev->xbutton.y - ob->y,
381                                              ev->xbutton.button);
382                 break;
383         case FL_MOUSE:
384                 if (!ev || ! area->scrollbar) break;
385                 if (ev->xmotion.x != x_old ||
386                     ev->xmotion.y != y_old ||
387                     fl_get_slider_value(area->scrollbar) != scrollbar_value_old) {
388                         lyxerr.debug() << "Workarea event: MOUSE" << endl;
389                         area->owner->workAreaMotionNotify(ev->xmotion.x - ob->x,
390                                                     ev->xmotion.y - ob->y,
391                                                     ev->xbutton.state);
392                 }
393                 break;
394         // Done by the raw callback:
395         //  case FL_KEYBOARD: WorkAreaKeyPress(ob, 0,0,0,ev,0); break;
396         case FL_FOCUS:
397                 lyxerr.debug() << "Workarea event: FOCUS" << endl;
398                 break;
399         case FL_UNFOCUS:
400                 lyxerr.debug() << "Workarea event: UNFOCUS" << endl;
401                 break;
402         case FL_ENTER:
403                 lyxerr.debug() << "Workarea event: ENTER" << endl;
404                 break;
405         case FL_LEAVE:
406                 lyxerr.debug() << "Workarea event: LEAVE" << endl;
407                 break;
408         case FL_DBLCLICK:
409                 if (!ev) break;
410                 lyxerr.debug() << "Workarea event: DBLCLICK" << endl;
411                 area->owner->doubleClick(ev->xbutton.x - ob->x,
412                                          ev->xbutton.y - ob->y,
413                                          ev->xbutton.button);
414                 break;
415         case FL_TRPLCLICK:
416                 if (!ev) break;
417                 lyxerr.debug() << "Workarea event: TRPLCLICK" << endl;
418                 area->owner->tripleClick(ev->xbutton.x - ob->x,
419                                          ev->xbutton.y - ob->y,
420                                          ev->xbutton.button);
421                 break;
422         case FL_OTHER:
423                 if (!ev) break;
424                 if (ev->type == SelectionNotify) {
425                         lyxerr.debug() << "Workarea event: SELECTION" << endl;
426                         area->owner->workAreaSelectionNotify(area->work_area->form->window, ev);
427                 } else
428                         lyxerr.debug() << "Workarea event: OTHER" << endl;
429
430                 break;
431         }
432   
433         return 1;
434 }