Re: Gtk2::ActionGroup problem




Martin Junius said:
Hi all!

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

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

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);
     $win->add_accel_group($ui->get_accel_group);
     $ui->add_ui_from_string($uixml);

     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:

  #!perl
  use use use
  set stuff up
  Gtk2->main;
  subroutines
  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]