我使用 GTK4 创建了一个简单的 C 应用程序,它有一个窗口、一个菜单栏、一个菜单和一个菜单项。我希望菜单读取“文件”,文件下方的菜单项读取“连接”。我如何编码菜单和菜单栏,以便我有一个菜单文件->连接?
#include <gtk/gtk.h>
static void connect(GApplication *app, gpointer *data) {
puts("Connect menu item clicked.\n");
}
static void activate(GApplication *app, gpointer *data) {
GtkWidget *win = gtk_application_window_new(GTK_APPLICATION(app));
gtk_window_set_title(GTK_WINDOW(win), "IRC Client");
gtk_window_set_default_size(GTK_WINDOW(win), 800, 800);
GSimpleAction *act_connect = g_simple_action_new("Connect", NULL);
g_action_map_add_action(G_ACTION_MAP(app), G_ACTION(act_connect));
g_signal_connect(act_connect, "activate", G_CALLBACK(connect), NULL);
GMenu *menubar = g_menu_new();
GMenuItem *menu_item_menu = g_menu_item_new("File", NULL);
GMenu *menu = g_menu_new();
GMenuItem *menu_item_connect = g_menu_item_new("Connect", "app.connect");
g_menu_append_item(menu, menu_item_connect);
g_object_unref(menu_item_connect);
g_menu_item_set_submenu(menu_item_menu, G_MENU_MODEL(menu));
g_menu_append_item(menubar, menu_item_menu);
g_object_unref(menu_item_menu);
gtk_application_set_menubar(GTK_APPLICATION(app), G_MENU_MODEL(menubar));
gtk_application_window_set_show_menubar(GTK_APPLICATION_WINDOW(win), TRUE);
gtk_window_present(GTK_WINDOW(win));
}
int main(int argc, char **argv) {
GtkApplication *app;
int stat;
app = gtk_application_new("com.ircclient", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
stat = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return stat;
}