Gtk2-Ex-WidgetCursor
view release on metacpan or search on metacpan
lib/Gtk2/Ex/WidgetCursor.pm view on Meta::CPAN
# Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2017, 2019 Kevin Ryde
# This file is part of Gtk2-Ex-WidgetCursor.
#
# Gtk2-Ex-WidgetCursor is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 3, or (at your option) any
# later version.
#
# Gtk2-Ex-WidgetCursor is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with Gtk2-Ex-WidgetCursor. If not, see <http://www.gnu.org/licenses/>.
package Gtk2::Ex::WidgetCursor;
use 5.006;
use strict;
use warnings;
use Carp;
use Gtk2;
use List::Util;
use POSIX ();
use Scalar::Util 1.18; # 1.18 for pure-perl refaddr() fix
# uncomment this to run the ### lines
#use Smart::Comments;
our $VERSION = 16;
# Gtk 2.2 for get_display()
# could work without it, but probably not worth bothering
Gtk2->CHECK_VERSION(2,2,0)
or die "WidgetCursor requires Gtk 2.2 or higher";
#------------------------------------------------------------------------------
# Cribs on widgets using gdk_window_set_cursor directly:
#
# GtkAboutDialog [not handled]
# Puts "email" and "link" tags on text in the credits GtkTextView and
# then does set_cursor on entering or leaving those.
#
# GtkCombo [ok mostly, with a hack]
# Does a single set_cursor for a 'top-left-arrow' on a GtkEventBox in
# its popup when realized. We dig that out for include_children,
# primarily so a busy() shows the watch on the popup window if it
# happens to be open. Of course GtkCombo is one of the ever-lengthening
# parade of working and well-defined widgets which Gtk says you're not
# meant to use any more.
#
# GtkCurve [not handled]
# Multiple set_cursor calls according to mode and motion. A rarely used
# widget so ignore it for now.
#
# GtkEntry [ok, with a hack]
# An Entry uses a private GdkWindow subwindow 4 pixels smaller than the
# main and sets a GDK_CURSOR_XTERM there when sensitive. That window
# isn't presented in the public fields/functions but can be dug out from
# $win->get_children. We set the cursor on both the main window and the
# subwindow then have a hack to restore the insertion point cursor on
# the latter when done. Getting the subwindow is fast since Gtk
# maintains the list of children for gdk_window_get_children() itself
# (as opposed to the way plain Xlib queries the server).
#
# The Entry can be made to restore the insertion cursor by toggling
# 'sensitive'. Hard to know which is worse: toggling sensitive
# needlessly, or forcibly setting the cursor back. The latter is needed
# for the SpinButton subclass below, so it's easier to do that.
#
# GtkFileChooser [probably ok]
# Sets a GDK_CURSOR_WATCH temporarily when busy. That probably kills
# any WidgetCursor setting, but probably GtkFileChooser isn't something
# you'll manipulate externally.
#
# GtkLabel [not handled]
# Puts GDK_XTERM on a private selection window when sensitive and
# selectable text, or something. This misses out on include_children
# for now.
#
# GtkLinkButton [not very good]
# A GtkButton subclass which does 'hand' set_cursor on its windowed
# parent for enter and leave events on its input-only event window.
#
# The cursor applied to the event window (per GtkButton above) defeats
# the hand on the parent, so that gets the right effect. But any
# WidgetCursor setting on the parent is lost when LinkButton turns off
# its hand under a leave-event. Might have to make a hack connecting to
# leave-event and re-applying the parent window.
#
# GtkPaned [not handled]
# Puts a cursor on its GdkWindow handle when sensitive. Not covered by
# include_children for now.
#
# GtkRecentChooser [probably ok]
# A GDK_WATCH when busy, similar to GtkFileChooser above. Hopefully ok
# most of the time with no special attention.
#
# GtkSpinButton [imperfect]
# Subclass of GtkEntry, but adds a "panel" window of arrows. In Gtk
# 2.12 it was overlaid on the normal Entry widget window, ie. the main
# outer one. In Gtk 2.14 it's a child of that outer window.
#
# For 2.12 it can be dug out by looking for sibling windows with events
# directed to the widget. Then it's a case of operating on three
# windows: the Entry main, the Entry 4-pixel smaller subwindow and the
# SpinButton panel.
#
# As of Gtk 2.12 toggling sensitive doesn't work to restore the
# insertion point cursor for a SpinButton, unlike its Entry superclass.
# Something not chaining up presumably, so the only choice is to
# forcibly put the cursor back.
#
# GtkStatusBar [not handled]
# A cursor on its private grip GdkWindow.
#
# GtkTextView [ok]
# Sets a GDK_XTERM insertion point cursor on its get_window('text')
# sub-window when sensitive. We operate on the get_window('widget') and
# get_window('text') both.
#
# Toggling sensitive will put back the insertion point cursor, like for
# a GtkEntry above, and like for the Entry it's hard to know whether
# it's worse to toggle sensitive or forcibly set back the cursor. For
# now the latter can share code with Entry and SpinButton and thus is
# what's used.
#
#------------------------------------------------------------------------------
use Glib::Object::Subclass
'Glib::Object',
properties => [ Glib::ParamSpec->object
('widget',
'Widget',
'The widget to show the cursor in, if just one widget.',
'Gtk2::Widget',
Glib::G_PARAM_READWRITE),
Glib::ParamSpec->scalar
('widgets',
'Widgets',
'An arrayref of widgets to show the cursor in.',
Glib::G_PARAM_READWRITE),
Glib::ParamSpec->object
('add-widget',
'Add widget',
'Pseudo-property to add a widget to the cursor in.',
'Gtk2::Widget',
['writable']),
Glib::ParamSpec->scalar
('cursor',
'Cursor',
lib/Gtk2/Ex/WidgetCursor.pm view on Meta::CPAN
my ($widget) = @_;
my $win = $widget->window || return; # if unrealized
return (_widget_sibling_windows ($widget),
$win->get_children);
}
# GtkButton secret input-only "event_window" overlay found as a "sibling".
#
sub Gtk2::Button::Gtk2_Ex_WidgetCursor_windows {
my ($widget) = @_;
return _widget_sibling_windows ($widget);
}
# _widget_sibling_windows() returns a list of the "sibling" windows of
# $widget. This means all the windows which are under $widget's parent and
# have their events directed to $widget. If $widget is a windowed widget
# then this will include its main $widget->window (or should do).
#
# The search works by seeing where a dummy expose event is directed by
# gtk_get_event_widget(). It'd also be possible to inspect
# gdk_window_get_user_data(), but Gtk2-Perl only returns an "unsigned" for
# that so it'd need some nasty digging for the widget address.
#
# In the past the code here cached the result against the widget (what was
# then just GtkButton's "event_window" sibling), with weakening of course so
# unrealize would destroy the windows as normal. But don't bother with that
# now, on the basis that cursor changes hopefully aren't so frequent as to
# need too much trouble, and that it's less prone to mistakes if not cached
# :-).
#
sub _widget_sibling_windows {
my ($widget) = @_;
my $parent_win = ($widget->flags & 'no-window'
? $widget->window
: $widget->get_parent_window)
|| return; # if unrealized
my $event = Gtk2::Gdk::Event->new ('expose');
return grep { $event->window ($_);
($widget == (Gtk2->get_event_widget($event) || 0))
} $parent_win->get_children;
}
# Return true if $widget is the Gtk2::EventBox child of a Gtk2::Combo popup
# window (it's a child of the popup window, not of the Combo itself).
#
sub _widget_is_combo_eventbox {
my ($widget) = @_;
my $parent;
return ($widget->isa('Gtk2::EventBox')
&& ($parent = $widget->get_parent) # might not have a parent
&& $parent->get_name eq 'gtk-combo-popup-window');
}
#------------------------------------------------------------------------------
# Could think about documenting this idle level to the world, maybe like the
# following, but would it be any use?
#
# =item C<$Gtk2::Ex::WidgetCursor::busy_idle_priority>
#
# The priority level of the (C<< Glib::Idle->add >>) handler installed by
# C<busy>. This is C<G_PRIORITY_DEFAULT_IDLE - 10> by default, which is
# designed to stay busy through Gtk resizing and redrawing at around
# C<G_PRIORITY_HIGH_IDLE>, but end the busy before ordinary "default idle"
# tasks.
#
# You can change this depending what things you set running at what idle
# levels and where you consider the application no longer busy for user
# purposes. But note changing this variable only affects future C<busy>
# calls, not any currently active one.
#
use constant BUSY_IDLE_PRIORITY => Glib::G_PRIORITY_DEFAULT_IDLE - 10;
my $busy_wc;
my $busy_id;
my $realize_id;
sub busy {
my ($class) = @_;
my @widgets = Gtk2::Window->list_toplevels;
### busy on toplevels: join(' ',@widgets)
if ($busy_wc) {
$busy_wc->add_widgets (@widgets);
} else {
### new busy with class: $class
$busy_wc = $class->new (widgets => \@widgets,
cursor => 'watch',
include_children => 1,
priority => 1000,
active => 1);
}
_flush_mapped_widgets (@widgets);
# This is a hack to persuade Gtk2-Perl 1.160 and 1.181 to finish loading
# Gtk2::Widget. Without this if no Gtk2::Widget has ever been created the
# signal_add_emission_hook() fails. 1.160 needs the combination of isa()
# and find_property(). 1.181 is ok with find_property() alone. Either
# way these can be removed when ready to depend on 1.200 and up.
Gtk2::Widget->isa ('Gtk2::Widget');
Gtk2::Widget->find_property ('name');
$realize_id ||= Gtk2::Widget->signal_add_emission_hook
(realize => \&_do_busy_realize_emission);
$busy_id ||= Glib::Idle->add
(\&_busy_idle_handler, undef, BUSY_IDLE_PRIORITY);
}
# While busy notice extra toplevels which have been realized.
# The cursor setting is applied at the realize so it's there ready for when
# the map is done.
sub _do_busy_realize_emission {
my ($invocation_hint, $param_list) = @_;
my ($widget) = @$param_list;
### WidgetCursor _do_busy_realize_emission(): "$widget"
if ($widget->isa ('Gtk2::Window')) {
$busy_wc->add_widgets (Gtk2::Window->list_toplevels);
### _do_busy_realize_emission() flush
$widget->get_display->flush;
}
return 1; # stay connected
}
# Call unbusy() through $busy_wc to allow for possible subclassing.
# Using unbusy does a flush, which is often unnecessary but will ensure that
# if there's lower priority idles still to run then our cursors go out
# before the time they take.
#
sub _busy_idle_handler {
### _busy_idle_handler finished
$busy_id = undef;
if ($busy_wc) { $busy_wc->unbusy; }
return 0; # Glib::SOURCE_REMOVE, one run only
}
sub unbusy {
# my ($class_or_self) = @_;
### WidgetCursor unbusy()
# Some freaky stuff can happen during perl "global destruction" with
# classes being destroyed and disconecting emission hooks on their own,
# provoking warnings from code like the following that does a cleanup
# itself. Fairly confident that doesn't apply to Gtk2::Widget because
# that class probably, hopefully, maybe, never gets destroyed, or at least
# not until well after any Perl code might get a chance to call unbusy().
#
if ($realize_id) {
Gtk2::Widget->signal_remove_emission_hook (realize => $realize_id);
undef $realize_id;
}
if ($busy_id) {
Glib::Source->remove ($busy_id);
$busy_id = undef;
}
if ($busy_wc) {
my @widgets = $busy_wc->widgets;
$busy_wc = undef;
# flush to show new cursors immediately, per busy() below
_flush_mapped_widgets (@widgets);
}
}
# flush the Gtk2::Gdk::Display's of all the given widgets, if they're mapped
# (with the idea being if they're unmapped then there's nothing to see so no
# need to flush)
#
sub _flush_mapped_widgets {
my @widget_list = @_;
my %done;
### _flush_mapped_widgets
foreach my $widget (@widget_list) {
if ($widget->mapped) {
my $display = $widget->get_display;
$done{Scalar::Util::refaddr($display)} ||= do {
### flush display: "$display"
$display->flush;
1
};
}
}
}
#------------------------------------------------------------------------------
# list_values() creates a slew of hash records, so don't want to do that on
# every invisible_cursor() call. Doing it once at BEGIN time also allows
# the result to be inlined and the unused code discarded.
#
use constant _HAVE_BLANK_CURSOR =>
(!! List::Util::first
{$_->{'nick'} eq 'blank-cursor'}
Glib::Type->list_values('Gtk2::Gdk::CursorType'));
### _HAVE_BLANK_CURSOR: _HAVE_BLANK_CURSOR()
sub invisible_cursor {
my ($class, $target) = @_;
my $display;
if (! defined $target) {
$display = Gtk2::Gdk::Display->get_default
|| croak 'invisible_cursor(): no default display';
} elsif ($target->isa('Gtk2::Gdk::Display')) {
$display = $target;
} else {
$display = $target->get_display
|| croak "invisible_cursor(): get_display undef on $target";
}
if (_HAVE_BLANK_CURSOR) {
# gdk_cursor_new_for_display() returns same object each time so no need
# to cache, though being a Glib::Boxed it's a new perl object every time
return Gtk2::Gdk::Cursor->new_for_display ($display,'blank-cursor');
} else {
return ($display->{__PACKAGE__.'.invisible_cursor'}
||= do {
### invisible_cursor() new for: "$display"
my $window = $display->get_default_screen->get_root_window;
my $mask = Gtk2::Gdk::Bitmap->create_from_data ($window,"\0",1,1);
my $color = Gtk2::Gdk::Color->new (0,0,0);
Gtk2::Gdk::Cursor->new_from_pixmap ($mask,$mask,$color,$color,0,0);
});
}
}
#------------------------------------------------------------------------------
# generic helpers
sub _splice_out {
my ($aref, $target) = @_;
for (my $i = 0; $i < @$aref; $i++) {
if (! defined $aref->[$i] || $aref->[$i] == $target) {
splice @$aref, $i,1;
}
}
}
#------------------------------------------------------------------------------
1;
__END__
=head1 NAME
Gtk2::Ex::WidgetCursor -- mouse pointer cursor management for widgets
=for test_synopsis my ($mywidget)
=head1 SYNOPSIS
use Gtk2::Ex::WidgetCursor;
my $wc = Gtk2::Ex::WidgetCursor->new (widget => $mywidget,
cursor => 'fleur',
active => 1);
# show wristwatch everywhere while number crunching
Gtk2::Ex::WidgetCursor->busy;
# bonus invisible cursor creator
my $cursor = Gtk2::Ex::WidgetCursor->invisible_cursor;
=head1 OBJECT HIERARCHY
C<Gtk2::Ex::WidgetCursor> is a subclass of C<Glib::Object>.
Glib::Object
Gtk2::Ex::WidgetCursor
=head1 DESCRIPTION
WidgetCursor manages the mouse pointer cursor shown in widget windows as per
C<Gtk2::Gdk::Window> C<set_cursor>. A "busy" mechanism can display a
wristwatch in all windows when the whole application is blocked.
With the plain window C<set_cursor>, it's difficult for widget add-ons or
independent parts of an application to cooperate with what should be shown
at different times or in different modes.
A C<Gtk2::Ex::WidgetCursor> object represents a desired cursor in one or
more widgets. When "active" and when it's the newest or highest priority
then the specified cursor is set onto those widget window(s). If the
WidgetCursor object is later made inactive or destroyed then the next
remaining highest WidgetCursor takes effect, etc.
The idea is to have say a base WidgetCursor for an overall mode, then
something else temporarily while dragging, and perhaps a wristwatch "busy"
indication overriding one or both those (like the global "busy" mechanism
below).
See the F<examples> directory in the sources for some variously contrived
sample programs.
=head1 WIDGETCURSOR OBJECTS
=head2 Construction
=over
=item C<< $wc = Gtk2::Ex::WidgetCursor->new (key => value, ...) >>
Create and return a new C<WidgetCursor> object. Optional key/value
parameters set initial properties as per C<< Glib::Object->new >> (see
L<Glib::Object>).
$wc = Gtk2::Ex::WidgetCursor->new (widget => $mywidget,
cursor => 'fleur',
active => 1);
Note that C<active> is false by default and the WidgetCursor does nothing to
the widgets until made C<active> by the property or the method call below.
WidgetCursor objects can be applied to unrealized widgets. The cursor
settings take effect if/when the widgets are realized.
=back
=head2 Methods
=over
=item C<< $bool = $wc->active () >>
=item C<< $wc->active ($newval) >>
Get or set the "active" state of C<$wc>. This is the C<active> property.
=item C<< $cursor = $wc->cursor () >>
=item C<< $wc->cursor ($cursor) >>
Get or set the cursor of C<$wc>. This is the C<cursor> property, see
L</PROPERTIES> below for possible values. Eg.
$wc->cursor ('umbrella');
=item C<< @widgets = $wc->widgets () >>
Return a list of the widgets currently in C<$wc>. Eg.
my @array = $wc->widgets;
or if you know you're only acting on one widget then say
my ($widget) = $wc->widgets;
=item C<< $wc->add_widgets ($widget, $widget, ...) >>
lib/Gtk2/Ex/WidgetCursor.pm view on Meta::CPAN
pixels set" pixmap cursor.
=item *
A C<Gtk2::Gdk::Cursor> object.
If your program uses multiple displays then remember the cursor object must
be on the same display (ie. C<Gtk2::Gdk::Display>) as the widget(s). If you
have more than one widget then they must be all on the same display in this
case. (For a named cursor they don't have to be.)
=item *
C<undef> to inherit the parent window's cursor, which may be the default
little pointing arrow or whatever from the root window.
=back
=item C<cursor-name> (string, default C<undef>)
=item C<cursor-object> (C<Gtk2::Gdk::Cursor>, default C<undef>)
The cursor to show in the widgets, as a plain Glib string or object
property. These are designed for use from C<Gtk2::Builder> where the scalar
type C<cursor> property can't be set.
Reading from C<cursor-name> when a cursor object has been set gives the type
nick if it has one, or if it's a pixmap then currently C<undef>.
Reading from C<cursor-object> when a cursor name string has been set gives
C<undef> currently. It'd be possible to make or return the cursor object in
use (or which will be used when realized) but that doesn't seem worth
bothering with as yet.
=item C<active> (boolean, default false)
Whether to apply the cursor to the widgets. This can be set at any time,
including before widgets are added or before they're realized. Widgets
added get the setting when realized.
=item C<priority> (number, default 0)
The priority level of this WidgetCursor among multiple WidgetCursors acting
on a widget.
Higher values are higher priority. A low value (perhaps negative) can act
as a fallback, or a high value can be above other added cursors.
=item C<include-children> (boolean, default false)
Whether to apply the cursor to child widgets of the given widgets too.
Normally the cursor in a child widget overrides its parents (as
C<set_cursor> does at the window level). But with C<include-children>, a
setting in a parent applies to the children too, with priority+newest
applied as usual.
=back
=head1 APPLICATION BUSY
The C<busy> mechanism sets a "watch" cursor on all windows to tell the user
the program is doing CPU-intensive work and might not run the main loop to
draw or interact for a while.
If your busy state isn't CPU-intensive, but instead perhaps a Glib timer or
an I/O watch on a socket, then this is not what you want, it'll turn off too
soon. (Instead simply make a C<WidgetCursor> with a C<"watch"> and turn it
on or off at your start and end points. See F<examples/timebusy.pl> for
that sort of thing.)
=over 4
=item C<< Gtk2::Ex::WidgetCursor->busy () >>
Show the C<"watch"> cursor (a little wristwatch) in all the application's
widget windows (toplevels, dialogs, popups, etc). An idle handler
(C<< Glib::Idle->add >>) removes the watch automatically upon returning to
the main loop.
The X queue is flushed to set the cursor immediately, so the program can go
straight into its work. For example
Gtk2::Ex::WidgetCursor->busy;
foreach my $i (1 .. 1_000_000) {
# do much number crunching
}
If you create new windows within a C<busy> then they too get the busy cursor
(or they're supposed to, something fishy in Gtk 2.20 and maybe 2.18 has
broken it). You can even go busy before creating any windows at all. But
note WidgetCursor doesn't do any extra X flush for new creations; if you
want them to show immediately then you must flush in the usual way.
C<busy> uses a C<WidgetCursor> object as described above and so cooperates
with application uses of that. Priority level 1000 is set to be above other
cursor settings.
=item C<< Gtk2::Ex::WidgetCursor->unbusy () >>
Explicitly remove the watch cursor setup by C<busy> above. The X request
queue is flushed to ensure any cursor change appears immediately. If
C<busy> is not active then do nothing.
It's unlikely you'll need C<unbusy>, because if your program hasn't yet
reached the idle handler in the main loop then it's probably still busy!
But perhaps if most of your work is done then you could unbusy while the
remainder is finishing up.
=back
=head1 INVISIBLE CURSOR
The following invisible cursor is used by WidgetCursor for the
C<"invisible"> cursor and is made available for general use.
=over 4
=item C<< $cursor = Gtk2::Ex::WidgetCursor->invisible_cursor () >>
=item C<< $cursor = Gtk2::Ex::WidgetCursor->invisible_cursor ($target) >>
Return a C<Gtk2::Gdk::Cursor> object which is invisible, ie. displays no
cursor at all. This is the C<blank-cursor> in Gtk 2.16 and up, or for
earlier versions a "no pixels set" cursor as described by
C<gdk_cursor_new()>.
With no arguments (or C<undef>) the cursor is for the default display
C<< Gtk2::Gdk::Display->get_default >>. If your program only uses one
display then that's all you need.
my $cursor = Gtk2::Ex::WidgetCursor->invisible_cursor;
For multiple displays, a cursor is a per-display resource so you must pass a
C<$target>. This can be a C<Gtk2::Gdk::Display>, or anything with a
C<get_display> method, including C<Gtk2::Widget>, C<Gtk2::Gdk::Window>,
C<Gtk2::Gdk::Drawable>, another C<Gtk2::Gdk::Cursor>, etc.
my $cursor = Gtk2::Ex::WidgetCursor->invisible_cursor($widget);
When passing a widget, note the display comes from its toplevel
C<Gtk2::Window> parent and until added as a child somewhere under a toplevel
window its C<get_display> is the default display and C<invisible_cursor>
will give a cursor for that display.
The invisible cursor is cached against the display so repeated calls don't
make a new one every time.
=back
Gtk had its own "no pixels set" cursor constructor code in C<GtkEntry> and
C<GtkTextView> prior to "blank-cursor" but didn't make it available to
applications.
=head1 BUILDABLE
WidgetCursor is a C<Glib::Object> and can be created by C<Gtk2::Builder> in
the usual way. Each is a separate toplevel object and the C<widget>
property sets what it should act on. The C<add-widget> pseudo-property
allows multiple widgets to be set.
<object class="Gtk2__Ex__WidgetCursor" id="wcursor">
<property name="widget">mywidget</property>
<property name="active">1</property>
</object>
See F<examples/builder.pl> and F<examples/builder-add.pl> for complete
( run in 1.253 second using v1.01-cache-2.11-cpan-39bf76dae61 )