PopupMenu.h

Go to the documentation of this file.
00001 #ifndef POPUPMENU_H_
00002 #define POPUPMENU_H_
00003 
00004 #include <gtk/gtkwidget.h>
00005 #include <gtk/gtkmenuitem.h>
00006 #include <list>
00007 
00008 namespace gtkutil
00009 {
00010 
00015     class PopupMenu
00016     {
00017         public:
00018 
00019             typedef bool (*SensitivityTest)(void);
00020 
00021         private:
00022 
00023             // Main menu widget
00024             GtkWidget* _menu;
00025 
00026             // Data class containing the elements of a menu item
00027             struct MenuItem
00028             {
00029                     GtkWidget* widget;
00030                     GFunc callback;
00031                     gpointer userData;
00032                     SensitivityTest test;
00033 
00034                     MenuItem (GtkWidget* w, GFunc c, gpointer ud, SensitivityTest t) :
00035                         widget(w), callback(c), userData(ud), test(t)
00036                     {
00037                     }
00038             };
00039 
00040             // List of menu items
00041             typedef std::list<MenuItem> MenuItemList;
00042             MenuItemList _menuItems;
00043 
00044         private:
00045 
00046             /*
00047              * Default sensitivity test. Return true in all cases. If a menu item does
00048              * not specify its own sensitivity test, this will be used to ensure the
00049              * item is always visible.
00050              */
00051             static bool _alwaysVisible ()
00052             {
00053                 return true;
00054             }
00055 
00056             /* GTK CALLBACKS */
00057 
00058             // Main activation callback from GTK
00059             static void _onActivate (GtkMenuItem* item, MenuItem* menuItem)
00060             {
00061                 menuItem->callback(menuItem->widget, menuItem->userData);
00062             }
00063 
00064             // Mouse click callback (if required)
00065             static gboolean _onClick (GtkWidget* w, GdkEventButton* e, PopupMenu* self);
00066 
00067         public:
00068 
00078             PopupMenu (GtkWidget* widget = NULL);
00079 
00083             ~PopupMenu ()
00084             {
00085                 g_object_unref(_menu);
00086             }
00087 
00103             void addItem (GtkWidget* widget, GFunc callback, gpointer userData, SensitivityTest test = _alwaysVisible);
00104 
00110             void show ();
00111     };
00112 } // namespace gtkutil
00113 
00114 #endif /*POPUPMENU_H_*/

Generated by  doxygen 1.6.2