]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XWorkArea.C
move splash into screen greyOut(). Still not enabled (needs grfx work)
[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                 break;
339         case FL_PUSH:
340                 if (!ev || ev->xbutton.button == 0) break;
341                 // Should really have used xbutton.state
342                 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
343                 area->workAreaButtonPress(ev->xbutton.x - ob->x,
344                                           ev->xbutton.y - ob->y,
345                                           x_button_state(ev->xbutton.button));
346                 break;
347         case FL_RELEASE:
348                 if (!ev || ev->xbutton.button == 0) break;
349                 // Should really have used xbutton.state
350                 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
351                 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
352                                       ev->xbutton.y - ob->y,
353                                       x_button_state(ev->xbutton.button));
354                 break;
355 #if FL_VERSION < 1 && FL_REVISION < 89
356         case FL_MOUSE:
357 #else
358         case FL_DRAG:
359 #endif
360                 if (!ev || ! area->scrollbar) break;
361                 if (ev->xmotion.x != x_old ||
362                     ev->xmotion.y != y_old ||
363                     fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
364                         ) {
365                         lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
366                         area->workAreaMotionNotify(ev->xmotion.x - ob->x,
367                                              ev->xmotion.y - ob->y,
368                                              x_motion_state(ev->xbutton.state));
369                 }
370                 break;
371 #if FL_VERSION < 1 && FL_REVISION < 89
372         case FL_KEYBOARD:
373 #else
374         case FL_KEYPRESS:
375 #endif
376         {
377                 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
378
379                 KeySym keysym = 0;
380                 char dummy[1];
381                 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
382 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
383                 // XForms < 0.89.5 does not have compose support
384                 // so we are using our own compose support
385                 LyXLookupString(ev, dummy, 1, &keysym);
386 #else
387                 XLookupString(xke, dummy, 1, &keysym, 0);
388 //              int num_keys = XLookupString(xke, dummy, 10, &keysym, &xcs);
389 //              lyxerr << "We have " << num_keys << " keys in the returned buffer" << endl;
390 //              lyxerr << "Our dummy string is " << dummy << endl;
391 #endif
392                 
393                 if (lyxerr.debugging(Debug::KEY)) {
394                         char const * tmp = XKeysymToString(key);
395                         char const * tmp2 = XKeysymToString(keysym);
396                         string const stm = (tmp ? tmp : "");
397                         string const stm2 = (tmp2 ? tmp2 : "");
398
399                         lyxerr[Debug::KEY] << "XWorkArea: Key is `" << stm << "' ["
400                                << key << "]" << endl;
401                         lyxerr[Debug::KEY] << "XWorkArea: Keysym is `" << stm2 << "' ["
402                                << keysym << "]" << endl;
403                 }
404
405 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
406                 if (keysym == NoSymbol) {
407                         lyxerr[Debug::KEY]
408                                 << "Empty kdb action (probably composing)"
409                                 << endl;
410                         break;
411                 }
412                 KeySym ret_key = keysym;
413 #else
414                 // Note that we need this handling because of a bug
415                 // in XForms 0.89, if this bug is resolved in the way I hope
416                 // we can just use the keysym directly with out looking
417                 // at key at all. (Lgb)
418                 KeySym ret_key = 0;
419                 if (!key) {
420                         // We migth have to add more keysyms here also,
421                         // we will do that as the issues arise. (Lgb)
422                         if (keysym == XK_space) {
423                                 ret_key = keysym;
424                                 lyxerr[Debug::KEY] << "Using keysym [A]"
425                                                    << endl;
426                         } else
427                                 break;
428                 } else {
429                         // It seems that this was a bit optimistic...
430                         // With this hacking things seems to be better (Lgb)
431                         //if (!iscntrl(key)) {
432                         //      ret_key = key;
433                         //      lyxerr[Debug::KEY]
434                         //              << "Using key [B]\n"
435                         //              << "Uchar["
436                         //              << static_cast<unsigned char>(key)
437                         //              << endl;
438                         //} else {
439                                 ret_key = (keysym ? keysym : key);
440                                 lyxerr[Debug::KEY] << "Using keysym [B]"
441                                                    << endl;
442                                 //}
443                 }
444
445 #endif
446                 unsigned int const ret_state = xke->state;
447
448                 // If you have a better way to handle "wild-output" of
449                 // characters after the key has been released than the one
450                 // below, please contact me. (Lgb)
451                 static Time last_time_pressed;
452                 static unsigned int last_key_pressed;
453                 static unsigned int last_state_pressed;
454                 lyxerr[Debug::KEY] << "Workarea Diff: "
455                                    << xke->time - last_time_pressed
456                                    << endl;
457                 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
458                     && ret_state == last_state_pressed
459                     && xke->keycode == last_key_pressed) {
460                         lyxerr[Debug::KEY]
461                                 << "Workarea: Purging X events." << endl;
462                         //lyxerr << "Workarea Events: "
463                         //       << XEventsQueued(fl_get_display(), QueuedAlready)
464                         //       << endl;
465                         if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
466                                 XSync(fl_get_display(), 1);
467                         // This purge make f.ex. scrolling stop immidiatly when
468                         // releasing the PageDown button. The question is if
469                         // this purging of XEvents can cause any harm...
470                         // after some testing I can see no problems, but
471                         // I'd like other reports too.
472                         break;
473                 }
474                 last_time_pressed = xke->time;
475                 last_key_pressed = xke->keycode;
476                 last_state_pressed = ret_state;
477
478                 XLyXKeySym * xlk = new XLyXKeySym;
479                 xlk->initFromKeySym(ret_key);
480                 
481                 area->workAreaKeyPress(LyXKeySymPtr(xlk),
482                                        x_key_state(ret_state));
483         }
484         break;
485
486 #if FL_VERSION > 0 || FL_REVISION >= 89
487         case FL_KEYRELEASE:
488                 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
489                 break;
490 #endif
491
492         case FL_ENTER:
493                 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
494                 break;
495         case FL_LEAVE:
496                 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
497                 break;
498         case FL_DBLCLICK:
499                 if (!ev) break;
500                 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
501                 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
502                                           ev->xbutton.y - ob->y,
503                                           x_button_state(ev->xbutton.button));
504                 break;
505         case FL_TRPLCLICK:
506                 if (!ev) break;
507                 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
508                 area->workAreaTripleClick(ev->xbutton.x - ob->x,
509                                           ev->xbutton.y - ob->y,
510                                           x_button_state(ev->xbutton.button));
511                 break;
512         case FL_OTHER:
513                 if (!ev) break;
514                 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
515                 break;
516         }
517
518         return 1;
519 }
520
521
522 namespace {
523
524 string clipboard_selection;
525 bool clipboard_read = false;
526
527 extern "C" {
528
529         static
530         int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
531                                  void const * data, long size)
532         {
533                 clipboard_selection.erase();
534
535                 if (size > 0)
536                         clipboard_selection.reserve(size);
537                 for (int i = 0; i < size; ++i)
538                         clipboard_selection +=
539                                 static_cast<char const *>(data)[i];
540                 clipboard_read = true;
541                 return 0;
542         }
543
544 }
545
546 } // namespace anon
547
548
549 int XWorkArea::event_cb(XEvent * xev)
550 {
551         int ret = 0;
552         switch (xev->type) {
553                 case SelectionRequest:
554                         lyxerr[Debug::GUI] << "X requested selection." << endl;
555                         selectionRequested();
556                         break;
557                 case SelectionClear:
558                         lyxerr[Debug::GUI] << "Lost selection." << endl;
559                         selectionLost();
560                         break;
561         }
562         return ret;
563 }
564
565
566 void XWorkArea::haveSelection(bool yes) const
567 {
568         if (!yes) {
569                 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
570                 return;
571         }
572
573         XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
574 }
575
576
577 string const XWorkArea::getClipboard() const
578 {
579         clipboard_read = false;
580
581         if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
582                 return string();
583
584         XEvent ev;
585
586         while (!clipboard_read) {
587                 if (fl_check_forms() == FL_EVENT) {
588                         fl_XNextEvent(&ev);
589                         lyxerr << "Received unhandled X11 event" << endl;
590                         lyxerr << "Type: 0x" << hex << ev.xany.type <<
591                                 " Target: 0x" << hex << ev.xany.window << endl;
592                 }
593         }
594         return clipboard_selection;
595 }
596
597
598 void XWorkArea::putClipboard(string const & s) const
599 {
600         static string hold;
601         hold = s;
602
603         fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);
604 }