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