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