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