]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XWorkArea.C
Removed all redundant using directives from the source.
[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
26
27 namespace {
28
29 inline
30 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) return 1;
275
276         switch (event) {
277         case FL_DRAW:
278                 if (!area->work_area || !area->work_area->form->visible)
279                         return 1;
280                 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
281                 area->redraw(area->workWidth(), area->workHeight());
282                 break;
283         case FL_PUSH:
284                 if (!ev || ev->xbutton.button == 0) break;
285                 // Should really have used xbutton.state
286                 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
287                 area->dispatch(
288                         FuncRequest(LFUN_MOUSE_PRESS,
289                                     ev->xbutton.x - ob->x,
290                                     ev->xbutton.y - ob->y,
291                                     x_button_state(key)));
292                 break;
293         case FL_RELEASE:
294                 if (!ev || ev->xbutton.button == 0) break;
295                 // Should really have used xbutton.state
296                 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
297                 area->dispatch(
298                         FuncRequest(LFUN_MOUSE_RELEASE,
299                                     ev->xbutton.x - ob->x,
300                                     ev->xbutton.y - ob->y,
301                                     x_button_state(key)));
302                 break;
303         case FL_DRAG:
304         {
305                 if (!ev || !area->scrollbar)
306                         break;
307
308                 int const drag_x = ev->xmotion.x;
309                 int const drag_y = ev->xmotion.y;
310                 int const area_y = ob->y;
311                 int const area_h = ob->h;
312
313                 // Check if the mouse is above or below the workarea
314                 if (drag_y <= area_y || drag_y >= area_y + area_h) {
315                         // The mouse button is depressed and we are outside the
316                         // workarea. That means we are simultaneously selecting
317                         // text and scrolling the view.
318                         // Use a Timeout to react to a drag events only every
319                         // 200ms. All intervening events are discarded,
320                         // allowing the user to control position easily.
321                         static int const discard_interval = 200;
322                         static Timeout timeout(discard_interval);
323
324                         if (timeout.running())
325                                 break;
326                         // The timeout is not running, so process the
327                         // event, first starting the timeout to discard future
328                         // events.
329                         timeout.start();
330                 }
331
332                 static int x_old = -1;
333                 static int y_old = -1;
334                 static double scrollbar_value_old = -1.0;
335
336                 double const scrollbar_value =
337                         fl_get_scrollbar_value(area->scrollbar);
338
339                 if (drag_x != x_old || drag_y != y_old ||
340                     scrollbar_value != scrollbar_value_old) {
341                         x_old = drag_x;
342                         y_old = drag_y;
343                         scrollbar_value_old = scrollbar_value;
344
345                         lyxerr[Debug::WORKAREA] << "Workarea event: DRAG"
346                                                 << endl;
347
348                         // It transpires that ev->xbutton.button == 0 when
349                         // the mouse is dragged, so it cannot be used to
350                         // initialise x_button_state and hence FuncRequest.
351
352                         // The 'key' that is passed into the function does
353                         // contain the necessary info, however.
354
355                         // It is for this reason that x_button_state has
356                         // been modified to work with key
357                         // rather than ev->xbutton.button.
358
359                         // Angus 15 Oct 2002.
360                         FuncRequest cmd(LFUN_MOUSE_MOTION,
361                                         ev->xbutton.x - ob->x,
362                                         ev->xbutton.y - ob->y,
363                                         x_button_state(key));
364                         area->dispatch(cmd);
365                 }
366                 break;
367         }
368
369         case FL_KEYPRESS:
370         {
371                 lyxerr[Debug::WORKAREA] << "Workarea event: KEYPRESS" << endl;
372
373                 KeySym keysym = 0;
374                 char dummy[1];
375                 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
376                 XLookupString(xke, dummy, 1, &keysym, 0);
377
378                 if (lyxerr.debugging(Debug::KEY)) {
379                         char const * const tmp  = XKeysymToString(key);
380                         char const * const tmp2 = XKeysymToString(keysym);
381                         string const stm  = (tmp ? tmp : string());
382                         string const stm2 = (tmp2 ? tmp2 : string());
383
384                         lyxerr << "XWorkArea: Key is `" << stm
385                                << "' [" << key << "]\n"
386                                << "XWorkArea: Keysym is `" << stm2
387                                << "' [" << keysym << ']' << endl;
388                 }
389
390                 // Note that we need this handling because of a bug
391                 // in XForms 0.89, if this bug is resolved in the way I hope
392                 // we can just use the keysym directly without looking
393                 // at key at all. (Lgb)
394                 KeySym ret_key = 0;
395                 if (!key) {
396                         // We might have to add more keysyms here also,
397                         // we will do that as the issues arise. (Lgb)
398                         if (keysym == XK_space) {
399                                 ret_key = keysym;
400                                 lyxerr[Debug::KEY] << "Using keysym [A]"
401                                                    << endl;
402                         } else
403                                 break;
404                 } else {
405                         // It seems that this was a bit optimistic...
406                         // With this hacking things seems to be better (Lgb)
407                         //if (!iscntrl(key)) {
408                         //      ret_key = key;
409                         //      lyxerr[Debug::KEY]
410                         //              << "Using key [B]\n"
411                         //              << "Uchar["
412                         //              << static_cast<unsigned char>(key)
413                         //              << endl;
414                         //} else {
415                         ret_key = (keysym ? keysym : key);
416                         lyxerr[Debug::KEY] << "Using keysym [B]"
417                                            << endl;
418                                 //}
419                 }
420
421                 unsigned int const ret_state = xke->state;
422
423                 // If you have a better way to handle "wild-output" of
424                 // characters after the key has been released than the one
425                 // below, please contact me. (Lgb)
426                 static Time last_time_pressed;
427                 static unsigned int last_key_pressed;
428                 static unsigned int last_state_pressed;
429                 lyxerr[Debug::KEY] << "Workarea Diff: "
430                                    << xke->time - last_time_pressed
431                                    << endl;
432                 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
433                     && ret_state == last_state_pressed
434                     && xke->keycode == last_key_pressed) {
435                         lyxerr[Debug::KEY]
436                                 << "Workarea: Purging X events." << endl;
437                         //lyxerr << "Workarea Events: "
438                         //       << XEventsQueued(fl_get_display(), QueuedAlready)
439                         //       << endl;
440                         if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
441                                 waitForX(true);
442                         // This purge make f.ex. scrolling stop immediately when
443                         // releasing the PageDown button. The question is if
444                         // this purging of XEvents can cause any harm...
445                         // after some testing I can see no problems, but
446                         // I'd like other reports too.
447                         break;
448                 }
449                 last_time_pressed = xke->time;
450                 last_key_pressed = xke->keycode;
451                 last_state_pressed = ret_state;
452
453                 XLyXKeySym * xlk = new XLyXKeySym;
454                 xlk->initFromKeySym(ret_key);
455
456                 area->workAreaKeyPress(LyXKeySymPtr(xlk),
457                                        x_key_state(ret_state));
458         }
459         break;
460
461         case FL_KEYRELEASE:
462                 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
463                 break;
464
465         case FL_ENTER:
466                 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
467                 fl_set_cursor(FL_ObjWin(area->work_area), XC_xterm);
468                 break;
469         case FL_LEAVE:
470                 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
471                 // There should be no need for this. But there is.
472                 fl_set_cursor(FL_ObjWin(area->work_area), FL_DEFAULT_CURSOR);
473                 break;
474         case FL_DBLCLICK:
475                 if (ev) {
476                         lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
477                         FuncRequest cmd(LFUN_MOUSE_DOUBLE,
478                                         ev->xbutton.x - ob->x,
479                                         ev->xbutton.y - ob->y,
480                                         x_button_state(key));
481                         area->dispatch(cmd);
482                 }
483                 break;
484         case FL_TRPLCLICK:
485                 if (ev) {
486                         lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
487                         FuncRequest cmd(LFUN_MOUSE_TRIPLE,
488                                         ev->xbutton.x - ob->x,
489                                         ev->xbutton.y - ob->y,
490                                         x_button_state(key));
491                         area->dispatch(cmd);
492                 }
493                 break;
494         case FL_OTHER:
495                 if (ev)
496                         lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
497                 break;
498         }
499
500         return 1;
501 }
502
503
504 namespace {
505
506 string clipboard_selection;
507 bool clipboard_read = false;
508
509 extern "C" {
510
511 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
512                          void const * data, long size)
513 {
514         clipboard_selection.erase();
515
516         if (size > 0)
517                 clipboard_selection.reserve(size);
518         for (int i = 0; i < size; ++i)
519                 clipboard_selection += static_cast<char const *>(data)[i];
520         clipboard_read = true;
521         return 0;
522 }
523
524 } // extern "C"
525 } // namespace anon
526
527
528 int XWorkArea::event_cb(XEvent * xev)
529 {
530         switch (xev->type) {
531         case SelectionRequest:
532                 lyxerr[Debug::GUI] << "X requested selection." << endl;
533                 selectionRequested();
534                 break;
535         case SelectionClear:
536                 lyxerr[Debug::GUI] << "Lost selection." << endl;
537                 selectionLost();
538                 break;
539         }
540         return 0;
541 }
542
543
544 void XWorkArea::haveSelection(bool yes) const
545 {
546         Window const owner = yes ? FL_ObjWin(work_area) : None;
547         XSetSelectionOwner(fl_get_display(), XA_PRIMARY, owner, CurrentTime);
548 }
549
550
551 string const XWorkArea::getClipboard() const
552 {
553         clipboard_read = false;
554
555         if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
556                 return string();
557
558         XEvent ev;
559
560         while (!clipboard_read) {
561                 if (fl_check_forms() == FL_EVENT) {
562                         fl_XNextEvent(&ev);
563                         lyxerr << "Received unhandled X11 event\n"
564                                << "Type: 0x" << hex << ev.xany.type
565                                << " Target: 0x" << hex << ev.xany.window
566                                << dec << endl;
567                 }
568         }
569         return clipboard_selection;
570 }
571
572
573 void XWorkArea::putClipboard(string const & s) const
574 {
575         static string hold;
576         hold = s;
577
578         fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);
579 }