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