]> git.lyx.org Git - lyx.git/blob - src/WorkArea.C
remove the old painter, remove support for mono_video, reverse_video, fast selection...
[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         painter_.setDrawable(workareapixmap);
168         
169         // We add this object as late as possible to avoit problems
170         // with drawing.
171         lyxerr << "\tfree object: +"
172                << xpos + bw << '+' << ypos + bw << ' '
173                << width - 15 - 2 * bw << 'x' << height - 2 * bw << endl;
174         work_area = obj = fl_add_free(FL_INPUT_FREE,
175                                       xpos + bw, ypos + bw,
176                                       width - 15 - 2 * bw, // scrollbarwidth
177                                       height - 2 * bw, "",
178                                       C_WorkArea_work_area_handler);
179         obj->wantkey = FL_KEY_TAB;
180         obj->u_vdata = this; /* This is how we pass the WorkArea
181                                        to the work_area_handler. */
182         fl_set_object_boxtype(obj,FL_DOWN_BOX);
183         fl_set_object_resize(obj, FL_RESIZE_ALL);
184         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
185
186         fl_unfreeze_all_forms();
187 }
188
189
190 WorkArea::~WorkArea()
191 {
192         if (workareapixmap)
193                 XFreePixmap(fl_display, workareapixmap);
194 }
195
196
197 void WorkArea::resize(int xpos, int ypos, int width, int height)
198 {
199         fl_freeze_all_forms();
200         
201         const int bw = int(abs(float(fl_get_border_width())));
202
203         // a box
204         fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
205         
206         //
207         // THE SCROLLBAR
208         //
209
210         // up - scrollbar button
211         fl_set_object_geometry(button_up, xpos + width - 15,
212                                ypos,
213                                15, 15);
214         // the scrollbar slider
215         fl_set_object_geometry(scrollbar, xpos + width - 15,
216                                ypos + 15,
217                                15, height - 30);
218         // down - scrollbar button
219         fl_set_object_geometry(button_down, xpos + width - 15,
220                                ypos + height - 15,
221                                15, 15);
222
223         // Create the workarea pixmap
224         createPixmap(width - 15 - 2 * bw, height - 2 * bw);
225
226         // reset the painter
227         painter_.setDrawable(workareapixmap);
228         
229         // the free object
230         fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
231                                width - 15 - 2 * bw,
232                                height - 2 * bw);
233
234         fl_unfreeze_all_forms();
235
236 }
237
238
239 void WorkArea::createPixmap(int width, int height)
240 {
241         static int cur_width = -1;
242         static int cur_height = -1;
243
244         if (cur_width == width && cur_height == height && workareapixmap)
245                 return;
246         
247         cur_width = width;
248         cur_height = height;
249
250         if (workareapixmap)
251                 XFreePixmap(fl_display, workareapixmap);
252         
253         lyxerr << "Creating pixmap (" << width << 'x' << height << ")" << endl;
254         
255         workareapixmap = XCreatePixmap(fl_display,
256                                        RootWindow(fl_display, 0),
257                                        width,
258                                        height, 
259                                        fl_get_visual_depth());
260         lyxerr << "\tpixmap=" << workareapixmap << endl;
261 }
262
263
264 void WorkArea::greyOut() const
265 {
266         fl_winset(FL_ObjWin(work_area));
267         fl_rectangle(1, work_area->x, work_area->y,
268                      work_area->w, work_area->h, FL_GRAY63);
269 }
270
271
272 void WorkArea::setFocus() const
273 {
274         fl_set_focus_object(work_area->form, work_area);
275 }
276
277
278 void WorkArea::setScrollbar(double pos, double length_fraction) const
279 {
280         fl_set_slider_value(scrollbar, pos);
281         fl_set_slider_size(scrollbar, scrollbar->h * length_fraction);
282 }
283
284
285 void WorkArea::setScrollbarBounds(double l1, double l2) const
286 {
287         fl_set_slider_bounds(scrollbar, l1, l2);
288 }
289
290
291 void WorkArea::setScrollbarIncrements(float inc) const
292 {
293         fl_set_slider_increment(scrollbar, work_area->h - inc, inc);
294 }
295
296
297 void WorkArea::up_cb(FL_OBJECT * ob, long)
298 {
299         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
300         XEvent const * ev2 = fl_last_event();
301         static long time = 0;
302         ev2 = fl_last_event();
303         if (ev2->type == ButtonPress || ev2->type == ButtonRelease) 
304                 time = 0;
305         //area->up(time++, fl_get_button_numb(ob));
306         area->owner->upCB(time++, fl_get_button_numb(ob));
307 }
308
309
310 void WorkArea::down_cb(FL_OBJECT * ob, long)
311 {
312         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
313         XEvent const * ev2 = fl_last_event();
314         static long time = 0;
315         if (ev2->type == ButtonPress || ev2->type == ButtonRelease)
316                 time = 0;
317         //area->down(time++, fl_get_button_numb(ob));
318         area->owner->downCB(time++, fl_get_button_numb(ob));
319 }
320
321
322 // Callback for scrollbar slider
323 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
324 {
325         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
326
327         //area->scroll(fl_get_slider_value(ob));
328         area->owner->scrollCB(fl_get_slider_value(ob));
329         waitForX();
330 }
331
332 bool Lgb_bug_find_hack = false;
333
334 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
335                                   FL_Coord, FL_Coord ,
336                                   int /*key*/, void * xev)
337 {
338         static int x_old = -1;
339         static int y_old = -1;
340         static long scrollbar_value_old = -1;
341         
342         XEvent * ev = static_cast<XEvent*>(xev);
343         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
344
345         if (!area) return 1;
346         
347         switch (event){   
348         case FL_DRAW:
349                 if (!area->work_area ||
350                     !area->work_area->form->visible)
351                         return 1;
352                 lyxerr.debug() << "Workarea event: DRAW" << endl;
353                 area->createPixmap(area->workWidth(), area->height());
354                 Lgb_bug_find_hack = true;
355                 area->owner->workAreaExpose();
356                 Lgb_bug_find_hack = false;
357                 break;
358         case FL_PUSH:
359                 if (!ev) break;
360                 // Should really have used xbutton.state
361                 lyxerr.debug() << "Workarea event: PUSH" << endl;
362                 area->owner->workAreaButtonPress(ev->xbutton.x - ob->x,
363                                            ev->xbutton.y - ob->y,
364                                            ev->xbutton.button);
365                 break; 
366         case FL_RELEASE:
367                 if (!ev) break;
368                 // Should really have used xbutton.state
369                 lyxerr.debug() << "Workarea event: RELEASE" << endl;
370                 area->owner->workAreaButtonRelease(ev->xbutton.x - ob->x,
371                                              ev->xbutton.y - ob->y,
372                                              ev->xbutton.button);
373                 break;
374         case FL_MOUSE:
375                 if (!ev || ! area->scrollbar) break;
376                 if (ev->xmotion.x != x_old ||
377                     ev->xmotion.y != y_old ||
378                     fl_get_slider_value(area->scrollbar) != scrollbar_value_old) {
379                         lyxerr.debug() << "Workarea event: MOUSE" << endl;
380                         area->owner->workAreaMotionNotify(ev->xmotion.x - ob->x,
381                                                     ev->xmotion.y - ob->y,
382                                                     ev->xbutton.state);
383                 }
384                 break;
385         // Done by the raw callback:
386         //  case FL_KEYBOARD: WorkAreaKeyPress(ob, 0,0,0,ev,0); break;
387         case FL_FOCUS:
388                 lyxerr.debug() << "Workarea event: FOCUS" << endl;
389                 break;
390         case FL_UNFOCUS:
391                 lyxerr.debug() << "Workarea event: UNFOCUS" << endl;
392                 break;
393         case FL_ENTER:
394                 lyxerr.debug() << "Workarea event: ENTER" << endl;
395                 break;
396         case FL_LEAVE:
397                 lyxerr.debug() << "Workarea event: LEAVE" << endl;
398                 break;
399         case FL_DBLCLICK:
400                 if (!ev) break;
401                 lyxerr.debug() << "Workarea event: DBLCLICK" << endl;;
402                 break;
403         case FL_TRPLCLICK:
404                 if (!ev) break;
405                 lyxerr.debug() << "Workarea event: TRPLCLICK" << endl;
406                 break;
407         case FL_OTHER:
408                 if (!ev) break;
409                 if (ev->type == SelectionNotify) {
410                         lyxerr.debug() << "Workarea event: SELECTION" << endl;
411                         area->owner->workAreaSelectionNotify(area->work_area->form->window, ev);
412                 } else
413                         lyxerr.debug() << "Workarea event: OTHER" << endl;
414
415                 break;
416         }
417   
418         return 1;
419 }