diff options
Diffstat (limited to 'clock.pm')
-rwxr-xr-x | clock.pm | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/clock.pm b/clock.pm new file mode 100755 index 00000000..21835075 --- /dev/null +++ b/clock.pm @@ -0,0 +1,135 @@ +#!/usr/bin/perl + +use Data::Dumper; +use Gtk; +init Gtk; + +$::isEmbedded = ($::XID, $::CCPID) = "@ARGV" =~ /--embedded (\w+) (\w+)/; +my $pi=3.141592653589; +my $pixmap; +my $radius; +my $window = $::isEmbedded ? new Gtk::Plug ($::XID) : new Gtk::Window -toplevel; +my $vbox = new Gtk::VBox(0,0); +$window->add($vbox); +$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->signal_connect ( expose_event => \&expose_event ); +$drawing_area->signal_connect ( configure_event => \&configure_event ); + +$window->show_all; +Gtk->timeout_add(120, [\&Repaint, $drawing_area]); + +Gtk->main; + + +sub quit_global { + $::isEmbedded ? kill(USR1, $::CCPID) : Gtk->exit(0); +} + +sub expose_event { +my ($widget) = @_; +$widget->window->draw_pixmap ( + $widget->style->fg_gc('normal'), + $pixmap, + 0, 0, 0, 0, + $widget->allocation->[2], $widget->allocation->[3]); +} + +sub configure_event { + my ($widget) = @_; + $pixmap = new Gtk::Gdk::Pixmap ($widget->window, + $widget->allocation->[2], + $widget->allocation->[3], + -1); +} + +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; + $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]) } + my $now = time; + ($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]); + } + $update_rect->[0] = $update_rect->[1] = 0; + $update_rect->[$_] = $drawing_area->allocation->[$_] foreach (2,3); + $drawing_area->draw($update_rect); + 1; +} + +sub DrawSec { + my ($pixmap, $gc, $midx, $midy, $dRadians, $dec) = @_; + $pixmap->draw_line ($gc, + $midx+$dec, $midy+$_->[1], + $midx+$dec + (0.80 * $radius * sin ($dRadians)), + $midy+$dec - (0.80 * $radius * cos ($dRadians))) +} + +sub DrawMin { + my ($pixmap, $gc, $midx, $midy, $dRadians, $dec) = @_; + $pixmap->draw_polygon ($gc, 1, + $midx+$dec - 0.03 * $radius * sin ($dRadians), + $midy+$dec + 0.03 * $radius * cos ($dRadians), + $midx+$dec - 0.03 * $radius * sin ($dRadians+$pi/2), + $midy+$dec + 0.03 * $radius * cos ($dRadians+$pi/2), + $midx+$dec + 0.80 * $radius * sin ($dRadians), + $midy+$dec - 0.80 * $radius * cos ($dRadians), + $midx+$dec + 0.03 * $radius * sin ($dRadians+$pi/2), + $midy+$dec - 0.03 * $radius * cos ($dRadians+$pi/2)); +} + +sub DrawHour { + my ($pixmap, $gc, $midx, $midy, $dRadians, $dec) = @_; + $pixmap->draw_polygon ($gc, 1, + $midx+$dec - 0.05 * $radius * sin ($dRadians), + $midy+$dec + 0.05 * $radius * cos ($dRadians), + $midx+$dec - 0.05 * $radius * sin ($dRadians+$pi/2), + $midy+$dec + 0.05 * $radius * cos ($dRadians+$pi/2), + $midx+$dec + 0.70 * $radius * sin ($dRadians), + $midy+$dec - 0.70 * $radius * cos ($dRadians), + $midx+$dec + 0.05 * $radius * sin ($dRadians+$pi/2), + $midy+$dec - 0.05 * $radius * cos ($dRadians+$pi/2)); +} + +sub DrawTickAt { + my ($pixmap, $black_gc, $gray_gc, $nHour, $cx, $cy) = @_; + my $dRadians = $nHour * $pi / 6.0; + + $pixmap->draw_line ($_->[0], + $cx+$_->[1] + 0.90 * $radius * sin ($dRadians), + $cy+$_->[1] - 0.90 * $radius * cos ($dRadians), + $cx+$_->[1] + 1.0 * $radius * sin ($dRadians), + $cy+$_->[1] - 1.0 * $radius * cos ($dRadians)) + foreach ([$gray_gc, 5], [$black_gc, 0]); +} + +sub DrawPointAt { + my ($pixmap, $black_gc, $gray_gc, $nHour, $cx, $cy) = @_; + my $dRadians = $nHour * $pi / 30.0; + + $pixmap->draw_point ($black_gc, + $cx + 0.95 * $radius * sin ($dRadians), + $cy - 0.95 * $radius * cos ($dRadians)) +} |