Date and Time Entry Boxes




I have made these two Widget, each under Gtk2::Entry. I want to release them for the benefit of the community, but have no experience in making the Installation Package, Man page etc. They also require a lot of work, which I will do soon, but the provide basic functionality now.

The have to be placed under /usr/lib/perl5/site_perl/5.8.4/Gtk2/Ex/

I would appreciate the guidance on the the gurus.

--
Regards,


Nasir.

# Written by A.R. Nasir Qureshi (nasir mitco ws)
#COPYRIGHT & LICENSE
#
#Copyright 2006 A.R. Nasir Qureshi, All Rights Reserved.
#
#This library is free software; you can redistribute it and/or modify it under
#the terms of the GNU Library General Public License as published by the Free
#Software Foundation; either version 2.1 of the License, or (at your option) any
#later version.
#
#This library 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 Library General Public License for more
#details.
#
#You should have received a copy of the GNU Library General Public License along
#with this library; if not, write to the Free Software Foundation, Inc., 59
#Temple Place - Suite 330, Boston, MA  02111-1307  USA.

package Gtk2::Ex::EntryTime;

use strict;
use warnings;
use Carp;
use Gtk2;
use Gtk2::Gdk::Keysyms ;
use Glib qw(TRUE FALSE);

use base 'Gtk2::Entry';
 
sub new {
        my $class = shift;
        # create an entry
        my $self = Gtk2::Entry->new_with_max_length(10);
        # rebless it to an Gtk2::EntryTime
        bless $self, $class;
        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime() ;
        if(length($hour) < 2){$hour = "0$hour";}
        if(length($min) < 2){$min = "0$min";}
        my $time = "$hour:$min" ;
        my $month ;
        my $day ;
        $self->set_text($time) ;
        $self->signal_connect (key_press_event => sub {
                my ($widget, $event) = @_;
                my $keyval = $event->keyval ;
                my $pos = $self->get_property('cursor-position') ;
                my $spos = $self->get_property('selection-bound') ;
                my $mpos ;
                if(abs($pos-$spos) == 5){
                        $mpos = 'C' ;
                } elsif($pos >= 0 && $pos <= 2){
                        $mpos = 'H' ;
                } elsif ($pos >= 3 && $pos <= 5){
                        $mpos = 'M' ;
                }
                my $text = $self->get_text ;
                my $hour = substr($self->get_text,0,2) ;
                my $min = substr($self->get_text,3,2) ;
                my $num = $self->_GetNumber($keyval) ;

                return (TRUE) if($keyval >= 65 && $keyval <= 90) ;    # A - Z
                return (TRUE) if($keyval >= 97 && $keyval <= 122) ;   # a - z
                return (TRUE) if($keyval >= 33 && $keyval <= 47) ;    # special
                return (TRUE) if($keyval >= 59 && $keyval <= 64) ;    # special
                return (TRUE) if($keyval >= 91 && $keyval <= 96) ;    # special
                return (TRUE) if($keyval >= 123 && $keyval <= 126) ;  # special
                if($keyval == 65293 || $keyval == 65421){             # Enter Keys
                        $self->CheckTime ;
                        return (FALSE) ;
                }
                if($keyval == 65363 || $keyval == 58){                # Right Arrow Key or ':' key
                        if($mpos eq 'H'){
                                $self->select_region(3,5) ;
                                $self->CheckTime ;
                                return (TRUE) ;
                        }
                        if($mpos eq 'M' || $mpos eq 'C'){
                                $self->select_region(3,5) ;
                                return (TRUE) ;
                        }
                        return (TRUE) if $pos == 5 && $spos > 3 ;
                        return (FALSE) ;
                }
                if($keyval == 65361){                                 # Left Arrow Key
                        if($mpos eq 'M' || $mpos eq 'H'){
                                $self->CheckTime ;
                                $self->select_region(0,2) ;
                                return (TRUE) ;
                        }
                        if($pos eq '5' && $spos == 0){
                                $self->select_region(0,2) ;
                                return (TRUE) ;
                        }
                        if($pos == 2){
                                return (TRUE) ;
                        }
                        return (FALSE) ;
                }
                if($keyval == 65362){                                   # Up Arrow Key
                        if($mpos eq 'H' || $mpos eq 'C'){
                                if($hour == 23){
                                        $time = "00:$min" ;
                                } else {
                                        $hour ++ ;
                                        if(length($hour) < 2){$hour = "0$hour";}
                                        $time = "$hour:$min" ;
                                }
                                $self->set_text($time) ;
                        } elsif($mpos eq 'M'){
                                $self->select_region(0,2) ;
                                if($min == 59){
                                        $time = "$hour:00" ;
                                } else {
                                        $min ++ ;
                                        if(length($min) < 2){$min = "0$min";}
                                        $time = "$hour:$min" ;
                                }
                                $self->set_text($time) ;
                                $self->select_region(3,5) ;
                        }
                        return (TRUE) ;
                }
                if($keyval == 65364){                                   # Up Down Key
                        if($mpos eq 'H' || $mpos eq 'C'){
                                if($hour == 00){
                                        $time = "23:$min" ;
                                } else {
                                        $hour -- ;
                                        if(length($hour) < 2){$hour = "0$hour";}
                                        $time = "$hour:$min" ;
                                }
                                $self->set_text($time) ;
                        } elsif($mpos eq 'M'){
                                $self->select_region(0,2) ;
                                if($min == 00){
                                        $time = "$hour:59" ;
                                } else {
                                        $min -- ;
                                        if(length($min) < 2){$min = "0$min";}
                                        $time = "$hour:$min" ;
                                }
                                $self->set_text($time) ;
                                $self->select_region(3,5) ;
                        }
                        return (TRUE) ;
                }
                if(($keyval >= 48 && $keyval <= 57) || ($keyval >= 65456 && $keyval <= 65465)){ # Numbers
                        if($mpos eq 'H' || $mpos eq 'C'){
                                if((abs($pos-$spos) == 2 || $pos == 0 || ($pos == 1 && $spos == 0)) && $num 
=0 and $num <=2){
                                        $self->set_text($num . substr($text,1)) ;
                                        $self->select_region(1,2) ;
                                } elsif(($num < 4 && substr($text,0,1) == 2) || substr($text,0,1) < 2){
                                        $self->set_text(substr($text,0,1) . $num . substr($text,2)) ;
                                        $self->select_region(0,2) ;
                                }
                        } elsif($mpos eq 'M'){
                                if((abs($pos-$spos) == 2 || $pos == 3 || ($pos == 4 && $spos == 3)) && $num 
=0 and $num <=6){
                                        $self->set_text(substr($text,0,3) . $num . substr($text,4)) ;
                                        $self->select_region(4,5) ;
                                } else {
                                        $self->set_text(substr($text,0,4) . $num) ;
                                        $self->select_region(3,5) ;
                                }
                                
                        }
                        return (TRUE) ;
                }
                
        }) ;
        $self->signal_connect (focus_out_event => sub {
                $self->CheckTime ;
                return (FALSE) ;
        }) ;

        return $self;
}

sub show
{
        my $entrydate = shift;
        # this doesn't do anything but show you how to call 
        # a parent method if you really override this function
        # you'd do something here. note that this won't really
        # work as you'd expect for various reasons explained in
        # Glib::Subclass, take a look at that if you're wanting
        # to override C object functions. This is just an
        # example of using SUPER
        $entrydate->SUPER::show;
}

sub _GetNumber {
        my $self = shift ;
        my $keyval = shift ;
        if($keyval >= 48 && $keyval <= 57){
                return($keyval - 48) ;
        } elsif($keyval >= 65456 && $keyval <= 65465){
                return($keyval - 65456) ;
        } else {
                return(0) ;
        }
}

1;
# Written by A.R. Nasir Qureshi (nasir mitco ws)
#COPYRIGHT & LICENSE
#
#Copyright 2006 A.R. Nasir Qureshi, All Rights Reserved.
#
#This library is free software; you can redistribute it and/or modify it under
#the terms of the GNU Library General Public License as published by the Free
#Software Foundation; either version 2.1 of the License, or (at your option) any
#later version.
#
#This library 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 Library General Public License for more
#details.
#
#You should have received a copy of the GNU Library General Public License along
#with this library; if not, write to the Free Software Foundation, Inc., 59
#Temple Place - Suite 330, Boston, MA  02111-1307  USA.

package Gtk2::Ex::EntryDate;

use strict;
use warnings;
use Carp;
use Gtk2;
use Gtk2::Gdk::Keysyms ;
use Glib qw(TRUE FALSE);
use Class::Date qw(:errors date localdate gmdate now -DateParse -EnvC);
$Class::Date::RANGE_CHECK = 0;
$Class::Date::DATE_FORMAT="%d-%m-%Y" ;

use base 'Gtk2::Entry';
 
sub new {
        my $class = shift;
        # create an entry
        my $self = Gtk2::Entry->new_with_max_length(10);
        # rebless it to an Gtk2::EntryDate
        bless $self, $class;
        my $date = now ;
        $self->set_text($date) ;
        $self->signal_connect (key_press_event => sub {
                my ($widget, $event) = @_;
                my $keyval = $event->keyval ;
                my $pos = $self->get_property('cursor-position') ;
                my $spos = $self->get_property('selection-bound') ;
                my $mpos ;
                if(abs($pos-$spos) == 10){
                        $mpos = 'C' ;
                } elsif($pos >= 0 && $pos <= 2){
                        $mpos = 'D' ;
                } elsif ($pos >= 3 && $pos <= 5){
                        $mpos = 'M' ;
                } elsif ($pos >= 6 && $pos <= 10){
                        $mpos = 'Y' ;
                }
                my $text = $self->get_text ;
                my $year = substr($self->get_text,6,4) ;
                my $month = substr($self->get_text,3,2) ;
                my $day = substr($self->get_text,0,2) ;
                my $num = $self->_GetNumber($keyval) ;

                return (TRUE) if($keyval >= 65 && $keyval <= 90) ;    # A - Z
                return (TRUE) if($keyval >= 97 && $keyval <= 122) ;   # a - z
                return (TRUE) if($keyval >= 33 && $keyval <= 44) ;    # special
                return (TRUE) if($keyval >= 46 && $keyval <= 47) ;    # special
                return (TRUE) if($keyval >= 58 && $keyval <= 64) ;    # special
                return (TRUE) if($keyval >= 91 && $keyval <= 96) ;    # special
                return (TRUE) if($keyval >= 123 && $keyval <= 126) ;  # special
                if($keyval == 65293 || $keyval == 65421){             # Enter Keys
                        $self->CheckDate ;
                        return (FALSE) ;
                }
                if($keyval == 65363 || $keyval == 45){                                 # Right Arrow Key or 
'-' key
                        if($mpos eq 'D'){
                                $self->select_region(3,5) ;
                                $self->CheckDate ;
                                return (TRUE) ;
                        }
                        if($mpos eq 'M'){
                                $self->select_region(6,10) ;
                                $self->CheckDate ;
                                return (TRUE) ;
                        }
                        if($pos == 10 && $spos == 0){
                                $self->select_region(6,10) ;
                                return (TRUE) ;
                        }
                        return (TRUE) if $pos == 10 && $spos > 6 ;
                        return (FALSE) ;
                }
                if($keyval == 65361){                                 # Left Arrow Key
                        if($mpos eq 'M' || $mpos eq 'D'){
                                $self->CheckDate ;
                                $self->select_region(0,2) ;
                                return (TRUE) ;
                        }
                        if($mpos eq 'Y' && $spos != 0){
                                $self->select_region(3,5) ;
                                $self->CheckDate ;
                                return (TRUE) ;
                        }
                        if($pos eq '10' && $spos == 0){
                                $self->select_region(0,2) ;
                                return (TRUE) ;
                        }
                        if($pos == 2){
                                return (TRUE) ;
                        }
                        return (FALSE) ;
                }
                if($keyval == 65362){                                   # Up Arrow Key
                        my $date = now ;
                        $Class::Date::RANGE_CHECK = 0;
                        if($mpos eq 'D' || $mpos eq 'C'){
                                $date = date {year => $year, month => $month,day => $day + 1} ;
                                $self->set_text($date) ;
                                $self->select_region(0,2) ;
                        } elsif($mpos eq 'M'){
                                $date = date {year => $year, month => $month + 1,day => $day} ;
                                $self->set_text($date) ;
                                $self->select_region(3,5) ;
                        } elsif($mpos eq 'Y'){
                                $date = date {year => $year + 1, month => $month,day => $day} ;
                                $self->set_text($date) ;
                                $self->select_region(6,10) ;
                        }
                        return (TRUE) ;
                }
                if($keyval == 65364){                                   # Up Down Key
                        my $date = now ;
                        $Class::Date::RANGE_CHECK = 0;
                        if($mpos eq 'D' || $mpos eq 'C'){
                                $date = date {year => $year, month => $month,day => $day - 1} ;
                                $self->set_text($date) ;
                                $self->select_region(0,2) ;
                        } elsif($mpos eq 'M'){
                                $date = date {year => $year, month => $month - 1,day => $day} ;
                                $self->set_text($date) ;
                                $self->select_region(3,5) ;
                        } elsif($mpos eq 'Y'){
                                $date = date {year => $year - 1, month => $month,day => $day} ;
                                $self->set_text($date) ;
                                $self->select_region(6,10) ;
                        }
                        return (TRUE) ;
                }
                if(($keyval >= 48 && $keyval <= 57) || ($keyval >= 65456 && $keyval <= 65465)){ # Numbers
                        my $date = now ;
                        $Class::Date::RANGE_CHECK = 0;
                        if($mpos eq 'D' || $mpos eq 'C'){
                                if($pos == 0 || $pos == 10 || abs($pos-$spos) == 2){
                                        if(($month == 2 && $num < 3) || ($month != 2 && $num < 4)){
                                                $self->set_text($num . substr($text,1)) ;
                                                $self->select_region(1,2) ;
                                        }
                                } else {
                                        my $dd = substr($text,0,1) ;
                                        if($month == 2 && ((($year%4) == 0 || $day < 20 || $num < 9) && ($dd 
0 || $num > 0))){
                                                        $self->set_text($dd . $num . substr($text,2)) ;
                                                        $self->select_region(0,2) ;
                                        } elsif($month == 1 || $month == 3 || $month == 5 || $month == 7 || 
$month == 8 || $month == 10 || $month == 12){
                                                if(($dd == 3 && $num < 2) || ($dd > 0 && $dd < 3) || ($num > 
0 && $dd == 0)){
                                                        $self->set_text(substr($text,0,1) . $num . 
substr($text,2)) ;
                                                        $self->select_region(0,2) ;
                                                } 
                                        } elsif(($dd == 3 && $num == 0) || ($dd > 0 && $dd < 3) || ($num > 0 
&& $dd == 0)){
                                                        $self->set_text(substr($text,0,1) . $num . 
substr($text,2)) ;
                                                        $self->select_region(0,2) ;
                                        }
                                }
                        } elsif($mpos eq 'M'){
                                if(($pos == 3 || abs($pos-$spos) == 2) && $num < 2){
                                        $self->set_text(substr($text,0,3) . $num . substr($text,4)) ;
                                        $self->select_region(4,5) ;
                                } elsif(($pos > 3 && abs($pos-$spos) < 2) && (substr($text,3,1) == 0 && $num 
0) || (substr($text,3,1) == 1 && $num < 3)){
                                        $self->set_text(substr($text,0,4) . $num . substr($text,5)) ;
                                        $self->select_region(3,5) ;
                                }
                        } elsif($mpos eq 'Y'){
                                if(($pos == 6 || abs($pos-$spos) == 4) && $num == 2){
                                                $self->set_text(substr($text,0,6) . $num . substr($text,7)) ;
                                                $self->select_region(7,10) ;
                                } elsif(($pos == 7 || abs($pos-$spos) == 3) && $num == 0){
                                                $self->set_text(substr($text,0,7) . $num . substr($text,8)) ;
                                                $self->select_region(8,10) ;
                                } elsif($pos == 8 || abs($pos-$spos) == 2){
                                                $self->set_text(substr($text,0,8) . $num . substr($text,9)) ;
                                                $self->select_region(9,10) ;
                                } elsif($pos == 9 || abs($pos-$spos) == 1){
                                                $self->set_text(substr($text,0,9) . $num) ;
                                                $self->select_region(6,10) ;
                                }
                        }
                        return (TRUE) ;
                }
                
        }) ;
        $self->signal_connect (focus_out_event => sub {
                $self->CheckDate ;
                return (FALSE) ;
        }) ;

        return $self;
}

sub show
{
        my $entrydate = shift;
        # this doesn't do anything but show you how to call 
        # a parent method if you really override this function
        # you'd do something here. note that this won't really
        # work as you'd expect for various reasons explained in
        # Glib::Subclass, take a look at that if you're wanting
        # to override C object functions. This is just an
        # example of using SUPER
        $entrydate->SUPER::show;
}

sub CheckDate {
        $Class::Date::RANGE_CHECK = 0;
        my $self = shift ;
        my $year = substr($self->get_text,6,4) ;
        my $month = substr($self->get_text,3,2) ;
        my $day = substr($self->get_text,0,2) ;
        my $date = date {year => $year, month => $month,day => $day, hour => 0, min => 0, sec => 0} ;
        $self->set_text($date) ;
        return (FALSE) ;
}

sub _GetNumber {
        my $self = shift ;
        my $keyval = shift ;
        if($keyval >= 48 && $keyval <= 57){
                return($keyval - 48) ;
        } elsif($keyval >= 65456 && $keyval <= 65465){
                return($keyval - 65456) ;
        } else {
                return(0) ;
        }
}

1;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]