]> git.lyx.org Git - lyx.git/blob - src/WorkArea.C
Fix small bug in reading \set_color in lyxrc
[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                 //workAreaExpose();
285                 area->owner_->workAreaExpose();
286                 Lgb_bug_find_hack = false;
287                 break;
288         case FL_PUSH:
289                 if (!ev) break;
290                 // Should really have used xbutton.state
291                 lyxerr.debug() << "Workarea event: PUSH" << endl;
292                 //workAreaButtonPress(ev->xbutton.x - ob->x,
293                 //                  ev->xbutton.y - ob->y,
294                 //                  ev->xbutton.button);
295                 
296                 area->owner_->workAreaButtonPress(ev->xbutton.x - ob->x,
297                                            ev->xbutton.y - ob->y,
298                                            ev->xbutton.button);
299                 break; 
300         case FL_RELEASE:
301                 if (!ev) break;
302                 // Should really have used xbutton.state
303                 lyxerr.debug() << "Workarea event: RELEASE" << endl;
304                 //workAreaButtonRelease(ev->xbutton.x - ob->x,
305                 //                    ev->xbutton.y - ob->y,
306                 //                    ev->xbutton.button);
307                 area->owner_->workAreaButtonRelease(ev->xbutton.x - ob->x,
308                                              ev->xbutton.y - ob->y,
309                                              ev->xbutton.button);
310                 break;
311         case FL_MOUSE:
312                 if (!ev || ! area->scrollbar) break;
313                 if (ev->xmotion.x != x_old ||
314                     ev->xmotion.y != y_old ||
315                     fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
316                         ) {
317                         lyxerr.debug() << "Workarea event: MOUSE" << endl;
318                         //workAreaMotionNotify(ev->xmotion.x - ob->x,
319                         //                   ev->xmotion.y - ob->y,
320                         //                   ev->xbutton.state);
321                         
322                         area->owner_->workAreaMotionNotify(ev->xmotion.x - ob->x,
323                                                     ev->xmotion.y - ob->y,
324                                                     ev->xbutton.state);
325                 }
326                 break;
327         // Done by the raw callback:
328         //  case FL_KEYBOARD: WorkAreaKeyPress(ob, 0,0,0,ev,0); break;
329         case FL_FOCUS:
330                 lyxerr.debug() << "Workarea event: FOCUS" << endl;
331                 //workAreaFocus();
332                 break;
333         case FL_UNFOCUS:
334                 lyxerr.debug() << "Workarea event: UNFOCUS" << endl;
335                 //workAreaUnfocus();
336                 break;
337         case FL_ENTER:
338                 lyxerr.debug() << "Workarea event: ENTER" << endl;
339                 //workAreaEnter();
340                 area->owner_->enterView();
341                 break;
342         case FL_LEAVE:
343                 lyxerr.debug() << "Workarea event: LEAVE" << endl;
344                 //workAreaLeave();
345                 area->owner_->leaveView();
346                 break;
347         case FL_DBLCLICK:
348                 if (!ev) break;
349                 lyxerr.debug() << "Workarea event: DBLCLICK" << endl;
350                 //workAreaDoubleClick(ev->xbutton.x - ob->x,
351                 //                  ev->xbutton.y - ob->y,
352                 //                  ev->xbutton.button);
353                 area->owner_->doubleClick(ev->xbutton.x - ob->x,
354                                          ev->xbutton.y - ob->y,
355                                          ev->xbutton.button);
356                 break;
357         case FL_TRPLCLICK:
358                 if (!ev) break;
359                 lyxerr.debug() << "Workarea event: TRPLCLICK" << endl;
360                 //workAreaTripleClick(ev->xbutton.x - ob->x,
361                 //                  ev->xbutton.y - ob->y,
362                 //                  ev->xbutton.button);
363                 area->owner_->tripleClick(ev->xbutton.x - ob->x,
364                                          ev->xbutton.y - ob->y,
365                                          ev->xbutton.button);
366                 break;
367         case FL_OTHER:
368                 if (!ev) break;
369                         lyxerr.debug() << "Workarea event: OTHER" << endl;
370
371                 break;
372         }
373   
374         return 1;
375 }
376
377
378 static string clipboard_selection;
379 static bool clipboard_read = false;
380
381 extern "C" {
382         static
383 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
384                         void const * data, long size) 
385 {
386         clipboard_selection.erase();
387
388         if (size > 0)
389                 clipboard_selection.reserve(size);
390         for (int i = 0; i < size; ++i)
391                 clipboard_selection += static_cast<char const *>(data)[i];
392         clipboard_read = true;
393         return 0;
394 }
395 }
396
397 string WorkArea::getClipboard() const 
398 {
399         clipboard_read = false;
400         
401         if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
402                 return string();
403
404         XEvent ev;
405         
406         while (!clipboard_read) {
407                 if (fl_check_forms() == FL_EVENT) {
408                         lyxerr << "LyX: This shouldn't happen..." << endl;
409                         fl_XNextEvent(&ev);
410                 }
411         }
412         return clipboard_selection;
413 }
414
415         
416 void WorkArea::putClipboard(string const & s) const
417 {
418         static string hold;
419         hold = s;
420         
421         fl_stuff_clipboard(work_area, 0, hold.c_str(), hold.size(), 0);
422 }