Commit 26009610 authored by Max's avatar Max

create recursive for submenu

parent ec1abb75
......@@ -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");
......
......@@ -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; }
......
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