use Win32::GUI;

# just for debugging/testing purposes...
use Win32::API;
$GetDlgCtrlID = new Win32::API("user32", "GetDlgCtrlID", [N], N);

$W = new Win32::GUI::Window(
    -name => "Window",
    -text => "Win32::GUI::Rebar test",
    -left => 100,
    -top => 100,
    -width => 200,
    -height => 300,
);
$W->Text("Win32::GUI::Rebar test");

new Win32::GUI::Rebar(
    $W,
    -name => "Rebar",
    -text => "I'm a placeholder",
    -left => 0,
    -top => 0,
    -width => 200,
    -height => 50,
    -menu => 33,
);
if(!$W->Rebar) {
    print "Error creating Rebar: ", Win32::GetLastError(), "\n";
}

$TB = new Win32::GUI::Toolbar(
    $W->Rebar,
    -name   => "Toolbar",
    -width  => 100,
    -style  => 2048 + 8 + 4,
    -width  => 100,
    -height => 22,
);

$B = new Win32::GUI::Bitmap("tools.bmp");

$TB->SetBitmapSize(16, 16);

$TB->AddBitmap($B, 3);

$TB->AddString("One");
$TB->AddString("Two");
$TB->AddString("Three");

$TB->AddButtons(
    3,
    0, 1, 4, 0, 0,
    1, 2, 4, 0, 1,
    2, 3, 4, 0, 2,
);

$BK = new Win32::GUI::Bitmap("zapotec.bmp");

$W->Rebar->InsertBand(
    -child => $TB,
    -width => 16*4,
    -minwidth => 100,
    -minheight => $TB->Height*2,
    -bitmap => $BK,
);

$E = new Win32::GUI::Textfield(
#     $W->Rebar,
    $W,
    -name       => "Textfield",
    -text       => "Hello world!",
    -width      => 100,
    -height     => 22,
    -background => [0, 255, 0],
    -foreground => [255, 0, 0],
    -menu       => 55,
);

$W->Rebar->InsertBand(
    -text => "Text:",
    -child => $E,
    -width => 100,
    -minwidth => 100,
    -minheight => $E->Height,
    -background => [0, 255, 0],
    -foreground => [255, 128, 128],
);

$W->AddTextfield(
    -name => "Info",
    -left => 0,
    -top  => $W->Rebar->Height,
    -width => $W->ScaleWidth,
    -height => $W->ScaleHeight-$W->Rebar->Height,
    -multiline => 1,
);

print "R.BANDCOUNT = ", $W->Rebar->SendMessage(1024+12, 0, 0), "\n";
print "R.ROWCOUNT = ", $W->Rebar->SendMessage(1024+13, 0, 0), "\n";
print "R.ROWHEIGHT = ", $W->Rebar->SendMessage(1024+14, 0, 0), "\n";

$W->Show();

# $W->Rebar->Show();

Win32::GUI::Dialog();

sub Window_Terminate {
    return -1;
}

sub Window_Resize {
    $W->Rebar->Resize($W->ScaleWidth, $W->ScaleHeight);
    $W->Info->Move(0, $W->Rebar->Height);
    $W->Info->Resize(
        $W->ScaleWidth,
        $W->ScaleHeight-$W->Rebar->Height,
    );
}

sub Toolbar_ButtonClick {
    my($button) = @_;
    $W->Info->Text("You pressed the button:\r\n".$button);
    $i = $GetDlgCtrlID->Call($W->Rebar->{-handle});
    print "Rebar.ID = $i\n";
    $i = $GetDlgCtrlID->Call($W->Textfield->{-handle});
    print "Textfield.ID = $i\n";
}

sub Textfield_Change {
    $W->Info->Text("You typed:\r\n".$E->Text);
}

sub Rebar_HeightChange {
    if(exists($W->{Info})) {
        $W->Info->Move(0, $W->Rebar->Height);
        $W->Info->Resize(
            $W->ScaleWidth,
            $W->ScaleHeight-$W->Rebar->Height,
        );
    }
}