]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XWorkArea.C
Tell dispatch which mouse button is being dragged.
[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 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "XWorkArea.h"
19 #include "debug.h"
20 #include "LyXView.h"
21 #include "XLyXKeySym.h"
22 #include "ColorHandler.h"
23 #include "funcrequest.h"
24
25 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
26 #include "lyxlookup.h"
27 #endif
28
29 #include "support/filetools.h" // LibFileSearch
30 #include "support/lstrings.h"
31 #include "support/LAssert.h"
32
33 #include <cmath>
34 #include <cctype>
35
36 // xforms doesn't define this (but it should be in <forms.h>).
37 extern "C"
38 FL_APPEVENT_CB fl_set_preemptive_callback(Window, FL_APPEVENT_CB, void *);
39
40 using std::endl;
41 using std::abs;
42 using std::hex;
43 using std::dec;
44
45 namespace {
46
47 inline
48 void waitForX()
49 {
50         XSync(fl_get_display(), 0);
51 }
52
53
54 void setXtermCursor(Window win)
55 {
56         static Cursor cursor;
57         static bool cursor_undefined = true;
58         if (cursor_undefined) {
59                 cursor = XCreateFontCursor(fl_get_display(), XC_xterm);
60                 XFlush(fl_get_display());
61                 cursor_undefined = false;
62         }
63         XDefineCursor(fl_get_display(), win, cursor);
64         XFlush(fl_get_display());
65 }
66
67
68 mouse_button::state x_button_state(unsigned int button)
69 {
70         mouse_button::state b = mouse_button::none;
71         switch (button) {
72         case FL_MBUTTON1:
73                 b = mouse_button::button1;
74                 break;
75         case FL_MBUTTON2:
76                 b = mouse_button::button2;
77                 break;
78         case FL_MBUTTON3:
79                 b = mouse_button::button3;
80                 break;
81         case FL_MBUTTON4:
82                 b = mouse_button::button4;
83                 break;
84         case FL_MBUTTON5:
85                 b = mouse_button::button5;
86                 break;
87         default: // FIXME
88                 break;
89         }
90         return b;
91 }
92
93
94 key_modifier::state x_key_state(unsigned int state)
95 {
96         key_modifier::state k = key_modifier::none;
97         if (state & ControlMask)
98                 k |= key_modifier::ctrl;
99         if (state & ShiftMask)
100                 k |= key_modifier::shift;
101         if (state & Mod1Mask)
102                 k |= key_modifier::alt;
103         return k;
104 }
105
106
107 } // anon namespace
108
109
110 extern "C" {
111         // Just a bunch of C wrappers around static members of XWorkArea
112         static
113         void C_XWorkArea_scroll_cb(FL_OBJECT * ob, long)
114         {
115                 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
116                 area->scroll_cb();
117         }
118
119
120         static
121         int C_XWorkArea_work_area_handler(FL_OBJECT * ob, int event,
122                                          FL_Coord, FL_Coord,
123                                          int key, void * xev)
124         {
125                 return XWorkArea::work_area_handler(ob, event,
126                                                    0, 0, key, xev);
127         }
128
129         static
130         int C_XWorkAreaEventCB(FL_FORM * form, void * xev) {
131                 XWorkArea * wa = static_cast<XWorkArea*>(form->u_vdata);
132                 return wa->event_cb(static_cast<XEvent*>(xev));
133         }
134 }
135
136
137 XWorkArea::XWorkArea(int x, int y, int w, int h)
138         : workareapixmap(0), painter_(*this)
139 {
140         fl_freeze_all_forms();
141
142         FL_OBJECT * obj;
143
144         if (lyxerr.debugging(Debug::WORKAREA))
145                 lyxerr << "\tbackground box: +"
146                        << x << '+' << y << ' '
147                        << w - 15 << 'x' << h << endl;
148         backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
149                                          x, y,
150                                          w - 15,
151                                          h, "");
152         fl_set_object_resize(obj, FL_RESIZE_ALL);
153         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
154
155         scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
156                                            x + w - 15,
157                                            y, 17, h, "");
158         fl_set_object_boxtype(obj, FL_UP_BOX);
159         fl_set_object_resize(obj, FL_RESIZE_ALL);
160         fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
161         obj->u_vdata = this;
162         fl_set_object_callback(obj, C_XWorkArea_scroll_cb, 0);
163         fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
164         fl_set_scrollbar_value(scrollbar, 0.0);
165         fl_set_scrollbar_size(scrollbar, scrollbar->h);
166
167         int const bw = int(abs(fl_get_border_width()));
168
169         // Create the workarea pixmap
170         // FIXME remove redraw(w - 15 - 2 * bw, h - 2 * bw);
171
172         if (lyxerr.debugging(Debug::WORKAREA))
173                 lyxerr << "\tfree object: +"
174                        << x + bw << '+' << y + bw << ' '
175                        << w - 15 - 2 * bw << 'x'
176                        << h - 2 * bw << endl;
177
178         // We add this object as late as possible to avoid problems
179         // with drawing.
180         // FIXME: like ??
181         work_area = obj = fl_add_free(FL_ALL_FREE,
182                                       x + bw, y + bw,
183                                       w - 15 - 2 * bw,
184                                       h - 2 * bw, "",
185                                       C_XWorkArea_work_area_handler);
186         obj->wantkey = FL_KEY_ALL;
187         obj->u_vdata = this;
188
189         fl_set_object_boxtype(obj,FL_DOWN_BOX);
190         fl_set_object_resize(obj, FL_RESIZE_ALL);
191         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
192
193         /// X selection hook - xforms gets it wrong
194         fl_current_form->u_vdata = this;
195         fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_XWorkAreaEventCB);
196
197         fl_unfreeze_all_forms();
198
199         XGCValues val;
200
201         val.function = GXcopy;
202         copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
203                 GCFunction, &val);
204 }
205
206
207 XWorkArea::~XWorkArea()
208 {
209         XFreeGC(fl_get_display(), copy_gc);
210         if (workareapixmap)
211                 XFreePixmap(fl_get_display(), workareapixmap);
212 }
213
214
215 namespace {
216 void destroy_object(FL_OBJECT * obj)
217 {
218         if (!obj)
219                 return;
220
221         if (obj->visible) {
222                 fl_hide_object(obj);
223         }
224         fl_delete_object(obj);
225         fl_free_object(obj);
226 }
227 } // namespace anon
228
229
230 void XWorkArea::redraw(int width, int height)
231 {
232         static int cur_width = -1;
233         static int cur_height = -1;
234
235         if (cur_width == width && cur_height == height && workareapixmap) {
236                 XCopyArea(fl_get_display(),
237                         getPixmap(), getWin(), copy_gc,
238                         0, 0, width, height, xpos(), ypos());
239                 return;
240         }
241
242         cur_width = width;
243         cur_height = height;
244
245         if (lyxerr.debugging(Debug::WORKAREA)) {
246                 lyxerr << "(Re)creating pixmap ("
247                        << width << 'x' << height << ")" << endl;
248         }
249
250         if (workareapixmap) {
251                 XFreePixmap(fl_get_display(), workareapixmap);
252         }
253
254         workareapixmap = XCreatePixmap(fl_get_display(),
255                                        RootWindow(fl_get_display(), 0),
256                                        width,
257                                        height,
258                                        fl_get_visual_depth());
259
260         workAreaResize();
261 }
262
263
264 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
265 {
266         // we need to cache this for scroll_cb
267         doc_height_ = height;
268
269         if (height == 0) {
270                 fl_set_scrollbar_value(scrollbar, 0.0);
271                 fl_set_scrollbar_size(scrollbar, scrollbar->h);
272                 return;
273         }
274
275         long const work_height = workHeight();
276
277         lyxerr[Debug::GUI] << "scroll: height now " << height << endl;
278         lyxerr[Debug::GUI] << "scroll: work_height " << work_height << endl;
279
280         /* If the text is smaller than the working area, the scrollbar
281          * maximum must be the working area height. No scrolling will
282          * be possible */
283         if (height <= work_height) {
284                 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !" << endl;
285                 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
286                 fl_set_scrollbar_value(scrollbar, pos);
287                 fl_set_scrollbar_size(scrollbar, scrollbar->h);
288                 return;
289         }
290
291         fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
292         fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
293
294         fl_set_scrollbar_value(scrollbar, pos);
295
296         double const slider_size =
297                 (height == 0) ? 1.0 : 1.0 / double(height);
298
299         fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
300 }
301
302
303 // callback for scrollbar slider
304 void XWorkArea::scroll_cb()
305 {
306         double const val = fl_get_scrollbar_value(scrollbar);
307         lyxerr[Debug::GUI] << "scroll: val: " << val << endl;
308         lyxerr[Debug::GUI] << "scroll: height: " << scrollbar->h << endl;
309         lyxerr[Debug::GUI] << "scroll: docheight: " << doc_height_ << endl;
310         scrollDocView(int(val));
311         waitForX();
312 }
313
314
315 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
316                                 FL_Coord, FL_Coord,
317                                 int key, void * xev)
318 {
319         static int x_old = -1;
320         static int y_old = -1;
321         static double scrollbar_value_old = -1.0;
322
323         XEvent * ev = static_cast<XEvent*>(xev);
324         XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
325
326         if (!area) return 1;
327
328         switch (event) {
329         case FL_DRAW:
330                 if (!area->work_area ||
331                     !area->work_area->form->visible)
332                         return 1;
333                 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
334                 area->redraw(area->workWidth(), area->workHeight());
335                 break;
336         case FL_PUSH:
337                 if (!ev || ev->xbutton.button == 0) break;
338                 // Should really have used xbutton.state
339                 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
340                 area->dispatch(
341                         FuncRequest(LFUN_MOUSE_PRESS,
342                                     ev->xbutton.x - ob->x,
343                                     ev->xbutton.y - ob->y,
344                                     x_button_state(key)));
345                 break;
346         case FL_RELEASE:
347                 if (!ev || ev->xbutton.button == 0) break;
348                 // Should really have used xbutton.state
349                 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
350                 area->dispatch(
351                         FuncRequest(LFUN_MOUSE_RELEASE,
352                                     ev->xbutton.x - ob->x,
353                                     ev->xbutton.y - ob->y,
354                                     x_button_state(key)));
355                 break;
356 #if FL_VERSION < 1 && FL_REVISION < 89
357         case FL_MOUSE:
358 #else
359         case FL_DRAG:
360 #endif
361                 if (!ev || !area->scrollbar)
362                         break;
363                 if (ev->xmotion.x != x_old ||
364                     ev->xmotion.y != y_old ||
365                     fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
366                         ) {
367                         x_old = ev->xmotion.x;
368                         y_old = ev->xmotion.y;
369                         scrollbar_value_old = fl_get_scrollbar_value(area->scrollbar);
370                         lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
371                         area->dispatch(
372                                 FuncRequest(LFUN_MOUSE_MOTION,
373                                             ev->xbutton.x - ob->x,
374                                             ev->xbutton.y - ob->y,
375                                             x_button_state(key)));
376                 }
377                 break;
378 #if FL_VERSION < 1 && FL_REVISION < 89
379         case FL_KEYBOARD:
380 #else
381         case FL_KEYPRESS:
382 #endif
383         {
384                 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
385
386                 KeySym keysym = 0;
387                 char dummy[1];
388                 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
389 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
390                 // XForms < 0.89.5 does not have compose support
391                 // so we are using our own compose support
392                 LyXLookupString(ev, dummy, 1, &keysym);
393 #else
394                 XLookupString(xke, dummy, 1, &keysym, 0);
395 //              int num_keys = XLookupString(xke, dummy, 10, &keysym, &xcs);
396 //              lyxerr << "We have " << num_keys << " keys in the returned buffer" << endl;
397 //              lyxerr << "Our dummy string is " << dummy << endl;
398 #endif
399
400                 if (lyxerr.debugging(Debug::KEY)) {
401                         char const * tmp = XKeysymToString(key);
402                         char const * tmp2 = XKeysymToString(keysym);
403                         string const stm = (tmp ? tmp : "");
404                         string const stm2 = (tmp2 ? tmp2 : "");
405
406                         lyxerr[Debug::KEY] << "XWorkArea: Key is `" << stm << "' ["
407                                << key << "]" << endl;
408                         lyxerr[Debug::KEY] << "XWorkArea: Keysym is `" << stm2 << "' ["
409                                << keysym << "]" << endl;
410                 }
411
412 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
413                 if (keysym == NoSymbol) {
414                         lyxerr[Debug::KEY]
415                                 << "Empty kdb action (probably composing)"
416                                 << endl;
417                         break;
418                 }
419                 KeySym ret_key = keysym;
420 #else
421                 // Note that we need this handling because of a bug
422                 // in XForms 0.89, if this bug is resolved in the way I hope
423                 // we can just use the keysym directly with out looking
424                 // at key at all. (Lgb)
425                 KeySym ret_key = 0;
426                 if (!key) {
427                         // We migth have to add more keysyms here also,
428                         // we will do that as the issues arise. (Lgb)
429                         if (keysym == XK_space) {
430                                 ret_key = keysym;
431                                 lyxerr[Debug::KEY] << "Using keysym [A]"
432                                                    << endl;
433                         } else
434                                 break;
435                 } else {
436                         // It seems that this was a bit optimistic...
437                         // With this hacking things seems to be better (Lgb)
438                         //if (!iscntrl(key)) {
439                         //      ret_key = key;
440                         //      lyxerr[Debug::KEY]
441                         //              << "Using key [B]\n"
442                         //              << "Uchar["
443                         //              << static_cast<unsigned char>(key)
444                         //              << endl;
445                         //} else {
446                                 ret_key = (keysym ? keysym : key);
447                                 lyxerr[Debug::KEY] << "Using keysym [B]"
448                                                    << endl;
449                                 //}
450                 }
451
452 #endif
453                 unsigned int const ret_state = xke->state;
454
455                 // If you have a better way to handle "wild-output" of
456                 // characters after the key has been released than the one
457                 // below, please contact me. (Lgb)
458                 static Time last_time_pressed;
459                 static unsigned int last_key_pressed;
460                 static unsigned int last_state_pressed;
461                 lyxerr[Debug::KEY] << "Workarea Diff: "
462                                    << xke->time - last_time_pressed
463                                    << endl;
464                 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
465                     && ret_state == last_state_pressed
466                     && xke->keycode == last_key_pressed) {
467                         lyxerr[Debug::KEY]
468                                 << "Workarea: Purging X events." << endl;
469                         //lyxerr << "Workarea Events: "
470                         //       << XEventsQueued(fl_get_display(), QueuedAlready)
471                         //       << endl;
472                         if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
473                                 XSync(fl_get_display(), 1);
474                         // This purge make f.ex. scrolling stop immidiatly when
475                         // releasing the PageDown button. The question is if
476                         // this purging of XEvents can cause any harm...
477                         // after some testing I can see no problems, but
478                         // I'd like other reports too.
479                         break;
480                 }
481                 last_time_pressed = xke->time;
482                 last_key_pressed = xke->keycode;
483                 last_state_pressed = ret_state;
484
485                 XLyXKeySym * xlk = new XLyXKeySym;
486                 xlk->initFromKeySym(ret_key);
487
488                 area->workAreaKeyPress(LyXKeySymPtr(xlk),
489                                        x_key_state(ret_state));
490         }
491         break;
492
493 #if FL_VERSION > 0 || FL_REVISION >= 89
494         case FL_KEYRELEASE:
495                 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
496                 break;
497 #endif
498
499         case FL_ENTER:
500                 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
501                 break;
502         case FL_LEAVE:
503                 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
504                 break;
505         case FL_DBLCLICK:
506                 if (ev) {
507                         lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
508                         FuncRequest cmd(LFUN_MOUSE_DOUBLE,
509                                         ev->xbutton.x - ob->x,
510                                         ev->xbutton.y - ob->y,
511                                         x_button_state(key));
512                         area->dispatch(cmd);
513                 }
514                 break;
515         case FL_TRPLCLICK:
516                 if (ev) {
517                         lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
518                         FuncRequest cmd(LFUN_MOUSE_TRIPLE,
519                                         ev->xbutton.x - ob->x,
520                                         ev->xbutton.y - ob->y,
521                                         x_button_state(key));
522                         area->dispatch(cmd);
523                 }
524                 break;
525         case FL_OTHER:
526                 if (ev)
527                         lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
528                 break;
529         }
530
531         return 1;
532 }
533
534
535 namespace {
536
537 string clipboard_selection;
538 bool clipboard_read = false;
539
540 extern "C" {
541
542         static
543         int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
544                                  void const * data, long size)
545         {
546                 clipboard_selection.erase();
547
548                 if (size > 0)
549                         clipboard_selection.reserve(size);
550                 for (int i = 0; i < size; ++i)
551                         clipboard_selection +=
552                                 static_cast<char const *>(data)[i];
553                 clipboard_read = true;
554                 return 0;
555         }
556
557 }
558
559 } // namespace anon
560
561
562 int XWorkArea::event_cb(XEvent * xev)
563 {
564         int ret = 0;
565         switch (xev->type) {
566                 case SelectionRequest:
567                         lyxerr[Debug::GUI] << "X requested selection." << endl;
568                         selectionRequested();
569                         break;
570                 case SelectionClear:
571                         lyxerr[Debug::GUI] << "Lost selection." << endl;
572                         selectionLost();
573                         break;
574         }
575         return ret;
576 }
577
578
579 void XWorkArea::haveSelection(bool yes) const
580 {
581         if (!yes) {
582                 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
583                 return;
584         }
585
586         XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
587 }
588
589
590 string const XWorkArea::getClipboard() const
591 {
592         clipboard_read = false;
593
594         if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
595                 return string();
596
597         XEvent ev;
598
599         while (!clipboard_read) {
600                 if (fl_check_forms() == FL_EVENT) {
601                         fl_XNextEvent(&ev);
602                         lyxerr << "Received unhandled X11 event" << endl;
603                         lyxerr << "Type: 0x" << hex << ev.xany.type <<
604                                 " Target: 0x" << hex << ev.xany.window << dec << endl;
605                 }
606         }
607         return clipboard_selection;
608 }
609
610
611 void XWorkArea::putClipboard(string const & s) const
612 {
613         static string hold;
614         hold = s;
615
616         fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);
617 }