~nafai/+junk/minimal-menu-example

« back to all changes in this revision

Viewing changes to src/main.c

  • Committer: Travis B. Hartwell
  • Date: 2010-04-21 19:01:49 UTC
  • Revision ID: nafai@travishartwell.net-20100421190149-4vyelhawpbfjuw4w
Adding menu items via actions to a placeholder.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
#include <config.h>
27
27
 
 
28
#include <glib.h>
 
29
#include <glib/gprintf.h>
 
30
 
28
31
#include <gtk/gtk.h>
29
32
 
30
33
#include <libappindicator/app-indicator.h>
37
40
 
38
41
static GtkBuilder *xml = NULL;
39
42
static GtkActionGroup *minimal_action_group = NULL;
 
43
static int counter = 0;
40
44
 
41
45
static GtkWidget
42
46
*create_menu(void)
73
77
        return GTK_WIDGET (gtk_builder_get_object (xml, "minimal-popup"));
74
78
}
75
79
 
 
80
void
 
81
add_submenu (void)
 
82
{
 
83
        GtkUIManager  *uimanager;
 
84
        GtkAction *action;
 
85
        gchar *name;
 
86
        guint menu_merge_id;
 
87
 
 
88
        uimanager = GTK_UI_MANAGER (gtk_builder_get_object (xml, "minimal-ui-manager"));
 
89
 
 
90
        name = g_strdup_printf ("menu-%02d", ++counter);
 
91
 
 
92
        action = gtk_action_new (name, name, NULL, NULL);
 
93
        gtk_action_group_add_action (minimal_action_group, action);
 
94
        g_object_unref (action);
 
95
 
 
96
        menu_merge_id = gtk_ui_manager_new_merge_id (uimanager);
 
97
        gtk_ui_manager_add_ui (uimanager, menu_merge_id,
 
98
                                                   "/minimal-popup/menu-placeholder",
 
99
                                                   name, name,
 
100
                                                   GTK_UI_MANAGER_MENU, FALSE);
 
101
        g_object_set_data_full (G_OBJECT (action), "merge-id",
 
102
                                                        GUINT_TO_POINTER (menu_merge_id), NULL);
 
103
 
 
104
        g_printf("Adding %s\n", name);
 
105
 
 
106
        /* Make sure things are visible */
 
107
        gtk_action_group_set_visible (minimal_action_group, TRUE);
 
108
        gtk_ui_manager_ensure_update (uimanager);
 
109
}
 
110
 
 
111
static void display_menu (GtkWidget *widget, gpointer data)
 
112
{
 
113
        GtkWidget *submenu;
 
114
        const gchar *label;
 
115
        const gchar *type;
 
116
 
 
117
        if (GTK_IS_MENU(widget))
 
118
        {
 
119
                label = gtk_menu_get_title(GTK_MENU(widget));
 
120
                submenu = widget;
 
121
        }
 
122
        else if (GTK_IS_SEPARATOR_MENU_ITEM(widget))
 
123
        {
 
124
                label = g_strdup_printf("----------");
 
125
                submenu = NULL;
 
126
        }
 
127
        else
 
128
        {
 
129
                label = gtk_menu_item_get_label (GTK_MENU_ITEM (widget));
 
130
                submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM(widget));
 
131
        }
 
132
 
 
133
        type = G_OBJECT_TYPE_NAME(widget);
 
134
 
 
135
        if (label != NULL)
 
136
        {
 
137
                if (gtk_widget_get_visible(widget))
 
138
                {
 
139
                        g_printf("menu label: %s on object type: %s\n", label, type);
 
140
                }
 
141
                else
 
142
                {
 
143
                        g_printf("menu label: (%s) on object type: %s\n", label, type);
 
144
                }
 
145
        }
 
146
 
 
147
        if (submenu != NULL)
 
148
        {
 
149
        gtk_container_foreach( GTK_CONTAINER (submenu),
 
150
                               display_menu,
 
151
                               NULL );
 
152
        }
 
153
}
 
154
 
76
155
int
77
156
main (int argc, char *argv[])
78
157
{
79
158
        AppIndicator *indicator;
80
159
        GtkWidget *menu;
 
160
        int i;
81
161
 
82
162
        gtk_init (&argc, &argv);
83
163
 
89
169
        app_indicator_set_menu (indicator, GTK_MENU (menu));
90
170
        app_indicator_set_status (indicator, APP_INDICATOR_STATUS_ACTIVE);
91
171
 
 
172
        display_menu (menu, NULL);
 
173
 
 
174
        for (i=0; i < 5; i++)
 
175
        {
 
176
                add_submenu ();
 
177
        }
 
178
 
 
179
        display_menu (menu, NULL);
 
180
 
92
181
        gtk_main ();
93
182
 
94
183
        return 0;