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