]> git.lyx.org Git - lyx.git/blob - src/WorkArea.C
the export patch from Dekel
[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 #include "LyXView.h"
23 #include "lyxfunc.h"
24
25 #if FL_REVISION < 89
26 #include "lyxlookup.h"
27 #endif
28
29 using std::endl;
30
31 FL_OBJECT * figinset_canvas;
32
33 // needed to make the c++ compiler find the correct version of abs.
34 // This is at least true for g++.
35 //using std::abs;
36
37 static inline
38 void waitForX()
39 {
40         XSync(fl_get_display(), 0);
41 }
42
43 extern "C" {
44 // Just a bunch of C wrappers around static members of WorkArea
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(Debug::GUI))
69                 lyxerr << "Creating work area: +"
70                        << xpos << '+' << ypos << ' '
71                        << width << 'x' << height << endl;
72         //
73         FL_OBJECT * obj;
74         const int bw = int(std::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(Debug::GUI))
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         scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
101                                            xpos + width - 15,
102                                            ypos, 17, height, "");
103         fl_set_object_boxtype(obj, FL_UP_BOX);
104         fl_set_object_resize(obj, FL_RESIZE_ALL);
105         fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
106         obj->u_vdata = this;
107         fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
108
109         ///
110         /// The free object
111
112         // Create the workarea pixmap
113         createPixmap(width - 15 - 2 * bw, height - 2 * bw);
114
115         // We add this object as late as possible to avoit problems
116         // with drawing.
117         if (lyxerr.debugging(Debug::GUI))
118                 lyxerr << "\tfree object: +"
119                        << xpos + bw << '+' << ypos + bw << ' '
120                        << width - 15 - 2 * bw << 'x'
121                        << height - 2 * bw << endl;
122         
123         work_area = obj = fl_add_free(FL_INPUT_FREE,
124                                       xpos + bw, ypos + bw,
125                                       width - 15 - 2 * bw, // scrollbarwidth
126                                       height - 2 * bw, "",
127                                       C_WorkArea_work_area_handler);
128         //obj->wantkey = FL_KEY_TAB;
129         obj->wantkey = FL_KEY_ALL;
130         obj->u_vdata = this; /* This is how we pass the WorkArea
131                                        to the work_area_handler. */
132         fl_set_object_boxtype(obj,FL_DOWN_BOX);
133         fl_set_object_resize(obj, FL_RESIZE_ALL);
134         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
135
136         fl_unfreeze_all_forms();
137 }
138
139
140 WorkArea::~WorkArea()
141 {
142         if (workareapixmap)
143                 XFreePixmap(fl_display, workareapixmap);
144 }
145
146
147 bool WorkArea::belowMouse() const
148 {
149         FL_Coord x, y;
150         unsigned int button;
151         fl_get_mouse(&x, &y, &button);
152         FL_Coord ulx = work_area->form->x + work_area->x;
153         FL_Coord uly = work_area->form->y + work_area->y;
154         FL_Coord w = work_area->w;
155         FL_Coord h = work_area->h;
156         if (x > ulx && y > uly && x < ulx + h && y < uly + w)
157                 return true;
158         return false;
159         
160         
161         //lyxerr << "Mouse: (" << x << ", " << y <<") button = " << button << endl;
162         //lyxerr << "Workarea: (" << work_area->x + work_area->form->x << ", " << work_area->y + work_area->form->y << ", " << work_area->w << ", " << work_area->h << ")" << endl;
163         //lyxerr << "Below mouse: " << work_area->belowmouse << endl;
164         //return work_area->belowmouse;
165 }
166
167
168 void WorkArea::resize(int xpos, int ypos, int width, int height)
169 {
170         fl_freeze_all_forms();
171         
172         const int bw = int(std::abs(float(fl_get_border_width())));
173
174         // a box
175         fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
176         
177         //
178         // THE SCROLLBAR
179         //
180         fl_set_object_geometry(scrollbar, xpos + width - 15,
181                                ypos, 17, height);
182
183         // Create the workarea pixmap
184         createPixmap(width - 15 - 2 * bw, height - 2 * bw);
185
186         // the free object
187         fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
188                                width - 15 - 2 * bw,
189                                height - 2 * bw);
190
191         fl_unfreeze_all_forms();
192
193 }
194
195
196 void WorkArea::createPixmap(int width, int height)
197 {
198         static int cur_width = -1;
199         static int cur_height = -1;
200
201         if (cur_width == width && cur_height == height && workareapixmap)
202                 return;
203         
204         cur_width = width;
205         cur_height = height;
206
207         if (workareapixmap)
208                 XFreePixmap(fl_display, workareapixmap);
209
210         if (lyxerr.debugging(Debug::GUI))
211                 lyxerr << "Creating pixmap ("
212                        << width << 'x' << height << ")" << endl;
213         
214         workareapixmap = XCreatePixmap(fl_display,
215                                        RootWindow(fl_display, 0),
216                                        width,
217                                        height, 
218                                        fl_get_visual_depth());
219         if (lyxerr.debugging(Debug::GUI))
220                 lyxerr << "\tpixmap=" << workareapixmap << endl;
221 }
222
223
224 void WorkArea::greyOut() const
225 {
226         fl_winset(FL_ObjWin(work_area));
227         fl_rectangle(1, work_area->x, work_area->y,
228                      work_area->w, work_area->h, FL_GRAY63);
229 }
230
231
232 void WorkArea::setFocus() const
233 {
234         fl_set_focus_object(work_area->form, work_area);
235 }
236
237
238 void WorkArea::setScrollbar(double pos, double length_fraction) const
239 {
240         fl_set_scrollbar_value(scrollbar, pos);
241         fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
242 }
243
244
245 void WorkArea::setScrollbarBounds(double l1, double l2) const
246 {
247         fl_set_scrollbar_bounds(scrollbar, l1, l2);
248 }
249
250
251 void WorkArea::setScrollbarIncrements(double inc) const
252 {
253         fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
254 }
255
256
257 // Callback for scrollbar slider
258 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
259 {
260         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
261         // If we really want the accellerating scroll we can do that
262         // from here. IMHO that is a waste of effort since we already
263         // have other ways to move fast around in the document. (Lgb)
264         area->owner_->scrollCB(fl_get_scrollbar_value(ob));
265         waitForX();
266 }
267
268 bool Lgb_bug_find_hack = false;
269
270 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
271                                 FL_Coord, FL_Coord ,
272                                 int key, void * xev)
273 {
274         static int x_old = -1;
275         static int y_old = -1;
276         static long scrollbar_value_old = -1;
277         
278         XEvent * ev = static_cast<XEvent*>(xev);
279         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
280
281         if (!area) return 1;
282         
283         switch (event){
284         case FL_DRAW:
285                 if (!area->work_area ||
286                     !area->work_area->form->visible)
287                         return 1;
288                 lyxerr[Debug::GUI] << "Workarea event: DRAW" << endl;
289                 area->createPixmap(area->workWidth(), area->height());
290                 Lgb_bug_find_hack = true;
291                 area->workAreaExpose();
292                 Lgb_bug_find_hack = false;
293                 break;
294         case FL_PUSH:
295                 if (!ev) break;
296                 // Should really have used xbutton.state
297                 lyxerr[Debug::GUI] << "Workarea event: PUSH" << endl;
298                 area->workAreaButtonPress(ev->xbutton.x - ob->x,
299                                           ev->xbutton.y - ob->y,
300                                           ev->xbutton.button);
301                 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
302                 break; 
303         case FL_RELEASE:
304                 if (!ev) break;
305                 // Should really have used xbutton.state
306                 lyxerr[Debug::GUI] << "Workarea event: RELEASE" << endl;
307                 area->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::GUI] << "Workarea event: MOUSE" << endl;
318                         area->workAreaMotionNotify(ev->xmotion.x - ob->x,
319                                              ev->xmotion.y - ob->y,
320                                              ev->xbutton.state);
321                 }
322                 break;
323         case FL_KEYBOARD:
324         {
325                 lyxerr[Debug::KEY] << "Workarea event: KEYBOARD" << endl;
326                 
327                 KeySym keysym = 0;
328                 char dummy[1];
329                 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
330 #if FL_REVISION < 89
331                 // XForms < 0.89 does not have compose support
332                 // so we are using our own compose support
333                 LyXLookupString(ev, dummy, 1, &keysym);
334 #else
335                 XLookupString(xke, dummy, 1, &keysym, 0);
336 #endif
337                 if (lyxerr.debugging(Debug::KEY)) {
338                         char const * tmp = XKeysymToString(key);
339                         char const * tmp2 = XKeysymToString(keysym);
340                         string stm = (tmp ? tmp : "");
341                         string stm2 = (tmp2 ? tmp2 : "");
342                         
343                         lyxerr << "WorkArea: Key is `" << stm << "' ["
344                                << key << "]" << endl;
345                         lyxerr << "WorkArea: Keysym is `" << stm2 << "' ["
346                                << keysym << "]" << endl;
347                 }
348
349 #if FL_REVISION < 89
350                 if (keysym == NoSymbol) {
351                         lyxerr[Debug::KEY]
352                                 << "Empty kdb action (probably composing)"
353                                 << endl;
354                         break;
355                 }
356                 KeySym ret_key = keysym;
357 #else
358                 // Ok, this is a bit simplistic...seems that the rules
359                 // need to be a bit more...
360                 if (!key) break;
361                 KeySym ret_key = (keysym ? keysym : key);
362 #endif  
363                 unsigned int ret_state = xke->state;
364                 
365                 static Time last_time_pressed = 0;
366                 static unsigned int last_key_pressed = 0;
367                 static unsigned int last_state_pressed = 0;
368                 //lyxerr << "Workarea Diff: " << xke->time - last_time_pressed
369                 //       << endl;
370                 if (xke->time - last_time_pressed < 40 // should perhaps be tunable
371                     && xke->state == last_state_pressed
372                     && xke->keycode == last_key_pressed) {
373                         lyxerr[Debug::KEY]
374                                 << "Workarea: Purging X events." << endl;
375                         //lyxerr << "Workarea Events: "
376                         //       << XEventsQueued(fl_get_display(), QueuedAlready)
377                         //       << endl;
378                         if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
379                                 XSync(fl_get_display(), 1);
380                         // This purge make f.ex. scrolling stop immidiatly when
381                         // releasing the PageDown button. The question is if
382                         // this purging of XEvents can cause any harm...
383                         // after some testing I can see no problems, but
384                         // I'd like other reports too.
385                         break;
386                 }
387                 last_time_pressed = xke->time;
388                 last_key_pressed = xke->keycode;
389                 last_state_pressed = xke->state;
390                 
391                 area->workAreaKeyPress(ret_key, ret_state);
392         }
393         break;
394         case FL_FOCUS:
395                 lyxerr[Debug::GUI] << "Workarea event: FOCUS" << endl;
396                 area->workAreaFocus();
397                 break;
398         case FL_UNFOCUS:
399                 lyxerr[Debug::GUI] << "Workarea event: UNFOCUS" << endl;
400                 area->workAreaUnfocus();
401                 break;
402         case FL_ENTER:
403                 lyxerr[Debug::GUI] << "Workarea event: ENTER" << endl;
404                 area->workAreaEnter();
405                 break;
406         case FL_LEAVE:
407                 lyxerr[Debug::GUI] << "Workarea event: LEAVE" << endl;
408                 area->workAreaLeave();
409                 break;
410         case FL_DBLCLICK:
411                 if (!ev) break;
412                 lyxerr[Debug::GUI] << "Workarea event: DBLCLICK" << endl;
413                 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
414                                           ev->xbutton.y - ob->y,
415                                           ev->xbutton.button);
416                 break;
417         case FL_TRPLCLICK:
418                 if (!ev) break;
419                 lyxerr[Debug::GUI] << "Workarea event: TRPLCLICK" << endl;
420                 area->workAreaTripleClick(ev->xbutton.x - ob->x,
421                                           ev->xbutton.y - ob->y,
422                                           ev->xbutton.button);
423                 break;
424         case FL_OTHER:
425                 if (!ev) break;
426                         lyxerr[Debug::GUI] << "Workarea event: OTHER" << endl;
427
428                 break;
429         }
430   
431         return 1;
432 }
433
434
435 static string clipboard_selection;
436 static bool clipboard_read = false;
437
438 extern "C" {
439         static
440 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
441                         void const * data, long size) 
442 {
443         clipboard_selection.erase();
444
445         if (size > 0)
446                 clipboard_selection.reserve(size);
447         for (int i = 0; i < size; ++i)
448                 clipboard_selection += static_cast<char const *>(data)[i];
449         clipboard_read = true;
450         return 0;
451 }
452 }
453
454 string WorkArea::getClipboard() const 
455 {
456         clipboard_read = false;
457         
458         if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
459                 return string();
460
461         XEvent ev;
462         
463         while (!clipboard_read) {
464                 if (fl_check_forms() == FL_EVENT) {
465                         lyxerr << "LyX: This shouldn't happen..." << endl;
466                         fl_XNextEvent(&ev);
467                 }
468         }
469         return clipboard_selection;
470 }
471
472         
473 void WorkArea::putClipboard(string const & s) const
474 {
475         static string hold;
476         hold = s;
477         
478         fl_stuff_clipboard(work_area, 0, hold.c_str(), hold.size(), 0);
479 }