
use Win32::GUI;

my ($DOS) = Win32::GUI::GetPerlWindow();
    Win32::GUI::Minimize($DOS);

# These things are self evident - they set the timer, size of nodes and inter node gap

my	$timer1_value = 200;
my 	$min_node_X = 50;
my 	$min_node_Y = 50;
my	$gap_X = 100;
my      $gap_Y = 100;
my	$node_width  = $min_node_X + $gap_X;
my	$node_height = $min_node_Y + $gap_Y;

Create_Classes();

$Win = new Win32::GUI::Window(
     -left    => 100,
     -top     => 100,
     -topmost => 0,
     -width   => 500,
     -minsize => [200,200],
     -class   => $WC,
     -height  => 500,
     -name    => "Window",
     -visible => 0,
     -font    => $FC,
     -text    => "Mark's Win32::GUI Experiment",
);

my $Timer1 = $Win->AddTimer("Timer1", $timer1_value);

$Win->Show();

AddNodes();

Win32::GUI::Dialog();

Win32::GUI::Show($DOS);

sub Window_Resize() {
	$number_of_nodes = 0;
	AddNodes();
#	print "Window_Resize() \n";
}

sub Graphic_Paint {
      my $DC = $Window->Graphic->GetDC();
      $DC->MoveTo(0, 0);
      $DC->LineTo(100, 100);
      $DC->Validate();
}


sub Window_Terminate() {
    	return -1;
}

sub Timer1_Timer {

	$number_of_nodes = 0;
	AddNodes();
	return 1;
}


sub AddNodes {

        $DC = $Win->GetDC;

	$W = $Win->ScaleWidth;
	$H = $Win->ScaleHeight;

	$node_count = 0;
	while (Where_to_put_the_node($W,$H)) {$node_count++}

	$loop_counter = 0;
	while ($loop_counter < $node_count) {

      	  	$DC->SelectObject($WhitePen);


                $loop_counter_2 = 0 ;
		while ($loop_counter_2 < $node_count) {
 		       	$DC->BeginPath();
        		$DC->MoveTo($nodes[$loop_counter]{centreX},$nodes[$loop_counter]{centreY});
        		$DC->LineTo($nodes[$loop_counter_2]{centreX},$nodes[$loop_counter_2]{centreY});
			$DC->EndPath();
        		$DC->StrokePath();
			$loop_counter_2++;
		}

		$loop_counter++;

	}

	$loop_counter = 0;
     	while ($loop_counter < $node_count) {
	        $DC->SelectObject($Pen);
		$DC->SelectObject($Brush);	
		$DC->Ellipse($nodes[$loop_counter]{left},
			$nodes[$loop_counter]{top},
			$nodes[$loop_counter]{right},
			$nodes[$loop_counter]{bottom});
		$loop_counter++;
	}

    }



sub Create_Classes() {

	$Pen = new Win32::GUI::Pen(   
 	        -color => [0,0,0], 
        	-width => 1,
        	);

	$WhitePen = new Win32::GUI::Pen(   
 	        -color => [255,255,255], 
        	-width => 1,
        	);

	$Brush = new Win32::GUI::Brush( [0,255,0] );

	$WC = new Win32::GUI::Class(
    		-name => "Colored Window", 
	    	-color => 2,
	);

	if(!$WC) {
	    	die "Window class creation error: ", Win32::GetLastError(), "\n";
	}

	$FC = new Win32::GUI::Font(
		-name => "Arial",
		-size => 18,
		-bold => 1,
	);

	if(!$FC) {
    		die "Font class creation error: ", Win32::GetLastError(), "\n";
	}
}

sub Where_to_put_the_node() {

        my $screen_width  = @_[0] ;
	my $screen_height = @_[1] ;

	$nodes_per_row = int($screen_width / $node_width);

	if ($nodes_per_row < 1) { return 0 };   

	$row = int($number_of_nodes / $nodes_per_row) * $node_height;
	
	$column = ($number_of_nodes % $nodes_per_row) * $node_width;

       	$left_adjust = int((($screen_width  % $node_width)  + $gap_X)/2);
	$top_adjust  = int((($screen_height % $node_height) + $gap_Y)/2);

	$left   = $column 	+ $left_adjust;
        $top    = $row 		+ $top_adjust;
        $right  = $left 	+ $min_node_X;
        $bottom = $top  	+ $min_node_Y;

	if ($bottom > $screen_height) {	return 0; }

	$centreX = $left + $min_node_X/2;
	$centreY = $top + $min_node_Y/2;

	$nodes[$number_of_nodes] = {
		left   => $left,
		top    => $top,
		right  => $right,
		bottom => $bottom,
		centreX	=> $centreX,
		centreY => $centreY
	};

	$number_of_nodes++;
		
#       print "$number_of_nodes W $screen_width H $screen_height left $left top $top right $right bottom $bottom\n";
        
        return 1;
}

