Thanks to Peter Eisengrein and Leonard Jacobsen for giving me the pointers to 
build this example.

The code below creates a window with a richtext control. The richtext can be 
populated with strings separated by commas. When the menu button is clicked, a 
popup menu is shown, with a sub menu containing all the strings in the richtext 
control. When the sub menu items are selected a message is displayed in the 
status bar.

Problems. As been discussed on this list before:
  * Memory leaks when using menus in this way (about 4K everytime the menu is 
recreated, or more depending on the number of items in the menu). This can be 
minimised by only recreating the menu when you actually need to.
  * Using Menus and the NEM in the same window.

Comments, thoughts?

Cheers,

jez.

==========

use Win32::GUI;
use strict;


my $menu;   #The menu
my @colors; #A list of the colors in the popup

#create the main window
my $mainwindow = new Win32::GUI::Window(
    -title    => "Win32::GUI::Rebar test",
    -left     => 100,
    -top      => 100,
    -width    => 600,
    -height   => 200,
    -name     => "Window",   
);

$mainwindow->AddStatusBar(
    -name   => "Status",
);

$mainwindow->AddRichEdit (
    -name      => 'ColorRE',
    -pos       => [2, 2],
    -size      => [320, 40],
    -multiline => 1,
    -addstyle  => 2097152,
    -addstyle  => 4096,
    );    
$mainwindow->ColorRE->Text('Red,Green,Blue,Yellow,Orange,Brown');    
$mainwindow->AddButton (
    -name     => 'Menu',
    -pos      => [70, 50],
    -size     => [60, 20],
    -text     => 'Menu',
    -tip      => 'Show Menu',
);

$mainwindow->Show;

Win32::GUI::Dialog;

sub Window_Resize { $mainwindow->Status->Resize(0,0); }

sub Menu_Click {
  CreateMenu();
  $mainwindow->TrackPopupMenu($menu->{PopUpMenu}, Win32::GUI::GetCursorPos());
  }

sub GenericColorClick {
  my $item=shift;
  $mainwindow->Status->Text('Item clicked '.$item.' '.$colors[$item]);
}

sub CreateMenu {
  #Get the colors from the richedit control
  @colors=split(',',$mainwindow->ColorRE->Text);
  my @items;
  my $subname;
  my $count;
  #loop round all colors, create a new sub to handle the event
  foreach my $col (@colors) {
    $subname = "Colour$count".'_Click';
    eval ("sub $subname { GenericColorClick ($count);}");
    die "error in eval $@ " if $@;
    push @items,">> $col";
    push @items,"Colour$count";
    $count++;
  }

  $menu = new Win32::GUI::Menu( 
     "" => 'PopUpMenu', 
     "> &Hide" => "Edit", 
     "> &Minimize" => "Edit", 
     "> -" => 0, 
     "> Colors" => "Edit", 
     @items,
     "> Cars" => "Edit1", 
     ">> Aston Martin" => "Edit", 
     "> -" => 0, 
     "> &Quit" => "Exit", 
         );
};

Reply via email to