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