Commit 721107bf authored by Serge Zaitsev's avatar Serge Zaitsev Committed by GitHub

Merge pull request #1 from angelskieglazki/master

Added submenu
parents 83a182dd a66de7c4
...@@ -39,14 +39,42 @@ static void quit_cb(struct tray_menu *item) { ...@@ -39,14 +39,42 @@ static void quit_cb(struct tray_menu *item) {
tray_exit(); tray_exit();
} }
static void submenu_cb(struct tray_menu *item) {
(void)item;
printf("submenu cb!!!\n");
tray_update(&tray);
}
// Test tray init
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[]){
{"Checked", 0, 1, toggle_cb, NULL}, {"Hello", 0, 0, NULL, hello_cb, NULL},
{"Disabled", 1, 0, NULL, NULL}, {"Checked", 0, 1, NULL, toggle_cb, NULL},
{"-", 0, 0, NULL, NULL}, {"Disabled", 1, 0, NULL, NULL, NULL},
{"Quit", 0, 0, quit_cb, NULL}, {"-", 0, 0, NULL, NULL, NULL},
{NULL, 0, 0, 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() { int main() {
...@@ -58,4 +86,4 @@ int main() { ...@@ -58,4 +86,4 @@ int main() {
printf("iteration\n"); printf("iteration\n");
} }
return 0; return 0;
} }
\ No newline at end of file
...@@ -12,6 +12,7 @@ struct tray_menu { ...@@ -12,6 +12,7 @@ struct tray_menu {
char *text; char *text;
int disabled; int disabled;
int checked; int checked;
struct tray_menu *submenu;
void (*cb)(struct tray_menu *); void (*cb)(struct tray_menu *);
void *context; void *context;
...@@ -51,17 +52,56 @@ static int tray_loop(int blocking) { ...@@ -51,17 +52,56 @@ static int tray_loop(int blocking) {
return loop_result; 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++) {
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) {
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);
}
}
}
}
static void tray_update(struct tray *tray) { 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) {
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(); 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) {
...@@ -170,8 +210,8 @@ static NOTIFYICONDATA nid; ...@@ -170,8 +210,8 @@ static NOTIFYICONDATA nid;
static HWND hwnd; static HWND hwnd;
static HMENU hmenu = NULL; static HMENU hmenu = NULL;
static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, static LRESULT CALLBACK
LPARAM lparam) { _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
switch (msg) { switch (msg) {
case WM_CLOSE: case WM_CLOSE:
DestroyWindow(hwnd); DestroyWindow(hwnd);
...@@ -309,4 +349,4 @@ static void tray_update(struct tray *tray) {} ...@@ -309,4 +349,4 @@ static void tray_update(struct tray *tray) {}
static void tray_exit(); static void tray_exit();
#endif #endif
#endif /* TRAY_H */ #endif /* TRAY_H */
\ No newline at end of file
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