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