#!/usr/bin/perl use Data::Dumper; use Gtk; init Gtk; #use strict; $::isEmbedded = ($::XID, $::CCPID) = "@ARGV" =~ /--embedded (\w+) (\w+)/; 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); $window->signal_connect ( delete_event => \&quit_global ); my $hbox = new Gtk::HBox(0,0); $vbox->pack_start($hbox, 1, 1, 0); my $calendar = new Gtk::Calendar; $hbox->pack_start($calendar, 1, 1, 0); my $vbox2 = new Gtk::VBox(0,0); $hbox->pack_start($vbox2, 1, 1, 0); my $drawing_area = new Gtk::DrawingArea; $drawing_area->size(200,200); $vbox2->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 ); my $hbox11 = new Gtk::HBox(1,0); $vbox2->pack_start($hbox11, 0, 0, 10); my $hbox2 = new Gtk::HBox(1,0); $hbox11->pack_start($hbox2, 0, 0, 0); my $adj_h = new Gtk::Adjustment 0.0, 0.0, 23.0, 1.0, 5.0, 0.0; my $spinner_h = new Gtk::SpinButton $adj_h, 0, 0; $spinner_h->set_wrap(1); $hbox2->pack_start($spinner_h, 0, 0, 0); my $adj_m = new Gtk::Adjustment 0.0, 0.0, 59.0, 1.0, 5.0, 0.0; my $spinner_m = new Gtk::SpinButton $adj_m, 0, 0; $spinner_m->set_wrap(1); $hbox2->pack_start($spinner_m, 0, 0, 0); my $adj_s = new Gtk::Adjustment 0.0, 0.0, 59.0, 1.0, 5.0, 0.0; my $spinner_s = new Gtk::SpinButton $adj_s, 0, 0; $spinner_s->set_wrap(1); $hbox2->pack_start($spinner_s, 0, 0, 0); my $bbox = new Gtk::HButtonBox; $bbox->set_layout(-end); $vbox->pack_start($bbox, 1, 1, 5); my $button_ok = new Gtk::Button "OK"; $bbox->add($button_ok ); my $button_cancel = new Gtk::Button "Cancel"; $bbox->add($button_cancel ); my $button_reset = new Gtk::Button "Reset"; $button_reset->signal_connect ( clicked => sub { $timer=Gtk->timeout_add(120, sub { time_to_rad(); Repaint($drawing_area) }); time_to_rad(); Repaint($drawing_area); $button_reset->set_sensitive(0); }); $bbox->add($button_reset); $button_reset->set_sensitive(0); $window->show_all; $timer=Gtk->timeout_add(120, sub { time_to_rad(); Repaint($drawing_area) }); Gtk->main; sub motion_event { my ($widget, $event) = @_; $pressed or return; if ($first) { Gtk->timeout_remove($timer); $Radian = determine_radian($event->{'x'}, $event->{'y'}); $button_reset->set_sensitive(1); } $$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); } 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 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) = @_; $pixmap->draw_rectangle($drawing_area->style->white_gc, 1, 0, 0, $drawing_area->allocation->[2], $drawing_area->allocation->[3]); $midx = $drawing_area->allocation->[2] / 2; $midy = $drawing_area->allocation->[3] / 2; $radius = $midx < $midy ? $midx -10 : $midy - 10; my $nHour; my $gray_gc = $drawing_area->style->bg_gc('normal'); my $black_gc = $drawing_area->style->black_gc; foreach my $i ([\&DrawTickAt, 12], [\&DrawPointAt, 60]) { $i->[0]($pixmap, $black_gc, $gray_gc, $_, $midx, $midy) foreach (1..$i->[1]) } my $now = time; 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]) { DrawHour ($pixmap, $_->[0], $midx, $midy, $dRadians_hour, $_->[1]); DrawMin ($pixmap, $_->[0], $midx, $midy, $dRadians_min, $_->[1]); DrawSec ($pixmap, $_->[0], $midx, $midy, $dRadians_sec, $_->[1]); } $drawing_area->draw(undef); 1; } sub DrawSec { my ($pixmap, $gc, $midx, $midy, $dRadians, $dec) = @_; $pixmap->draw_line ($gc, $midx+$dec, $midy+$dec, $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)) }