]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XWorkArea.C
Hold on to your hats.
[lyx.git] / src / frontends / xforms / XWorkArea.C
1 /**
2  * \file XWorkArea.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14
15 #include "XWorkArea.h"
16 #include "debug.h"
17 #include "XLyXKeySym.h"
18 #include "funcrequest.h"
19 #include "Timeout.h"
20
21 using std::endl;
22 using std::abs;
23 using std::hex;
24 using std::dec;
25
26 namespace {
27
28 inline
29 void waitForX(bool discard)
30 {
31         XSync(fl_get_display(), discard);
32 }
33
34
35 mouse_button::state x_button_state(unsigned int button)
36 {
37         mouse_button::state b = mouse_button::none;
38         switch (button) {
39         case FL_MBUTTON1:
40                 b = mouse_button::button1;
41                 break;
42         case FL_MBUTTON2:
43                 b = mouse_button::button2;
44                 break;
45         case FL_MBUTTON3:
46                 b = mouse_button::button3;
47                 break;
48         case FL_MBUTTON4:
49                 b = mouse_button::button4;
50                 break;
51         case FL_MBUTTON5:
52                 b = mouse_button::button5;
53                 break;
54         }
55         return b;
56 }
57
58
59 key_modifier::state x_key_state(unsigned int state)
60 {
61         key_modifier::state k = key_modifier::none;
62         if (state & ControlMask)
63                 k |= key_modifier::ctrl;
64         if (state & ShiftMask)
65                 k |= key_modifier::shift;
66         if (state & Mod1Mask)
67                 k |= key_modifier::alt;
68         return k;
69 }
70
71
72 extern "C" {
73
74 void C_scroll_cb(FL_OBJECT * ob, long)
75 {
76         XWorkArea * area = static_cast<XWorkArea *>(ob->u_vdata);
77         area->scroll_cb();
78 }
79
80
81 int C_work_area_handler(FL_OBJECT * ob, int event, FL_Coord, FL_Coord,
82                         int key, void * xev)
83 {
84         return XWorkArea::work_area_handler(ob, event, 0, 0, key, xev);
85 }
86
87
88 int C_event_cb(FL_FORM * form, void * xev)
89 {
90         XWorkArea * area = static_cast<XWorkArea *>(form->u_vdata);
91         return area->event_cb(static_cast<XEvent *>(xev));
92 }
93
94 } // extern "C"
95 } // namespace anon
96
97
98 XWorkArea::XWorkArea(int x, int y, int w, int h)
99         : workareapixmap(0), painter_(*this)
100 {
101         if (lyxerr.debugging(Debug::WORKAREA)) {
102                 lyxerr << "\tbackground box: +"
103                        << x << '+' << y << ' '
104                        << w - 15 << 'x' << h << endl;
105         }
106
107         fl_freeze_all_forms();
108
109         FL_OBJECT * obj;
110
111         obj = fl_add_box(FL_BORDER_BOX, x, y, w - 15, h, "");
112         fl_set_object_resize(obj, FL_RESIZE_ALL);
113         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
114
115         scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
116                                            x + w - 15, y, 17, h, "");
117         fl_set_object_boxtype(obj, FL_UP_BOX);
118         fl_set_object_resize(obj, FL_RESIZE_ALL);
119         fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
120         obj->u_vdata = this;
121         fl_set_object_callback(obj, C_scroll_cb, 0);
122         fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
123         fl_set_scrollbar_value(scrollbar, 0.0);
124         fl_set_scrollbar_size(scrollbar, scrollbar->h);
125
126         int const bw = int(abs(fl_get_border_width()));
127
128         // Create the workarea pixmap
129         // FIXME remove redraw(w - 15 - 2 * bw, h - 2 * bw);
130
131         if (lyxerr.debugging(Debug::WORKAREA))
132                 lyxerr << "\tfree object: +"
133                        << x + bw << '+' << y + bw << ' '
134                        << w - 15 - 2 * bw << 'x'
135                        << h - 2 * bw << endl;
136
137         // We add this object as late as possible to avoid problems
138         // with drawing.
139         // FIXME: like ??
140         work_area = obj = fl_add_free(FL_ALL_FREE,
141                                       x + bw, y + bw,
142                                       w - 15 - 2 * bw,
143                                       h - 2 * bw, "",
144                                       C_work_area_handler);
145         obj->wantkey = FL_KEY_ALL;
146         obj->u_vdata = this;
147
148         fl_set_object_boxtype(obj,FL_DOWN_BOX);
149         fl_set_object_resize(obj, FL_RESIZE_ALL);
150         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
151
152         /// X selection hook - xforms gets it wrong
153         fl_current_form->u_vdata = this;
154         fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_event_cb);
155
156         fl_unfreeze_all_forms();
157
158         XGCValues val;
159
160         val.function = GXcopy;
161         copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
162                             GCFunction, &val);
163 }
164
165
166 XWorkArea::~XWorkArea()
167 {
168         XFreeGC(fl_get_display(), copy_gc);
169         if (workareapixmap)
170                 XFreePixmap(fl_get_display(), workareapixmap);
171 }
172
173
174 void XWorkArea::redraw(int width, int height)
175 {
176         static int cur_width = -1;
177         static int cur_height = -1;
178
179         if (cur_width == width && cur_height == height && workareapixmap) {
180                 XCopyArea(fl_get_display(),
181                           getPixmap(), getWin(), copy_gc,
182                           0, 0, width, height, xpos(), ypos());
183                 return;
184         }
185
186         cur_width = width;
187         cur_height = height;
188
189         if (lyxerr.debugging(Debug::WORKAREA)) {
190                 lyxerr << "(Re)creating pixmap ("
191                        << width << 'x' << height << ')' << endl;
192         }
193
194         if (workareapixmap) {
195                 XFreePixmap(fl_get_display(), workareapixmap);
196         }
197
198         workareapixmap = XCreatePixmap(fl_get_display(),
199                                        RootWindow(fl_get_display(), 0),
200                                        width,
201                                        height,
202                                        fl_get_visual_depth());
203
204         workAreaResize();
205 }
206
207
208 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
209 {
210         // we need to cache this for scroll_cb
211         doc_height_ = height;
212
213         if (height == 0) {
214                 fl_set_scrollbar_value(scrollbar, 0.0);
215                 fl_set_scrollbar_size(scrollbar, scrollbar->h);
216                 return;
217         }
218
219         long const work_height = workHeight();
220
221         if (lyxerr.debugging(Debug::GUI)) {
222                 lyxerr << "scroll: height now " << height << '\n'
223                        << "scroll: work_height " << work_height << endl;
224         }
225
226         /* If the text is smaller than the working area, the scrollbar
227          * maximum must be the working area height. No scrolling will
228          * be possible */
229         if (height <= work_height) {
230                 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !"
231                                    << endl;
232                 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
233                 fl_set_scrollbar_value(scrollbar, pos);
234                 fl_set_scrollbar_size(scrollbar, scrollbar->h);
235                 return;
236         }
237
238         fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
239         fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
240
241         fl_set_scrollbar_value(scrollbar, pos);
242
243         double const slider_size =
244                 (height == 0) ? 1.0 : 1.0 / double(height);
245
246         fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
247 }
248
249
250 // callback for scrollbar slider
251 void XWorkArea::scroll_cb()
252 {
253         double const val = fl_get_scrollbar_value(scrollbar);
254
255         if (lyxerr.debugging(Debug::GUI)) {
256                 lyxerr << "scroll: val: " << val << '\n'
257                        << "scroll: height: " << scrollbar->h << '\n'
258                        << "scroll: docheight: " << doc_height_ << endl;
259         }
260
261         scrollDocView(int(val));
262         waitForX(false);
263 }
264
265
266 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
267                                  FL_Coord, FL_Coord,
268                                  int key, void * xev)
269 {
270         XEvent * ev = static_cast<XEvent*>(xev);
271         XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
272
273         if (!area) return 1;
274
275         switch (event) {
276         case FL_DRAW:
277                 if (!area->work_area || !area->work_area->form->visible)
278                         return 1;
279                 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
280                 area->redraw(area->workWidth(), area->workHeight());
281                 break;
282         case FL_PUSH:
283                 if (!ev || ev->xbutton.button == 0) break;
284                 // Should really have used xbutton.state
285                 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
286                 area->dispatch(
287                         FuncRequest(LFUN_MOUSE_PRESS,
288                                     ev->xbutton.x - ob->x,
289                                     ev->xbutton.y - ob->y,
290                                     x_button_state(key)));
291                 break;
292         case FL_RELEASE:
293                 if (!ev || ev->xbutton.button == 0) break;
294                 // Should really have used xbutton.state
295                 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
296                 area->dispatch(
297                         FuncRequest(LFUN_MOUSE_RELEASE,
298                                     ev->xbutton.x - ob->x,
299                                     ev->xbutton.y - ob->y,
300                                     x_button_state(key)));
301                 break;
302         case FL_DRAG:
303         {
304                 if (!ev || !area->scrollbar)
305                         break;
306
307                 int const drag_x = ev->xmotion.x;
308                 int const drag_y = ev->xmotion.y;
309                 int const area_y = ob->y;
310                 int const area_h = ob->h;
311
312                 // Check if the mouse is above or below the workarea
313                 if (drag_y <= area_y || drag_y >= area_y + area_h) {
314                         // The mouse button is depressed and we are outside the
315                         // workarea. That means we are simultaneously selecting
316                         // text and scrolling the view.
317                         // Use a Timeout to react to a drag events only every
318                         // 200ms. All intervening events are discarded,
319                         // allowing the user to control position easily.
320                         static int const discard_interval = 200;
321                         static Timeout timeout(discard_interval);
322
323                         if (timeout.running())
324                                 break;
325                         // The timeout is not running, so process the
326                         // event, first starting the timeout to discard future
327                         // events.
328                         timeout.start();
329                 }
330
331                 static int x_old = -1;
332                 static int y_old = -1;
333                 static double scrollbar_value_old = -1.0;
334
335                 double const scrollbar_value =
336                         fl_get_scrollbar_value(area->scrollbar);
337
338                 if (drag_x != x_old || drag_y != y_old ||
339                     scrollbar_value != scrollbar_value_old) {
340                         x_old = drag_x;
341                         y_old = drag_y;
342                         scrollbar_value_old = scrollbar_value;
343
344                         lyxerr[Debug::WORKAREA] << "Workarea event: DRAG"
345                                                 << endl;
346
347                         // It transpires that ev->xbutton.button == 0 when
348                         // the mouse is dragged, so it cannot be used to
349                         // initialise x_button_state and hence FuncRequest.
350
351                         // The 'key' that is passed into the function does
352                         // contain the necessary info, however.
353
354                         // It is for this reason that x_button_state has
355                         // been modified to work with key
356                         // rather than ev->xbutton.button.
357
358                         // Angus 15 Oct 2002.
359                         FuncRequest cmd(LFUN_MOUSE_MOTION,
360                                         ev->xbutton.x - ob->x,
361                                         ev->xbutton.y - ob->y,
362                                         x_button_state(key));
363                         area->dispatch(cmd);
364                 }
365                 break;
366         }
367
368         case FL_KEYPRESS:
369         {
370                 lyxerr[Debug::WORKAREA] << "Workarea event: KEYPRESS" << endl;
371                 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
372
373                 if (lyxerr.debugging(Debug::KEY)) {
374                         char const * const tmp  = XKeysymToString(key);
375                         lyxerr << "XWorkArea: Key is `" << tmp
376                                << "' [" << key << "]" << endl;
377                 }
378
379                 if (!key) {
380                         lyxerr << "Probably composing" << endl;
381                         break;
382                 }
383
384                 KeySym ret_key = key;
385                 unsigned int const ret_state = xke->state;
386
387                 // If you have a better way to handle "wild-output" of
388                 // characters after the key has been released than the one
389                 // below, please contact me. (Lgb)
390                 static Time last_time_pressed;
391                 static unsigned int last_key_pressed;
392                 static unsigned int last_state_pressed;
393                 lyxerr[Debug::KEY] << "Workarea Diff: "
394                                    << xke->time - last_time_pressed
395                                    << endl;
396                 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
397                     && ret_state == last_state_pressed
398                     && xke->keycode == last_key_pressed) {
399                         lyxerr[Debug::KEY]
400                                 << "Workarea: Purging X events." << endl;
401                         //lyxerr << "Workarea Events: "
402                         //       << XEventsQueued(fl_get_display(), QueuedAlready)
403                         //       << endl;
404                         if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
405                                 waitForX(true);
406                         // This purge make f.ex. scrolling stop immediately when
407                         // releasing the PageDown button. The question is if
408                         // this purging of XEvents can cause any harm...
409                         // after some testing I can see no problems, but
410                         // I'd like other reports too.
411                         break;
412                 }
413                 last_time_pressed = xke->time;
414                 last_key_pressed = xke->keycode;
415                 last_state_pressed = ret_state;
416
417                 XLyXKeySym * xlk = new XLyXKeySym;
418                 xlk->initFromKeySym(ret_key);
419
420                 area->workAreaKeyPress(LyXKeySymPtr(xlk),
421                                        x_key_state(ret_state));
422         }
423         break;
424
425         case FL_KEYRELEASE:
426                 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
427                 break;
428
429         case FL_ENTER:
430                 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
431                 fl_set_cursor(FL_ObjWin(area->work_area), XC_xterm);
432                 break;
433         case FL_LEAVE:
434                 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
435                 // There should be no need for this. But there is.
436                 fl_set_cursor(FL_ObjWin(area->work_area), FL_DEFAULT_CURSOR);
437                 break;
438         case FL_DBLCLICK:
439                 if (ev) {
440                         lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
441                         FuncRequest cmd(LFUN_MOUSE_DOUBLE,
442                                         ev->xbutton.x - ob->x,
443                                         ev->xbutton.y - ob->y,
444                                         x_button_state(key));
445                         area->dispatch(cmd);
446                 }
447                 break;
448         case FL_TRPLCLICK:
449                 if (ev) {
450                         lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
451                         FuncRequest cmd(LFUN_MOUSE_TRIPLE,
452                                         ev->xbutton.x - ob->x,
453                                         ev->xbutton.y - ob->y,
454                                         x_button_state(key));
455                         area->dispatch(cmd);
456                 }
457                 break;
458         case FL_OTHER:
459                 if (ev)
460                         lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
461                 break;
462         }
463
464         return 1;
465 }
466
467
468 namespace {
469
470 string clipboard_selection;
471 bool clipboard_read = false;
472
473 extern "C" {
474
475 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
476                          void const * data, long size)
477 {
478         clipboard_selection.erase();
479
480         if (size > 0)
481                 clipboard_selection.reserve(size);
482         for (int i = 0; i < size; ++i)
483                 clipboard_selection += static_cast<char const *>(data)[i];
484         clipboard_read = true;
485         return 0;
486 }
487
488 } // extern "C"
489 } // namespace anon
490
491
492 int XWorkArea::event_cb(XEvent * xev)
493 {
494         switch (xev->type) {
495         case SelectionRequest:
496                 lyxerr[Debug::GUI] << "X requested selection." << endl;
497                 selectionRequested();
498                 break;
499         case SelectionClear:
500                 lyxerr[Debug::GUI] << "Lost selection." << endl;
501                 selectionLost();
502                 break;
503         }
504         return 0;
505 }
506
507
508 void XWorkArea::haveSelection(bool yes) const
509 {
510         Window const owner = yes ? FL_ObjWin(work_area) : None;
511         XSetSelectionOwner(fl_get_display(), XA_PRIMARY, owner, CurrentTime);
512 }
513
514
515 string const XWorkArea::getClipboard() const
516 {
517         clipboard_read = false;
518
519         if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
520                 return string();
521
522         XEvent ev;
523
524         while (!clipboard_read) {
525                 if (fl_check_forms() == FL_EVENT) {
526                         fl_XNextEvent(&ev);
527                         lyxerr << "Received unhandled X11 event\n"
528                                << "Type: 0x" << hex << ev.xany.type
529                                << " Target: 0x" << hex << ev.xany.window
530                                << dec << endl;
531                 }
532         }
533         return clipboard_selection;
534 }
535
536
537 void XWorkArea::putClipboard(string const & s) const
538 {
539         static string hold;
540         hold = s;
541
542         fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);
543 }