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