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