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