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