]> git.lyx.org Git - lyx.git/blob - src/WorkArea.C
some sun compile fixes the need clipboard code patch from Dekel ans some other fixes
[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 "debug.h"
20 #include "support/lstrings.h"
21 #include "BufferView.h"
22
23 using std::endl;
24
25 FL_OBJECT * figinset_canvas;
26
27 // need to make the c++ compiler find 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_scroll_cb(FL_OBJECT * ob, long buf)
40         {
41                 WorkArea::scroll_cb(ob, buf);
42         }
43
44         int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
45                                            FL_Coord, FL_Coord, 
46                                            int key, void * xev)
47         {
48                 return WorkArea::work_area_handler(ob, event,
49                                                    0, 0, key, xev);
50         }
51 }
52
53
54
55 WorkArea::WorkArea(BufferView * o, int xpos, int ypos, int width, int height)
56         : owner(o), workareapixmap(0), painter_(*this)
57 {
58         fl_freeze_all_forms();
59
60         figinset_canvas = 0;
61
62         if (lyxerr.debugging())
63                 lyxerr << "Creating work area: +"
64                        << xpos << '+' << ypos << ' '
65                        << width << 'x' << height << endl;
66         //
67         FL_OBJECT * obj;
68         const int bw = int(std::abs(float(fl_get_border_width())));
69
70         // We really want to get rid of figinset_canvas.
71         ::figinset_canvas = figinset_canvas = obj =
72                   fl_add_canvas(FL_NORMAL_CANVAS,
73                                 xpos + 1, ypos + 1, 1, 1, "");
74         fl_set_object_boxtype(obj, FL_NO_BOX);
75         fl_set_object_resize(obj, FL_RESIZE_ALL);
76         fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
77         
78         // a box
79         if (lyxerr.debugging())
80                 lyxerr << "\tbackground box: +"
81                        << xpos << '+' << ypos << ' '
82                        << width - 15 << 'x' << height << endl;
83         backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
84                                          xpos, ypos,
85                                          width - 15,
86                                          height,"");
87         fl_set_object_resize(obj, FL_RESIZE_ALL);
88         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
89
90         //
91         // THE SCROLLBAR
92         //
93
94         scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
95                                            xpos + width - 15,
96                                            ypos, 17, height, "");
97         fl_set_object_boxtype(obj, FL_UP_BOX);
98         fl_set_object_resize(obj, FL_RESIZE_ALL);
99         fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
100         obj->u_vdata = this;
101         fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
102
103         ///
104         /// The free object
105
106         // Create the workarea pixmap
107         createPixmap(width - 15 - 2 * bw, height - 2 * bw);
108
109         // We add this object as late as possible to avoit problems
110         // with drawing.
111         if (lyxerr.debugging())
112                 lyxerr << "\tfree object: +"
113                        << xpos + bw << '+' << ypos + bw << ' '
114                        << width - 15 - 2 * bw << 'x'
115                        << height - 2 * bw << endl;
116         
117         work_area = obj = fl_add_free(FL_INPUT_FREE,
118                                       xpos + bw, ypos + bw,
119                                       width - 15 - 2 * bw, // scrollbarwidth
120                                       height - 2 * bw, "",
121                                       C_WorkArea_work_area_handler);
122         obj->wantkey = FL_KEY_TAB;
123         obj->u_vdata = this; /* This is how we pass the WorkArea
124                                        to the work_area_handler. */
125         fl_set_object_boxtype(obj,FL_DOWN_BOX);
126         fl_set_object_resize(obj, FL_RESIZE_ALL);
127         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
128
129         fl_unfreeze_all_forms();
130 }
131
132
133 WorkArea::~WorkArea()
134 {
135         if (workareapixmap)
136                 XFreePixmap(fl_display, workareapixmap);
137 }
138
139
140 bool WorkArea::belowMouse() const
141 {
142         FL_Coord x, y;
143         unsigned int button;
144         fl_get_mouse(&x, &y, &button);
145         FL_Coord ulx = work_area->form->x + work_area->x;
146         FL_Coord uly = work_area->form->y + work_area->y;
147         FL_Coord w = work_area->w;
148         FL_Coord h = work_area->h;
149         if (x > ulx && y > uly && x < ulx + h && y < uly + w)
150                 return true;
151         return false;
152         
153         
154         //lyxerr << "Mouse: (" << x << ", " << y <<") button = " << button << endl;
155         //lyxerr << "Workarea: (" << work_area->x + work_area->form->x << ", " << work_area->y + work_area->form->y << ", " << work_area->w << ", " << work_area->h << ")" << endl;
156         //lyxerr << "Below mouse: " << work_area->belowmouse << endl;
157         //return work_area->belowmouse;
158 }
159
160
161 void WorkArea::resize(int xpos, int ypos, int width, int height)
162 {
163         fl_freeze_all_forms();
164         
165         const int bw = int(std::abs(float(fl_get_border_width())));
166
167         // a box
168         fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
169         
170         //
171         // THE SCROLLBAR
172         //
173         fl_set_object_geometry(scrollbar, xpos + width - 15,
174                                ypos, 17, height);
175
176         // Create the workarea pixmap
177         createPixmap(width - 15 - 2 * bw, height - 2 * bw);
178
179         // the free object
180         fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
181                                width - 15 - 2 * bw,
182                                height - 2 * bw);
183
184         fl_unfreeze_all_forms();
185
186 }
187
188
189 void WorkArea::createPixmap(int width, int height)
190 {
191         static int cur_width = -1;
192         static int cur_height = -1;
193
194         if (cur_width == width && cur_height == height && workareapixmap)
195                 return;
196         
197         cur_width = width;
198         cur_height = height;
199
200         if (workareapixmap)
201                 XFreePixmap(fl_display, workareapixmap);
202
203         if (lyxerr.debugging())
204                 lyxerr << "Creating pixmap ("
205                        << width << 'x' << height << ")" << endl;
206         
207         workareapixmap = XCreatePixmap(fl_display,
208                                        RootWindow(fl_display, 0),
209                                        width,
210                                        height, 
211                                        fl_get_visual_depth());
212         if (lyxerr.debugging())
213                 lyxerr << "\tpixmap=" << workareapixmap << endl;
214 }
215
216
217 void WorkArea::greyOut() const
218 {
219         fl_winset(FL_ObjWin(work_area));
220         fl_rectangle(1, work_area->x, work_area->y,
221                      work_area->w, work_area->h, FL_GRAY63);
222 }
223
224
225 void WorkArea::setFocus() const
226 {
227         fl_set_focus_object(work_area->form, work_area);
228 }
229
230
231 void WorkArea::setScrollbar(double pos, double length_fraction) const
232 {
233         fl_set_scrollbar_value(scrollbar, pos);
234         fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
235 }
236
237
238 void WorkArea::setScrollbarBounds(double l1, double l2) const
239 {
240         fl_set_scrollbar_bounds(scrollbar, l1, l2);
241 }
242
243
244 void WorkArea::setScrollbarIncrements(double inc) const
245 {
246         fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
247 }
248
249
250 // Callback for scrollbar slider
251 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
252 {
253         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
254         // If we really want the accellerating scroll we can do that
255         // from here. IMHO that is a waste of effort since we already
256         // have other ways to move fast around in the document. (Lgb)
257         area->owner->scrollCB(fl_get_scrollbar_value(ob));
258         waitForX();
259 }
260
261 bool Lgb_bug_find_hack = false;
262
263 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
264                                 FL_Coord, FL_Coord ,
265                                 int /*key*/, void * xev)
266 {
267         static int x_old = -1;
268         static int y_old = -1;
269         static long scrollbar_value_old = -1;
270         
271         XEvent * ev = static_cast<XEvent*>(xev);
272         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
273
274         if (!area) return 1;
275         
276         switch (event){   
277         case FL_DRAW:
278                 if (!area->work_area ||
279                     !area->work_area->form->visible)
280                         return 1;
281                 lyxerr.debug() << "Workarea event: DRAW" << endl;
282                 area->createPixmap(area->workWidth(), area->height());
283                 Lgb_bug_find_hack = true;
284                 area->owner->workAreaExpose();
285                 Lgb_bug_find_hack = false;
286                 break;
287         case FL_PUSH:
288                 if (!ev) break;
289                 // Should really have used xbutton.state
290                 lyxerr.debug() << "Workarea event: PUSH" << endl;
291                 area->owner->workAreaButtonPress(ev->xbutton.x - ob->x,
292                                            ev->xbutton.y - ob->y,
293                                            ev->xbutton.button);
294                 break; 
295         case FL_RELEASE:
296                 if (!ev) break;
297                 // Should really have used xbutton.state
298                 lyxerr.debug() << "Workarea event: RELEASE" << endl;
299                 area->owner->workAreaButtonRelease(ev->xbutton.x - ob->x,
300                                              ev->xbutton.y - ob->y,
301                                              ev->xbutton.button);
302                 break;
303         case FL_MOUSE:
304                 if (!ev || ! area->scrollbar) break;
305                 if (ev->xmotion.x != x_old ||
306                     ev->xmotion.y != y_old ||
307                     fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
308                         ) {
309                         lyxerr.debug() << "Workarea event: MOUSE" << endl;
310                         area->owner->workAreaMotionNotify(ev->xmotion.x - ob->x,
311                                                     ev->xmotion.y - ob->y,
312                                                     ev->xbutton.state);
313                 }
314                 break;
315         // Done by the raw callback:
316         //  case FL_KEYBOARD: WorkAreaKeyPress(ob, 0,0,0,ev,0); break;
317         case FL_FOCUS:
318                 lyxerr.debug() << "Workarea event: FOCUS" << endl;
319                 break;
320         case FL_UNFOCUS:
321                 lyxerr.debug() << "Workarea event: UNFOCUS" << endl;
322                 break;
323         case FL_ENTER:
324                 lyxerr.debug() << "Workarea event: ENTER" << endl;
325                 area->owner->enterView();
326                 break;
327         case FL_LEAVE:
328                 lyxerr.debug() << "Workarea event: LEAVE" << endl;
329                 area->owner->leaveView();
330                 break;
331         case FL_DBLCLICK:
332                 if (!ev) break;
333                 lyxerr.debug() << "Workarea event: DBLCLICK" << endl;
334                 area->owner->doubleClick(ev->xbutton.x - ob->x,
335                                          ev->xbutton.y - ob->y,
336                                          ev->xbutton.button);
337                 break;
338         case FL_TRPLCLICK:
339                 if (!ev) break;
340                 lyxerr.debug() << "Workarea event: TRPLCLICK" << endl;
341                 area->owner->tripleClick(ev->xbutton.x - ob->x,
342                                          ev->xbutton.y - ob->y,
343                                          ev->xbutton.button);
344                 break;
345         case FL_OTHER:
346                 if (!ev) break;
347 #ifndef XFORMS_CLIPBOARD
348                 if (ev->type == SelectionNotify) {
349                         lyxerr.debug() << "Workarea event: SELECTION" << endl;
350                         area->owner->workAreaSelectionNotify(area->work_area->form->window, ev);
351                 } else
352 #endif
353                         lyxerr.debug() << "Workarea event: OTHER" << endl;
354
355                 break;
356         }
357   
358         return 1;
359 }
360
361
362 #ifdef XFORMS_CLIPBOARD
363 static string clipboard_selection;
364 static bool clipboard_read = false;
365
366 extern "C" {
367         static
368 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
369                         void const * data, long size) 
370 {
371         clipboard_selection.erase();
372         if (size == 0) return 0; // no selection
373          
374         clipboard_selection.reserve(size);
375         for (int i = 0; i < size; ++i) {
376                 clipboard_selection += static_cast<char const *>(data)[i];
377         }
378         clipboard_read = true;
379         return 0;
380 }
381 }
382
383 string WorkArea::getClipboard() const 
384 {
385         clipboard_read = false;
386         
387         if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
388                 return string();
389
390         XEvent ev;
391         
392         while (!clipboard_read) {
393                 if (fl_check_forms() == FL_EVENT) {
394                         lyxerr << "LyX: This shouldn't happen..." << endl;
395                         fl_XNextEvent(&ev);
396                 }
397         }
398         return clipboard_selection;
399 }
400
401         
402 void WorkArea::putClipboard(string const & s) const
403 {
404         static string hold;
405         hold = s;
406         
407         fl_stuff_clipboard(work_area, 0, hold.c_str(), hold.size(), 0);
408 }
409 #endif