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