| 1 | # Copyright 2010, 2011, 2012 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Glib-Ex-ConnectProperties. | ||
| 4 | # | ||
| 5 | # Glib-Ex-ConnectProperties 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 | # Glib-Ex-ConnectProperties is distributed in the hope that it will be | ||
| 11 | # useful, 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 Glib-Ex-ConnectProperties. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | |||
| 19 | package Glib::Ex::ConnectProperties::Element::response_sensitive; | ||
| 20 | use 5.008; | ||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | use Carp; | ||
| 24 | use Glib; | ||
| 25 | use Gtk2; | ||
| 26 | use base 'Glib::Ex::ConnectProperties::Element'; | ||
| 27 | |||
| 28 | our $VERSION = 20; | ||
| 29 | |||
| 30 | # uncomment this to run the ### lines | ||
| 31 | #use Smart::Comments; | ||
| 32 | |||
| 33 | |||
| 34 | # either method name "get_widget_for_response" or a fallback subr | ||
| 35 | # | ||
| 36 | my $get_widget_for_response | ||
| 37 | = (Gtk2::Dialog->can('get_widget_for_response') # new in Gtk 2.20 | ||
| 38 | ? 'get_widget_for_response' | ||
| 39 | : do { | ||
| 40 | require List::Util; | ||
| 41 | sub { | ||
| 42 | my ($dialog, $response) = @_; | ||
| 43 | return List::Util::first {$dialog->get_response_for_widget($_) | ||
| 44 | eq $response} | ||
| 45 | $dialog->get_action_area->get_children; | ||
| 46 | } | ||
| 47 | }); | ||
| 48 | |||
| 49 | my $pspec = Glib::ParamSpec->boolean ('sensitive', | ||
| 50 | 'sensitive', | ||
| 51 | '', # blurb | ||
| 52 | 1, # default | ||
| 53 | Glib::G_PARAM_READWRITE); | ||
| 54 | my $pspec_writeonly = Glib::ParamSpec->boolean ('sensitive', | ||
| 55 | 'sensitive', | ||
| 56 | '', # blurb | ||
| 57 | 1, # default | ||
| 58 | 'writable'); | ||
| 59 | |||
| 60 | # cf g_enum_get_value_by_nick | ||
| 61 | # sub _enum_get_value_by_nick { | ||
| 62 | # my ($enum_class, $nick) = @_; | ||
| 63 | # return List::Util::first {$nick eq $_->{'nick'}} | ||
| 64 | # Glib::Type->list_values($enum_class); | ||
| 65 | # } | ||
| 66 | # sub _enum_get_value_by_name { | ||
| 67 | # my ($enum_class, $name) = @_; | ||
| 68 | # return List::Util::first {$name eq $_->{'name'}} | ||
| 69 | # Glib::Type->list_values($enum_class); | ||
| 70 | # } | ||
| 71 | # return info hash { nick=>,name=>,value=> } representing GEnumValue, or undef | ||
| 72 | sub _enum_get_value_by_perl { | ||
| 73 | my ($enum_class, $str) = @_; | ||
| 74 | return List::Util::first {$str eq $_->{'nick'} | ||
| 75 | || $str eq $_->{'name'}} | ||
| 76 | Glib::Type->list_values($enum_class); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub find_property { | ||
| 80 | my ($self) = @_; | ||
| 81 | ### response-sensitive find_property(): $self->{'pname'} | ||
| 82 | ### can(get_response_for_widget): $self->{'object'}->can('get_response_for_widget') | ||
| 83 | my $pname = $self->{'pname'}; | ||
| 84 | return ((_enum_get_value_by_perl('Gtk2::ResponseType',$pname) | ||
| 85 | || $pname =~ /^-?\d+$/) | ||
| 86 | && ($self->{'object'}->can('get_response_for_widget') | ||
| 87 | ? $pspec | ||
| 88 | : $pspec_writeonly)); | ||
| 89 | } | ||
| 90 | |||
| 91 | sub connect_signals { | ||
| 92 | my ($self) = @_; | ||
| 93 | my $pname = $self->{'pname'}; | ||
| 94 | my $button = $self->{'object'}->$get_widget_for_response($pname) | ||
| 95 | || croak "No widget for response $pname"; | ||
| 96 | |||
| 97 | $self->{'ids'} = Glib::Ex::SignalIds->new | ||
| 98 | ($button, | ||
| 99 | $button->signal_connect ('notify::sensitive', | ||
| 100 | \&Glib::Ex::ConnectProperties::_do_read_handler, | ||
| 101 | $self)); | ||
| 102 | } | ||
| 103 | |||
| 104 | sub get_value { | ||
| 105 | my ($self) = @_; | ||
| 106 | ### response-sensitive get_value(): $self->{'pname'} | ||
| 107 | my $button; | ||
| 108 | return (($button = $self->{'object'}->$get_widget_for_response($self->{'pname'})) | ||
| 109 | ? $button->get('sensitive') | ||
| 110 | : 1); | ||
| 111 | } | ||
| 112 | sub set_value { | ||
| 113 | my ($self, $value) = @_; | ||
| 114 | ### response-sensitive set_value(): $self->{'pname'} | ||
| 115 | ### $value | ||
| 116 | $self->{'object'}->set_response_sensitive ($self->{'pname'}, $value); | ||
| 117 | } | ||
| 118 | |||
| 119 | 1; | ||
| 120 | __END__ |