# 	$Id: skin.pl,v 1.24 2006/04/10 23:57:41 brian Exp $
#
# skin.pl
# (C) 2006 Brian Millham (bmillham@hughes.net), All Rights Reserved.
# This program is free software, licensed under the same terms as perl.

# A simple example of a skin application.  A timer generates 2 random
# values which are displayed in normal window mode (in a label), and
# also in a bargraph in a skin.
# Notice how there is very little code in this to handle skins.  Just
# switching in and out of skin mode, and a special paint sub to draw
# the graphics in skin mode.
# All of the gory details of reading the skinfile, creating the skin
# display, and drawing (the currently very limited set) the controls
# in skin mode are all handled by the module.  Even the minimize and
# close buttons and dragging the skin are handled in the module.
#
# At this time, the module isn't Win32::GUI::Skin, but just Skin, to
# make it easier for me to test...

use strict;
use warnings;
use Win32::GUI;
use Skin;

my $mult = 0;
my $mult1 = 0;

# Create the popup menu for use in skin mode

my $popMenu = Win32::GUI::MakeMenu(
				   "Pop" => "Pop",
				   " > &File" => "File",
				   " >> E&xit" => "pop_menu_exit",
				   " > &Skin" => "Skin",
				   " >> Se&lect Skin File" => "select_skin_menu",
				   " >> &Remove" => "remove_skin_menu",
				   );

# Create the main menu used in normal mode

my $mainMenu = Win32::GUI::MakeMenu(
				    "&File" => "File",
				    " > E&xit" => "pop_menu_exit",
				    "&Skin" => "Skin",
				    " > Se&lect Skin File" => "select_skin_menu",
				   );

# Create some pens and brushes used in the graphics.

my $bluePen = new Win32::GUI::Pen(
				  -width => 1,
				  -color => 0xFF0000,
				  );

my $greenPen = new Win32::GUI::Pen(
				   -width => 1,
				   -color => 0x00FF00,
				   );

my $blueBrush = new Win32::GUI::Brush(
				      -style => 0,
				      -color => 0xFF0000,
				      );

my $greenBrush = new Win32::GUI::Brush(
				       -style => 0,
				       -color => 0x00FF00,
				       );

# Create a skin window
# Normal window options are used, with these additional options:
# -skinfile => the skin file
# -skinmenu => a menu to pop up in skin mode
# -skinpaint => reference to a special sub to draw graphics on the skin
# -startskinned => (0/1) Use 1 to start in skin mode, 0 for normal window mode.

my $winSkin = Skin->new(
			-menu => $mainMenu,
			-width  => 200,
			-height => 200,
			-name   => "winSkin",
			-text   => "Win32::GUI::Skin Test",
			-skinfile => 'skin3.pwgs',
			-skinmenu => $popMenu,
			-skinpaint => \&additional_Paint,
			-startskinned => 1,
			);

# Create the button to switch to skin mode.
my $btnSkin = $winSkin->AddButton(-name => "btnSkin",
				  -text => "Skin",
				  -width => 80,
				  -height => 20,
				  -left => ($winSkin->Width / 2) - 40,
				  -top => ($winSkin->Height / 2) - 10,
				  );

# This is a normal label shown when not skinned
my $normallabel = $winSkin->AddLabel (
				      -left => 10,
				      -top => 10,
				      -name => 'normal',
				      -caption => 'A Win32::GUI::Skin app!',
				      );


# Add a timer to randomly create data.
my $myTimer = $winSkin->AddTimer("myTimer", 1000);

$winSkin->Show();

Win32::GUI::Dialog();

# An additional paint handler.  This will be run after the skin bitmaps
# and title are drawn, and before any labels are drawn.
# You can use this to draw additional graphic objects.
# Just like a normal window paint event, the DC is supplied as an argument.
# In addition to the DC, the app region boundrys are also supplied.
# You do not need to validate this the DC in this sub, and it's only
# called when in skin mode.

sub additional_Paint
{
    my ($dc) = shift;
    my ($rgnLeft, $rgnTop, $rgnRight, $rgnBottom) = @_;

# Select the brush and pen to draw with
    $dc->SelectObject($bluePen);
    $dc->SelectObject($blueBrush);

# The clipping region is already defined, so you can draw here without
# worrying about drawing outside the app area.
# For this example, we just fill the region with up to the height created
# by the random number generator in the timer event.

# Get the height of the region
    my $rgnHeight = $rgnBottom - $rgnTop;
    my $rgnWidth = $rgnRight - $rgnLeft;

# Calculate the height based on the random number
    my $newtop = $rgnBottom - ($rgnHeight * $mult);

# Draw it!
    $dc->Rectangle($rgnLeft, $newtop, $rgnRight - ($rgnWidth/2), $rgnBottom);

    $dc->SelectObject($greenPen);
    $dc->SelectObject($greenBrush);

    $newtop = $rgnBottom - ($rgnHeight * $mult1);
    $dc->Rectangle($rgnRight - ($rgnWidth / 2), $newtop, $rgnRight, $rgnBottom);

}

# Show the skin, if not skinned
sub btnSkin_Click
{
    $winSkin->ShowSkin if !$winSkin->isSkinned;
}

# Bye!
sub winSkin_Terminate
{
    return -1;
}

# Bye!
sub pop_menu_exit_Click
{
    return -1;
}

# Change to normal mode, hide the skin
sub remove_skin_menu_Click
{
    $winSkin->HideSkin;
}

# Choose a skin, and show it.
sub select_skin_menu_Click
{
    $winSkin->SelectSkinFile;
# If you don't do this, the skin won't show, until the user clicks
# the Skin button.
    $winSkin->ShowSkin;
}

# Create random data
sub myTimer_Timer
{
    $mult = rand(100) / 100;
    $mult1 = rand(100) / 100;

# Change the text in the labels
# Notice that the labels were created by the skin file, so you don't
# have direct access to the object, i.e.: $lblValue->Caption, you must
# reference it as shown below.
    $winSkin->{value}->Caption("Value A: " . sprintf("%6.2f", $mult * 100) . "%");
    $winSkin->{skinvalue2}->Caption("Value B: " . sprintf("%6.2f", $mult1 * 100) . "%");

# Force a redraw, if in skin mode.
    $winSkin->InvalidateRect(1) if defined $winSkin->isSkinned;
}

