]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XWorkArea.C
ditch FileInfo -> use boost.filesystem
[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 boost::shared_ptr;
28
29 using std::abs;
30 using std::dec;
31 using std::endl;
32 using std::hex;
33 using std::string;
34
35 namespace lyx {
36 namespace frontend {
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         shared_ptr<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         shared_ptr<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         copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
179                             GCFunction, &val);
180 }
181
182
183 XWorkArea::~XWorkArea()
184 {
185         XFreeGC(fl_get_display(), copy_gc);
186         if (workareapixmap)
187                 XFreePixmap(fl_get_display(), workareapixmap);
188 }
189
190
191 void XWorkArea::redraw(int width, int height)
192 {
193         static int cur_width = -1;
194         static int cur_height = -1;
195
196         if (cur_width == width && cur_height == height && workareapixmap) {
197                 XCopyArea(fl_get_display(),
198                           getPixmap(), getWin(), copy_gc,
199                           0, 0, width, height, xpos(), ypos());
200                 return;
201         }
202
203         cur_width = width;
204         cur_height = height;
205
206         if (lyxerr.debugging(Debug::WORKAREA)) {
207                 lyxerr << "(Re)creating pixmap ("
208                        << width << 'x' << height << ')' << endl;
209         }
210
211         if (workareapixmap) {
212                 XFreePixmap(fl_get_display(), workareapixmap);
213         }
214
215         workareapixmap = XCreatePixmap(fl_get_display(),
216                                        RootWindow(fl_get_display(), 0),
217                                        width,
218                                        height,
219                                        fl_get_visual_depth());
220
221         workAreaResize();
222 }
223
224
225 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
226 {
227         // we need to cache this for scroll_cb
228         doc_height_ = height;
229
230         if (height == 0) {
231                 fl_set_scrollbar_value(scrollbar, 0.0);
232                 fl_set_scrollbar_size(scrollbar, scrollbar->h);
233                 return;
234         }
235
236         long const work_height = workHeight();
237
238         if (lyxerr.debugging(Debug::GUI)) {
239                 lyxerr << "scroll: height now " << height << '\n'
240                        << "scroll: work_height " << work_height << endl;
241         }
242
243         /* If the text is smaller than the working area, the scrollbar
244          * maximum must be the working area height. No scrolling will
245          * be possible */
246         if (height <= work_height) {
247                 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !"
248                                    << endl;
249                 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
250                 fl_set_scrollbar_value(scrollbar, pos);
251                 fl_set_scrollbar_size(scrollbar, scrollbar->h);
252                 return;
253         }
254
255         fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
256         fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
257
258         fl_set_scrollbar_value(scrollbar, pos);
259
260         double const slider_size =
261                 (height == 0) ? 1.0 : 1.0 / double(height);
262
263         fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
264 }
265
266
267 // callback for scrollbar slider
268 void XWorkArea::scroll_cb()
269 {
270         double const val = fl_get_scrollbar_value(scrollbar);
271
272         if (lyxerr.debugging(Debug::GUI)) {
273                 lyxerr << "scroll: val: " << val << '\n'
274                        << "scroll: height: " << scrollbar->h << '\n'
275                        << "scroll: docheight: " << doc_height_ << endl;
276         }
277
278         scrollDocView(int(val));
279         waitForX(false);
280 }
281
282
283 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
284                                  FL_Coord, FL_Coord,
285                                  int key, void * xev)
286 {
287         if (event != 10 && event != 11)
288                 lyxerr[Debug::WORKAREA] << "Workarea event: EVENT: " << event << endl;
289
290         XEvent * ev = static_cast<XEvent*>(xev);
291         XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
292
293         if (!area)
294                 return 1;
295
296         switch (event) {
297
298         case FL_DRAW:
299                 if (!area->work_area || !area->work_area->form->visible)
300                         return 1;
301                 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
302                 area->redraw(area->workWidth(), area->workHeight());
303                 break;
304
305         case FL_PUSH:
306                 if (!ev || ev->xbutton.button == 0) break;
307
308                 if (ev->xbutton.button == 4 || ev->xbutton.button == 5) {
309                         static long last_wheel;
310
311                         long cur_wheel = ev->xbutton.time;
312                         if (last_wheel == cur_wheel)
313                                 break;
314
315                         last_wheel = cur_wheel;
316
317                         float l, r;
318                         fl_get_scrollbar_increment(area->scrollbar, &l, &r);
319
320                         if (ev->xbutton.button == 4)
321                                 l *= -1.0;
322
323                         fl_set_scrollbar_value(
324                                 area->scrollbar,
325                                 fl_get_scrollbar_value(area->scrollbar) + l);
326
327                         area->scroll_cb();
328                         break;
329                 }
330
331                 // Should really have used xbutton.state
332                 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
333                 area->dispatch(
334                         FuncRequest(LFUN_MOUSE_PRESS,
335                                     ev->xbutton.x - ob->x,
336                                     ev->xbutton.y - ob->y,
337                                     x_button_state(key)));
338                 break;
339
340         case FL_RELEASE:
341                 if (!ev || ev->xbutton.button == 0) break;
342                 // Should really have used xbutton.state
343
344                 if (ev->xbutton.button == 4 || ev->xbutton.button == 5) {
345                         // We ingnore wheel event here
346                         break;
347                 }
348
349                 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
350
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                 lyxerr[Debug::WORKAREA] << "Workarea event: DRAG 0" << endl;
360
361                 if (!ev || !area->scrollbar)
362                         break;
363
364                 int const drag_x = ev->xmotion.x;
365                 int const drag_y = ev->xmotion.y;
366                 int const area_y = ob->y;
367                 int const area_h = ob->h;
368
369                 // Check if the mouse is above or below the workarea
370                 if (drag_y <= area_y || drag_y >= area_y + area_h) {
371                         // The mouse button is depressed and we are outside the
372                         // workarea. That means we are simultaneously selecting
373                         // text and scrolling the view.
374                         // Use a Timeout to react to a drag events only every
375                         // 200ms. All intervening events are discarded,
376                         // allowing the user to control position easily.
377                         static int const discard_interval = 200;
378                         static Timeout timeout(discard_interval);
379
380                         if (timeout.running())
381                                 break;
382                         // The timeout is not running, so process the
383                         // event, first starting the timeout to discard future
384                         // events.
385                         timeout.start();
386                 }
387
388                 static int x_old = -1;
389                 static int y_old = -1;
390                 static double scrollbar_value_old = -1.0;
391
392                 double const scrollbar_value =
393                         fl_get_scrollbar_value(area->scrollbar);
394
395                 if (drag_x != x_old || drag_y != y_old ||
396                     scrollbar_value != scrollbar_value_old) {
397                         x_old = drag_x;
398                         y_old = drag_y;
399                         scrollbar_value_old = scrollbar_value;
400
401                         lyxerr[Debug::WORKAREA] << "Workarea event: DRAG"
402                                                 << endl;
403
404                         // It transpires that ev->xbutton.button == 0 when
405                         // the mouse is dragged, so it cannot be used to
406                         // initialise x_button_state and hence FuncRequest.
407
408                         // The 'key' that is passed into the function does
409                         // contain the necessary info, however.
410
411                         // It is for this reason that x_button_state has
412                         // been modified to work with key
413                         // rather than ev->xbutton.button.
414
415                         // Angus 15 Oct 2002.
416                         FuncRequest cmd(LFUN_MOUSE_MOTION,
417                                         ev->xbutton.x - ob->x,
418                                         ev->xbutton.y - ob->y,
419                                         x_button_state(key));
420                         area->dispatch(cmd);
421                 }
422                 break;
423         }
424
425         case FL_KEYPRESS: {
426                 lyxerr[Debug::WORKAREA] << "Workarea event: KEYPRESS" << endl;
427
428                 KeySym keysym = 0;
429                 char dummy[1];
430                 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
431                 XLookupString(xke, dummy, 1, &keysym, 0);
432
433                 if (lyxerr.debugging(Debug::KEY)) {
434                         char const * const tmp  = XKeysymToString(key);
435                         char const * const tmp2 = XKeysymToString(keysym);
436                         string const stm  = (tmp ? tmp : string());
437                         string const stm2 = (tmp2 ? tmp2 : string());
438
439                         lyxerr << "XWorkArea: Key is `" << stm
440                                << "' [" << key << "]\n"
441                                << "XWorkArea: Keysym is `" << stm2
442                                << "' [" << keysym << ']' << endl;
443                 }
444
445                 // Note that we need this handling because of a bug
446                 // in XForms 0.89, if this bug is resolved in the way I hope
447                 // we can just use the keysym directly without looking
448                 // at key at all. (Lgb)
449                 KeySym ret_key = 0;
450                 if (!key) {
451                         // We might have to add more keysyms here also,
452                         // we will do that as the issues arise. (Lgb)
453                         if (keysym == XK_space) {
454                                 ret_key = keysym;
455                                 lyxerr[Debug::KEY] << "Using keysym [A]"
456                                                    << endl;
457                         } else
458                                 break;
459                 } else {
460                         // It seems that this was a bit optimistic...
461                         // With this hacking things seems to be better (Lgb)
462                         //if (!iscntrl(key)) {
463                         //      ret_key = key;
464                         //      lyxerr[Debug::KEY]
465                         //              << "Using key [B]\n"
466                         //              << "Uchar["
467                         //              << static_cast<unsigned char>(key)
468                         //              << endl;
469                         //} else {
470                         ret_key = (keysym ? keysym : key);
471                         lyxerr[Debug::KEY] << "Using keysym [B]"
472                                            << endl;
473                                 //}
474                 }
475
476                 unsigned int const ret_state = xke->state;
477
478                 // If you have a better way to handle "wild-output" of
479                 // characters after the key has been released than the one
480                 // below, please contact me. (Lgb)
481                 static Time last_time_pressed;
482                 static unsigned int last_key_pressed;
483                 static unsigned int last_state_pressed;
484                 lyxerr[Debug::KEY] << "Workarea Diff: "
485                                    << xke->time - last_time_pressed
486                                    << endl;
487                 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
488                     && ret_state == last_state_pressed
489                     && xke->keycode == last_key_pressed) {
490                         lyxerr[Debug::KEY]
491                                 << "Workarea: Purging X events." << endl;
492                         //lyxerr << "Workarea Events: "
493                         //       << XEventsQueued(fl_get_display(), QueuedAlready)
494                         //       << endl;
495                         if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
496                                 waitForX(true);
497                         // This purge make f.ex. scrolling stop immediately when
498                         // releasing the PageDown button. The question is if
499                         // this purging of XEvents can cause any harm...
500                         // after some testing I can see no problems, but
501                         // I'd like other reports too.
502                         break;
503                 }
504                 last_time_pressed = xke->time;
505                 last_key_pressed = xke->keycode;
506                 last_state_pressed = ret_state;
507
508                 XLyXKeySym * xlk = new XLyXKeySym;
509                 xlk->initFromKeySym(ret_key);
510
511                 area->workAreaKeyPress(LyXKeySymPtr(xlk),
512                                        x_key_state(ret_state));
513                 break;
514         }
515
516         case FL_KEYRELEASE:
517                 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
518                 break;
519
520         case FL_ENTER:
521                 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
522                 fl_set_cursor(FL_ObjWin(area->work_area), XC_xterm);
523                 break;
524
525         case FL_LEAVE:
526                 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
527                 // There should be no need for this. But there is.
528                 fl_set_cursor(FL_ObjWin(area->work_area), FL_DEFAULT_CURSOR);
529                 break;
530
531         case FL_DBLCLICK:
532                 if (ev) {
533                         if (ev->xbutton.button == 4 || ev->xbutton.button == 5) {
534                                 // Ignore wheel events
535                                 break;
536                         }
537
538
539                         lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
540                         FuncRequest cmd(LFUN_MOUSE_DOUBLE,
541                                         ev->xbutton.x - ob->x,
542                                         ev->xbutton.y - ob->y,
543                                         x_button_state(key));
544                         area->dispatch(cmd);
545                 }
546                 break;
547
548         case FL_TRPLCLICK:
549                 if (ev) {
550                         if (ev->xbutton.button == 4 || ev->xbutton.button == 5) {
551                                 // Ignore wheel events
552                                 break;
553                         }
554
555                         lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
556                         FuncRequest cmd(LFUN_MOUSE_TRIPLE,
557                                         ev->xbutton.x - ob->x,
558                                         ev->xbutton.y - ob->y,
559                                         x_button_state(key));
560                         area->dispatch(cmd);
561                 }
562                 break;
563
564         case FL_OTHER:
565                 if (ev)
566                         lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
567                 break;
568         }
569
570         return 1;
571 }
572
573
574 namespace {
575
576 string clipboard_selection;
577 bool clipboard_read = false;
578
579 extern "C" {
580
581 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
582                          void const * data, long size)
583 {
584         clipboard_selection.erase();
585
586         if (size > 0)
587                 clipboard_selection.reserve(size);
588         for (int i = 0; i < size; ++i)
589                 clipboard_selection += static_cast<char const *>(data)[i];
590         clipboard_read = true;
591         return 0;
592 }
593
594 } // extern "C"
595 } // namespace anon
596
597
598 int XWorkArea::event_cb(XEvent * xev)
599 {
600         switch (xev->type) {
601         case SelectionRequest:
602                 lyxerr[Debug::GUI] << "X requested selection." << endl;
603                 selectionRequested();
604                 break;
605         case SelectionClear:
606                 lyxerr[Debug::GUI] << "Lost selection." << endl;
607                 selectionLost();
608                 break;
609         }
610         return 0;
611 }
612
613
614 void XWorkArea::haveSelection(bool yes) const
615 {
616         Window const owner = yes ? FL_ObjWin(work_area) : None;
617         XSetSelectionOwner(fl_get_display(), XA_PRIMARY, owner, CurrentTime);
618 }
619
620
621 string const XWorkArea::getClipboard() const
622 {
623         clipboard_read = false;
624
625         if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
626                 return string();
627
628         XEvent ev;
629
630         while (!clipboard_read) {
631                 if (fl_check_forms() == FL_EVENT) {
632                         fl_XNextEvent(&ev);
633                         lyxerr << "Received unhandled X11 event\n"
634                                << "Type: 0x" << hex << ev.xany.type
635                                << " Target: 0x" << hex << ev.xany.window
636                                << dec << endl;
637                 }
638         }
639         return clipboard_selection;
640 }
641
642
643 void XWorkArea::putClipboard(string const & s) const
644 {
645         static string hold;
646         hold = s;
647
648         fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);
649 }
650
651 } // namespace frontend
652 } // namespace lyx