|
1 | 1 | #include "terminal.h" |
2 | 2 | #include <gtk/gtk.h> |
3 | 3 |
|
4 | | -#define APP_MENU_ELEM 1 |
5 | | -#define TOP_MENU_ELEM 2 |
6 | | -#define SUB_MENU_ELEM 3 |
7 | | - |
8 | | -typedef struct { |
9 | | - GMenu *menu; |
10 | | - char *label; |
11 | | -} menu_elem; |
12 | | - |
13 | 4 | static GHashTable *menu_table = NULL; |
14 | 5 |
|
15 | | -typedef struct { |
16 | | - GMenuItem *item; |
17 | | - char *label; |
18 | | - char *action; |
19 | | -} menu_item; |
| 6 | +void menubar_create() { |
| 7 | + menu_table = g_hash_table_new(g_direct_hash, g_direct_equal); |
| 8 | + GMenu *menu = g_menu_new(); |
| 9 | + g_hash_table_insert(menu_table, GINT_TO_POINTER(0), menu); |
| 10 | + gtk_application_set_menubar(app, G_MENU_MODEL(menu)); |
| 11 | + g_object_unref(menu); |
| 12 | +} |
20 | 13 |
|
21 | 14 | void menu_node_add(int id, int parent, char *label) { |
22 | | - if (menu_table == NULL) { |
23 | | - menu_table = g_hash_table_new(g_direct_hash, g_direct_equal); |
24 | | - } |
25 | | - menu_elem *m = g_malloc(sizeof(menu_elem)); |
26 | | - m->menu = g_menu_new(); |
27 | | - m->label = label; |
28 | | - g_hash_table_insert(menu_table, GINT_TO_POINTER(id), m); |
29 | | - if (parent == 0) { |
30 | | - g_menu_append_submenu(barmenu, m->label, G_MENU_MODEL(m->menu)); |
31 | | - } else { |
32 | | - menu_elem *p = g_hash_table_lookup(menu_table, GINT_TO_POINTER(parent)); |
33 | | - g_menu_append_submenu(p->menu, m->label, G_MENU_MODEL(m->menu)); |
34 | | - } |
35 | | - g_object_unref(m->menu); |
| 15 | + GMenu *menu = g_menu_new(); |
| 16 | + g_hash_table_insert(menu_table, GINT_TO_POINTER(id), menu); |
| 17 | + GMenu *parent_menu = g_hash_table_lookup(menu_table, GINT_TO_POINTER(parent)); |
| 18 | + g_menu_append_submenu(parent_menu, label, G_MENU_MODEL(menu)); |
| 19 | + g_object_unref(menu); |
36 | 20 | } |
37 | 21 |
|
38 | | -void menu_item_click(GSimpleAction *action, GVariant *parameter, gpointer p) { |
39 | | - menu_item *m = (menu_item *)p; |
40 | | - s_menu_action(m->action); |
| 22 | +static void menu_item_click(GSimpleAction *self, GVariant *parameter, gpointer data) { |
| 23 | + char *action = data; |
| 24 | + s_menu_action(action); |
41 | 25 | } |
42 | 26 |
|
43 | 27 | void menu_item_add(int id, int parent, char *label, char *action) { |
44 | | - menu_item *i = g_malloc(sizeof(menu_item)); |
45 | | - i->item = g_menu_item_new(label, action); |
46 | | - i->label = label; |
47 | | - i->action = action; |
48 | | - menu_elem *m = g_hash_table_lookup(menu_table, GINT_TO_POINTER(parent)); |
49 | | - g_menu_append_item(G_MENU(m->menu), i->item); |
50 | | - g_object_unref(i->item); |
| 28 | + GMenuItem *item = g_menu_item_new(label, action); |
| 29 | + GMenu *menu = g_hash_table_lookup(menu_table, GINT_TO_POINTER(parent)); |
| 30 | + g_menu_append_item(menu, item); |
| 31 | + g_object_unref(item); |
51 | 32 | GSimpleAction *sa = g_simple_action_new(action + 4, NULL); |
52 | 33 | g_action_map_add_action(G_ACTION_MAP(app), G_ACTION(sa)); |
53 | | - g_signal_connect(sa, "activate", G_CALLBACK(menu_item_click), i); |
| 34 | + g_signal_connect(sa, "activate", G_CALLBACK(menu_item_click), action); |
54 | 35 | } |
0 commit comments