Customizing the menu

If you click with button 3 on an existing applet, you'll notice that a stock menu pops up, with a "Remove" and a "Move" option. Some applets also have custom menu items like "About" or "Properties". These are the functions provided to manipulate the menu:

void applet_widget_register_callback(AppletWidget *applet, char *name, char *menutext, AppletCallbackFunc func, gpointer data);

void applet_widget_register_stock_callback(AppletWidget *applet, char *name, char *stock_type, char *menutext, AppletCallbackFunc func, gpointer data);

Inserts a menu item into the panel menu. name is used to identify the menu item. It is also the path of the menu item, this way you can specify which submenu the new item should go in. menutext is simply the label of the item. func is a pointer to a void function (it is called with two arguments: an AppletWidget* and a gpointer). The gpointer that is passed to *func is specified in data.

applet_widget_register_stock_callback is the same as applet_widget_register_callback, the stock_type argument is used to specify a stock GNOME pixmap (see libgnomeui/gnome-stock.h for a list of definitions).

void applet_widget_register_callback_dir(AppletWidget *applet, char *name, char *menutext);

void applet_widget_register_stock_callback_dir(AppletWidget *applet, char *name, char *stock_type, char *menutext);

Creates a new sub-menu. You can later insert menu items to it by using applet_widget_register_callback, specifying name in the path. Again, applet_widget_register_stock_callback_dir also inserts a stock pixmap.

void applet_widget_unregister_callback(AppletWidget *applet, char *name);

void applet_widget_unregister_callback_dir(AppletWidget *applet, char *name);

Removes the menu item or submenu specified by name.

Examples

Example 4. Menu example

    applet_widget_register_stock_callback (APPLET_WIDGET(applet),
                                           "about",
                                           GNOME_STOCK_MENU_ABOUT,
                                           _("About"),
                                           &my_applet_cb_about,
                                           NULL);
				      
    applet_widget_register_callback_dir (APPLET_WIDGET(applet),
				         "submenu",
				         _("Test submenu"));
				   
    applet_widget_register_callback (APPLET_WIDGET(applet),
				     "submenu/window",
				     _("Open window"),
				     &my_applet_cb_showwnd,
				     NULL);
	

Note: The _("") functions are simply macros for GNU Gettext support, defined in one of the GNOME support headers.

If you use GTK--, the data argument can be used to call an instance method, as seen in the following example:

Example 5. Using data for keeping the instance

void my_applet_cb_foo(AppletWidget* caller, gpointer data)
{
    (My_Class*)data->foo();
}

void My_Class::foo()
{
    /* For example, bar could be a member field */
    bar = quux();
    
}

void My_Class::My_Class()
{
    /* ... */
    applet_widget_register_callback (APPLET_WIDGET(applet),
				     "foo",
				     "Update foo",
				     &my_applet_cb_foo,
				     (gpointer)this);
}
	

Note: If we passed &My_Class::foo as the callback function, there would be no way to get the object instance from the member function (this is, of course, not an issue with static members)

Note: When you pass this as a gpointer, be sure to cast it back exactly to the same type in the callback function, otherwise data corruption could occur (you can always use the various _cast<> functions for runtime pointer casting)