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