callback_data in Gtk2::SimpleMenu [and Gtk2::ItemFactory (well, Gtk2.pm)]



I ran into an interesting problem recently with Gtk2::SimpleMenu. For
some reason, Gtk2::SimpleMenu and Gtk2::ItemFactory::create_items do
not support per-item callback_data, even though it's supported by the
Gtk+ API.

While you can rather easily emulate callback_data using 

callback => sub {foo(@_,$bar)};

this seems less than optimal.

The following diff fixes Gtk2::SimpleMenu and Gtk2.pm to allow using
callback_data. [I'm not quite sure if I like
Gtk2::ItemFactory::create_item(s) being in Gtk2, but whatever...]

Before applying it, I would suggest reconsidering how the
$callback_data is passed, and perhaps not passing a $callback_data
from create_items at all, just use it when $callback_data is not
defined for the entry, or (pre|app)pend the $callback_data to the
entries $callback_data instead of defaulting to the passed
$callback_data as this patch does.


--- Gtk2.pm.orig        2004-01-30 19:24:48.000000000 -0800
+++ Gtk2.pm     2004-01-30 19:31:25.000000000 -0800
@@ -69,11 +69,12 @@
 sub create_item {
        my ($factory, $entry, $callback_data) = @_;
        my ($path, $accelerator, $callback, $callback_action, $item_type, 
-           $extra_data, $cleanpath);
+           $extra_data, $cleanpath, $entry_callback_data);
 
        if ('ARRAY' eq ref $entry) {
                ($path, $accelerator, $callback, $callback_action, 
-                $item_type, $extra_data) = @$entry;
+                $item_type, $extra_data,$entry_callback_data) = @$entry;
+               $callback_data ||= $entry_callback_data;
        } elsif ('HASH' eq ref $entry) {
                foreach (keys %$entry)
                {
@@ -101,13 +102,18 @@
                        {
                                $extra_data = $entry->{extra_data};
                        }
+                       elsif( $_ eq 'callback_data')
+                       {
+                               $callback_data ||= $entry->{callback_data};
+                       }
                        else
                        {
                                use Carp;
                                carp("Gtk Item Factory Entry; unknown key ($_) "
                                   . "ignored, legal keys are: path, "
                                   . "accelerator, accel, callback, "
-                                  . "callback_action, item_type, extra_data");
+                                  . "callback_action, item_type, extra_data"
+                                  . "callback_data");
                        }
                }
        } else {
@@ -123,6 +129,7 @@
                    . "           callback_action => \$action,\n"
                    . "           item_type       => \$type,    # optional\n"
                    . "           extra_data      => \$extra,   # optional\n"
+                   . "           callback_data   => \$callback_data #optional\n"
                    . "         }\n"
                    . "  ";
        }


--- SimpleMenu.pm.orig  2004-01-30 19:32:32.000000000 -0800
+++ SimpleMenu.pm       2004-01-30 19:32:10.000000000 -0800
@@ -116,7 +116,9 @@
                                         (exists($groups[$grp]) ? 
                                                 $groups[$grp] :
                                                 $itms->{item_type}), 
-                                        $itms->{extra_data}, ];
+                                        $itms->{extra_data},
+                                        $itms->{callback_data},
+                                      ];
 
                        # create the group identifier (path)
                        # so that next button in this group will
@@ -137,7 +139,9 @@
                                                 $default_callback ),
                                         $itms->{callback_action},
                                         $itms->{item_type},
-                                        $itms->{extra_data}, ];
+                                        $itms->{extra_data},
+                                        $itms->{callback_data},
+                                      ];
                }
        }

[My appologies for not making the above patch against CVS... for some
reason SF's CVS repository is down right now.]


Don Armstrong

-- 
We were at a chinese resturant.
He was yelling at the waitress because there was a typo in his fortune
cookie.
 -- hugh macleod http://www.gapingvoid.com/batch31.php

http://www.donarmstrong.com
http://www.anylevel.com
http://rzlab.ucr.edu

Attachment: signature.asc
Description: Digital signature



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