blib/lib/Gtk2/Ex/WidgetCursor.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 # Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2017, 2019 Kevin Ryde
2  
3 # This file is part of Gtk2-Ex-WidgetCursor.
4 #
5 # Gtk2-Ex-WidgetCursor is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as published
7 # by the Free Software Foundation; either version 3, or (at your option) any
8 # later version.
9 #
10 # Gtk2-Ex-WidgetCursor is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Gtk2-Ex-WidgetCursor. If not, see <http://www.gnu.org/licenses/>.
17  
18 package Gtk2::Ex::WidgetCursor;
19 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 0 ▶use Gtk2;
Subroutine BEGIN: called CC=1 SCAR=2.6
24 use List::Util;
25 use POSIX ();
26 use Scalar::Util 1.18; # 1.18 for pure-perl refaddr() fix
27  
28 # uncomment this to run the ### lines
29 #use Smart::Comments;
30  
31 our $VERSION = 16;
32  
33 # Gtk 2.2 for get_display()
34 # could work without it, but probably not worth bothering
35 Gtk2->CHECK_VERSION(2,2,0)
36 or die "WidgetCursor requires Gtk 2.2 or higher";
37  
38  
39 #------------------------------------------------------------------------------
40 # Cribs on widgets using gdk_window_set_cursor directly:
41 #
42 # GtkAboutDialog [not handled]
43 # Puts "email" and "link" tags on text in the credits GtkTextView and
44 # then does set_cursor on entering or leaving those.
45 #
46 # GtkCombo [ok mostly, with a hack]
47 # Does a single set_cursor for a 'top-left-arrow' on a GtkEventBox in
48 # its popup when realized. We dig that out for include_children,
49 # primarily so a busy() shows the watch on the popup window if it
50 # happens to be open. Of course GtkCombo is one of the ever-lengthening
51 # parade of working and well-defined widgets which Gtk says you're not
52 # meant to use any more.
53 #
54 # GtkCurve [not handled]
55 # Multiple set_cursor calls according to mode and motion. A rarely used
56 # widget so ignore it for now.
57 #
58 # GtkEntry [ok, with a hack]
59 # An Entry uses a private GdkWindow subwindow 4 pixels smaller than the
60 # main and sets a GDK_CURSOR_XTERM there when sensitive. That window
61 # isn't presented in the public fields/functions but can be dug out from
62 # $win->get_children. We set the cursor on both the main window and the
63 # subwindow then have a hack to restore the insertion point cursor on
64 # the latter when done. Getting the subwindow is fast since Gtk
65 # maintains the list of children for gdk_window_get_children() itself
66 # (as opposed to the way plain Xlib queries the server).
67 #
68 # The Entry can be made to restore the insertion cursor by toggling
69 # 'sensitive'. Hard to know which is worse: toggling sensitive
70 # needlessly, or forcibly setting the cursor back. The latter is needed
71 # for the SpinButton subclass below, so it's easier to do that.
72 #
73 # GtkFileChooser [probably ok]
74 # Sets a GDK_CURSOR_WATCH temporarily when busy. That probably kills
75 # any WidgetCursor setting, but probably GtkFileChooser isn't something
76 # you'll manipulate externally.
77 #
78 # GtkLabel [not handled]
79 # Puts GDK_XTERM on a private selection window when sensitive and
80 # selectable text, or something. This misses out on include_children
81 # for now.
82 #
83 # GtkLinkButton [not very good]
84 # A GtkButton subclass which does 'hand' set_cursor on its windowed
85 # parent for enter and leave events on its input-only event window.
86 #
87 # The cursor applied to the event window (per GtkButton above) defeats
88 # the hand on the parent, so that gets the right effect. But any
89 # WidgetCursor setting on the parent is lost when LinkButton turns off
90 # its hand under a leave-event. Might have to make a hack connecting to
91 # leave-event and re-applying the parent window.
92 #
93 # GtkPaned [not handled]
94 # Puts a cursor on its GdkWindow handle when sensitive. Not covered by
95 # include_children for now.
96 #
97 # GtkRecentChooser [probably ok]
98 # A GDK_WATCH when busy, similar to GtkFileChooser above. Hopefully ok
99 # most of the time with no special attention.
100 #
101 # GtkSpinButton [imperfect]
102 # Subclass of GtkEntry, but adds a "panel" window of arrows. In Gtk
103 # 2.12 it was overlaid on the normal Entry widget window, ie. the main
104 # outer one. In Gtk 2.14 it's a child of that outer window.
105 #
106 # For 2.12 it can be dug out by looking for sibling windows with events
107 # directed to the widget. Then it's a case of operating on three
108 # windows: the Entry main, the Entry 4-pixel smaller subwindow and the
109 # SpinButton panel.
110 #
111 # As of Gtk 2.12 toggling sensitive doesn't work to restore the
112 # insertion point cursor for a SpinButton, unlike its Entry superclass.
113 # Something not chaining up presumably, so the only choice is to
114 # forcibly put the cursor back.
115 #
116 # GtkStatusBar [not handled]
117 # A cursor on its private grip GdkWindow.
118 #
119 # GtkTextView [ok]
120 # Sets a GDK_XTERM insertion point cursor on its get_window('text')
121 # sub-window when sensitive. We operate on the get_window('widget') and
122 # get_window('text') both.
123 #
124 # Toggling sensitive will put back the insertion point cursor, like for
125 # a GtkEntry above, and like for the Entry it's hard to know whether
126 # it's worse to toggle sensitive or forcibly set back the cursor. For
127 # now the latter can share code with Entry and SpinButton and thus is
128 # what's used.
129 #
130  
131  
132 #------------------------------------------------------------------------------
133  
134 use Glib::Object::Subclass
135 'Glib::Object',
136 properties => [ Glib::ParamSpec->object
137 ('widget',
138 'Widget',
139 'The widget to show the cursor in, if just one widget.',
140 'Gtk2::Widget',
141 Glib::G_PARAM_READWRITE),
142  
143 Glib::ParamSpec->scalar
144 ('widgets',
145 'Widgets',
146 'An arrayref of widgets to show the cursor in.',
147 Glib::G_PARAM_READWRITE),
148  
149 Glib::ParamSpec->object
150 ('add-widget',
151 'Add widget',
152 'Pseudo-property to add a widget to the cursor in.',
153 'Gtk2::Widget',
154 ['writable']),
155  
156 Glib::ParamSpec->scalar
157 ('cursor',
158 'Cursor',
159 'Cursor to use while dragging, as any name or object accepted by Gtk2::Ex::WidgetCursor.',
160 Glib::G_PARAM_READWRITE),
161 #
162 # when glib 1.240 has fix for this pspec/get/set style
163 # {
164 # pspec => ...,
165 # get => \&cursor,
166 # set => \&cursor,
167 # }
168  
169 Glib::ParamSpec->string
170 ('cursor-name',
171 'Cursor name',
172 'Cursor to use while dragging, as cursor type enum nick plus "invisible".',
173 (eval {Glib->VERSION(1.240);1}
174 ? undef # default
175 : ''), # no undef/NULL before Perl-Glib 1.240
176 Glib::G_PARAM_READWRITE),
177  
178 Glib::ParamSpec->boxed
179 ('cursor-object',
180 'Cursor object',
181 'Cursor to use while dragging, as cursor object.',
182 'Gtk2::Gdk::Cursor',
183 Glib::G_PARAM_READWRITE),
184  
185 Glib::ParamSpec->boolean
186 ('active',
187 'Active',
188 'Whether to show this cursor.',
189 0, # default no
190 Glib::G_PARAM_READWRITE),
191 # when glib 1.240 has fix for this pspec/get/set style
192 # {
193 # pspec => '',
194 # get => \&active,
195 # set => \&active,
196 # }
197  
198 Glib::ParamSpec->double
199 ('priority',
200 'Priority',
201 'The priority of this cursor among multiple WidgetCursors on a given widget. Higher numbers are higher priority.',
202 - POSIX::DBL_MAX(), # min
203 POSIX::DBL_MAX(), # max
204 0, # default
205 Glib::G_PARAM_READWRITE),
206  
207 Glib::ParamSpec->boolean
208 ('include-children',
209 'Include children',
210 'Whether to apply the cursor to child widgets too.',
211 0, # default no
212 Glib::G_PARAM_READWRITE),
213  
214 ];
215  
216 # @wobjs is all the WidgetCursor objects which currently exist, sorted from
217 # highest to lowest priority, and from newest to oldest among those of equal
218 # priority
219 #
220 # Elements are weakened so they don't keep the objects alive. The DESTROY
221 # method strips elements and undefs from here, but not sure if undef could
222 # still be seen in here by certain funcs at certain times.
223 #
224 my @wobjs = ();
225  
226 sub INIT_INSTANCE {
227 my ($self) = @_;
228 ### WidgetCursor INIT_INSTANCE: "$self"
229  
230 $self->{'installed_widgets'} = [];
231 _wobjs_insert ($self);
232 }
233  
234 sub FINALIZE_INSTANCE {
235 my ($self) = @_;
236 ### WidgetCursor FINALIZE_INSTANCE: "$self"
237 ### installed_widgets: join(' ',@{$self->{'installed_widgets'}})
238  
239 _splice_out (\@wobjs, $self);
240 if (delete $self->{'active'}) {
241 _wobj_deactivated ($self);
242 }
243 }
244  
245 sub GET_PROPERTY {
246 my ($self, $pspec) = @_;
247 ### WidgetCursor GET_PROPERTY(): $pspec->get_name
248 my $pname = $pspec->get_name;
249  
250 if ($pname eq 'cursor_name') {
251 my $cursor = $self->{'cursor'};
252 if (Scalar::Util::blessed($cursor)) {
253 $cursor = $cursor->type;
254 # think prefer undef over cursor-is-pixmap for the get()
255 if ($cursor eq 'cursor-is-pixmap') {
256 undef $cursor;
257 }
258 }
259 return $cursor;
260 }
261 if ($pname eq 'cursor_object') {
262 my $cursor = $self->{'cursor'};
263 return (Scalar::Util::blessed($cursor)
264 && $cursor->isa('Gtk2::Gdk::Cursor')
265 && $cursor);
266 }
267  
268 return $self->{$pname};
269 }
270  
271 sub SET_PROPERTY {
272 my ($self, $pspec, $newval) = @_;
273 ### WidgetCursor SET_PROPERTY(): $pspec->get_name
274 my $pname = $pspec->get_name;
275  
276 if ($pname eq 'active') {
277 $self->active($newval);
278 return;
279 }
280 if ($pname =~ /^cursor/) {
281 $self->cursor($newval);
282 return;
283 }
284 if ($pname eq 'add_widget') {
285 $self->add_widgets ($newval);
286 return;
287 }
288  
289 if ($pname eq 'widget') {
290 $pname = 'widgets';
291 $newval = [ $newval ];
292 }
293 if ($pname eq 'widgets') {
294 my @array;
295 push @array, @$newval;
296 foreach (@array) { Scalar::Util::weaken ($_); }
297 $self->{'widgets'} = \@array;
298 if ($self->{'active'}) {
299 _wobj_activated ($self);
300 }
301 return;
302 }
303  
304 $self->{$pname} = $newval;
305  
306 if ($pname eq 'priority') {
307 _wobjs_insert ($self);
308 if ($self->{'active'}) {
309 _wobj_activated ($self);
310 }
311 }
312 }
313  
314 # insert $self into @wobjs according to $self->{'priority'}
315 sub _wobjs_insert {
316 my ($self) = @_;
317  
318 my $priority = ($self->{'priority'}||0);
319 my $pos = 0;
320 while ($pos < @wobjs) {
321 # FINALIZE_INSTANCE removes on destroy, but beware of undef just in case
322 if (my $wobj = $wobjs[$pos]) {
323 if ($wobj == $self) {
324 splice @wobjs,$pos,1; # remove from old position
325 next;
326 }
327 if ($priority >= ($wobj->{'priority'}||0)) {
328 last;
329 }
330 }
331 $pos++;
332 }
333 splice @wobjs,$pos,0, $self;
334 Scalar::Util::weaken ($wobjs[$pos]);
335 }
336  
337 # get or set "active"
338 sub active {
339 my ($self, $newval) = @_;
340 if (@_ < 2) { return $self->{'active'}; } # get
341  
342 ### active() set: "$self"
343 $newval = ($newval ? 1 : 0); # don't capture arbitrary input
344 my $oldval = $self->{'active'};
345 $self->{'active'} = $newval;
346 if ($oldval && ! $newval) {
347 _wobj_deactivated ($self);
348 $self->notify('active');
349 } elsif ($newval && ! $oldval) {
350 _wobj_activated ($self);
351 $self->notify('active');
352 }
353 }
354  
355 # newly turned off or destroyed
356 sub _wobj_deactivated {
357 my ($self) = @_;
358 ### _wobj_deactivated()
359 ### installed_widgets: join(' ',@{$self->{'installed_widgets'}})
360 my $aref = $self->{'installed_widgets'};
361 $self->{'installed_widgets'} = [];
362 foreach my $widget (@$aref) {
363 _update_widget ($widget);
364 }
365 }
366  
367 # newly turned on or created on
368 sub _wobj_activated {
369 my ($self) = @_;
370 ### _wobj_activated()
371  
372 if ($self->{'include_children'}) {
373 # go through widgets in other wobjs as well as ourselves since they may
374 # be affected if they're children of one of ours (%done skips duplicates
375 # among the lists)
376 ### include_children other wobjs
377 my %done;
378 foreach my $wobj (@wobjs) {
379 foreach my $widget (@{$wobj->{'widgets'}}) {
380 if (! $widget) { next; } # possible undef by weakening
381 $done{Scalar::Util::refaddr($widget)}
382 ||= do { _update_widget ($widget); 1 };
383 }
384 }
385  
386 # special handling for children of certain types that might be present
387 # deep in the tree
388 ### include_children special TextView etc
389 foreach my $widget (@{$self->{'widgets'}}) {
390 if (! $widget) { next; } # possible undef by weakening
391  
392 foreach my $widget (_container_recursively ($widget)) {
393 if ($widget->isa('Gtk2::Entry')
394 || $widget->isa('Gtk2::TextView')
395 || _widget_is_combo_eventbox ($widget)) {
396 $done{Scalar::Util::refaddr($widget)}
397 ||= do { _update_widget ($widget); 1 };
398 }
399 }
400 }
401  
402 } else {
403 # simple non-include-children for this wobj, look only at its immediate
404 # widgets
405 foreach my $widget (@{$self->{'widgets'}}) {
406 _update_widget ($widget);
407 }
408 }
409 }
410  
411 sub _update_widget {
412 my ($widget) = @_;
413 if (! $widget) { return; } # possible undef from weakening
414 ### _update_widget: "$widget", $widget->get_name
415  
416 # find wobj with priority on this $widget
417 my $wobj = List::Util::first
418 { $_->{'active'} && _wobj_applies_to_widget($_,$widget)} @wobjs;
419  
420 my $old_wobj = $widget->{__PACKAGE__.'.installed'};
421 ### wobj was: defined $old_wobj && $old_wobj->{'cursor'}
422 ### now: defined $wobj && $wobj->{'cursor'}
423 ### window: "@{[$widget->window||'undef']}"
424  
425 if (($wobj||0) == ($old_wobj||0)) { return; } # unchanged
426  
427 # forget this widget under $old_wobj
428 if ($old_wobj) {
429 _splice_out ($old_wobj->{'installed_widgets'}, $widget);
430 }
431  
432 if (! $wobj) {
433 # no wobj applies to this widget any more
434 delete $widget->{__PACKAGE__.'.installed'};
435 delete $widget->{__PACKAGE__.'.realize_ids'};
436  
437 my ($hack_win, $hack_cursor) = $widget->Gtk2_Ex_WidgetCursor_hack_restore;
438 $hack_win ||= 0; # avoid undef
439 foreach my $win ($widget->Gtk2_Ex_WidgetCursor_windows) {
440 $win || next;
441 my $cursor = ($win == $hack_win
442 ? Gtk2::Gdk::Cursor->new_for_display ($widget->get_display,
443 $hack_cursor)
444 : undef);
445 ### set_cursor back to: "$win", $cursor && $cursor->type
446 $win->set_cursor ($cursor);
447 }
448 return;
449 }
450  
451 # install wobj on this widget
452  
453 # remember this widget under wobj
454 # this is when unrealized too, so remember to cleanup realize handler
455 { my $aref = $wobj->{'installed_widgets'};
456 push @$aref, $widget;
457 Scalar::Util::weaken ($aref->[-1]);
458 ### gives installed_widgets: join(' ',@{$wobj->{'installed_widgets'}})
459 }
460  
461 # note this wobj under the widget
462 $widget->{__PACKAGE__.'.installed'} = $wobj;
463 Scalar::Util::weaken ($widget->{__PACKAGE__.'.installed'});
464  
465 my @windows = $widget->Gtk2_Ex_WidgetCursor_windows;
466 if (! defined $windows[0]) {
467 ### not realized, defer setting
468 $widget->{__PACKAGE__.'.realize_ids'} ||= do {
469 require Glib::Ex::SignalIds;
470 Glib::Ex::SignalIds->new
471 ($widget,
472 $widget->signal_connect (realize => \&_do_widget_realize))
473 };
474 return;
475 }
476  
477 # and finally actually set the cursor
478 my $cursor = _resolve_cursor ($wobj, $widget);
479 foreach my $win (@windows) {
480 $win or next;
481 ### set_cursor: "$win", $cursor && $cursor->type
482 $win->set_cursor ($cursor);
483 }
484 ### gives installed_widgets: join(' ',@{$wobj->{'installed_widgets'}})
485 }
486  
487 # 'realize' signal handler on a WidgetCursor affected widget
488 sub _do_widget_realize {
489 my ($widget) = @_;
490 ### _do_widget_realize(): "$widget"
491 delete $widget->{__PACKAGE__.'.realize_ids'};
492 _update_widget ($widget);
493 }
494  
495  
496 # Return true if $wobj is applicable to $widget, either because $widget is
497 # in its widgets list or is a child of one of them for "include_children".
498 # Note $w->is_ancestor($w) is false, ie. it doesn't include itself.
499 #
500 sub _wobj_applies_to_widget {
501 my ($wobj, $widget) = @_;
502 return List::Util::first
503 { defined $_ # possible weakening during destroy
504 && ($_ == $widget
505 || ($wobj->{'include_children'} && $widget->is_ancestor($_))) }
506 @{$wobj->{'widgets'}};
507 }
508  
509 # get or set "cursor"
510 sub cursor {
511 my ($self, $newval) = @_;
512 if (@_ < 2) { return $self->{'cursor'}; } # get
513  
514 # set
515 if (_cursor_equal ($self->{'cursor'}, $newval)) { return; }
516 $self->{'cursor'} = $newval;
517  
518 if ($self->{'active'}) {
519 foreach my $widget (@{$self->{'installed_widgets'}}) {
520 foreach my $win ($widget->Gtk2_Ex_WidgetCursor_windows) {
521 $win || next; # only if realized
522 $win->set_cursor (_resolve_cursor ($self, $widget));
523 }
524 }
525 }
526  
527 $self->notify('cursor');
528 $self->notify('cursor-name');
529 $self->notify('cursor-object');
530 }
531  
532 # return true if two cursor settings $x and $y are the same
533 sub _cursor_equal {
534 my ($x, $y) = @_;
535 return ((! defined $x && ! defined $y) # undef == undef
536 || (ref $x && ref $y && $x == $y) # objects identical address
537 || (defined $x && defined $y && $x eq $y)); # strings by value
538 }
539  
540 # get widgets in wobj
541 sub widgets {
542 my ($self) = @_;
543 return grep {defined} @{$self->{'widgets'}};
544 }
545  
546 sub add_widgets {
547 my ($self, @widgets) = @_;
548 my $aref = $self->{'widgets'};
549  
550 # only those not already in our list
551 @widgets = grep { my $widget = @_;
552 ! List::Util::first {defined $_ && $_==$widget}
553 @$aref } @widgets;
554 if (! @widgets) { return; }
555  
556 foreach my $widget (@widgets) {
557 push @$aref, $widget;
558 Scalar::Util::weaken ($aref->[-1]);
559 }
560  
561 if ($self->{'include_children'}) {
562 # for include_children must have a deep look down through the new
563 # widgets, let the full code of _wobj_activated() do that (though it's a
564 # little wasteful to look again at the previously covered widgets)
565 _wobj_activated ($self);
566  
567 } else {
568 # for ordinary only the newly added widgets might change
569 foreach my $widget (@widgets) {
570 _update_widget ($widget);
571 }
572 }
573 $self->notify('widget');
574 $self->notify('widgets');
575 }
576  
577 # return an actual Gtk2::Gdk::Cursor from what may be only a string setting
578 # in $wobj->{'cursor'}
579 sub _resolve_cursor {
580 my ($wobj, $widget) = @_;
581 my $cursor = $wobj->{'cursor'};
582  
583 if (! defined $cursor || ref $cursor) {
584 # undef or cursor object
585 return $cursor;
586  
587 } elsif ($cursor eq 'invisible') {
588 # call through $wobj in case of subclassing
589 return $wobj->invisible_cursor ($widget);
590  
591 } else {
592 # string cursor name -- only ever call to resolve here when widget is
593 # realized, so get_display() isn't undef
594 if ($widget->can('get_display')) {
595 # gtk 2.2 up
596 my $display = $widget->get_display;
597 return Gtk2::Gdk::Cursor->new_for_display ($display, $cursor);
598 } else {
599 # gtk 2.0.x
600 return Gtk2::Gdk::Cursor->new ($cursor);
601 }
602 }
603 }
604  
605 # Return $widget and all its contained children, grandchildren, etc.
606 # Iterative avoids deep recursion warning for the unlikely case of nesting
607 # beyond 100 deep.
608 #
609 sub _container_recursively {
610 my @pending = @_;
611 my @ret;
612 while (@pending) {
613 my $widget = pop @pending;
614 push @ret, $widget;
615 if (my $func = $widget->can('get_children')) {
616 push @pending, $widget->$func;
617 }
618 }
619 return @ret;
620 }
621  
622 #------------------------------------------------------------------------------
623 # operative windows hacks
624 #
625 # $widget->Gtk2_Ex_WidgetCursor_windows() returns a list of windows in
626 # $widget to act on, with hacks to pickup multiple windows on core classes.
627 #
628 # $widget->Gtk2_Ex_WidgetCursor_hack_restore() returns ($win, $cursor).
629 # $cursor is a string cursor name to put back on $win when there's no more
630 # WidgetCursor objects. Or the return is an empty list or $win undef when
631 # nothing to hack (in which case all windows go back to "undef" cursor).
632 #
633  
634 # default to operate on $widget->window alone
635 *Gtk2::Widget::Gtk2_Ex_WidgetCursor_windows = \&Gtk2::Widget::window;
636 sub Gtk2::Widget::Gtk2_Ex_WidgetCursor_hack_restore { return (); }
637  
638 # GtkEventBox under a GtkComboBox popup window has a 'top-left-arrow'. It
639 # gets overridden by a special case in the recursive updates above, and
640 # hack_restore() here puts it back.
641 #
642 sub Gtk2::EventBox::Gtk2_Ex_WidgetCursor_hack_restore {
643 my ($widget) = @_;
644 return _widget_is_combo_eventbox($widget)
645 && ($widget->window, 'top-left-arrow');
646 }
647  
648 # GtkTextView operate on 'text' subwindow to override its insertion point
649 # cursor there, plus the main 'widget' window to cover the entire widget
650 # extent. The 'text' subwindow insertion point is supposed to be on when
651 # the widget is sensitive, so hack_restore() that.
652 #
653 sub Gtk2::TextView::Gtk2_Ex_WidgetCursor_windows {
654 my ($widget) = @_;
655 return ($widget->get_window ('widget'),
656 $widget->get_window ('text'));
657 }
658 sub Gtk2::TextView::Gtk2_Ex_WidgetCursor_hack_restore {
659 my ($widget) = @_;
660 return $widget->sensitive && ($widget->get_window('text'), 'xterm');
661 }
662  
663 # GtkEntry's extra subwindow is included here. And when sensitive it should
664 # be put back to an insertion point. For a bit of safety use list context
665 # etc to allow for no subwindows, since it's undocumented.
666 #
667 # In Gtk 2.14 the SpinButton sub-class has the arrow panel as a subwindow
668 # too (instead of an overlay in Gtk 2.12 and earlier). So look for the
669 # smaller height one among multiple subwindows.
670 #
671 sub Gtk2::Entry::Gtk2_Ex_WidgetCursor_windows {
672 my ($widget) = @_;
673 my $win = $widget->window || return; # if unrealized
674 return ($win, $win->get_children);
675 }
676 sub Gtk2::Entry::Gtk2_Ex_WidgetCursor_hack_restore {
677 my ($widget) = @_;
678 $widget->sensitive or return;
679 my $win = $widget->window || return; # if unrealized
680 my @children = $win->get_children;
681 # by increasing height
682 @children = sort {($a->get_size)[1] <=> ($b->get_size)[1]} @children;
683 return ($children[0], 'xterm');
684 }
685 # GtkSpinButton's extra "panel" overlay window either as a "sibling" (which
686 # also finds the main window) for Gtk 2.12 or in the get_children() for Gtk
687 # 2.13; plus the GtkEntry subwindow as per GtkEntry above. hack_restore()
688 # inherited from GtkEntry above.
689 #
690 sub Gtk2::SpinButton::Gtk2_Ex_WidgetCursor_windows {
691 my ($widget) = @_;
692 my $win = $widget->window || return; # if unrealized
693 return (_widget_sibling_windows ($widget),
694 $win->get_children);
695 }
696  
697 # GtkButton secret input-only "event_window" overlay found as a "sibling".
698 #
699 sub Gtk2::Button::Gtk2_Ex_WidgetCursor_windows {
700 my ($widget) = @_;
701 return _widget_sibling_windows ($widget);
702 }
703  
704 # _widget_sibling_windows() returns a list of the "sibling" windows of
705 # $widget. This means all the windows which are under $widget's parent and
706 # have their events directed to $widget. If $widget is a windowed widget
707 # then this will include its main $widget->window (or should do).
708 #
709 # The search works by seeing where a dummy expose event is directed by
710 # gtk_get_event_widget(). It'd also be possible to inspect
711 # gdk_window_get_user_data(), but Gtk2-Perl only returns an "unsigned" for
712 # that so it'd need some nasty digging for the widget address.
713 #
714 # In the past the code here cached the result against the widget (what was
715 # then just GtkButton's "event_window" sibling), with weakening of course so
716 # unrealize would destroy the windows as normal. But don't bother with that
717 # now, on the basis that cursor changes hopefully aren't so frequent as to
718 # need too much trouble, and that it's less prone to mistakes if not cached
719 # :-).
720 #
721 sub _widget_sibling_windows {
722 my ($widget) = @_;
723 my $parent_win = ($widget->flags & 'no-window'
724 ? $widget->window
725 : $widget->get_parent_window)
726 || return; # if unrealized
727  
728 my $event = Gtk2::Gdk::Event->new ('expose');
729 return grep { $event->window ($_);
730 ($widget == (Gtk2->get_event_widget($event) || 0))
731 } $parent_win->get_children;
732 }
733  
734 # Return true if $widget is the Gtk2::EventBox child of a Gtk2::Combo popup
735 # window (it's a child of the popup window, not of the Combo itself).
736 #
737 sub _widget_is_combo_eventbox {
738 my ($widget) = @_;
739 my $parent;
740 return ($widget->isa('Gtk2::EventBox')
741 && ($parent = $widget->get_parent) # might not have a parent
742 && $parent->get_name eq 'gtk-combo-popup-window');
743 }
744  
745  
746 #------------------------------------------------------------------------------
747  
748 # Could think about documenting this idle level to the world, maybe like the
749 # following, but would it be any use?
750 #
751 # =item C<$Gtk2::Ex::WidgetCursor::busy_idle_priority>
752 #
753 # The priority level of the (C<< Glib::Idle->add >>) handler installed by
754 # C<busy>. This is C<G_PRIORITY_DEFAULT_IDLE - 10> by default, which is
755 # designed to stay busy through Gtk resizing and redrawing at around
756 # C<G_PRIORITY_HIGH_IDLE>, but end the busy before ordinary "default idle"
757 # tasks.
758 #
759 # You can change this depending what things you set running at what idle
760 # levels and where you consider the application no longer busy for user
761 # purposes. But note changing this variable only affects future C<busy>
762 # calls, not any currently active one.
763 #
764 use constant BUSY_IDLE_PRIORITY => Glib::G_PRIORITY_DEFAULT_IDLE - 10;
765  
766 my $busy_wc;
767 my $busy_id;
768 my $realize_id;
769  
770 sub busy {
771 my ($class) = @_;
772 my @widgets = Gtk2::Window->list_toplevels;
773 ### busy on toplevels: join(' ',@widgets)
774  
775 if ($busy_wc) {
776 $busy_wc->add_widgets (@widgets);
777 } else {
778 ### new busy with class: $class
779 $busy_wc = $class->new (widgets => \@widgets,
780 cursor => 'watch',
781 include_children => 1,
782 priority => 1000,
783 active => 1);
784 }
785 _flush_mapped_widgets (@widgets);
786  
787 # This is a hack to persuade Gtk2-Perl 1.160 and 1.181 to finish loading
788 # Gtk2::Widget. Without this if no Gtk2::Widget has ever been created the
789 # signal_add_emission_hook() fails. 1.160 needs the combination of isa()
790 # and find_property(). 1.181 is ok with find_property() alone. Either
791 # way these can be removed when ready to depend on 1.200 and up.
792 Gtk2::Widget->isa ('Gtk2::Widget');
793 Gtk2::Widget->find_property ('name');
794
795 $realize_id ||= Gtk2::Widget->signal_add_emission_hook
796 (realize => \&_do_busy_realize_emission);
797  
798 $busy_id ||= Glib::Idle->add
799 (\&_busy_idle_handler, undef, BUSY_IDLE_PRIORITY);
800 }
801  
802 # While busy notice extra toplevels which have been realized.
803 # The cursor setting is applied at the realize so it's there ready for when
804 # the map is done.
805 sub _do_busy_realize_emission {
806 my ($invocation_hint, $param_list) = @_;
807 my ($widget) = @$param_list;
808 ### WidgetCursor _do_busy_realize_emission(): "$widget"
809 if ($widget->isa ('Gtk2::Window')) {
810 $busy_wc->add_widgets (Gtk2::Window->list_toplevels);
811 ### _do_busy_realize_emission() flush
812 $widget->get_display->flush;
813 }
814 return 1; # stay connected
815 }
816  
817 # Call unbusy() through $busy_wc to allow for possible subclassing.
818 # Using unbusy does a flush, which is often unnecessary but will ensure that
819 # if there's lower priority idles still to run then our cursors go out
820 # before the time they take.
821 #
822 sub _busy_idle_handler {
823 ### _busy_idle_handler finished
824 $busy_id = undef;
825 if ($busy_wc) { $busy_wc->unbusy; }
826 return 0; # Glib::SOURCE_REMOVE, one run only
827 }
828  
829 sub unbusy {
830 # my ($class_or_self) = @_;
831 ### WidgetCursor unbusy()
832  
833 # Some freaky stuff can happen during perl "global destruction" with
834 # classes being destroyed and disconecting emission hooks on their own,
835 # provoking warnings from code like the following that does a cleanup
836 # itself. Fairly confident that doesn't apply to Gtk2::Widget because
837 # that class probably, hopefully, maybe, never gets destroyed, or at least
838 # not until well after any Perl code might get a chance to call unbusy().
839 #
840 if ($realize_id) {
841 Gtk2::Widget->signal_remove_emission_hook (realize => $realize_id);
842 undef $realize_id;
843 }
844  
845 if ($busy_id) {
846 Glib::Source->remove ($busy_id);
847 $busy_id = undef;
848 }
849 if ($busy_wc) {
850 my @widgets = $busy_wc->widgets;
851 $busy_wc = undef;
852 # flush to show new cursors immediately, per busy() below
853 _flush_mapped_widgets (@widgets);
854 }
855 }
856  
857 # flush the Gtk2::Gdk::Display's of all the given widgets, if they're mapped
858 # (with the idea being if they're unmapped then there's nothing to see so no
859 # need to flush)
860 #
861 sub _flush_mapped_widgets {
862 my @widget_list = @_;
863 my %done;
864 ### _flush_mapped_widgets
865 foreach my $widget (@widget_list) {
866 if ($widget->mapped) {
867 my $display = $widget->get_display;
868 $done{Scalar::Util::refaddr($display)} ||= do {
869 ### flush display: "$display"
870 $display->flush;
871 1
872 };
873 }
874 }
875 }
876  
877  
878 #------------------------------------------------------------------------------
879  
880 # list_values() creates a slew of hash records, so don't want to do that on
881 # every invisible_cursor() call. Doing it once at BEGIN time also allows
882 # the result to be inlined and the unused code discarded.
883 #
884 use constant _HAVE_BLANK_CURSOR =>
885 (!! List::Util::first
886 {$_->{'nick'} eq 'blank-cursor'}
887 Glib::Type->list_values('Gtk2::Gdk::CursorType'));
888 ### _HAVE_BLANK_CURSOR: _HAVE_BLANK_CURSOR()
889  
890 sub invisible_cursor {
891 my ($class, $target) = @_;
892 my $display;
893  
894 if (! defined $target) {
895 $display = Gtk2::Gdk::Display->get_default
896 || croak 'invisible_cursor(): no default display';
897  
898 } elsif ($target->isa('Gtk2::Gdk::Display')) {
899 $display = $target;
900  
901 } else {
902 $display = $target->get_display
903 || croak "invisible_cursor(): get_display undef on $target";
904 }
905  
906 if (_HAVE_BLANK_CURSOR) {
907 # gdk_cursor_new_for_display() returns same object each time so no need
908 # to cache, though being a Glib::Boxed it's a new perl object every time
909 return Gtk2::Gdk::Cursor->new_for_display ($display,'blank-cursor');
910 } else {
911 return ($display->{__PACKAGE__.'.invisible_cursor'}
912 ||= do {
913 ### invisible_cursor() new for: "$display"
914 my $window = $display->get_default_screen->get_root_window;
915 my $mask = Gtk2::Gdk::Bitmap->create_from_data ($window,"\0",1,1);
916 my $color = Gtk2::Gdk::Color->new (0,0,0);
917 Gtk2::Gdk::Cursor->new_from_pixmap ($mask,$mask,$color,$color,0,0);
918 });
919 }
920 }
921  
922  
923 #------------------------------------------------------------------------------
924 # generic helpers
925  
926 sub _splice_out {
927 my ($aref, $target) = @_;
928 for (my $i = 0; $i < @$aref; $i++) {
929 if (! defined $aref->[$i] || $aref->[$i] == $target) {
930 splice @$aref, $i,1;
931 }
932 }
933 }
934  
935 #------------------------------------------------------------------------------
936 1;
937 __END__
↑ Summary