diff options
-rwxr-xr-x | clock.pm | 73 |
1 files changed, 56 insertions, 17 deletions
@@ -3,11 +3,19 @@ use Data::Dumper; use Gtk; init Gtk; +#use strict; $::isEmbedded = ($::XID, $::CCPID) = "@ARGV" =~ /--embedded (\w+) (\w+)/; -my $pi=3.141592653589; +my $pi=3.1416; my $pixmap; my $radius; +my $dRadians_hour; +my $dRadians_min; +my $dRadians_sec; +my $Radian; +my $timer; +my $midx, $midy; +my $first=1; my $window = $::isEmbedded ? new Gtk::Plug ($::XID) : new Gtk::Window -toplevel; my $vbox = new Gtk::VBox(0,0); $window->add($vbox); @@ -16,15 +24,45 @@ $window->signal_connect ( delete_event => \&quit_global ); my $drawing_area = new Gtk::DrawingArea; $drawing_area->size(200,200); $vbox->pack_start($drawing_area, 1, 1, 0); +$drawing_area->set_events(['button_press_mask', 'button_release_mask', "pointer_motion_mask" ]); $drawing_area->signal_connect ( expose_event => \&expose_event ); $drawing_area->signal_connect ( configure_event => \&configure_event ); +$drawing_area->signal_connect ( button_press_event => sub { $pressed=1 }); +$drawing_area->signal_connect ( button_release_event => sub { $first=1; $pressed=0 }); +$drawing_area->signal_connect ( motion_notify_event => \&motion_event ); $window->show_all; -Gtk->timeout_add(120, [\&Repaint, $drawing_area]); +$timer=Gtk->timeout_add(120, sub { time_to_rad(); Repaint($drawing_area) }); Gtk->main; +sub motion_event { + my ($widget, $event) = @_; + $pressed or return; + $first and Gtk->timeout_remove($timer); + $first and $Radian = determine_radian($event->{'x'}, $event->{'y'}); + + $$Radian=-atan2(($event->{'x'}-$midx),($event->{'y'}-$midy)) + $pi; + Repaint($widget); +# print "AAAAAAAA" . $event->{'x'} . "\n"; +# print " -- # " . Data::Dumper->Dump([\$event], ['$event']) . "\n"; + $first=0; +} + +sub determine_radian { + my ($x, $y) = @_; + + my $res; + my $r; + foreach (\$dRadians_hour, \$dRadians_min, \$dRadians_sec) { + my $d=sqrt ( ($x - ($midx + 0.70 * $radius * sin ($$_)))**2 + ($y - ($midy - 0.70 * $radius * cos ($$_)))**2 ); + $res or $res=$d, $r=$_; + $d<$res and $res=$d, $r=$_; + } + $r; +} + sub quit_global { $::isEmbedded ? kill(USR1, $::CCPID) : Gtk->exit(0); } @@ -46,43 +84,44 @@ sub configure_event { -1); } +sub time_to_rad { + my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);# now_tm = localtime (&now); + $dRadians_hour = ($hour % 12) * $pi / 6.0 + ($pi * $min / 360.0); + $dRadians_min = ($min * $pi / 30.0) + ($pi * $sec / 1800.0); + $dRadians_sec = $sec * $pi / 30.0; +} + sub Repaint { my ($drawing_area) = @_; - my $update_rect = Gtk::Gdk::Rectangle; $pixmap->draw_rectangle($drawing_area->style->white_gc, 1, 0, 0, $drawing_area->allocation->[2], $drawing_area->allocation->[3]); - my $midx = $drawing_area->allocation->[2] / 2; - my $midy = $drawing_area->allocation->[3] / 2; + $midx = $drawing_area->allocation->[2] / 2; + $midy = $drawing_area->allocation->[3] / 2; $radius = $midx < $midy ? $midx : $midy; my $nHour; my $gray_gc = $drawing_area->style->bg_gc('normal'); my $black_gc = $drawing_area->style->black_gc; - foreach $i ([\&DrawTickAt, 12], [\&DrawPointAt, 60]) { $i->[0]($pixmap, $black_gc, $gray_gc, $_, $midx, $midy) foreach (1..$i->[1]) } + foreach my $i ([\&DrawTickAt, 12], [\&DrawPointAt, 60]) { $i->[0]($pixmap, $black_gc, $gray_gc, $_, $midx, $midy) foreach (1..$i->[1]) } my $now = time; - ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);# now_tm = localtime (&now); + my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);# now_tm = localtime (&now); my $dRadians; foreach ([$gray_gc, 5], [$black_gc, 0]) { - $dRadians = ($hour % 12) * $pi / 6.0 + ($pi * $min / 360.0); - DrawHour ($pixmap, $_->[0], $midx, $midy, $dRadians, $_->[1]); - $dRadians = ($min * $pi / 30.0) + ($pi * $sec / 1800.0); - DrawMin ($pixmap, $_->[0], $midx, $midy, $dRadians, $_->[1]); - $dRadians = $sec * $pi / 30.0; - DrawSec ($pixmap, $_->[0], $midx, $midy, $dRadians, $_->[1]); + DrawHour ($pixmap, $_->[0], $midx, $midy, $dRadians_hour, $_->[1]); + DrawMin ($pixmap, $_->[0], $midx, $midy, $dRadians_min, $_->[1]); + DrawSec ($pixmap, $_->[0], $midx, $midy, $dRadians_sec, $_->[1]); } - $update_rect->[0] = $update_rect->[1] = 0; - $update_rect->[$_] = $drawing_area->allocation->[$_] foreach (2,3); - $drawing_area->draw($update_rect); + $drawing_area->draw(undef); 1; } sub DrawSec { my ($pixmap, $gc, $midx, $midy, $dRadians, $dec) = @_; $pixmap->draw_line ($gc, - $midx+$dec, $midy+$_->[1], + $midx+$dec, $midy+$dec, $midx+$dec + (0.80 * $radius * sin ($dRadians)), $midy+$dec - (0.80 * $radius * cos ($dRadians))) } |