summaryrefslogtreecommitdiffstats
path: root/clock.pm
blob: 21835075ab3e51b0a5d7e1512a6b5007aee46568 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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))
}