| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # Copyright 2008, 2009, 2010, 2011 Kevin Ryde | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | # This file is part of Chart. | 
| 4 |  |  |  |  |  |  | # | 
| 5 |  |  |  |  |  |  | # Chart is free software; you can redistribute it and/or modify it under the | 
| 6 |  |  |  |  |  |  | # terms of the GNU General Public License as published by the Free Software | 
| 7 |  |  |  |  |  |  | # Foundation; either version 3, or (at your option) any later version. | 
| 8 |  |  |  |  |  |  | # | 
| 9 |  |  |  |  |  |  | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | 
| 10 |  |  |  |  |  |  | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 
| 11 |  |  |  |  |  |  | # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more | 
| 12 |  |  |  |  |  |  | # details. | 
| 13 |  |  |  |  |  |  | # | 
| 14 |  |  |  |  |  |  | # You should have received a copy of the GNU General Public License along | 
| 15 |  |  |  |  |  |  | # with Chart.  If not, see <http://www.gnu.org/licenses/>. | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | package App::Chart::Glib::Ex::MoreUtils; | 
| 18 | 2 |  |  | 2 |  | 471 | use 5.008; | 
|  | 2 |  |  |  |  | 7 |  | 
| 19 | 2 |  |  | 2 |  | 9 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 40 |  | 
| 20 | 2 |  |  | 2 |  | 10 | use warnings; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 51 |  | 
| 21 | 2 |  |  | 2 |  | 314 | use Glib; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | use Scalar::Util; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | use base 'Exporter'; | 
| 25 |  |  |  |  |  |  | our @EXPORT_OK = qw(ref_weak lang_select); | 
| 26 |  |  |  |  |  |  | our %EXPORT_TAGS = (all => \@EXPORT_OK); | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | sub ref_weak { | 
| 29 |  |  |  |  |  |  | my ($obj) = @_; | 
| 30 |  |  |  |  |  |  | Scalar::Util::weaken ($obj); | 
| 31 |  |  |  |  |  |  | return \$obj; | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | sub lang_select { | 
| 35 |  |  |  |  |  |  | my %choices = @_; | 
| 36 |  |  |  |  |  |  | my $default = $_[1]; | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | foreach my $lang (Glib::get_language_names()) { | 
| 39 |  |  |  |  |  |  | if (exists $choices{$lang}) { | 
| 40 |  |  |  |  |  |  | return $choices{$lang}; | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  | return $default; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | 1; | 
| 47 |  |  |  |  |  |  | __END__ | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =for stopwords Ryde userdata | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | =head1 NAME | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | App::Chart::Glib::Ex::MoreUtils -- more Glib utility functions | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | use App::Chart::Glib::Ex::MoreUtils; | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | =head1 FUNCTIONS | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | =over 4 | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | =item C<< App::Chart::Glib::Ex::MoreUtils::ref_weak ($obj) >> | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | Return a reference to a weak reference to C<$obj>.  This is good for the | 
| 66 |  |  |  |  |  |  | "userdata" in signal connections etc when you want some weakening so you | 
| 67 |  |  |  |  |  |  | don't keep C<$obj> alive forever due to the connection.  For example, | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | $model->signal_connect (row_deleted, \&deleted_handler, | 
| 70 |  |  |  |  |  |  | App::Chart::Glib::Ex::MoreUtils::ref_weak($self)); | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | sub deleted_handler { | 
| 73 |  |  |  |  |  |  | my ($model, $path, $ref_weak_self) = @_; | 
| 74 |  |  |  |  |  |  | my $self = $$ref_weak_self || return; | 
| 75 |  |  |  |  |  |  | ... | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | =item C<< App::Chart::Glib::Ex::MoreUtils::lang_select ($lang => $value, ...) >> | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | Choose a value according to the user's preferred language.  Each C<$lang> | 
| 81 |  |  |  |  |  |  | argument is a two-letter language code like "en".  The C<$value> arguments | 
| 82 |  |  |  |  |  |  | are any scalars to return.  For example | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | App::Chart::Glib::Ex::MoreUtils::lang_select (de => 'deutsch', | 
| 85 |  |  |  |  |  |  | en => 'english') | 
| 86 |  |  |  |  |  |  | # returns either 'deutsch' or 'english' | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | The user's preferred language is taken from C<Glib::get_language_names> (see | 
| 89 |  |  |  |  |  |  | L<Glib::Utils>).  If none of the given C<$lang> values are among the user's | 
| 90 |  |  |  |  |  |  | preferences then the first in the call is used as the default and its | 
| 91 |  |  |  |  |  |  | C<$value> returned. | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | This is meant for selecting semi-technical things from a fixed set of | 
| 94 |  |  |  |  |  |  | possibilities within the program code, for example different URLs for the | 
| 95 |  |  |  |  |  |  | English or German version of some web page which will be parsed.  If it was | 
| 96 |  |  |  |  |  |  | in a F<.mo> file (per C<Locale::TextDomain>) the choice would be locked down | 
| 97 |  |  |  |  |  |  | by the translator, but C<lang_select> allows a user preference. | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | =back | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | L<Glib::Utils> | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | =cut |