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.
lib/Gtk2/Ex/WidgetCursor.pm view on Meta::CPAN
# in $wobj->{'cursor'}
sub _resolve_cursor {
my ($wobj, $widget) = @_;
my $cursor = $wobj->{'cursor'};
if (! defined $cursor || ref $cursor) {
# undef or cursor object
return $cursor;
} elsif ($cursor eq 'invisible') {
# call through $wobj in case of subclassing
return $wobj->invisible_cursor ($widget);
} else {
# string cursor name -- only ever call to resolve here when widget is
# realized, so get_display() isn't undef
if ($widget->can('get_display')) {
# gtk 2.2 up
my $display = $widget->get_display;
return Gtk2::Gdk::Cursor->new_for_display ($display, $cursor);
} else {
# gtk 2.0.x
return Gtk2::Gdk::Cursor->new ($cursor);
}
}
}
# Return $widget and all its contained children, grandchildren, etc.
# Iterative avoids deep recursion warning for the unlikely case of nesting
# beyond 100 deep.
#
sub _container_recursively {
my @pending = @_;
my @ret;
while (@pending) {
my $widget = pop @pending;
push @ret, $widget;
if (my $func = $widget->can('get_children')) {
push @pending, $widget->$func;
}
}
return @ret;
}
#------------------------------------------------------------------------------
# operative windows hacks
#
# $widget->Gtk2_Ex_WidgetCursor_windows() returns a list of windows in
# $widget to act on, with hacks to pickup multiple windows on core classes.
#
# $widget->Gtk2_Ex_WidgetCursor_hack_restore() returns ($win, $cursor).
# $cursor is a string cursor name to put back on $win when there's no more
# WidgetCursor objects. Or the return is an empty list or $win undef when
# nothing to hack (in which case all windows go back to "undef" cursor).
#
# default to operate on $widget->window alone
*Gtk2::Widget::Gtk2_Ex_WidgetCursor_windows = \&Gtk2::Widget::window;
sub Gtk2::Widget::Gtk2_Ex_WidgetCursor_hack_restore { return (); }
# GtkEventBox under a GtkComboBox popup window has a 'top-left-arrow'. It
# gets overridden by a special case in the recursive updates above, and
# hack_restore() here puts it back.
#
sub Gtk2::EventBox::Gtk2_Ex_WidgetCursor_hack_restore {
my ($widget) = @_;
return _widget_is_combo_eventbox($widget)
&& ($widget->window, 'top-left-arrow');
}
# GtkTextView operate on 'text' subwindow to override its insertion point
# cursor there, plus the main 'widget' window to cover the entire widget
# extent. The 'text' subwindow insertion point is supposed to be on when
# the widget is sensitive, so hack_restore() that.
#
sub Gtk2::TextView::Gtk2_Ex_WidgetCursor_windows {
my ($widget) = @_;
return ($widget->get_window ('widget'),
$widget->get_window ('text'));
}
sub Gtk2::TextView::Gtk2_Ex_WidgetCursor_hack_restore {
my ($widget) = @_;
return $widget->sensitive && ($widget->get_window('text'), 'xterm');
}
# GtkEntry's extra subwindow is included here. And when sensitive it should
# be put back to an insertion point. For a bit of safety use list context
# etc to allow for no subwindows, since it's undocumented.
#
# In Gtk 2.14 the SpinButton sub-class has the arrow panel as a subwindow
# too (instead of an overlay in Gtk 2.12 and earlier). So look for the
# smaller height one among multiple subwindows.
#
sub Gtk2::Entry::Gtk2_Ex_WidgetCursor_windows {
my ($widget) = @_;
my $win = $widget->window || return; # if unrealized
return ($win, $win->get_children);
}
sub Gtk2::Entry::Gtk2_Ex_WidgetCursor_hack_restore {
my ($widget) = @_;
$widget->sensitive or return;
my $win = $widget->window || return; # if unrealized
my @children = $win->get_children;
# by increasing height
@children = sort {($a->get_size)[1] <=> ($b->get_size)[1]} @children;
return ($children[0], 'xterm');
}
# GtkSpinButton's extra "panel" overlay window either as a "sibling" (which
# also finds the main window) for Gtk 2.12 or in the get_children() for Gtk
# 2.13; plus the GtkEntry subwindow as per GtkEntry above. hack_restore()
# inherited from GtkEntry above.
#
sub Gtk2::SpinButton::Gtk2_Ex_WidgetCursor_windows {
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.
lib/Gtk2/Ex/WidgetCursor.pm view on Meta::CPAN
=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.
( run in 0.888 second using v1.01-cache-2.11-cpan-364913b4093 )