]> Panel Applet Library Reference Manual Mark McLoughlin
mark@skynet.ie
2001 2003 Sun Microsystems, Inc. This manual documents the interfaces of the panel applet library for MATE 2.x and a short guide to porting applets from the MATE 1.x interfaces.
Writing Applets Writing applets is very simple. You take some boiler plate code like below, change a couple of things and write the code that implements your widgetry. The hardest part is writing your widgetry - and its completely up to yourself how hard that should be. Hello World Applet As usual, following the pointless tradition of starting with an example of how get 'Hello World' on the screen in some form, here's just about the simplest applet you could write. #include <string.h> #include <mate-panel-applet.h> #include <gtk/gtklabel.h> static gboolean hello_applet_fill (MatePanelApplet *applet, const gchar *iid, gpointer data) { GtkWidget *label; if (strcmp (iid, "OAFIID:My_HelloApplet") != 0) return FALSE; label = gtk_label_new ("Hello World"); gtk_container_add (GTK_CONTAINER (applet), label); gtk_widget_show_all (GTK_WIDGET (applet)); return TRUE; } MATE_PANEL_APPLET_MATECOMPONENT_FACTORY ("OAFIID:My_HelloApplet_Factory", PANEL_TYPE_APPLET, "TheHelloWorldApplet", "0", hello_applet_fill, NULL); The code here is very similar to writing a normal MateComponent control. You define a factory using MATE_PANEL_APPLET_MATECOMPONENT_FACTORY(), passing it a factory function like hello_applet_fill(). libmate-panel-applet automatically creates a #MatePanelApplet object for you, passing this to your factory method. Here, you should fill the applet with your widgets just like a #GtkBin. For example, if you were writing a cdplayer applet you would create a #GtkHBox, pack the hbox with the cdplayer buttons and in turn add the hbox to the applet. MateComponent Activation .server Files For Applets Since an applet is a matecomponent component, you must write a .server file so that the matecomponent activation daemon is aware that your component exists and how to activate it. Copy and paste is your friend here ... <oaf_info> <oaf_server iid="OAFIID:My_HelloApplet_Factory" type="exe" location="test-matecomponent-applet"> <oaf_attribute name="repo_ids" type="stringv"> <item value="IDL:MateComponent/GenericFactory:1.0"/> <item value="IDL:MateComponent/Unknown:1.0"/> </oaf_attribute> <oaf_attribute name="name" type="string" value="Hello World Applet Factory"/> <oaf_attribute name="description" type="string" value="My first applet factory"/> </oaf_server> <oaf_server iid="OAFIID:My_HelloApplet" type="factory" location="OAFIID:My_HelloApplet_Factory"> <oaf_attribute name="repo_ids" type="stringv"> <item value="IDL:MATE/Vertigo/MatePanelAppletShell:1.0"/> <item value="IDL:MateComponent/Control:1.0"/> <item value="IDL:MateComponent/Unknown:1.0"/> </oaf_attribute> <oaf_attribute name="name" type="string" value="Hello World Applet"/> <oaf_attribute name="description" type="string" value="My first applet for the MATE2 panel"/> <oaf_attribute name="panel:icon" type="string" value="mate-applets.png"/> </oaf_server> </oaf_info> Probably the most important thing to note here is that, unlike .server files for other matecomponent components, applet .server files contain a special attribute called 'panel:icon'. This is used by the panel to display an entry for the applet in the 'Add to Panel' dialog. Defining a Popup Context Menu FIXME: write Detecting Changes in the Panel. FIXME: write Session/Preference Saving. FIXME: write Multiple Applets FIXME: write Porting Applets from the MATE 1.x interfaces In MATE 1.x the applet interface lived in a header called applet-widget.h. The interface was based on GOAD, the MATE 1.x object activation framework. A new interface was designed for MATE 2.x using the power of matecomponent UI embedding and the new object activation framework, matecomponent-activation. The interface is intended to be easy to use, cruft free, but semantically similar to the old API in order to make porting relatively painless. Applet Activation The first thing to change when porting to the new API is the header. Include mate-panel-applet.h instead of applet-widget.h. Next you need to change how the applet is activated. Browsing through old applet's code, its obvious that this was done in various ways in the past. The best advice is to hunt out the calls to applet_widget_init, applet_widget_new and applet_widget_add. applet_widget_new and applet_widget_add are now effectively merged into one call mate_panel_applet_new, to which the top-level widget of the applet should be passed. applet_widget_init is not neccessary anymore. So the new code should look something like this #include <mate-panel-applet.h> static MateComponentObject * blah_applet_new () { MatePanelApplet *applet; /* * The old code setting up the applet widgetry * goes here. So effectively delete calls to * applet_widget_init and applet_widget_new * and the replace applet_widget_add with a call * to mate_panel_applet_new. */ applet = mate_panel_applet_new (label); return MATECOMPONENT_OBJECT (mate_panel_applet_get_control (applet)); } static MateComponentObject * blah_applet_factory (MateComponentGenericFactory *this, const gchar *iid, gpointer data) { MateComponentObject *applet = NULL; if (!strcmp (iid, "OAFIID:BlahApplet")) applet = blah_applet_new (); return applet; } MATE_PANEL_APPLET_MATECOMPONENT_FACTORY ("OAFIID:BlahApplet_Factory", "Blah", "0", blah_applet_factory, NULL) You should use MATE_PANEL_APPLET_MATECOMPONENT_FACTORY or MATE_PANEL_APPLET_MATECOMPONENT_SHLIB_FACTORY depending on whether you want the applet to be out of process or in process. Activation files The next thing to do may be to port from a .gnorba file to a matecomponent-activation .server file. You no longer need a .desktop file for applets. A .gnorba looks something like this : [blah] type=exe repo_id=IDL:MATE/Applet:1.0 description=Blah location_info=blah-de-blah Your .server file should look like this : <oaf_info> <oaf_server iid="OAFIID:BlahApplet" type="exe" location="blah-de-blah-2"> <oaf_attribute name="repo_ids" type="stringv"> <item value="IDL:MateComponent/GenericFactory:1.0""/> <item value="IDL:MateComponent/Unknown:1.0"/> </oaf_attribute> <oaf_attribute name="name" type="string" value="Blah Factory"/> <oaf_attribute name="description" type="string" value="Blah De Blah"/> </oaf_server> <oaf_server iid="OAFIID:BlahApplet" type="factory" location="OAFIID:BlahApplet_Factory"> <oaf_attribute name="repo_ids" type="stringv"> <item value="IDL:MATE/MatePanelAppletShell:1.0"/> <item value="IDL:MateComponent/Control:1.0"/> <item value="IDL:MateComponent/Unknown:1.0"/> </oaf_attribute> <oaf_attribute name="name" type="string" value="Blah Applet"/> <oaf_attribute name="description" type="string" value="Blah De Blah"/> <oaf_attribute name="panel:icon" type="string" value="blah-de-blah.png"/> </oaf_server> </oaf_info> A lot of this should be copied and pasted. The most important bit is "panel:icon" which specfies the icon that should be displayed in the "Add to Panel" dialog. Context Menu Most applets also place extra menu items into it context menu. It might be a good idea to port this next. In MATE 1.x this was done using the applet_widget_register_stock_callback API call. In MATE 2.x 3 things must be done An xml desription of the popup menu must be written. A description of the verbs must be prepared. This is basically a list of callbacks to be call when a certain menu item is clicked in the popup. The menu is registered using a call to mate_panel_applet_setup_menu. The xml description should look something like this : static const char fish_menu_xml [] = "<popup name=\"button3\">\n" " <menuitem name=\"Properties Item\" verb=\"BlahProperties\" _label=\"Properties ...\"\n" " pixtype=\"stock\" pixname=\"gtk-properties\"/>\n" " <menuitem name=\"Help Item\" verb=\"BlahHelp\" _label=\"Help\"\n" " pixtype=\"stock\" pixname=\"gtk-help\"/>\n" " <menuitem name=\"About Item\" verb=\"BlahAbout\" _label=\"About ...\"\n" " pixtype=\"stock\" pixname=\"mate-stock-about\"/>\n" "</popup>\n"; This could also be in a seperate .xml file and loaded with mate_panel_applet_setup_menu_from_file. The description of the verbs should look something like : static const MateComponentUIVerb fish_menu_verbs [] = { MATECOMPONENT_UI_VERB ("BlahProperties", display_properties_dialog), MATECOMPONENT_UI_VERB ("BlahHelp", display_help_dialog), MATECOMPONENT_UI_VERB ("BlahAbout", display_about_dialog), MATECOMPONENT_UI_VERB_END }; This is just a list of callbacks invoked when the menu items are clicked. There are other macros you may use other than MATECOMPONENT_UI_VERB - see matecomponent-ui-component.h. To actually register the menu you just do something like : mate_panel_applet_setup_menu (MATE_PANEL_APPLET (blah->applet), blah_menu_xml, blah_menu_verbs, blah); The last argument is the user_data argument passed back to the callbacks. The Panel Applet Library &MatePanelApplet;