Re: Gtk2::ActionGroup problem

Martin Junius said:
Hi all!

I'm trying to replace ItemFactory from an old app with UIManager,
working along the example, but I can't seem to get it right:

my $uixml = <<ENDOFXML;
   <menubar name="menubar">
     <menu action="file">
       <menuitem action="quit"/>
     <menu action="script">
       <menuitem action="run"/>
     <menu action="help">
       <menuitem action="about"/>

my @uientries =
    # name,   stock id,       label
    [ "file", undef,          "_File"         ],
    [ "script",       undef,          "_Script"       ],
    [ "help",     undef,              "_Help"         ],
    # name,      stock id,      label,    accelerator,  tooltip
    [ "quit",    'gtk-quit',    "_Quit",  "<control>Q", "Quit",
\&gscript_exit ],
    [ "run",  'gtk-execute', "_Run",   "<control>R", "Run script", \&run
    [ "about",   'gtk-help',    "_About", undef,        "About",
\&gscript_about ],

sub gscript_create_menubar2 {
     my ($win) = @_;

     my $actions = Gtk2::ActionGroup->new("Actions");
     $actions->add_actions (\ uientries, undef);

     my $ui = Gtk2::UIManager->new;
     $ui->insert_action_group($actions, 0);

     return $ui->get_widget("/menubar");

The add_actions() barfs "action array is empty" at me. Any idea what
could be wrong?

According to the code of the binding for gtk_action_group_add_actions(),

        SV * action_entries = ST(1);
        av = (AV*) SvRV (action_entries);
        n_actions = av_len (av) + 1;
        if (n_actions < 1)
                croak ("action array is empty");

the array you're passing in, @uientries, is empty.

Since just a few lines above you initialize it to non-empty, i'm going to
guess that your source file is laid out like this:

  use use use
  set stuff up
  my @uientries = ();
  sub gscript_create_menubar2 { ...

That is, you're being bitten by order of execution --- the call to Gtk2->main
happens earlier in the file than the initialization of @uientries, so when you
get into gscript_create_menubar2, @uientries has never been filled.

Solution:  use a BEGIN block around the initialization, put it in module, or
otherwise rearrange things to make sure it executes first.

(I've fallen prey to this problem more times than i'd like to admit.)

muppet <scott at asofyet dot org>

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]