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