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