]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XWorkArea.C
Clean up of XWorkArea.C, no change in functionality.
[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_x = ob->x;
321                 int const area_y = ob->y;
322                 int const area_w = ob->w;
323                 int const area_h = ob->h;
324
325                 // Check if the mouse is above or below the workarea
326                 if (drag_y <= area_y || drag_y >= area_y + area_h) {
327                         // The mouse button is depressed and we are outside the
328                         // workarea. That means we are simultaneously selecting
329                         // text and scrolling the view.
330                         // Use a Timeout to react to a drag events only every
331                         // 200ms. All intervening events are discarded,
332                         // allowing the user to control position easily.
333                         static int const discard_interval = 200;
334                         static Timeout timeout(discard_interval);
335
336                         if (timeout.running())
337                                 break;
338                         // The timeout is not running, so process the
339                         // event, first starting the timeout to discard future
340                         // events.
341                         timeout.start();
342                 }
343
344                 static int x_old = -1;
345                 static int y_old = -1;
346                 static double scrollbar_value_old = -1.0;
347
348                 double const scrollbar_value =
349                         fl_get_scrollbar_value(area->scrollbar);
350
351                 if (drag_x != x_old || drag_y != y_old ||
352                     scrollbar_value != scrollbar_value_old) {
353                         x_old = drag_x;
354                         y_old = drag_y;
355                         scrollbar_value_old = scrollbar_value;
356
357                         lyxerr[Debug::WORKAREA] << "Workarea event: DRAG"
358                                                 << endl;
359
360                         // It transpires that ev->xbutton.button == 0 when
361                         // the mouse is dragged, so it cannot be used to
362                         // initialise x_button_state and hence FuncRequest.
363
364                         // The 'key' that is passed into the function does
365                         // contain the necessary info, however.
366
367                         // It is for this reason that x_button_state has
368                         // been modified to work with key
369                         // rather than ev->xbutton.button.
370
371                         // Angus 15 Oct 2002.
372                         FuncRequest cmd(LFUN_MOUSE_MOTION,
373                                         ev->xbutton.x - ob->x,
374                                         ev->xbutton.y - ob->y,
375                                         x_button_state(key));
376                         area->dispatch(cmd);
377                 }
378                 break;
379         }
380
381 #if FL_VERSION < 1 && FL_REVISION < 89
382         case FL_KEYBOARD:
383 #else
384         case FL_KEYPRESS:
385 #endif
386         {
387                 lyxerr[Debug::WORKAREA] << "Workarea event: KEYPRESS" << endl;
388
389                 KeySym keysym = 0;
390                 char dummy[1];
391                 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
392 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
393                 // XForms < 0.89.5 does not have compose support
394                 // so we are using our own compose support
395                 LyXLookupString(ev, dummy, 1, &keysym);
396 #else
397                 XLookupString(xke, dummy, 1, &keysym, 0);
398 #endif
399
400                 if (lyxerr.debugging(Debug::KEY)) {
401                         char const * const tmp  = XKeysymToString(key);
402                         char const * const tmp2 = XKeysymToString(keysym);
403                         string const stm  = (tmp ? tmp : string());
404                         string const stm2 = (tmp2 ? tmp2 : string());
405
406                         lyxerr << "XWorkArea: Key is `" << stm
407                                << "' [" << key << "]\n"
408                                << "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 without looking
424                 // at key at all. (Lgb)
425                 KeySym ret_key = 0;
426                 if (!key) {
427                         // We might 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                                 waitForX(true);
474                         // This purge make f.ex. scrolling stop immediately 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 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
543                          void const * data, long size)
544 {
545         clipboard_selection.erase();
546
547         if (size > 0)
548                 clipboard_selection.reserve(size);
549         for (int i = 0; i < size; ++i)
550                 clipboard_selection += static_cast<char const *>(data)[i];
551         clipboard_read = true;
552         return 0;
553 }
554
555 } // extern "C"
556 } // namespace anon
557
558
559 int XWorkArea::event_cb(XEvent * xev)
560 {
561         switch (xev->type) {
562         case SelectionRequest:
563                 lyxerr[Debug::GUI] << "X requested selection." << endl;
564                 selectionRequested();
565                 break;
566         case SelectionClear:
567                 lyxerr[Debug::GUI] << "Lost selection." << endl;
568                 selectionLost();
569                 break;
570         }
571         return 0;
572 }
573
574
575 void XWorkArea::haveSelection(bool yes) const
576 {
577         Window const owner = yes ? FL_ObjWin(work_area) : None;
578         XSetSelectionOwner(fl_get_display(), XA_PRIMARY, owner, CurrentTime);
579 }
580
581
582 string const XWorkArea::getClipboard() const
583 {
584         clipboard_read = false;
585
586         if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
587                 return string();
588
589         XEvent ev;
590
591         while (!clipboard_read) {
592                 if (fl_check_forms() == FL_EVENT) {
593                         fl_XNextEvent(&ev);
594                         lyxerr << "Received unhandled X11 event\n"
595                                << "Type: 0x" << hex << ev.xany.type
596                                << " Target: 0x" << hex << ev.xany.window
597                                << dec << endl;
598                 }
599         }
600         return clipboard_selection;
601 }
602
603
604 void XWorkArea::putClipboard(string const & s) const
605 {
606         static string hold;
607         hold = s;
608
609         fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);
610 }