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