]> git.lyx.org Git - lyx.git/blob - src/WorkArea.C
now fix the KP_ stuff, simplify keysym handling in Workarea, this might break other...
[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 #include <cctype>
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 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
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         int const 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_ALL_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         int const 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 || ev->xbutton.button == 0) 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 || ev->xbutton.button == 0) 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 #if FL_REVISION < 89
313         case FL_MOUSE:
314 #else
315         case FL_DRAG:
316 #endif
317                 if (!ev || ! area->scrollbar) break;
318                 if (ev->xmotion.x != x_old ||
319                     ev->xmotion.y != y_old ||
320                     fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
321                         ) {
322                         lyxerr[Debug::GUI] << "Workarea event: MOUSE" << endl;
323                         area->workAreaMotionNotify(ev->xmotion.x - ob->x,
324                                              ev->xmotion.y - ob->y,
325                                              ev->xbutton.state);
326                 }
327                 break;
328 #if FL_REVISION < 89
329         case FL_KEYBOARD:
330 #else
331         case FL_KEYPRESS:
332 #endif
333         {
334                 lyxerr[Debug::KEY] << "Workarea event: KEYBOARD" << endl;
335                 
336                 KeySym keysym = 0;
337                 char dummy[1];
338                 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
339 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
340                 // XForms < 0.89.5 does not have compose support
341                 // so we are using our own compose support
342                 LyXLookupString(ev, dummy, 1, &keysym);
343 #else
344                 XLookupString(xke, dummy, 1, &keysym, 0);
345 #endif
346                 if (lyxerr.debugging(Debug::KEY)) {
347                         char const * tmp = XKeysymToString(key);
348                         char const * tmp2 = XKeysymToString(keysym);
349                         string const stm = (tmp ? tmp : "");
350                         string const stm2 = (tmp2 ? tmp2 : "");
351                         
352                         lyxerr << "WorkArea: Key is `" << stm << "' ["
353                                << key << "]" << endl;
354                         lyxerr << "WorkArea: Keysym is `" << stm2 << "' ["
355                                << keysym << "]" << endl;
356                 }
357
358 #if FL_REVISION < 89
359                 if (keysym == NoSymbol) {
360                         lyxerr[Debug::KEY]
361                                 << "Empty kdb action (probably composing)"
362                                 << endl;
363                         break;
364                 }
365                 KeySym ret_key = keysym;
366 #else
367                 // Note that we need this handling because of a bug
368                 // in XForms 0.89, if this bug is resolved in the way I hope
369                 // we can just use the keysym directly with out looking
370                 // at key at all. (Lgb)
371                 KeySym ret_key = 0;
372                 if (!key) {
373                         // We migth have to add more keysyms here also,
374                         // we will do that as the issues arise. (Lgb)
375                         if (keysym == XK_space) {
376                                 ret_key = keysym;
377                                 lyxerr[Debug::KEY] << "Using keysym [A]"
378                                                    << endl;
379                         } else
380                                 break;
381                 } else {
382                         // It seems that this was a bit optimistic...
383                         // With this hacking things seems to be better (Lgb)
384                         //if (!iscntrl(key)) {
385                         //      ret_key = key;
386                         //      lyxerr[Debug::KEY]
387                         //              << "Using key [B]\n"
388                         //              << "Uchar["
389                         //              << static_cast<unsigned char>(key)
390                         //              << endl;
391                         //} else {
392                                 ret_key = (keysym ? keysym : key);
393                                 lyxerr[Debug::KEY] << "Using keysym [B]"
394                                                    << endl;
395                                 //}
396                 }
397                 
398 #endif  
399                 unsigned int const ret_state = xke->state;
400
401                 // If you have a better way to handle "wild-output" of
402                 // characters after the key has been released than the one
403                 // below, please contact me. (Lgb)
404                 static Time last_time_pressed = 0;
405                 static unsigned int last_key_pressed = 0;
406                 static unsigned int last_state_pressed = 0;
407                 lyxerr[Debug::KEY] << "Workarea Diff: "
408                                    << xke->time - last_time_pressed
409                                    << endl;
410                 if (xke->time - last_time_pressed < 35 // should perhaps be tunable
411                     && ret_state == last_state_pressed
412                     && xke->keycode == last_key_pressed) {
413                         lyxerr[Debug::KEY]
414                                 << "Workarea: Purging X events." << endl;
415                         //lyxerr << "Workarea Events: "
416                         //       << XEventsQueued(fl_get_display(), QueuedAlready)
417                         //       << endl;
418                         if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
419                                 XSync(fl_get_display(), 1);
420                         // This purge make f.ex. scrolling stop immidiatly when
421                         // releasing the PageDown button. The question is if
422                         // this purging of XEvents can cause any harm...
423                         // after some testing I can see no problems, but
424                         // I'd like other reports too.
425                         break;
426                 }
427                 last_time_pressed = xke->time;
428                 last_key_pressed = xke->keycode;
429                 last_state_pressed = ret_state;
430                 
431                 area->workAreaKeyPress(ret_key, ret_state);
432         }
433         break;
434
435 #if FL_REVISION >= 89
436         case FL_KEYRELEASE:
437                 lyxerr << "Workarea event: KEYRELEASE" << endl;
438                 break;
439 #endif
440
441         case FL_FOCUS:
442                 lyxerr[Debug::GUI] << "Workarea event: FOCUS" << endl;
443                 area->workAreaFocus();
444                 break;
445         case FL_UNFOCUS:
446                 lyxerr[Debug::GUI] << "Workarea event: UNFOCUS" << endl;
447                 area->workAreaUnfocus();
448                 break;
449         case FL_ENTER:
450                 lyxerr[Debug::GUI] << "Workarea event: ENTER" << endl;
451                 area->workAreaEnter();
452                 break;
453         case FL_LEAVE:
454                 lyxerr[Debug::GUI] << "Workarea event: LEAVE" << endl;
455                 area->workAreaLeave();
456                 break;
457         case FL_DBLCLICK:
458                 if (!ev) break;
459                 lyxerr[Debug::GUI] << "Workarea event: DBLCLICK" << endl;
460                 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
461                                           ev->xbutton.y - ob->y,
462                                           ev->xbutton.button);
463                 break;
464         case FL_TRPLCLICK:
465                 if (!ev) break;
466                 lyxerr[Debug::GUI] << "Workarea event: TRPLCLICK" << endl;
467                 area->workAreaTripleClick(ev->xbutton.x - ob->x,
468                                           ev->xbutton.y - ob->y,
469                                           ev->xbutton.button);
470                 break;
471         case FL_OTHER:
472                 if (!ev) break;
473                         lyxerr[Debug::GUI] << "Workarea event: OTHER" << endl;
474
475                 break;
476         }
477   
478         return 1;
479 }
480
481
482 static string clipboard_selection;
483 static bool clipboard_read = false;
484
485 extern "C" {
486         static
487 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
488                         void const * data, long size) 
489 {
490         clipboard_selection.erase();
491
492         if (size > 0)
493                 clipboard_selection.reserve(size);
494         for (int i = 0; i < size; ++i)
495                 clipboard_selection += static_cast<char const *>(data)[i];
496         clipboard_read = true;
497         return 0;
498 }
499 } // extern "C"
500
501
502 string const WorkArea::getClipboard() const 
503 {
504         clipboard_read = false;
505         
506         if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
507                 return string();
508
509         XEvent ev;
510         
511         while (!clipboard_read) {
512                 if (fl_check_forms() == FL_EVENT) {
513                         lyxerr << "LyX: This shouldn't happen..." << endl;
514                         fl_XNextEvent(&ev);
515                 }
516         }
517         return clipboard_selection;
518 }
519
520         
521 void WorkArea::putClipboard(string const & s) const
522 {
523         static string hold;
524         hold = s;
525         
526         fl_stuff_clipboard(work_area, 0, hold.c_str(), hold.size(), 0);
527 }