Kevin Ryde > Gtk2-Ex-TiedListColumn-5 > Gtk2::Ex::TiedMenuChildren



Annotate this POD


View/Report Bugs
Module Version: 5   Source  


Gtk2::Ex::TiedMenuChildren - tie an array to the items of a Gtk2 menu


 use Gtk2::Ex::TiedMenuChildren;

 my $menu = Gtk2::Menu->new;
 my @array;
 tie @array, 'Gtk2::Ex::TiedMenuChildren', $menu;

 my $menuitem = $array[3];   # fourth menu item

 my $aref = Gtk2::Ex::TiedMenuChildren->new ($menu);


Gtk2::Ex::TiedMenuChildren ties an array to the children of a Gtk2::Menu or Gtk2::MenuBar. Changes to the children are reflected in the array, and changes to the array update the menu.

push and unshift correspond to append and prepend. Storing to the array is a remove() of the old item at that position and insert of the new. Remember an item can only be in one menu at a time.

Like most tie things this is likely better in concept than actual use. Normally it's enough to get_children and act on that list.

This tie is named for Gtk2::Menu but works with Gtk2::MenuBar or any Gtk2::MenuShell subclass. But it can't be used on just any Gtk2::Container because a plain container doesn't have an "insert" at a particular position among its children -- that's something only in classes like MenuShell.

delete and exists

A menu has no notion of undef in a child item position. In the current code a delete removes the item and shuffles the remainder down, which is unlike a plain Perl array where the rest don't move (see "delete" in perlfunc). exists on a TiedChildren simply reports whether the array element is within the number of child items.

Deleting the endmost element of a TiedChildren works the same as an ordinary array though. In this case the menu is shortened and exists on that element is false, being beyond the available items.


In the following $menu is a Gtk2::Menu, Gtk2::MenuBar or other subclass of Gtk2::MenuShell.

tie @var, 'Gtk2::Ex::TiedMenuChildren', $menu

Tie array variable @var to the given menu so it accesses the child items of that widget.

Gtk2::Ex::TiedMenuChildren->new ($menu)

Return an arrayref which is tied to the child items of $menu. For example

    my $aref = Gtk2::Ex::TiedMenuChildren->new ($menu);

is the same as

    tie (my @array, 'Gtk2::Ex::TiedMenuChildren', $menu);
    my $aref = \@array;

If you want your own @array then the plain tie is easier. If you want an arrayref to pass around to other funcs then new saves a line of code.

Object Methods

The tie object under the array, as returned by the tie or obtained later with tied, has the following methods.


Return the underlying menu widget. Eg.

    my @array;
    tie @array, 'Gtk2::Ex::TiedMenuChildren', $menu;
    my $mtcobj = tied(@array);
    print $mtcobj->menu;

Or likewise on an arrayref

    my $aref = Gtk2::Ex::TiedMenuChildren->new($menu);
    my $menu = tied(@$aref)->menu;


Gtk2::Menu, Gtk2::MenuBar, Gtk2::MenuShell

Gtk2::Ex::TiedListColumn, Gtk2::Ex::TiedTreePath



Copyright 2010 Kevin Ryde

Gtk2-Ex-TiedListColumn is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

Gtk2-Ex-TiedListColumn is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Gtk2-Ex-TiedListColumn. If not, see

syntax highlighting: