Steven Lloyd wrote:
Does anyone have any good examples using  Win32::GUI::Toolbar?
I would like to create a format bar with Bold, Underline, and Italic.

Here' some code that I did a while back when exploring how toolbars work. It could do with a bit of tidying up and turning into a proper sample, but it does show most of the toolbar features.

Rob.

#!perl -w
# A demo of the toolbar capabilities.
# 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
#   Simply setting CCS_ADJUSTABLE style does not seem to be enough
# - handle the tooltip request for text - needs a handler like NeedsText in tooltip.xs # - 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.

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 # STYLE 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 tooltip's 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 the 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);
    ($l, $t) = $self->ClientToScreen($l, $t);
    ($r, $b) = $self->ClientToScreen($r, $b);

    # Create a menu.
    my $menu = Win32::GUI::Menu->new(
      "" => "PopUpMenu",
"> MenuItem&1" => { -name => "MI1", -onClick => sub { print "MenuItem1 clicked\n";} }, "> MenuItem&2" => { -name => "MI2", -onClick => sub { print "MenuItem2 clicked\n";} },
      "> -"          => 0,
"> MenuItem&3" => { -name => "MI3", -onClick => sub { print "MenuItem3 clicked\n";} },
    );

    # Display and interact with the popup menu.
    $self->TrackPopupMenu($menu->{PopUpMenu}, $l, $b, $l, $t, $r, $b);
  }

  return 1;
}

Reply via email to