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