Greetings fellow Monastarians...

I have been playing around with Perl/Tk widgets in general, menus in specific, and have run into an issue that I just can't seem to overcome.

Say you have a widget that has a menu, a text widget, for instance, and you want to add a custom option menu to the existing menu. That's pretty simple. Here's a minimal example.

#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Text; my $top = MainWindow->new; my $text = $top->Text->pack(-expand => 'yes', -fill => 'both'); my $menu = $text->menu; add_my_menu_item($menu, 'bogus', $text); $top->configure(-menu => $menu); MainLoop; sub add_my_menu_item{ my ($menu, $name, $widget) = @_; my $entry = $menu->Cascade( -label => $name); map ( $entry->command ( -label => $_, -command => [sub {$widget->insert('insert', $_[0])}, $ +_], ) (qw/1 2 3 4 5/) ); }

No problem. Works great. But.... Say you wanted to insert the new custom menu option at someplace other than the end of the menu. How to do that? Well, Tk::Menu provides an insert method, but when you insert a menu item with the insert method, it doesn't return a reference. And I can't figure out any (good) way to get a reference to it once it has been created.

#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Text; my $top = MainWindow->new; my $text = $top->Text->pack(-expand => 'yes', -fill => 'both'); my $menu = $text->menu; add_my_menu_item($menu, 2, 'bogus', $text); $top->configure(-menu => $menu); MainLoop; sub add_my_menu_item{ my ($menu, $index, $name, $widget) = @_; my $entry = $menu->insert($index, 'cascade', -label => $name); return unless defined $entry; # No returned ref! map ( $entry->command ( -label => $_, -command => [sub {$widget->insert('insert', $_[0])}, $ +_], ) ,(qw/1 2 3 4 5/) ); }

Now I have found a (broken, ugly) work-around. It works but throws a warning that can't be suppressed without redirecting STDERR. I'm creating the menu item at the end of the menu, deleting it, which undefs the hash table for that entry, then inserting the item with the same name at the index I want it. The names collide in the hash, the new menu item gets written into the location formerly held by the old entry but I now have a reference which points to the memory location now occupied by the inserted item so I can populate the cascade menu.

#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Text; my $top = MainWindow->new; my $text = $top->Text->pack(-expand => 'yes', -fill => 'both'); my $menu = $text->menu; add_my_menu_item($menu, 2, 'bogus', $text); $top->configure(-menu => $menu); MainLoop; sub add_my_menu_item{ my ($menu, $index, $name, $widget) = @_; my $entry = $menu->Cascade( -label => $name); $menu->delete('last'); $menu->insert($index, 'cascade', -label => $name); map ( $entry->command ( -label => $_, -command => [sub {$widget->insert('insert', $_[0])}, $ +_], ) ,(qw/1 2 3 4 5/) ); }

Does anyone know of a better way to do this? I've been trying to figure it out for about 3 days now and am not much further than when I started.

For those interested: The warning I am getting is

Had to (re-)reate menu for bogus at F:/Perl/site/lib/Tk/Menu/Item.pm l +ine 135.

The misspelling in the warning and the fact that it can't be suppressed leads me to believe that it might just be a left-over debugging warning. I'm running Active Perl 5.8.6 with Tk804.027 under Win2k & WinXP

Thanks


In reply to $menu->insert problem in Perl/Tk by thundergnat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.