# -*-Perl-*-

# clock, like xclock

package MGMmodule::clock;
use vars qw($xpm $PI @days);

$PI=3.14159265358979323846;  # assume not near black hole nor in Tennessee

# called once to initialize the module.  The xpath here is a Class.
sub module_init{
    my$this=shift;
    my$toplevel=$this->{"toplevel"};
    my$xclass=$this->{"xclass"};
    $toplevel->optionAdd("$xclass.order",               '.1',20);
    $this;
}

# called to build an instance.  The xpath here is a name path.
sub module_instance{
    my$this=shift;
    my$widget=$this->{"widget"};
    my$toplevel=$this->{"toplevel"};
    my$xpath=$this->{"xpath"};

    $toplevel->optionAdd("$xpath.relief", 'sunken' ,21);
    $toplevel->optionAdd("$xpath.ipad", 20 ,21);
    $toplevel->optionAdd("$xpath.borderwidth", 1 ,21);
    $toplevel->optionAdd("$xpath.scalewidadj", 150,21);
    $toplevel->optionAdd("$xpath.scalelenadj", 60,21);
    $toplevel->optionAdd("$xpath.scalejustify", 0,20);  #centered    
    $toplevel->optionAdd("$xpath.scalerefresh", 2000,20);  

    $this->{"relief"}=$widget->optionGet("relief",'');
    my$border=$this->{"border"}=$widget->optionGet("borderwidth",'');
    $this->{"background"}=$widget->optionGet("background",'');
    $this->{"foreground"}=$widget->optionGet("foreground",'');
    $this->{"lit"}=$widget->optionGet("litbackground",'');

    # use our font and find the labelsize...
    my$testlabel=$this->{"widget"}->Label(-text=>"Wed 00:00",
					  -borderwidth=>0,
					  -padx=>1,-pady=>1);    
    my$textheight=$this->{"textheight"}=$testlabel->reqheight;
    my$textwidth=$this->{"textwidth"}=$testlabel->reqwidth;
    $testlabel->destroy;

    my$pad=$widget->optionGet("ipad","");
    my$min=35+$pad/5;
    my$minx=(($textwidth<$min?$min:$textwidth)+$border*2);
    my$miny=$textheight+$min+$border*2;

    $toplevel->optionAdd("$xpath.minx", $minx,21);      # safe
    $toplevel->optionAdd("$xpath.miny", $miny,21);      # safe

    $this;
}

sub module_run{
    my$this=shift;

    my$width=$this->{"width"};
    my$height=$this->{"height"};
    my$textwidth=$this->{"textwidth"};
    my$textheight=$this->{"textheight"};
    my$toplevel=$this->{"toplevel"};
    my$border=$this->{"border"};
    my$back=$this->{"background"};
    my$widget=$this->{"widget"}=$toplevel->Canvas(-class=>$this->{"name"},
						  Name=>$this->{"sequence"},
						  width=>$width-$border*2,
						  height=>$height-$border*2,
						  borderwidth=>$border,
						  highlightthickness=>0);
    my$pad=$widget->optionGet("ipad","");
    $height-=$textheight;
    my$square=$width;

    $square=$height if($square>$height);
    $square-=$border*2;
    $square*=(100-$pad)/100;
    $this->{"square"}=$square=int($square);
    $this->{"time"}=-1;

    my$xpm=MGM::Xpm::data($xpm);
    $xpm->scale($square,$square);
    my$pixmap=$widget->Pixmap($widget->PathName.".clock",-data=>$xpm->
			      write($this->{"background"},
				    $this->{"lit"},8));

    $widget->createImage($width/2-1,$height/2-1,-image=>$pixmap,
			 -anchor=>'center',-tags=>['clock']);

    $this->module_update;
    $widget;
}

@days=('Sun','Mon','Tue','Wed','Thu','Fri','Sat');

sub module_update{
    my$this=shift;
    my@l=localtime;
    my$t= sprintf "%s %02d:%02d",$days[$l[6]],$l[2],$l[1];

    if($t ne $this->{"time"}){

	my$widget=$this->{"widget"};
	my$square=$this->{"square"};
	my$width=$this->{"width"};
	my$height=$this->{"height"};
	my$textwidth=$this->{"textwidth"};
	my$textheight=$this->{"textheight"};
	$widget->delete("date");

	# minute hand
	my$phi=2*$PI/60*$l[1];
	my$A=$square/2*.8;
	my$B=$A*sin($phi);
	my$C=-$A*cos($phi);
	my$base=.08;
	my$b=$A*cos($phi)*$base;
	my$c=$A*sin($phi)*$base;
	my$cx=$width/2-1;
	my$cy=($height-$textheight)/2-1;

	$widget->createPolygon($b+$cx-$B*.25,$c+$cy-$C*.25,
			       $B+$cx,$C+$cy,
			       -$b-$B*.25+$cx,-$c-$C*.25+$cy,
			       -fill=>$this->{"foreground"},
			       -outline=>$this->{"foreground"},
			       -tags=>['date']);
			    
	# hour hand
	$phi=2*$PI/12/60*($l[2]*60+$l[1]);
	$A=$square/2*.55;
	$B=$A*sin($phi);
	$C=-$A*cos($phi);
	$base=.15;
	$b=$A*cos($phi)*$base;
	$c=$A*sin($phi)*$base;

	$widget->createPolygon($b+$cx-$B*.35,$c+$cy-$C*.35,
			       $B+$cx,$C+$cy,
			       -$b-$B*.35+$cx,-$c-$C*.35+$cy,
			       -fill=>$this->{"foreground"},
			       -outline=>$this->{"foreground"},
			       -tags=>['date']);

	my$font=$widget->optionGet("font","");
	my$y=$height-($height-$textheight+$square)/2;
	$widget->createText($width/2-1,$height-$y/2-1,
			    -text=>"$t",
			    -fill=>$this->{"foreground"},-anchor=>'center',
			    -font=>$font,-tags=>['date']);

			    
	$this->{"time"}=$t;
    }
}

$xpm= <<'EOXPM';
/* XPM */
static char * clockface_xpm[] = {
"119 119 8 1",
".	g #020202",
"+	g #FBFBFB",
"@	g #2D2D2D",
"#	g #717171",
"$	g #4D4D4D",
"%	g #CFCFCF",
"&	g #8E8E8E",
"*	g #B0B0B0",
"..........................................................+++..........................................................",
"..................................................@#######+++#######@...$.@............................................",
"..........................................%++$############+++##########&%%&@...........................................",
".........................................@%++*############+++##########*++%&#@.........................................",
"......................................@###*++%############+++##########*++%#####@......................................",
"...................................@#######+++####$$@.....+++.....@$$##%++&########@...................................",
".................................@#########%+%$@..........+++..........$&*&##########@.................................",
"...............................@##########$$@.............+++..............@$###########...............................",
".............................++&#######$@.................+++.................@$######*+%#.............................",
"............................*++%#####$....................+++....................$####+++*@............................",
"..........................@#&+++*##@...............................................@#*+++###@..........................",
".........................@###*+++@..................................................@+++*####@.........................",
".......................@######+++#..................................................&++%#######@.......................",
"......................@########+++@................................................$+++$@#######@......................",
".....................@######$..%++*................................................%++*...$######@.....................",
"....................@######@...$+++$..............................................$+++@....@######@....................",
"...................@#####$......*+*@...............................................$*&.......$####&&@..................",
"................*+&#####$.......@#............................................................$##*++*..................",
"...............@%++*&##$.......................................................................$*+++%$.................",
"...............@*+++%#@.........................................................................#++%&#@................",
"...............@#%+++$...........................................................................#%&###@...............",
"..............@###%+#.............................................................................@#####@..............",
".............@####&$...............................................................................@#####@.............",
"............@#####$.................................................................................$#####@............",
"............#####$...................................................................................$#####............",
"...........@####$.....................................................................................$####@...........",
"..........@#####.......................................................................................#####@..........",
"..........#####@.......................................................................................@#####..........",
".........@####$.........................................................................................$###&&.........",
"........+*####...........................................................................................##*++#........",
"........+++*#@...........................................................................................#+++++........",
".......%++++%$.........................................................................................$%++++%&@.......",
".......#*+++++*@.....................................................................................@*+++++*###.......",
"......@###*+++++&....................................................................................#++++#@####@......",
"......#####@&+++*.....................................................................................*+*@..#####......",
".....@####@..$*+$.....................................................................................@$....@####@.....",
".....#####.....$.............................................................................................#####.....",
".....####$...................................................................................................$####.....",
"....@####.....................................................................................................####@....",
"....####$.....................................................................................................$####....",
"....####@.....................................................................................................@####....",
"...@####.......................................................................................................####@...",
"..@####$.......................................................................................................$##*%...",
".*%%*&&@.......................................................................................................$%+++$..",
".#++++%........................................................................................................@++++&..",
".*++++%.........................................................................................................%+%*$..",
"..&*%+&.........................................................................................................$####..",
"..###&@.........................................................................................................@####..",
"..####...........................................................................................................####..",
"..####...........................................................................................................####..",
".@###$...........................................................................................................$###@.",
".####$...........................................................................................................$####.",
".####@...........................................................................................................@####.",
".####.............................................................................................................####.",
".####.............................................................................................................####.",
".####.............................................................................................................####.",
".####.............................................................................................................####.",
".####.............................................................................................................####.",
"++++++++++...................................................................................................++++++++++",
"++++++++++...................................................................................................++++++++++",
"++++++++++...................................................................................................++++++++++",
".####.............................................................................................................####.",
".####.............................................................................................................####.",
".####.............................................................................................................####.",
".####.............................................................................................................####.",
".####.............................................................................................................####.",
".####@...........................................................................................................@####.",
".####$...........................................................................................................$####.",
".@###$...........................................................................................................$###@.",
"..####...........................................................................................................####..",
"..####...........................................................................................................####..",
"..####@.........................................................................................................@####..",
"..&*%+&.........................................................................................................#&&##..",
"..++++%.........................................................................................................%+%%@..",
"..++++%........................................................................................................@+++%@..",
"..%%&##@.......................................................................................................$*%+*...",
"...####$.......................................................................................................$##&&...",
"...@####.......................................................................................................####@...",
"....####@.....................................................................................................@####....",
"....####$.....................................................................................................$####....",
"....@####.....................................................................................................####@....",
".....####$...................................................................................................$####.....",
".....#####.....$.............................................................................................#####.....",
".....@####@..$%+$.....................................................................................@$....@####@.....",
"......#####@*+++*.....................................................................................*+&@..#####......",
"......@###*+++++#....................................................................................#++++#@####@......",
".......#*+++++*@.....................................................................................@*+++++*###.......",
".......%++++%$.........................................................................................$%++++%&@.......",
"........+++*#@...........................................................................................#+++++........",
"........+*####...........................................................................................##*++#........",
".........@####$.........................................................................................$###&&.........",
"..........#####@.......................................................................................@#####..........",
"..........@#####.......................................................................................#####@..........",
"...........@####$.....................................................................................$####@...........",
"............#####$...................................................................................$#####............",
"............@#####$.................................................................................$#####@............",
".............@####*#...............................................................................@#####@.............",
"..............@##&%+#.............................................................................$&####@..............",
"...............@&+++%$...........................................................................#+%###@...............",
"...............$%++%&#@.........................................................................$+++%&@................",
"................$+%&###$.......................................................................$#*++%$.................",
".................%######$.......@#............................................................$##&*%$..................",
"...................@#####$......*+*@...............................................$*#.......$#####$...................",
"....................@######@...$+++$..............................................$+++@....@######@....................",
".....................@######$..%++&................................................%++*...$######@.....................",
"......................@########+++@................................................$+++$@#######@......................",
".......................@######+++#..................................................*++%#######@.......................",
".........................@###*+++@..................................................@+++*####@.........................",
"..........................@#&+++*##@...............................................@#*+++###@..........................",
"............................*++%#####$....................+++....................$####+++*@............................",
".............................++&#######$@.................+++.................@$######*++#.............................",
"...............................@###########$@.............+++..............@$###########...............................",
".................................@########*++*$@..........+++..........@&%%##########@.................................",
"...................................@######%++*####$$@.....+++.....@$$###+++########@...................................",
"......................................@##&+++&############+++###########%++&####@......................................",
".........................................#%+%&############+++###########*++%#@.........................................",
"..........................................$&$@############+++###########&%&$...........................................",
"..................................................@#######+++#######@..................................................",
"..........................................................+++.........................................................."};
EOXPM

bless {};

