From 19d958a831f3732bbc364fb152ecd060ce22e76f Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 10 Jan 2017 00:43:05 +0300 Subject: [PATCH 1/7] some example for --- example.c | 1 + tray.h | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/example.c b/example.c index fe9efdd..4bfaec7 100644 --- a/example.c +++ b/example.c @@ -45,6 +45,7 @@ static struct tray tray = { {"Checked", 0, 1, toggle_cb, NULL}, {"Disabled", 1, 0, NULL, NULL}, {"-", 0, 0, NULL, NULL}, + {"+", 0, 0, NULL, NULL}, {"Quit", 0, 0, quit_cb, NULL}, {NULL, 0, 0, NULL, NULL}}, }; diff --git a/tray.h b/tray.h index 7f8fc23..b91629d 100644 --- a/tray.h +++ b/tray.h @@ -55,15 +55,28 @@ static void tray_update(struct tray *tray) { GtkMenuShell *menu = (GtkMenuShell *)gtk_menu_new(); for (struct tray_menu *m = tray->menu; m != NULL && m->text != NULL; m++) { GtkWidget *item; - if (strcmp(m->text, "-") == 0) { - item = gtk_separator_menu_item_new(); - } else { - item = gtk_check_menu_item_new_with_label(m->text); - gtk_widget_set_sensitive(item, !m->disabled); - gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), !!m->checked); + if (strcmp(m->text, "+") == 0) { + GtkWidget *submenu1 = gtk_menu_new(); + item = gtk_menu_item_new_with_label("SubMenu1"); + gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu1); + GtkWidget* submenu1_option; + submenu1_option = gtk_menu_item_new_with_label("Submenu option!"); + gtk_menu_shell_append(GTK_MENU_SHELL(submenu1), submenu1_option); + } + else { + if (strcmp(m->text, "-") == 0) { + item = gtk_separator_menu_item_new(); + } else { + item = gtk_check_menu_item_new_with_label(m->text); + gtk_widget_set_sensitive(item, !m->disabled); + gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), !!m->checked); + } } gtk_widget_show(item); gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); + + + if (m->cb != NULL) { g_signal_connect(item, "activate", G_CALLBACK(_tray_menu_cb), m); } From a09222db557b4d6517f5d6cbb50b4b991fcb55b8 Mon Sep 17 00:00:00 2001 From: angelskieglazki Date: Tue, 10 Jan 2017 17:47:28 +0300 Subject: [PATCH 2/7] added submenu --- example.c | 22 +++++++++++++++------- tray.h | 55 +++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 52 insertions(+), 25 deletions(-) diff --git a/example.c b/example.c index 4bfaec7..334aed9 100644 --- a/example.c +++ b/example.c @@ -39,15 +39,23 @@ static void quit_cb(struct tray_menu *item) { 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 = { .icon = TRAY_ICON1, - .menu = (struct tray_menu[]){{"Hello", 0, 0, hello_cb, NULL}, - {"Checked", 0, 1, toggle_cb, NULL}, - {"Disabled", 1, 0, NULL, NULL}, - {"-", 0, 0, NULL, NULL}, - {"+", 0, 0, NULL, NULL}, - {"Quit", 0, 0, quit_cb, NULL}, - {NULL, 0, 0, NULL, NULL}}, + .menu = (struct tray_menu[]){{"Hello", 0, 0, NULL, hello_cb, NULL}, + {"Checked", 0, 1, NULL, toggle_cb, NULL}, + {"Disabled", 1, 0, NULL, NULL, NULL}, + {"-", 0, 0, NULL, NULL, NULL}, + {"SubMenu", 0, 0, (struct tray_submenu[]){{"First",submenu_cb, NULL}, {"Second",submenu_cb, NULL}, {NULL, NULL, NULL},}, NULL, NULL}, + {"Quit", 0, 0, NULL, quit_cb, NULL}, + {NULL, 0, 0, NULL, NULL, NULL}}, }; int main() { diff --git a/tray.h b/tray.h index b91629d..f3d71fb 100644 --- a/tray.h +++ b/tray.h @@ -8,10 +8,17 @@ struct tray { struct tray_menu *menu; }; +struct tray_submenu { + char *text; + void (*cb)(struct tray_submenu *); + void *context; +}; + struct tray_menu { char *text; int disabled; int checked; + struct tray_submenu *submenu; void (*cb)(struct tray_menu *); void *context; @@ -35,6 +42,12 @@ static void _tray_menu_cb(GtkMenuItem *item, gpointer data) { 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) { if (gtk_init_check(0, NULL) == FALSE) { return -1; @@ -55,28 +68,34 @@ static void tray_update(struct tray *tray) { GtkMenuShell *menu = (GtkMenuShell *)gtk_menu_new(); for (struct tray_menu *m = tray->menu; m != NULL && m->text != NULL; m++) { GtkWidget *item; - if (strcmp(m->text, "+") == 0) { - GtkWidget *submenu1 = gtk_menu_new(); - item = gtk_menu_item_new_with_label("SubMenu1"); - gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu1); - GtkWidget* submenu1_option; - submenu1_option = gtk_menu_item_new_with_label("Submenu option!"); - gtk_menu_shell_append(GTK_MENU_SHELL(submenu1), submenu1_option); + if (m->submenu != NULL) { + + GtkWidget *submenu = gtk_menu_new(); + item = gtk_menu_item_new_with_label(m->text); + gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu); + + 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) { - item = gtk_separator_menu_item_new(); - } else { - item = gtk_check_menu_item_new_with_label(m->text); - gtk_widget_set_sensitive(item, !m->disabled); - gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), !!m->checked); - } + else if (strcmp(m->text, "-") == 0) { + item = gtk_separator_menu_item_new(); + } else { + item = gtk_check_menu_item_new_with_label(m->text); + gtk_widget_set_sensitive(item, !m->disabled); + gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), !!m->checked); } + gtk_widget_show(item); gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); - - - if (m->cb != NULL) { g_signal_connect(item, "activate", G_CALLBACK(_tray_menu_cb), m); } From ec1abb7520c384ddfe5d123169073cfe419902eb Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 11 Jan 2017 00:17:06 +0300 Subject: [PATCH 3/7] some fix --- tray.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tray.h b/tray.h index f3d71fb..172bdc7 100644 --- a/tray.h +++ b/tray.h @@ -70,9 +70,9 @@ static void tray_update(struct tray *tray) { GtkWidget *item; if (m->submenu != NULL) { - GtkWidget *submenu = gtk_menu_new(); + GtkMenuShell *submenu = (GtkMenuShell *)gtk_menu_new(); item = gtk_menu_item_new_with_label(m->text); - gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu); + gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), (GtkWidget *)submenu); for (struct tray_submenu *s_m = m->submenu; s_m != NULL && s_m->text != NULL; s_m++) { From 26009610c1b1dbbafe21c53aea59927f0b691f94 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 11 Jan 2017 18:27:30 +0300 Subject: [PATCH 4/7] create recursive for submenu --- example.c | 96 ++++++++++++++++++++++++------------- tray.h | 139 +++++++++++++++++++++++++++++++++++------------------- 2 files changed, 154 insertions(+), 81 deletions(-) diff --git a/example.c b/example.c index 334aed9..6761e63 100644 --- a/example.c +++ b/example.c @@ -16,48 +16,78 @@ static struct tray tray; -static void toggle_cb(struct tray_menu *item) { - printf("toggle cb\n"); - item->checked = !item->checked; - tray_update(&tray); -} +// static void toggle_cb(struct tray_menu *item) { +// printf("toggle cb\n"); +// item->checked = !item->checked; +// tray_update(&tray); +// } -static void hello_cb(struct tray_menu *item) { - (void)item; - printf("hello cb\n"); - if (strcmp(tray.icon, TRAY_ICON1) == 0) { - tray.icon = TRAY_ICON2; - } else { - tray.icon = TRAY_ICON1; - } - tray_update(&tray); -} +// static void hello_cb(struct tray_menu *item) { +// (void)item; +// printf("hello cb\n"); +// if (strcmp(tray.icon, TRAY_ICON1) == 0) { +// tray.icon = TRAY_ICON2; +// } else { +// tray.icon = TRAY_ICON1; +// } +// tray_update(&tray); +// } -static void quit_cb(struct tray_menu *item) { - (void)item; - printf("quit cb\n"); - tray_exit(); -} +// static void quit_cb(struct tray_menu *item) { +// (void)item; +// printf("quit cb\n"); +// tray_exit(); +// } -static void submenu_cb(struct tray_submenu *item) { - (void)item; - printf("submenu cb\n"); - tray_update(&tray); -} +// 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 = { +// .icon = TRAY_ICON1, +// .menu = (struct tray_menu[]){{"Hello", 0, 0, NULL, hello_cb, NULL}, +// {"Checked", 0, 1, NULL, toggle_cb, NULL}, +// {"Disabled", 1, 0, NULL, NULL, NULL}, +// {"-", 0, 0, NULL, NULL, NULL}, +// {"SubMenu", 0, 0, (struct tray_submenu[]){{"First", NULL,submenu_cb, NULL}, +// {"Second", +// (struct tray_submenu[]){{"Third", NULL,submenu_cb, NULL}, +// {NULL, NULL, NULL, NULL},}, +// submenu_cb, NULL}, +// {NULL, NULL, NULL},}, NULL, NULL}, +// {"Quit", 0, 0, NULL, quit_cb, NULL}, +// {NULL, 0, 0, NULL, NULL, NULL}}, +// }; static struct tray tray = { .icon = TRAY_ICON1, - .menu = (struct tray_menu[]){{"Hello", 0, 0, NULL, hello_cb, NULL}, - {"Checked", 0, 1, NULL, toggle_cb, NULL}, - {"Disabled", 1, 0, NULL, NULL, NULL}, - {"-", 0, 0, NULL, NULL, NULL}, - {"SubMenu", 0, 0, (struct tray_submenu[]){{"First",submenu_cb, NULL}, {"Second",submenu_cb, NULL}, {NULL, NULL, NULL},}, NULL, NULL}, - {"Quit", 0, 0, NULL, quit_cb, NULL}, - {NULL, 0, 0, NULL, NULL, NULL}}, + .menu = (struct tray_menu[]){{"Hello", NULL}, + {"Checked", NULL}, + {"SubMenu", (struct tray_menu[]){{"FIRST", NULL}, + {"SECOND", (struct tray_menu[]){{"THIRD", (struct tray_menu[]){{"7", NULL}, + {"8", NULL}, + {NULL, NULL}}}, + {"FOUR", (struct tray_menu[]){{"5", NULL}, + {"6", NULL}, + {NULL, NULL}}}, + {NULL, NULL}}}, + {NULL, NULL}} + }, + {NULL, NULL}}, }; - +// static struct tray tray = { +// .icon = TRAY_ICON1, +// .menu = (struct tray_menu[]){{"Hello", NULL}, +// {"Checked", NULL}, +// {"SubMenu", (struct tray_menu[]){{"FIRST", NULL}, +// {"SECOND", NULL}, +// {NULL, NULL}} +// }, +// {NULL, NULL}}, +// }; int main() { if (tray_init(&tray) < 0) { printf("failed to create tray\n"); diff --git a/tray.h b/tray.h index 172bdc7..465e99e 100644 --- a/tray.h +++ b/tray.h @@ -8,20 +8,14 @@ struct tray { struct tray_menu *menu; }; -struct tray_submenu { - char *text; - void (*cb)(struct tray_submenu *); - void *context; -}; - struct tray_menu { char *text; - int disabled; - int checked; - struct tray_submenu *submenu; + // int disabled; + // int checked; + struct tray_menu *submenu; - void (*cb)(struct tray_menu *); - void *context; + // void (*cb)(struct tray_menu *); + // void *context; }; static void tray_update(struct tray *tray); @@ -36,17 +30,17 @@ static void tray_update(struct tray *tray); static AppIndicator *indicator = NULL; static int loop_result = 0; -static void _tray_menu_cb(GtkMenuItem *item, gpointer data) { - (void)item; - struct tray_menu *m = (struct tray_menu *)data; - m->cb(m); -} +// static void _tray_menu_cb(GtkMenuItem *item, gpointer data) { +// (void)item; +// struct tray_menu *m = (struct tray_menu *)data; +// 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 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) { if (gtk_init_check(0, NULL) == FALSE) { @@ -64,47 +58,96 @@ static int tray_loop(int blocking) { return loop_result; } +static void submenu_update(struct tray_menu *m, + GtkWidget *_item, GtkMenuShell *_submenu) { + + GtkMenuShell *submenu; //= (GtkMenuShell *)gtk_menu_new(); + + for (struct tray_menu *s_m = m->submenu; s_m!=NULL && s_m->text!=NULL; s_m++) { + GtkWidget *item; + if (s_m->submenu != NULL) { + printf("GO TO REC SUB %s\n", s_m->text); + item = gtk_menu_item_new_with_label(s_m->text); + submenu = (GtkMenuShell *)gtk_menu_new(); + gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), (GtkWidget *)submenu); + submenu_update(s_m, item, submenu); + gtk_widget_show(item); + gtk_menu_shell_append(GTK_MENU_SHELL(_submenu), item); + + } + else{ + gtk_menu_item_set_submenu(GTK_MENU_ITEM(_item), (GtkWidget *)_submenu); + item = gtk_menu_item_new_with_label(s_m->text); + + printf("--%s add to %s\n", gtk_menu_item_get_label((GtkMenuItem*)item), gtk_menu_item_get_label((GtkMenuItem*)_item)); + + gtk_widget_show(item); + gtk_menu_shell_append(GTK_MENU_SHELL(_submenu), item); + } + } +} + static void tray_update(struct tray *tray) { GtkMenuShell *menu = (GtkMenuShell *)gtk_menu_new(); for (struct tray_menu *m = tray->menu; m != NULL && m->text != NULL; m++) { GtkWidget *item; - if (m->submenu != NULL) { - + if (m->submenu != NULL) { + printf("GO TO REC MAIN %s\n", m->text); GtkMenuShell *submenu = (GtkMenuShell *)gtk_menu_new(); item = gtk_menu_item_new_with_label(m->text); gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), (GtkWidget *)submenu); - - 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) { - item = gtk_separator_menu_item_new(); - } else { - item = gtk_check_menu_item_new_with_label(m->text); - gtk_widget_set_sensitive(item, !m->disabled); - gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), !!m->checked); + submenu_update(m, item, submenu); + } + else { + item = gtk_menu_item_new_with_label(m->text); } - gtk_widget_show(item); + gtk_widget_show(item); gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); - if (m->cb != NULL) { - g_signal_connect(item, "activate", G_CALLBACK(_tray_menu_cb), m); - } } app_indicator_set_icon(indicator, tray->icon); - // GTK is all about reference counting, so previous menu should be destroyed - // here app_indicator_set_menu(indicator, GTK_MENU(menu)); } +// static void tray_update(struct tray *tray) { +// GtkMenuShell *menu = (GtkMenuShell *)gtk_menu_new(); +// for (struct tray_menu *m = tray->menu; m != NULL && m->text != NULL; m++) { +// GtkWidget *item; +// if (m->submenu != NULL) { +// GtkMenuShell *submenu = (GtkMenuShell *)gtk_menu_new(); +// item = gtk_menu_item_new_with_label(m->text); +// gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), (GtkWidget *)submenu); + +// 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) { +// item = gtk_separator_menu_item_new(); +// } else { +// item = gtk_check_menu_item_new_with_label(m->text); +// gtk_widget_set_sensitive(item, !m->disabled); +// gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), !!m->checked); +// } + +// gtk_widget_show(item); +// gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); +// if (m->cb != NULL) { +// g_signal_connect(item, "activate", G_CALLBACK(_tray_menu_cb), m); +// } +// } +// app_indicator_set_icon(indicator, tray->icon); +// // GTK is all about reference counting, so previous menu should be destroyed +// // here +// app_indicator_set_menu(indicator, GTK_MENU(menu)); +// } static void tray_exit() { loop_result = -1; } From ed255d1a0357305cac6b697ee5c7ab9018646a5b Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 11 Jan 2017 20:57:36 +0300 Subject: [PATCH 5/7] added recursive procedure for submenu. Used one struct for submenu: tray_menu --- example.c | 91 ++++++++++++++++++++++------------------------ tray.h | 105 ++++++++++++++++++++---------------------------------- 2 files changed, 82 insertions(+), 114 deletions(-) diff --git a/example.c b/example.c index 6761e63..82e01b4 100644 --- a/example.c +++ b/example.c @@ -16,34 +16,34 @@ static struct tray tray; -// static void toggle_cb(struct tray_menu *item) { -// printf("toggle cb\n"); -// item->checked = !item->checked; -// tray_update(&tray); -// } +static void toggle_cb(struct tray_menu *item) { + printf("toggle cb\n"); + item->checked = !item->checked; + tray_update(&tray); +} -// static void hello_cb(struct tray_menu *item) { -// (void)item; -// printf("hello cb\n"); -// if (strcmp(tray.icon, TRAY_ICON1) == 0) { -// tray.icon = TRAY_ICON2; -// } else { -// tray.icon = TRAY_ICON1; -// } -// tray_update(&tray); -// } +static void hello_cb(struct tray_menu *item) { + (void)item; + printf("hello cb\n"); + if (strcmp(tray.icon, TRAY_ICON1) == 0) { + tray.icon = TRAY_ICON2; + } else { + tray.icon = TRAY_ICON1; + } + tray_update(&tray); +} -// static void quit_cb(struct tray_menu *item) { -// (void)item; -// printf("quit cb\n"); -// tray_exit(); -// } +static void quit_cb(struct tray_menu *item) { + (void)item; + printf("quit cb\n"); + tray_exit(); +} -// static void submenu_cb(struct tray_submenu *item) { -// (void)item; -// printf("submenu cb\n"); -// tray_update(&tray); -// } +static void submenu_cb(struct tray_menu *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},}; @@ -62,32 +62,27 @@ static struct tray tray; // {"Quit", 0, 0, NULL, quit_cb, NULL}, // {NULL, 0, 0, NULL, NULL, NULL}}, // }; + +//Test tray init static struct tray tray = { .icon = TRAY_ICON1, - .menu = (struct tray_menu[]){{"Hello", NULL}, - {"Checked", NULL}, - {"SubMenu", (struct tray_menu[]){{"FIRST", NULL}, - {"SECOND", (struct tray_menu[]){{"THIRD", (struct tray_menu[]){{"7", NULL}, - {"8", NULL}, - {NULL, NULL}}}, - {"FOUR", (struct tray_menu[]){{"5", NULL}, - {"6", NULL}, - {NULL, NULL}}}, - {NULL, NULL}}}, - {NULL, NULL}} - }, - {NULL, NULL}}, + .menu = (struct tray_menu[]){{"Hello", 0, 0, NULL, hello_cb, NULL}, + {"Checked", 0, 1, NULL, toggle_cb, NULL}, + {"Disabled", 1, 0, NULL, NULL, NULL}, + {"-", 0, 0, NULL, NULL, NULL}, + {"Quit", 0, 0, NULL, quit_cb, NULL}, + {"SubMenu", 0, 0, (struct tray_menu[]){{"FIRST", 0, 1, NULL, submenu_cb, NULL}, + {"SECOND", 0, 0, (struct tray_menu[]){{"THIRD", 0, 0, (struct tray_menu[]){{"7", 0, 0, NULL, submenu_cb, NULL}, + {"-", 0, 0, NULL, NULL, NULL}, + {"8", 0, 0, NULL, submenu_cb, NULL}, + {NULL, 0, 0, NULL, NULL, NULL}}, NULL, NULL}, + {"FOUR", 0, 0, (struct tray_menu[]){{"5", 0, 0, NULL, submenu_cb, NULL}, + {"6", 0, 0, NULL, submenu_cb, NULL}, + {NULL, 0, 0, NULL, NULL, NULL}}, NULL, NULL}, + {NULL, 0, 0, NULL, NULL, NULL}} , NULL, NULL}, + {NULL, 0, 0, NULL, NULL, NULL}} , NULL, NULL }, + {NULL, 0, 0, NULL, NULL, NULL}}, }; -// static struct tray tray = { -// .icon = TRAY_ICON1, -// .menu = (struct tray_menu[]){{"Hello", NULL}, -// {"Checked", NULL}, -// {"SubMenu", (struct tray_menu[]){{"FIRST", NULL}, -// {"SECOND", NULL}, -// {NULL, NULL}} -// }, -// {NULL, NULL}}, -// }; int main() { if (tray_init(&tray) < 0) { printf("failed to create tray\n"); diff --git a/tray.h b/tray.h index 465e99e..e78e1cb 100644 --- a/tray.h +++ b/tray.h @@ -10,12 +10,12 @@ struct tray { struct tray_menu { char *text; - // int disabled; - // int checked; + int disabled; + int checked; struct tray_menu *submenu; - // void (*cb)(struct tray_menu *); - // void *context; + void (*cb)(struct tray_menu *); + void *context; }; static void tray_update(struct tray *tray); @@ -30,11 +30,11 @@ static void tray_update(struct tray *tray); static AppIndicator *indicator = NULL; static int loop_result = 0; -// static void _tray_menu_cb(GtkMenuItem *item, gpointer data) { -// (void)item; -// struct tray_menu *m = (struct tray_menu *)data; -// m->cb(m); -// } +static void _tray_menu_cb(GtkMenuItem *item, gpointer data) { + (void)item; + struct tray_menu *m = (struct tray_menu *)data; + m->cb(m); +} // static void _tray_submenu_cb(GtkMenuItem *item, gpointer data) { // (void)item; @@ -58,15 +58,13 @@ static int tray_loop(int blocking) { return loop_result; } +//recursive proc static void submenu_update(struct tray_menu *m, - GtkWidget *_item, GtkMenuShell *_submenu) { - - GtkMenuShell *submenu; //= (GtkMenuShell *)gtk_menu_new(); - + GtkWidget *_item, GtkMenuShell *_submenu) { + GtkMenuShell *submenu; for (struct tray_menu *s_m = m->submenu; s_m!=NULL && s_m->text!=NULL; s_m++) { GtkWidget *item; - if (s_m->submenu != NULL) { - printf("GO TO REC SUB %s\n", s_m->text); + if (s_m->submenu != NULL) { item = gtk_menu_item_new_with_label(s_m->text); submenu = (GtkMenuShell *)gtk_menu_new(); gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), (GtkWidget *)submenu); @@ -74,16 +72,23 @@ static void submenu_update(struct tray_menu *m, gtk_widget_show(item); gtk_menu_shell_append(GTK_MENU_SHELL(_submenu), item); - } - else{ - gtk_menu_item_set_submenu(GTK_MENU_ITEM(_item), (GtkWidget *)_submenu); - item = gtk_menu_item_new_with_label(s_m->text); - - printf("--%s add to %s\n", gtk_menu_item_get_label((GtkMenuItem*)item), gtk_menu_item_get_label((GtkMenuItem*)_item)); - + }else if (strcmp(s_m->text, "-") == 0) { + gtk_menu_item_set_submenu(GTK_MENU_ITEM(_item), (GtkWidget *)_submenu); + item = gtk_separator_menu_item_new(); gtk_widget_show(item); gtk_menu_shell_append(GTK_MENU_SHELL(_submenu), item); } + else{ + gtk_menu_item_set_submenu(GTK_MENU_ITEM(_item), (GtkWidget *)_submenu); + item = gtk_check_menu_item_new_with_label(s_m->text); + gtk_widget_set_sensitive(item, !s_m->disabled); + gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), !!s_m->checked); + gtk_widget_show(item); + gtk_menu_shell_append(GTK_MENU_SHELL(_submenu), item); + if (s_m->cb != NULL) { + g_signal_connect(item, "activate", G_CALLBACK(_tray_menu_cb), s_m); + } + } } } @@ -91,63 +96,31 @@ static void tray_update(struct tray *tray) { GtkMenuShell *menu = (GtkMenuShell *)gtk_menu_new(); for (struct tray_menu *m = tray->menu; m != NULL && m->text != NULL; m++) { GtkWidget *item; - if (m->submenu != NULL) { - printf("GO TO REC MAIN %s\n", m->text); + if (m->submenu != NULL) { GtkMenuShell *submenu = (GtkMenuShell *)gtk_menu_new(); item = gtk_menu_item_new_with_label(m->text); gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), (GtkWidget *)submenu); submenu_update(m, item, submenu); - } - else { - item = gtk_menu_item_new_with_label(m->text); + } else if (strcmp(m->text, "-") == 0) { + item = gtk_separator_menu_item_new(); + } else { + item = gtk_check_menu_item_new_with_label(m->text); + gtk_widget_set_sensitive(item, !m->disabled); + gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), !!m->checked); } gtk_widget_show(item); gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); + if (m->cb != NULL) { + g_signal_connect(item, "activate", G_CALLBACK(_tray_menu_cb), m); + } } app_indicator_set_icon(indicator, tray->icon); + // GTK is all about reference counting, so previous menu should be destroyed + // here app_indicator_set_menu(indicator, GTK_MENU(menu)); } -// static void tray_update(struct tray *tray) { -// GtkMenuShell *menu = (GtkMenuShell *)gtk_menu_new(); -// for (struct tray_menu *m = tray->menu; m != NULL && m->text != NULL; m++) { -// GtkWidget *item; -// if (m->submenu != NULL) { -// GtkMenuShell *submenu = (GtkMenuShell *)gtk_menu_new(); -// item = gtk_menu_item_new_with_label(m->text); -// gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), (GtkWidget *)submenu); -// 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) { -// item = gtk_separator_menu_item_new(); -// } else { -// item = gtk_check_menu_item_new_with_label(m->text); -// gtk_widget_set_sensitive(item, !m->disabled); -// gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), !!m->checked); -// } - -// gtk_widget_show(item); -// gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); -// if (m->cb != NULL) { -// g_signal_connect(item, "activate", G_CALLBACK(_tray_menu_cb), m); -// } -// } -// app_indicator_set_icon(indicator, tray->icon); -// // GTK is all about reference counting, so previous menu should be destroyed -// // here -// app_indicator_set_menu(indicator, GTK_MENU(menu)); -// } static void tray_exit() { loop_result = -1; } From 22f65a80a9a3c78482fa759dc82564b11a7a9715 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 11 Jan 2017 20:59:47 +0300 Subject: [PATCH 6/7] deleted old callback --- tray.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tray.h b/tray.h index e78e1cb..c284527 100644 --- a/tray.h +++ b/tray.h @@ -36,12 +36,6 @@ static void _tray_menu_cb(GtkMenuItem *item, gpointer data) { 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) { if (gtk_init_check(0, NULL) == FALSE) { return -1; From a66de7c433ab4afc31c38ce8d7b35a93ca722acb Mon Sep 17 00:00:00 2001 From: angelskieglazki Date: Thu, 12 Jan 2017 11:02:46 +0300 Subject: [PATCH 7/7] 1)deleted old struct; 2) formated code --- example.c | 68 +++++++++++++++++++++++++------------------------------ tray.h | 48 +++++++++++++++++++-------------------- 2 files changed, 54 insertions(+), 62 deletions(-) diff --git a/example.c b/example.c index 82e01b4..424f9ac 100644 --- a/example.c +++ b/example.c @@ -41,48 +41,42 @@ static void quit_cb(struct tray_menu *item) { static void submenu_cb(struct tray_menu *item) { (void)item; - printf("submenu cb!!!\n"); + 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 = { -// .icon = TRAY_ICON1, -// .menu = (struct tray_menu[]){{"Hello", 0, 0, NULL, hello_cb, NULL}, -// {"Checked", 0, 1, NULL, toggle_cb, NULL}, -// {"Disabled", 1, 0, NULL, NULL, NULL}, -// {"-", 0, 0, NULL, NULL, NULL}, -// {"SubMenu", 0, 0, (struct tray_submenu[]){{"First", NULL,submenu_cb, NULL}, -// {"Second", -// (struct tray_submenu[]){{"Third", NULL,submenu_cb, NULL}, -// {NULL, NULL, NULL, NULL},}, -// submenu_cb, NULL}, -// {NULL, NULL, NULL},}, NULL, NULL}, -// {"Quit", 0, 0, NULL, quit_cb, NULL}, -// {NULL, 0, 0, NULL, NULL, NULL}}, -// }; - -//Test tray init +// Test tray init static struct tray tray = { .icon = TRAY_ICON1, - .menu = (struct tray_menu[]){{"Hello", 0, 0, NULL, hello_cb, NULL}, - {"Checked", 0, 1, NULL, toggle_cb, NULL}, - {"Disabled", 1, 0, NULL, NULL, NULL}, - {"-", 0, 0, NULL, NULL, NULL}, - {"Quit", 0, 0, NULL, quit_cb, NULL}, - {"SubMenu", 0, 0, (struct tray_menu[]){{"FIRST", 0, 1, NULL, submenu_cb, NULL}, - {"SECOND", 0, 0, (struct tray_menu[]){{"THIRD", 0, 0, (struct tray_menu[]){{"7", 0, 0, NULL, submenu_cb, NULL}, - {"-", 0, 0, NULL, NULL, NULL}, - {"8", 0, 0, NULL, submenu_cb, NULL}, - {NULL, 0, 0, NULL, NULL, NULL}}, NULL, NULL}, - {"FOUR", 0, 0, (struct tray_menu[]){{"5", 0, 0, NULL, submenu_cb, NULL}, - {"6", 0, 0, NULL, submenu_cb, NULL}, - {NULL, 0, 0, NULL, NULL, NULL}}, NULL, NULL}, - {NULL, 0, 0, NULL, NULL, NULL}} , NULL, NULL}, - {NULL, 0, 0, NULL, NULL, NULL}} , NULL, NULL }, - {NULL, 0, 0, NULL, NULL, NULL}}, + .menu = (struct tray_menu[]){ + {"Hello", 0, 0, NULL, hello_cb, NULL}, + {"Checked", 0, 1, NULL, toggle_cb, NULL}, + {"Disabled", 1, 0, NULL, NULL, NULL}, + {"-", 0, 0, NULL, NULL, NULL}, + {"Quit", 0, 0, NULL, quit_cb, NULL}, + {"SubMenu", 0, 0, + (struct tray_menu[]){ + {"FIRST", 0, 1, NULL, submenu_cb, NULL}, + {"SECOND", 0, 0, + (struct tray_menu[]){ + {"THIRD", 0, 0, + (struct tray_menu[]){{"7", 0, 0, NULL, submenu_cb, NULL}, + {"-", 0, 0, NULL, NULL, NULL}, + {"8", 0, 0, NULL, submenu_cb, NULL}, + {NULL, 0, 0, NULL, NULL, NULL}}, + NULL, NULL}, + {"FOUR", 0, 0, + (struct tray_menu[]){{"5", 0, 0, NULL, submenu_cb, NULL}, + {"6", 0, 0, NULL, submenu_cb, NULL}, + {NULL, 0, 0, NULL, NULL, NULL}}, + NULL, NULL}, + {NULL, 0, 0, NULL, NULL, NULL}}, + NULL, NULL}, + {NULL, 0, 0, NULL, NULL, NULL}}, + NULL, NULL}, + {NULL, 0, 0, NULL, NULL, NULL}}, }; + int main() { if (tray_init(&tray) < 0) { printf("failed to create tray\n"); @@ -92,4 +86,4 @@ int main() { printf("iteration\n"); } return 0; -} +} \ No newline at end of file diff --git a/tray.h b/tray.h index c284527..b192693 100644 --- a/tray.h +++ b/tray.h @@ -52,27 +52,26 @@ static int tray_loop(int blocking) { return loop_result; } -//recursive proc -static void submenu_update(struct tray_menu *m, - GtkWidget *_item, GtkMenuShell *_submenu) { - GtkMenuShell *submenu; - for (struct tray_menu *s_m = m->submenu; s_m!=NULL && s_m->text!=NULL; s_m++) { +// recursive proc +static void submenu_update(struct tray_menu *m, GtkWidget *_item, + GtkMenuShell *_submenu) { + GtkMenuShell *submenu; + for (struct tray_menu *s_m = m->submenu; s_m != NULL && s_m->text != NULL; + s_m++) { GtkWidget *item; - if (s_m->submenu != NULL) { - item = gtk_menu_item_new_with_label(s_m->text); - submenu = (GtkMenuShell *)gtk_menu_new(); - gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), (GtkWidget *)submenu); - submenu_update(s_m, item, submenu); - gtk_widget_show(item); - gtk_menu_shell_append(GTK_MENU_SHELL(_submenu), item); - - }else if (strcmp(s_m->text, "-") == 0) { + if (s_m->submenu != NULL) { + item = gtk_menu_item_new_with_label(s_m->text); + submenu = (GtkMenuShell *)gtk_menu_new(); + gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), (GtkWidget *)submenu); + submenu_update(s_m, item, submenu); + gtk_widget_show(item); + gtk_menu_shell_append(GTK_MENU_SHELL(_submenu), item); + } else if (strcmp(s_m->text, "-") == 0) { gtk_menu_item_set_submenu(GTK_MENU_ITEM(_item), (GtkWidget *)_submenu); item = gtk_separator_menu_item_new(); gtk_widget_show(item); gtk_menu_shell_append(GTK_MENU_SHELL(_submenu), item); - } - else{ + } else { gtk_menu_item_set_submenu(GTK_MENU_ITEM(_item), (GtkWidget *)_submenu); item = gtk_check_menu_item_new_with_label(s_m->text); gtk_widget_set_sensitive(item, !s_m->disabled); @@ -83,27 +82,27 @@ static void submenu_update(struct tray_menu *m, g_signal_connect(item, "activate", G_CALLBACK(_tray_menu_cb), s_m); } } - } + } } static void tray_update(struct tray *tray) { GtkMenuShell *menu = (GtkMenuShell *)gtk_menu_new(); for (struct tray_menu *m = tray->menu; m != NULL && m->text != NULL; m++) { GtkWidget *item; - if (m->submenu != NULL) { + if (m->submenu != NULL) { GtkMenuShell *submenu = (GtkMenuShell *)gtk_menu_new(); item = gtk_menu_item_new_with_label(m->text); gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), (GtkWidget *)submenu); submenu_update(m, item, submenu); } else if (strcmp(m->text, "-") == 0) { item = gtk_separator_menu_item_new(); - } else { + } else { item = gtk_check_menu_item_new_with_label(m->text); 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); if (m->cb != NULL) { g_signal_connect(item, "activate", G_CALLBACK(_tray_menu_cb), m); @@ -115,7 +114,6 @@ static void tray_update(struct tray *tray) { app_indicator_set_menu(indicator, GTK_MENU(menu)); } - static void tray_exit() { loop_result = -1; } #elif defined(TRAY_APPKIT) @@ -212,8 +210,8 @@ static NOTIFYICONDATA nid; static HWND hwnd; static HMENU hmenu = NULL; -static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, - LPARAM lparam) { +static LRESULT CALLBACK + _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { switch (msg) { case WM_CLOSE: DestroyWindow(hwnd); @@ -351,4 +349,4 @@ static void tray_update(struct tray *tray) {} static void tray_exit(); #endif -#endif /* TRAY_H */ +#endif /* TRAY_H */ \ No newline at end of file