| 1 | package Gapp::Gtk2::TimeEntry; | ||
| 2 | { | ||
| 3 | $Gapp::Gtk2::TimeEntry::VERSION = '0.60'; | ||
| 4 | } | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Carp; | ||
| 8 | |||
| 9 | use Gtk2; | ||
| 10 | |||
| 11 | use Glib qw(TRUE FALSE); | ||
| 12 | |||
| 13 | use Glib::Object::Subclass | ||
| 14 | Gtk2::Entry::, | ||
| 15 | interfaces => [ 'Gtk2::CellEditable' ], | ||
| 16 | properties => [ | ||
| 17 | Glib::ParamSpec->string( | ||
| 18 | 'value' , | ||
| 19 | 'value' , | ||
| 20 | 'ISO format time string like 13:00' , | ||
| 21 | '' , #default value | ||
| 22 | Glib::G_PARAM_READWRITE | ||
| 23 | ), | ||
| 24 | ], | ||
| 25 | signals => { | ||
| 26 | value_changed => { | ||
| 27 | class_closure => \&_do_value_changed, | ||
| 28 | flags => ['run-first'] , | ||
| 29 | return_type => undef , | ||
| 30 | param_types => [] , | ||
| 31 | }, | ||
| 32 | } | ||
| 33 | ; | ||
| 34 | |||
| 35 | sub INIT_INSTANCE { | ||
| 36 | my $self = shift; | ||
| 37 | $self->signal_connect('key-press-event' => \&_do_key_press_event); | ||
| 38 | $self->signal_connect('focus-out-event' => \&_do_focus_out_event); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub SET_PROPERTY { | ||
| 42 | my ($self, $pspec, $newval) = @_; | ||
| 43 | |||
| 44 | my $pname = $pspec->get_name; | ||
| 45 | |||
| 46 | # handle changes to the value parameter (emit a signal on change) | ||
| 47 | if ($pname eq 'value') { | ||
| 48 | $newval = $self->_parse_input($newval); | ||
| 49 | # set the new value if it is different | ||
| 50 | if (!defined $self->{$pname} && defined $newval || $self->{$pname} ne $newval) { | ||
| 51 | $self->{$pname} = $newval; | ||
| 52 | $self->signal_emit('value-changed'); | ||
| 53 | } | ||
| 54 | else { | ||
| 55 | # update the display | ||
| 56 | $self->set_text($self->_format_output($self->get_value)); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | $self->{$pname} = $newval; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | sub get_value { | ||
| 65 | my $self = shift; | ||
| 66 | $self->get('value'); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub set_value { | ||
| 70 | my $self = shift; | ||
| 71 | my $new_value = shift; | ||
| 72 | $self->set('value', $new_value); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub set_now { | ||
| 76 | my $self = shift; | ||
| 77 | my ($hour, $minute) = (localtime time)[2,1]; | ||
| 78 | |||
| 79 | my $value = sprintf '%02d:%02d', $hour, $minute; | ||
| 80 | |||
| 81 | $self->set_value( $value ); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _do_value_changed { | ||
| 85 | my $self = shift; | ||
| 86 | my $value = $self->get_value; | ||
| 87 | $self->set_text($self->_format_output($self->get_value)); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub _do_focus_out_event { | ||
| 91 | my $self = shift; | ||
| 92 | $self->set_value($self->get_text); | ||
| 93 | return FALSE; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub _do_key_press_event { | ||
| 97 | my $self = shift; | ||
| 98 | my $key = shift; | ||
| 99 | my $key_val = $key->keyval; | ||
| 100 | |||
| 101 | # entry pressed, parse input | ||
| 102 | if ($key_val == 65293) { | ||
| 103 | $self->set_value($self->get_text); | ||
| 104 | return FALSE; | ||
| 105 | } | ||
| 106 | # laft arrow key pressed | ||
| 107 | elsif ($key_val >= 65361 && $key_val <= 65364) { | ||
| 108 | $self->set_value($self->get_text); | ||
| 109 | return $self->_do_key_left if $key_val == 65361; | ||
| 110 | return $self->_do_key_right if $key_val == 65363; | ||
| 111 | return $self->_do_key_up if $key_val == 65362; | ||
| 112 | return $self->_do_key_down if $key_val == 65364; | ||
| 113 | } | ||
| 114 | # pass everything else on | ||
| 115 | else { | ||
| 116 | return FALSE; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | sub _do_key_left { | ||
| 121 | my $self = shift; | ||
| 122 | my $selected = $self->get_selected_component; | ||
| 123 | |||
| 124 | if (! $selected) { | ||
| 125 | return FALSE if $self->get_position == 0; | ||
| 126 | $self->_select_closest_component('left'); | ||
| 127 | return TRUE; | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | if ($selected eq 'all' ) { $self->set_selected_component('meridiem')} | ||
| 131 | elsif ($selected eq 'hours' ) { return FALSE; } | ||
| 132 | elsif ($selected eq 'minutes' ) { $self->set_selected_component('hours') } | ||
| 133 | elsif ($selected eq 'meridiem') { $self->set_selected_component('minutes') } | ||
| 134 | return TRUE; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | sub _do_key_right { | ||
| 139 | my $self = shift; | ||
| 140 | my $selected = $self->get_selected_component; | ||
| 141 | $self->_select_closest_component('right') and return TRUE if !$selected; | ||
| 142 | |||
| 143 | if (! $selected) { | ||
| 144 | return FALSE if $self->get_position == 0; | ||
| 145 | $self->_select_closest_component('left'); | ||
| 146 | return TRUE; | ||
| 147 | } | ||
| 148 | else { | ||
| 149 | if ($selected eq 'all' ) { $self->set_selected_component('hours') } | ||
| 150 | elsif ($selected eq 'hours' ) { $self->set_selected_component('minutes') } | ||
| 151 | elsif ($selected eq 'minutes' ) { $self->set_selected_component('meridiem') } | ||
| 152 | elsif ($selected eq 'meridiem') { return FALSE; } | ||
| 153 | return TRUE; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | sub _do_key_up { | ||
| 158 | my $self = shift; | ||
| 159 | my $selected = $self->get_selected_component; | ||
| 160 | $self->_select_closest_component('up') and return TRUE unless $selected; | ||
| 161 | |||
| 162 | my ($hours, $minutes) = split /:/, $self->get_value; | ||
| 163 | |||
| 164 | if ($selected eq 'hours') { | ||
| 165 | $hours++; | ||
| 166 | } | ||
| 167 | elsif ($selected eq 'minutes') { | ||
| 168 | $minutes++; | ||
| 169 | } | ||
| 170 | elsif ($selected eq 'meridiem') { | ||
| 171 | $hours+=12; | ||
| 172 | } | ||
| 173 | elsif ($selected eq 'all') { | ||
| 174 | $minutes+=15; | ||
| 175 | } | ||
| 176 | |||
| 177 | my $new_value = join ':', $hours, $minutes; | ||
| 178 | $self->set_value($new_value); | ||
| 179 | $self->set_selected_component($selected); | ||
| 180 | |||
| 181 | return TRUE; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub _do_key_down { | ||
| 185 | my $self = shift; | ||
| 186 | my $selected = $self->get_selected_component; | ||
| 187 | $self->_select_closest_component('down') and return TRUE unless $selected; | ||
| 188 | |||
| 189 | my ($hours, $minutes, $seconds) = split /:/, $self->get_value; | ||
| 190 | |||
| 191 | if ($selected eq 'hours') { | ||
| 192 | $hours--; | ||
| 193 | } | ||
| 194 | elsif ($selected eq 'minutes') { | ||
| 195 | $minutes--; | ||
| 196 | } | ||
| 197 | elsif ($selected eq 'meridiem') { | ||
| 198 | $hours-=12; | ||
| 199 | } | ||
| 200 | elsif ($selected eq 'all') { | ||
| 201 | $minutes-=15; | ||
| 202 | } | ||
| 203 | |||
| 204 | if ($minutes < 0) { | ||
| 205 | # get the absolute value | ||
| 206 | my $absolute_minutes = $minutes * -1; | ||
| 207 | |||
| 208 | # hours to subtract | ||
| 209 | my $delta_hours = int ($absolute_minutes / 60) + 1; | ||
| 210 | $hours -= $delta_hours; | ||
| 211 | |||
| 212 | # minutes to subtract (from 60) | ||
| 213 | my $delta_minutes = $absolute_minutes % 60; | ||
| 214 | $minutes = 60 - $delta_minutes; | ||
| 215 | } | ||
| 216 | if ($hours < 0) { | ||
| 217 | # get the absolute value | ||
| 218 | my $absolute_hours = $hours * -1; | ||
| 219 | my $delta_hours = $absolute_hours % 24; | ||
| 220 | $hours = 24 - $delta_hours; | ||
| 221 | } | ||
| 222 | |||
| 223 | my $new_value = sprintf '%02d:%02d', $hours, $minutes; | ||
| 224 | $self->set_value($new_value); | ||
| 225 | $self->set_selected_component($selected); | ||
| 226 | return TRUE; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub _format_output { | ||
| 230 | my $self = shift; | ||
| 231 | my $value = shift; | ||
| 232 | |||
| 233 | # throw exception unless passed a time in a valid format | ||
| 234 | carp q[Usage is $self->_format_output($value) where ] . | ||
| 235 | q[$value is a valid time in the format HH:MM:SS: ] . | ||
| 236 | qq[you supplied ($value)] | ||
| 237 | unless defined $value && $value !~ /^\d\d:\d\d:\s\s$/; | ||
| 238 | |||
| 239 | # return empty string if no value | ||
| 240 | return '' if $value eq ''; | ||
| 241 | |||
| 242 | # format the value if there is one | ||
| 243 | my ($hours, $minutes, $seconds) = split /:/, $value; | ||
| 244 | my $ampm; | ||
| 245 | |||
| 246 | if ($hours == 0) { | ||
| 247 | $hours = 12; | ||
| 248 | $ampm = 'AM'; | ||
| 249 | } | ||
| 250 | elsif ($hours == 12) { | ||
| 251 | $ampm = 'PM'; | ||
| 252 | } | ||
| 253 | elsif ($hours > 12) { | ||
| 254 | $hours -= 12; | ||
| 255 | $ampm = 'PM'; | ||
| 256 | } | ||
| 257 | else { | ||
| 258 | $ampm = 'AM'; | ||
| 259 | } | ||
| 260 | |||
| 261 | |||
| 262 | return sprintf qq[%02d:%02d $ampm], $hours, $minutes; | ||
| 263 | |||
| 264 | } | ||
| 265 | |||
| 266 | sub get_selected_component { | ||
| 267 | my $self = shift; | ||
| 268 | |||
| 269 | my ($select_start, $select_end) = $self->get_selection_bounds; | ||
| 270 | $select_start = 0 unless $select_start; | ||
| 271 | $select_end = 0 unless $select_end; | ||
| 272 | |||
| 273 | my $selected = $select_end - $select_start; | ||
| 274 | return unless $selected == 2 || 8; | ||
| 275 | |||
| 276 | if ($select_start == 0 && $select_end == 2) { | ||
| 277 | return 'hours'; | ||
| 278 | } | ||
| 279 | elsif ($select_start == 3 && $select_end == 5) { | ||
| 280 | return 'minutes'; | ||
| 281 | } | ||
| 282 | elsif ($select_start == 6 && $select_end == 8) { | ||
| 283 | return 'meridiem'; | ||
| 284 | } | ||
| 285 | elsif ($select_start == 0 && $select_end == 8) { | ||
| 286 | return 'all'; | ||
| 287 | } | ||
| 288 | else { | ||
| 289 | return undef; | ||
| 290 | } | ||
| 291 | } | ||
| 292 | |||
| 293 | sub set_selected_component { | ||
| 294 | my $self = shift; | ||
| 295 | my $field = shift; | ||
| 296 | |||
| 297 | # throw exception if $field var is not valid | ||
| 298 | confess q[Usage is $self->set_selected_component($field) where field is one of ] . | ||
| 299 | q[hours, minutes, ampm, meridiem, all, or an empty string: ] . | ||
| 300 | qq[you supplied($field)] | ||
| 301 | and return unless $field =~ /(hours|minutes|ampm|meridiem|all|none)/ | ||
| 302 | || $field eq ''; | ||
| 303 | |||
| 304 | if ($field eq 'hours') { | ||
| 305 | $self->select_region(0,2); | ||
| 306 | } | ||
| 307 | elsif ($field eq 'minutes') { | ||
| 308 | $self->select_region(3,5); | ||
| 309 | } | ||
| 310 | elsif ($field =~ /ampm|meridiem/) { | ||
| 311 | $self->select_region(6,8); | ||
| 312 | } | ||
| 313 | elsif ($field eq 'all') { | ||
| 314 | $self->select_region(0,8); | ||
| 315 | } | ||
| 316 | elsif ($field eq 'none' || $field eq '') { | ||
| 317 | $self->select_region(0,0); | ||
| 318 | } | ||
| 319 | else { | ||
| 320 | confess qq[Invalid argument passed: $field]; | ||
| 321 | } | ||
| 322 | } | ||
| 323 | |||
| 324 | sub _select_closest_component { | ||
| 325 | my $self = shift; | ||
| 326 | my $direction = shift; | ||
| 327 | my $cursor = $self->get_position; | ||
| 328 | |||
| 329 | if ($cursor == 0 || $cursor == 1) { | ||
| 330 | $self->set_selected_component('hours'); | ||
| 331 | } | ||
| 332 | elsif ($cursor == 2 && $direction ne 'right') { | ||
| 333 | $self->set_selected_component('hours'); | ||
| 334 | } | ||
| 335 | elsif ($cursor == 2 && $direction eq 'right') { | ||
| 336 | $self->set_selected_component('minutes'); | ||
| 337 | } | ||
| 338 | elsif ($cursor == 3 && $direction eq 'left') { | ||
| 339 | $self->set_selected_component('hours'); | ||
| 340 | } | ||
| 341 | elsif ($cursor == 3 && $direction ne 'left') { | ||
| 342 | $self->set_selected_component('minutes'); | ||
| 343 | } | ||
| 344 | elsif ($cursor == 4) { | ||
| 345 | $self->set_selected_component('minutes'); | ||
| 346 | } | ||
| 347 | elsif ($cursor == 5 && $direction ne 'right') { | ||
| 348 | $self->set_selected_component('minutes'); | ||
| 349 | } | ||
| 350 | elsif ($cursor == 5 && $direction eq 'right') { | ||
| 351 | $self->set_selected_component('meridiem'); | ||
| 352 | } | ||
| 353 | elsif ($cursor == 6 && $direction eq 'left') { | ||
| 354 | $self->set_selected_component('minutes'); | ||
| 355 | } | ||
| 356 | elsif ($cursor == 6 && $direction ne 'left') { | ||
| 357 | $self->set_selected_component('meridiem'); | ||
| 358 | } | ||
| 359 | elsif ($cursor == 7 || $cursor == 8) { | ||
| 360 | $self->set_selected_component('meridiem'); | ||
| 361 | } | ||
| 362 | |||
| 363 | return TRUE; | ||
| 364 | } | ||
| 365 | |||
| 366 | sub _parse_input { | ||
| 367 | my $self = shift; | ||
| 368 | my $value = shift || ''; | ||
| 369 | $value =~ s/^\s+//; | ||
| 370 | $value =~ s/\s+$//; | ||
| 371 | return '' unless $value; | ||
| 372 | |||
| 373 | my ($h, $m, $s); | ||
| 374 | if ($value =~ /^(\d{1,2})$/) { | ||
| 375 | $h = $1; | ||
| 376 | $m = 0; | ||
| 377 | $s = 0; | ||
| 378 | } | ||
| 379 | elsif ($value =~ /^([012]?[0-9]):?([0-5][0-9]):?([0-5][0-9])?$/) { | ||
| 380 | $h = $1; | ||
| 381 | $m = $2; | ||
| 382 | $s = $3 || 0; | ||
| 383 | } | ||
| 384 | elsif ($value =~ /^([012]?[0-9]):?([0-9][0-9])?\s*(am|pm)?$/i) { | ||
| 385 | $h = $1; | ||
| 386 | $m = $2 || 0; | ||
| 387 | $s = 0; | ||
| 388 | $h -= 12 if $h == 12 && lc $3 eq 'am'; | ||
| 389 | $h += 12 if $h <12 && lc $3 eq 'pm'; | ||
| 390 | } | ||
| 391 | elsif ($value eq '') { | ||
| 392 | return ''; | ||
| 393 | } | ||
| 394 | else { | ||
| 395 | return $self->get_value; | ||
| 396 | } | ||
| 397 | |||
| 398 | if ($s > 59) { | ||
| 399 | $m++; | ||
| 400 | $s = $s % 60; | ||
| 401 | } | ||
| 402 | |||
| 403 | if ($m > 59) { | ||
| 404 | $h++; | ||
| 405 | $m = $m % 60; | ||
| 406 | } | ||
| 407 | |||
| 408 | # roll over 24 hours to the eqivelent time | ||
| 409 | if ($h >= 24) { | ||
| 410 | $h -= int($h / 24) * 24; | ||
| 411 | } | ||
| 412 | |||
| 413 | my $parsed = sprintf '%02d:%02d:%02d', $h, $m, $s; | ||
| 414 | return $parsed; | ||
| 415 | } | ||
| 416 | |||
| 417 | |||
| 418 | 1; | ||
| 419 | |||
| 420 | |||
| 421 | __END__ |