Commit a09222db authored by angelskieglazki's avatar angelskieglazki

added submenu

parent 19d958a8
...@@ -39,15 +39,23 @@ static void quit_cb(struct tray_menu *item) { ...@@ -39,15 +39,23 @@ static void quit_cb(struct tray_menu *item) {
tray_exit(); tray_exit();
} }
static void submenu_cb(struct tray_submenu *item) {
(void)item;
printf("submenu cb\n");
tray_update(&tray);
}
//struct tray_submenu *t_sm = (struct tray_submenu[]){{"First",submenu_cb}, {"Second",submenu_cb}, {NULL, NULL},};
static struct tray tray = { static struct tray tray = {
.icon = TRAY_ICON1, .icon = TRAY_ICON1,
.menu = (struct tray_menu[]){{"Hello", 0, 0, hello_cb, NULL}, .menu = (struct tray_menu[]){{"Hello", 0, 0, NULL, hello_cb, NULL},
{"Checked", 0, 1, toggle_cb, NULL}, {"Checked", 0, 1, NULL, toggle_cb, NULL},
{"Disabled", 1, 0, NULL, NULL}, {"Disabled", 1, 0, NULL, NULL, NULL},
{"-", 0, 0, NULL, NULL}, {"-", 0, 0, NULL, NULL, NULL},
{"+", 0, 0, NULL, NULL}, {"SubMenu", 0, 0, (struct tray_submenu[]){{"First",submenu_cb, NULL}, {"Second",submenu_cb, NULL}, {NULL, NULL, NULL},}, NULL, NULL},
{"Quit", 0, 0, quit_cb, NULL}, {"Quit", 0, 0, NULL, quit_cb, NULL},
{NULL, 0, 0, NULL, NULL}}, {NULL, 0, 0, NULL, NULL, NULL}},
}; };
int main() { int main() {
......
...@@ -8,10 +8,17 @@ struct tray { ...@@ -8,10 +8,17 @@ struct tray {
struct tray_menu *menu; struct tray_menu *menu;
}; };
struct tray_submenu {
char *text;
void (*cb)(struct tray_submenu *);
void *context;
};
struct tray_menu { struct tray_menu {
char *text; char *text;
int disabled; int disabled;
int checked; int checked;
struct tray_submenu *submenu;
void (*cb)(struct tray_menu *); void (*cb)(struct tray_menu *);
void *context; void *context;
...@@ -35,6 +42,12 @@ static void _tray_menu_cb(GtkMenuItem *item, gpointer data) { ...@@ -35,6 +42,12 @@ static void _tray_menu_cb(GtkMenuItem *item, gpointer data) {
m->cb(m); m->cb(m);
} }
static void _tray_submenu_cb(GtkMenuItem *item, gpointer data) {
(void)item;
struct tray_submenu *s_m = (struct tray_submenu *)data;
s_m->cb(s_m);
}
static int tray_init(struct tray *tray) { static int tray_init(struct tray *tray) {
if (gtk_init_check(0, NULL) == FALSE) { if (gtk_init_check(0, NULL) == FALSE) {
return -1; return -1;
...@@ -55,28 +68,34 @@ static void tray_update(struct tray *tray) { ...@@ -55,28 +68,34 @@ static void tray_update(struct tray *tray) {
GtkMenuShell *menu = (GtkMenuShell *)gtk_menu_new(); GtkMenuShell *menu = (GtkMenuShell *)gtk_menu_new();
for (struct tray_menu *m = tray->menu; m != NULL && m->text != NULL; m++) { for (struct tray_menu *m = tray->menu; m != NULL && m->text != NULL; m++) {
GtkWidget *item; GtkWidget *item;
if (strcmp(m->text, "+") == 0) { if (m->submenu != NULL) {
GtkWidget *submenu1 = gtk_menu_new();
item = gtk_menu_item_new_with_label("SubMenu1"); GtkWidget *submenu = gtk_menu_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu1); item = gtk_menu_item_new_with_label(m->text);
GtkWidget* submenu1_option; gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
submenu1_option = gtk_menu_item_new_with_label("Submenu option!");
gtk_menu_shell_append(GTK_MENU_SHELL(submenu1), submenu1_option); for (struct tray_submenu *s_m = m->submenu; s_m != NULL && s_m->text != NULL; s_m++) {
GtkWidget* submenu_item;
submenu_item = gtk_menu_item_new_with_label(s_m->text);
gtk_widget_show(submenu_item);
gtk_menu_shell_append(GTK_MENU_SHELL(submenu), submenu_item);
if (s_m->cb != NULL) {
g_signal_connect(submenu_item, "activate", G_CALLBACK(_tray_submenu_cb), s_m);
} }
else {
if (strcmp(m->text, "-") == 0) { }
}
else if (strcmp(m->text, "-") == 0) {
item = gtk_separator_menu_item_new(); item = gtk_separator_menu_item_new();
} else { } else {
item = gtk_check_menu_item_new_with_label(m->text); item = gtk_check_menu_item_new_with_label(m->text);
gtk_widget_set_sensitive(item, !m->disabled); gtk_widget_set_sensitive(item, !m->disabled);
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), !!m->checked); gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), !!m->checked);
} }
}
gtk_widget_show(item); gtk_widget_show(item);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
if (m->cb != NULL) { if (m->cb != NULL) {
g_signal_connect(item, "activate", G_CALLBACK(_tray_menu_cb), m); g_signal_connect(item, "activate", G_CALLBACK(_tray_menu_cb), m);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment