# A demo of the toolbar capabilities.
# Needs changes to toolbar.xs for dropdown menus to work
# Here is a list of things that I haven't yet made work or might be useful to add
# - get the customisation to work.  Needs research on exactly which notifications need handling
#   Simple setting CCS_ADJUSTABLE style is not enough
# - is it correct that a button with TBSTYLE_DROPDOWN on a toolbar without TB_STYLE_EX_DRAWDDARROWS
#   ows not depress when clicked?
# - handle the tooltip request for text - needs a handler like NeedsText in tooltip.xs
# - docs sya that passing I_IMAGENONE as bitmap index in AddButtons results in a button with no
#   space reserved for a bitmap - I cannot get this to work (would be nice to implement IE-like menu bar.
# - extend model to allow passing I_IMAGECALLBACK as bitmap reference in AddButton to get toolbar to
#   issue TBN_GETDISPINFO to retrieve image index.
# - toolbar.xs comments on AddString suggests that AddString should accept a list of strings.  This should
#   be fixed (or perhaps better to add method AddStrings() that does)
# - would be nice if AddButtons STRING field could be a string as well as an index.
# - add support for TrackPopupMenuEx, so that an exclude rectangle can be set.  Perl API probably
#   looks like $obj->TrackPopupEx(HMENU, X, Y, [FLAGS], [L, T, R, B]), where LTRB are the corners of an exclude
#   rectangle.
# - look into how to make the dropdown menu onClick handlers work.  Currently I need to return the index from
#   TrackPopupMenu().

use strict;
use warnings;

use Win32::GUI;
use Win32::GUI::BitmapInline ();

sub IMAGE_X() {16}; # X dimension for buttons in our toolbar
sub IMAGE_Y() {15}; # Y dimension for images in our toolbar
sub ILC_MASK() {1}; # Use an image list with a mask

sub TBN_DROPDOWN()   {-710};

# Main Window
my $mw = Win32::GUI::Window->new(
	-title => "Toolbar Demo",
	-size => [500,300],
);

# A bitmap for our toolbar
my $bmp = new Win32::GUI::BitmapInline( q(
Qk0GAwAAAAAAADYAAAAoAAAAEAAAAA8AAAABABgAAAAAANACAADEDgAAxA4AAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA////////////
////////////////////////////////////////////AAAAAAAA////////////////AAAAAAAA
AAAAAAAAAAAAAAAA////////////////AAAAAAAA////////////////////////AAAAAAAA////
////////////////////AAAAAAAA////////////////////////AAAAAAAA////////////////
////////AAAAAAAA////////////////////////AAAAAAAA////////////////////////AAAA
AAAA////////////////////////AAAAAAAA////////////////////////AAAAAAAA////////
////////////////AAAAAAAA////////////////////////AAAAAAAA////////////////////
////AAAAAAAA////////////////////////AAAAAAAA////////////////////////AAAAAAAA
////////////////////////AAAAAAAA////////////////AAAAAAAAAAAAAAAA////////////
////////////AAAAAAAA////////////////////AAAAAAAAAAAA////////////////////////
AAAAAAAA////////////////////////AAAAAAAA////////////////////////AAAAAAAA////
////////////////////////////////////////////////////AAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
) );

# A masked imagelist to put the toolbar buttons into
my $images = Win32::GUI::ImageList->new(
	IMAGE_X,
	IMAGE_Y,
	ILC_MASK,
	1,
	1,
);

# Add the bitmap to the imagelist, generating the mask from the background colour (0xFFFFFF)
$images->AddBitmapMasked($bmp, 0xFFFFFF);

# Add a toolbar to the main window
# TBSTYLE_LIST - puts label captions to the right of the labels, rather than below the labels
# TBSTYLE_TOOLTIPS - creates a tooltip control to manage tooltips associated with the toolbar
my $tools = $mw->AddToolbar(
	-imagelist => $images,
	-flat => 1,
	#-pushstyle => CCS_ADJUSTABLE,
	-pushstyle => TBSTYLE_LIST,
	-pushstyle => TBSTYLE_TOOLTIPS,
	-onButtonClick => \&ToolbarButtonClick,
);

# Set extended syles.  It took me a long time to determine that this was NOT
# the same as using -pushexstyle on the AddToolbar method.
# TBSTYLE_EX_DRAWDDARROWS - causes drop down arrows to be drawn to the right of toolbar icons
# TBSTYLE_EX_MIXEDBUTTONS - causes the text associated with a button not to be drawn, unless the 
#  button has the BTNS_SHOWTEXT style.  INstead the text is used by default for the tooltip(this
#  behaviour can be stopped by processing the TBN_GETTIPINFO notification)
$tools->SetExtendedStyle(TBSTYLE_EX_DRAWDDARROWS|TBSTYLE_EX_MIXEDBUTTONS);

# Add some strings to the toolbars string table
$tools->AddString("Button 1");
$tools->AddString("Button 2 - on/off");
$tools->AddString("Button 3 - group");
$tools->AddString("Button 4 - group");
$tools->AddString("Button 5 - dropdown");
$tools->AddString("DropMenu");

# Add the buttons
# BITMAP is a zero-based index to the image in the toolbar's image list, except when
#  STPLE is TBSTYLE_SEP.  In this case it is the spacing on either side of the seperator
#  in pixels.
# COMMAND is an (application-wide) unique number.
# STRING is a zeo-based index into the tooltips string table
$tools->AddButtons(
	8,
	# BITMAP, COMMAND, STATE,                           STYLE,                       STRING
	0,        1,       TBSTATE_ENABLED,                 TBSTYLE_BUTTON,                   0,
	0,        2,       TBSTATE_ENABLED,                 TBSTYLE_CHECK,                    1,
	20,       0,       TBSTATE_ENABLED,                 TBSTYLE_SEP,                      0,
	0,        3,       TBSTATE_ENABLED|TBSTATE_CHECKED, TBSTYLE_CHECK|TBSTYLE_GROUP,      2,
	0,        4,       TBSTATE_ENABLED,                 TBSTYLE_CHECK|TBSTYLE_GROUP,      3,
	20,       0,       TBSTATE_ENABLED,                 TBSTYLE_SEP,                      0,
	0,        5,       TBSTATE_ENABLED,                 TBSTYLE_DROPDOWN,                 4,
	0,        6,       TBSTATE_ENABLED,                 BTNS_WHOLEDROPDOWN|BTNS_SHOWTEXT, 5,
);

# Adjuxt trhe button sizes for the text and images
$tools->AutoSize();

$mw->Show();
Win32::GUI::Dialog();
exit(0);

sub ToolbarButtonClick
{
  my ($self, $index, $drawmenu) = @_;

  print "Toolbar:  Button index: $index, Draw menu: $drawmenu\n";

  if($drawmenu) {
    # Button has the TBSTYLE_DROPDOWN or BTNS_WHOLEDROPDOWN style

    # Get the button corners, and convert to screen co-ordinates
    my ($l, $t, $r, $b) = $self->GetRect($index);
    my ($x, $y) = $self->ClientToScreen($l, $b);

    # Create a menu.  The onClick handlers dont get called - need to investigate
    my $menu = Win32::GUI::Menu->new( 
      ""             => "PopUpMenu", 
      "> MenuItem&1" => { -onClick => sub { print "MenuItem1 clicked\n"; }, -id => "1" }, 
      "> MenuItem&2" => { -onClick => sub { print "MenuItem2 clicked\n"; }, -id => "2" }, 
      "> -"          => 0, 
      "> MenuItem&3" => { -onClick => sub { print "MenuItem3 clicked\n"; }, -id => "3" }, 
    );


    # Display and interact with the popup menu.  Returns command id if menu item clicked, 0 otherwise
    # Would like to use TrackPopupMenuEx to exclude the button area, so that if the toolbar is at the
    # bottom of the screen, the menu appears above the button (wather than over it)
    my $id = $self->TrackPopupMenu($menu->{PopUpMenu}, $x, $y,
        TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON | TPM_RETURNCMD);
    print "Menu Item $id clicked.\n" if $id != 0;
  }

  return 1;
}
