| 1 | # Copyright 2008, 2009, 2010, 2013, 2019 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Gtk2-Ex-DateSpinner. | ||
| 4 | # | ||
| 5 | # Gtk2-Ex-DateSpinner 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-DateSpinner is distributed in the hope that it will be useful, but | ||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 12 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 13 | # for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License along | ||
| 16 | # with Gtk2-Ex-DateSpinner. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | package Gtk2::Ex::DateSpinner; | ||
| 19 | 3 | ▶ | use 5.008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 3 | ▶ | use Date::Calc; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | 0 | ▶ | use Gtk2; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 24 | use Glib::Ex::ObjectBits 'set_property_maybe'; | ||
| 25 | use POSIX::Wide; | ||
| 26 | use Locale::Messages 1.16 # 1.16 for turn_utf_8_on() | ||
| 27 | 'dgettext', 'turn_utf_8_on'; | ||
| 28 | |||
| 29 | |||
| 30 | our $VERSION = 10; | ||
| 31 | |||
| 32 | # uncomment this to run the ### lines | ||
| 33 | # use Smart::Comments; | ||
| 34 | |||
| 35 | use Glib::Object::Subclass | ||
| 36 | 'Gtk2::HBox', | ||
| 37 | properties => [Glib::ParamSpec->string | ||
| 38 | ('value', | ||
| 39 | 'Date value', | ||
| 40 | 'ISO format date string like 2008-07-25.', | ||
| 41 | '2000-01-01', | ||
| 42 | Glib::G_PARAM_READWRITE), | ||
| 43 | ]; | ||
| 44 | |||
| 45 | sub INIT_INSTANCE { | ||
| 46 | my ($self) = @_; | ||
| 47 | ### DateSpinner INIT_INSTANCE() ... | ||
| 48 | |||
| 49 | $self->{'value'} = '2000-01-01'; | ||
| 50 | |||
| 51 | my $year_adj = Gtk2::Adjustment->new (2000, # initial | ||
| 52 | 0, 9999, # range | ||
| 53 | 1, # step increment | ||
| 54 | 10, # page_increment | ||
| 55 | 0); # page_size (not applicable) | ||
| 56 | my $year = $self->{'year'} = Gtk2::SpinButton->new ($year_adj, 1, 0); | ||
| 57 | $year->signal_connect (insert_text => \&_do_spin_insert_text); | ||
| 58 | $year->show; | ||
| 59 | $self->pack_start ($year, 0,0,0); | ||
| 60 | |||
| 61 | my $month_adj = Gtk2::Adjustment->new (1, # initial | ||
| 62 | 0, 99, # range | ||
| 63 | 1, # step_increment | ||
| 64 | 1, # page_increment | ||
| 65 | 0); # page_size (not applicable) | ||
| 66 | my $month = $self->{'month'} = Gtk2::SpinButton->new ($month_adj, 1, 0); | ||
| 67 | $month->signal_connect (insert_text => \&_do_spin_insert_text); | ||
| 68 | $month->show; | ||
| 69 | $self->pack_start ($month, 0,0,0); | ||
| 70 | |||
| 71 | my $day_adj = Gtk2::Adjustment->new (1, # initial | ||
| 72 | 0, 99, # range | ||
| 73 | 1, # step_increment | ||
| 74 | 1, # page_increment | ||
| 75 | 0); # page_size (not applicable) | ||
| 76 | my $day = $self->{'day'} = Gtk2::SpinButton->new ($day_adj, 1, 0); | ||
| 77 | $day->signal_connect (insert_text => \&_do_spin_insert_text); | ||
| 78 | $day->show; | ||
| 79 | $self->pack_start ($day, 0,0,0); | ||
| 80 | |||
| 81 | # translations from Gtk itself | ||
| 82 | # eg. from /usr/share/locale/de/LC_MESSAGES/gtk20-properties.mo | ||
| 83 | # tooltip-text new in Gtk 2.10 | ||
| 84 | set_property_maybe ($year, tooltip_text => | ||
| 85 | turn_utf_8_on(dgettext('gtk20-properties','Year'))); | ||
| 86 | set_property_maybe ($month, tooltip_text => | ||
| 87 | turn_utf_8_on(dgettext('gtk20-properties','Month'))); | ||
| 88 | set_property_maybe ($day, tooltip_text => | ||
| 89 | turn_utf_8_on(dgettext('gtk20-properties','Day'))); | ||
| 90 | |||
| 91 | my $dow = $self->{'dayofweek_label'} | ||
| 92 | = Gtk2::Label->new (_ymd_to_wday_str(2000,1,1)); | ||
| 93 | $dow->show; | ||
| 94 | $self->pack_start ($dow, 0,0,0); | ||
| 95 | |||
| 96 | $year->signal_connect (value_changed => \&_spin_value_changed); | ||
| 97 | $month->signal_connect (value_changed => \&_spin_value_changed); | ||
| 98 | $day->signal_connect (value_changed => \&_spin_value_changed); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub SET_PROPERTY { | ||
| 102 | my ($self, $pspec, $newval) = @_; | ||
| 103 | ### DateSpinner SET_PROPERTY() ... | ||
| 104 | |||
| 105 | my $pname = $pspec->get_name; | ||
| 106 | $self->{$pname} = $newval; # per default GET_PROPERTY | ||
| 107 | |||
| 108 | if ($pname eq 'value') { | ||
| 109 | my ($year, $month, $day) = split /-/, $newval; | ||
| 110 | $self->{'year'}->set_value ($year); | ||
| 111 | $self->{'month'}->set_value ($month); | ||
| 112 | $self->{'day'}->set_value ($day); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | sub _do_spin_insert_text { | ||
| 117 | my ($spin, $text, $len) = @_; | ||
| 118 | ### DateSpinner insert: $text | ||
| 119 | $text =~ s/^\s+//; | ||
| 120 | $text =~ s/\s+$//; | ||
| 121 | if ($text =~ /^\d\d\d\d-\d\d-\d\d$/) { | ||
| 122 | my $self = $spin->parent; | ||
| 123 | $self->set (value => $text); | ||
| 124 | $spin->signal_stop_emission_by_name ('insert-text'); | ||
| 125 | } | ||
| 126 | return; | ||
| 127 | } | ||
| 128 | |||
| 129 | # Signal handler for 'value-changed' on the year,month,day SpinButtons. | ||
| 130 | # $spin is one of $self->{'year'},'month', or 'day | ||
| 131 | sub _spin_value_changed { | ||
| 132 | my ($spin) = @_; | ||
| 133 | ### DateSpinner _spin_value_changed() ... | ||
| 134 | my $self = $spin->parent; | ||
| 135 | |||
| 136 | if ($self->{'update_in_progress'}) { return; } | ||
| 137 | local $self->{'update_in_progress'} = 1; | ||
| 138 | |||
| 139 | my $year_spin = $self->{'year'}; | ||
| 140 | my $month_spin = $self->{'month'}; | ||
| 141 | my $day_spin = $self->{'day'}; | ||
| 142 | |||
| 143 | my $year = $year_spin->get_value; | ||
| 144 | my $month = $month_spin->get_value; | ||
| 145 | my $day = $day_spin->get_value; | ||
| 146 | ### DateSpinner update: "$year, $month, $day" | ||
| 147 | |||
| 148 | ($year, $month, $day) = Date::Calc::Add_Delta_YMD | ||
| 149 | (2000, 1, 1, $year-2000, $month-1, $day-1); | ||
| 150 | |||
| 151 | $year_spin->set_value ($year); | ||
| 152 | $month_spin->set_value ($month); | ||
| 153 | $day_spin->set_value ($day); | ||
| 154 | |||
| 155 | $self->{'dayofweek_label'}->set_text (_ymd_to_wday_str($year,$month,$day)); | ||
| 156 | |||
| 157 | my $value = sprintf ('%04d-%02d-%02d', $year, $month, $day); | ||
| 158 | ### new value: $value | ||
| 159 | ### old value: $self->{'value'} | ||
| 160 | if ($value ne $self->{'value'}) { | ||
| 161 | ### notify ... | ||
| 162 | $self->{'value'} = $value; | ||
| 163 | $self->notify('value'); | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | # $year is 2000 etc, $month is 1 to 12, $day is 1 to 31. | ||
| 168 | # Return a wide-char string which is the short name of the day of the week | ||
| 169 | # to show, such as " Fri ". | ||
| 170 | # | ||
| 171 | # Prefer strftime() over Date::Calc's localized names, on the basis that | ||
| 172 | # strftime() will probably know more languages, and setlocale() is done | ||
| 173 | # automatically when perl starts. | ||
| 174 | # | ||
| 175 | # In the past (circa 2013) strftime() had looked only at wday to make "%a" | ||
| 176 | # day-of-week. Now (circa 2019) it seems to look at mday,month,year and | ||
| 177 | # ignore the wday field. Is that right? In any case, the code below makes | ||
| 178 | # a set of consistent fields based on midday Mon 1 Jan 1990. | ||
| 179 | # | ||
| 180 | # Don't want to put in the actual $year,$month,$day, since the idea of | ||
| 181 | # Date::Calc is to avoid limitations like Unix epoch 1970..2038 on a 32-bit | ||
| 182 | # system. | ||
| 183 | # | ||
| 184 | # I18N::Langinfo langinfo() interface to nl_langinfo() is another | ||
| 185 | # possibility and should get the same as strftime(). Recent versions of | ||
| 186 | # I18N::Langinfo apparently implement most of it even on systems like MS-DOS | ||
| 187 | # where the C library probably doesn't have a native nl_langinfo(). For now | ||
| 188 | # think strftime() is ok. | ||
| 189 | # | ||
| 190 | sub _ymd_to_wday_str { | ||
| 191 | my ($year,$month,$day) = @_; | ||
| 192 | |||
| 193 | # 1=Monday .. 7=Sunday | ||
| 194 | my $wday = Date::Calc::Day_of_Week ($year, $month, $day); | ||
| 195 | ### $wday | ||
| 196 | |||
| 197 | return POSIX::Wide::strftime(' %a ', | ||
| 198 | 12,0,0, # hour,minute,second | ||
| 199 | $wday,0,90, # mday, month-1, year-1900 | ||
| 200 | $wday%7, # wday 0=Sun, 1=Mon, ... | ||
| 201 | $wday-1, # yday 0 for 1Jan | ||
| 202 | 0); # isdst | ||
| 203 | } | ||
| 204 | |||
| 205 | sub get_value { | ||
| 206 | my ($self) = @_; | ||
| 207 | return $self->{'value'}; | ||
| 208 | } | ||
| 209 | |||
| 210 | sub set_today { | ||
| 211 | my ($self) = @_; | ||
| 212 | my ($year, $month, $day) = Date::Calc::Today(); | ||
| 213 | $self->set (value => sprintf ('%04d-%02d-%02d', $year, $month, $day)); | ||
| 214 | } | ||
| 215 | |||
| 216 | 1; | ||
| 217 | __END__ | ||