]> git.lyx.org Git - lyx.git/blob - src/WorkArea.C
The forms resize patch, and small updates
[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
44 extern "C" {
45 // Just a bunch of C wrappers around static members of WorkArea
46         void C_WorkArea_scroll_cb(FL_OBJECT * ob, long buf)
47         {
48                 WorkArea::scroll_cb(ob, buf);
49         }
50
51         int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
52                                            FL_Coord, FL_Coord, 
53                                            int key, void * xev)
54         {
55                 return WorkArea::work_area_handler(ob, event,
56                                                    0, 0, key, xev);
57         }
58 }
59
60
61
62 WorkArea::WorkArea(BufferView * o, int xpos, int ypos, int width, int height)
63         : owner_(o), workareapixmap(0), painter_(*this)
64 {
65         fl_freeze_all_forms();
66
67         figinset_canvas = 0;
68
69         if (lyxerr.debugging(Debug::GUI))
70                 lyxerr << "Creating work area: +"
71                        << xpos << '+' << ypos << ' '
72                        << width << 'x' << height << endl;
73         //
74         FL_OBJECT * obj;
75         const int bw = int(std::abs(float(fl_get_border_width())));
76
77         // We really want to get rid of figinset_canvas.
78         ::figinset_canvas = figinset_canvas = obj =
79                   fl_add_canvas(FL_NORMAL_CANVAS,
80                                 xpos + 1, ypos + 1, 1, 1, "");
81         fl_set_object_boxtype(obj, FL_NO_BOX);
82         fl_set_object_resize(obj, FL_RESIZE_ALL);
83         fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
84         
85         // a box
86         if (lyxerr.debugging(Debug::GUI))
87                 lyxerr << "\tbackground box: +"
88                        << xpos << '+' << ypos << ' '
89                        << width - 15 << 'x' << height << endl;
90         backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
91                                          xpos, ypos,
92                                          width - 15,
93                                          height,"");
94         fl_set_object_resize(obj, FL_RESIZE_ALL);
95         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
96
97         //
98         // THE SCROLLBAR
99         //
100
101         scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
102                                            xpos + width - 15,
103                                            ypos, 17, height, "");
104         fl_set_object_boxtype(obj, FL_UP_BOX);
105         fl_set_object_resize(obj, FL_RESIZE_ALL);
106         fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
107         obj->u_vdata = this;
108         fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
109
110         ///
111         /// The free object
112
113         // Create the workarea pixmap
114         createPixmap(width - 15 - 2 * bw, height - 2 * bw);
115
116         // We add this object as late as possible to avoit problems
117         // with drawing.
118         if (lyxerr.debugging(Debug::GUI))
119                 lyxerr << "\tfree object: +"
120                        << xpos + bw << '+' << ypos + bw << ' '
121                        << width - 15 - 2 * bw << 'x'
122                        << height - 2 * bw << endl;
123         
124         work_area = obj = fl_add_free(FL_INPUT_FREE,
125                                       xpos + bw, ypos + bw,
126                                       width - 15 - 2 * bw, // scrollbarwidth
127                                       height - 2 * bw, "",
128                                       C_WorkArea_work_area_handler);
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_get_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_get_display(), workareapixmap);
209
210         if (lyxerr.debugging(Debug::GUI))
211                 lyxerr << "Creating pixmap ("
212                        << width << 'x' << height << ")" << endl;
213         
214         workareapixmap = XCreatePixmap(fl_get_display(),
215                                        RootWindow(fl_get_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
269 bool Lgb_bug_find_hack = false;
270
271 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
272                                 FL_Coord, FL_Coord ,
273                                 int key, void * xev)
274 {
275         static int x_old = -1;
276         static int y_old = -1;
277         static long scrollbar_value_old = -1;
278         
279         XEvent * ev = static_cast<XEvent*>(xev);
280         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
281
282         if (!area) return 1;
283         
284         switch (event){
285         case FL_DRAW:
286                 if (!area->work_area ||
287                     !area->work_area->form->visible)
288                         return 1;
289                 lyxerr[Debug::GUI] << "Workarea event: DRAW" << endl;
290                 area->createPixmap(area->workWidth(), area->height());
291                 Lgb_bug_find_hack = true;
292                 area->workAreaExpose();
293                 Lgb_bug_find_hack = false;
294                 break;
295         case FL_PUSH:
296                 if (!ev) break;
297                 // Should really have used xbutton.state
298                 lyxerr[Debug::GUI] << "Workarea event: PUSH" << endl;
299                 area->workAreaButtonPress(ev->xbutton.x - ob->x,
300                                           ev->xbutton.y - ob->y,
301                                           ev->xbutton.button);
302                 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
303                 break; 
304         case FL_RELEASE:
305                 if (!ev) break;
306                 // Should really have used xbutton.state
307                 lyxerr[Debug::GUI] << "Workarea event: RELEASE" << endl;
308                 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
309                                       ev->xbutton.y - ob->y,
310                                       ev->xbutton.button);
311                 break;
312         case FL_MOUSE:
313                 if (!ev || ! area->scrollbar) break;
314                 if (ev->xmotion.x != x_old ||
315                     ev->xmotion.y != y_old ||
316                     fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
317                         ) {
318                         lyxerr[Debug::GUI] << "Workarea event: MOUSE" << endl;
319                         area->workAreaMotionNotify(ev->xmotion.x - ob->x,
320                                              ev->xmotion.y - ob->y,
321                                              ev->xbutton.state);
322                 }
323                 break;
324         case FL_KEYBOARD:
325         {
326                 lyxerr[Debug::KEY] << "Workarea event: KEYBOARD" << endl;
327                 
328                 KeySym keysym = 0;
329                 char dummy[1];
330                 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
331 #if FL_REVISION < 89
332                 // XForms < 0.89 does not have compose support
333                 // so we are using our own compose support
334                 LyXLookupString(ev, dummy, 1, &keysym);
335 #else
336                 XLookupString(xke, dummy, 1, &keysym, 0);
337 #endif
338                 if (lyxerr.debugging(Debug::KEY)) {
339                         char const * tmp = XKeysymToString(key);
340                         char const * tmp2 = XKeysymToString(keysym);
341                         string stm = (tmp ? tmp : "");
342                         string stm2 = (tmp2 ? tmp2 : "");
343                         
344                         lyxerr << "WorkArea: Key is `" << stm << "' ["
345                                << key << "]" << endl;
346                         lyxerr << "WorkArea: Keysym is `" << stm2 << "' ["
347                                << keysym << "]" << endl;
348                 }
349
350 #if FL_REVISION < 89
351                 if (keysym == NoSymbol) {
352                         lyxerr[Debug::KEY]
353                                 << "Empty kdb action (probably composing)"
354                                 << endl;
355                         break;
356                 }
357                 KeySym ret_key = keysym;
358 #else
359                 // Note that we need this handling because of a bug
360                 // in XForms 0.89, if this bug is resolved in the way I hope
361                 // we can just use the keysym directly with out looking
362                 // at key at all. (Lgb)
363                 KeySym ret_key = 0;
364                 if (!key) {
365                         // We migth have to add more keysyms here also,
366                         // we will do that as the issues arise. (Lgb)
367                         if (keysym == XK_space)
368                                 ret_key = keysym;
369                         else
370                                 break;
371                 } else {
372                         ret_key = (keysym ? keysym : key);
373                 }
374                 
375 #endif  
376                 unsigned int ret_state = xke->state;
377
378                 // If you have a better way to handle "wild-output" of
379                 // characters after the key has been released than the one
380                 // below, please contact me. (Lgb)
381                 static Time last_time_pressed = 0;
382                 static unsigned int last_key_pressed = 0;
383                 static unsigned int last_state_pressed = 0;
384                 lyxerr[Debug::KEY] << "Workarea Diff: "
385                                    << xke->time - last_time_pressed
386                                    << endl;
387                 if (xke->time - last_time_pressed < 35 // should perhaps be tunable
388                     && xke->state == last_state_pressed
389                     && xke->keycode == last_key_pressed) {
390                         lyxerr[Debug::KEY]
391                                 << "Workarea: Purging X events." << endl;
392                         //lyxerr << "Workarea Events: "
393                         //       << XEventsQueued(fl_get_display(), QueuedAlready)
394                         //       << endl;
395                         if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
396                                 XSync(fl_get_display(), 1);
397                         // This purge make f.ex. scrolling stop immidiatly when
398                         // releasing the PageDown button. The question is if
399                         // this purging of XEvents can cause any harm...
400                         // after some testing I can see no problems, but
401                         // I'd like other reports too.
402                         break;
403                 }
404                 last_time_pressed = xke->time;
405                 last_key_pressed = xke->keycode;
406                 last_state_pressed = xke->state;
407                 
408                 area->workAreaKeyPress(ret_key, ret_state);
409         }
410         break;
411         case FL_FOCUS:
412                 lyxerr[Debug::GUI] << "Workarea event: FOCUS" << endl;
413                 area->workAreaFocus();
414                 break;
415         case FL_UNFOCUS:
416                 lyxerr[Debug::GUI] << "Workarea event: UNFOCUS" << endl;
417                 area->workAreaUnfocus();
418                 break;
419         case FL_ENTER:
420                 lyxerr[Debug::GUI] << "Workarea event: ENTER" << endl;
421                 area->workAreaEnter();
422                 break;
423         case FL_LEAVE:
424                 lyxerr[Debug::GUI] << "Workarea event: LEAVE" << endl;
425                 area->workAreaLeave();
426                 break;
427         case FL_DBLCLICK:
428                 if (!ev) break;
429                 lyxerr[Debug::GUI] << "Workarea event: DBLCLICK" << endl;
430                 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
431                                           ev->xbutton.y - ob->y,
432                                           ev->xbutton.button);
433                 break;
434         case FL_TRPLCLICK:
435                 if (!ev) break;
436                 lyxerr[Debug::GUI] << "Workarea event: TRPLCLICK" << endl;
437                 area->workAreaTripleClick(ev->xbutton.x - ob->x,
438                                           ev->xbutton.y - ob->y,
439                                           ev->xbutton.button);
440                 break;
441         case FL_OTHER:
442                 if (!ev) break;
443                         lyxerr[Debug::GUI] << "Workarea event: OTHER" << endl;
444
445                 break;
446         }
447   
448         return 1;
449 }
450
451
452 static string clipboard_selection;
453 static bool clipboard_read = false;
454
455 extern "C" {
456         static
457 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
458                         void const * data, long size) 
459 {
460         clipboard_selection.erase();
461
462         if (size > 0)
463                 clipboard_selection.reserve(size);
464         for (int i = 0; i < size; ++i)
465                 clipboard_selection += static_cast<char const *>(data)[i];
466         clipboard_read = true;
467         return 0;
468 }
469 } // extern "C"
470
471
472 string const WorkArea::getClipboard() const 
473 {
474         clipboard_read = false;
475         
476         if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
477                 return string();
478
479         XEvent ev;
480         
481         while (!clipboard_read) {
482                 if (fl_check_forms() == FL_EVENT) {
483                         lyxerr << "LyX: This shouldn't happen..." << endl;
484                         fl_XNextEvent(&ev);
485                 }
486         }
487         return clipboard_selection;
488 }
489
490         
491 void WorkArea::putClipboard(string const & s) const
492 {
493         static string hold;
494         hold = s;
495         
496         fl_stuff_clipboard(work_area, 0, hold.c_str(), hold.size(), 0);
497 }