我正在尝试在gtk3中创建一个简单的菜单。我正在查看我在网络上找到的官方文档和其他代码,但我仍然无法让它工作。菜单没有出现。
我将不胜感激任何帮助。谢谢。
这是我到目前为止的代码:
#include <stdio.h>
#include <gtk/gtk.h>
//#include "minefield.h"
enum difficulty {BEGINNER, ADVANCED, EXPERT};
static void activate(GtkApplication* app, gpointer user_data);
GdkPixbuf *create_pixbuf(const gchar * filename);
void newGame();
void openSetupDialog();
void openOptionDialog();
int main(int argc, char *argv[]) {
GtkApplication *app;
int status;
app = gtk_application_new("my.ne.sweeper", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run(G_APPLICATION (app), argc, argv);
g_object_unref(app);
return status;
}
static void activate(GtkApplication* app, gpointer user_data) {
GdkPixbuf *app_icon;
GtkWidget *app_window;
GtkWidget *app_box;
GtkWidget *menubar;
GtkWidget *gameMenu;
GtkWidget *gameMenu_game;
GtkWidget *gameMenu_newGame;
GtkWidget *gameMenu_setup;
GtkWidget *gameMenu_options;
GtkWidget *gameMenu_quit;
GtkWidget *sep;
// App_window
app_window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(app_window), "Minesweeper");
gtk_window_set_default_size(GTK_WINDOW(app_window), 600, 400);
gtk_window_set_position(GTK_WINDOW(app_window), GTK_WIN_POS_CENTER);
// App_box
app_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add(GTK_CONTAINER(app_window), app_box);
// App_icon
app_icon = create_pixbuf("../img/app_icon.svg");
gtk_window_set_icon(GTK_WINDOW(app_window), app_icon);
// Menubar
menubar = gtk_menu_bar_new();
gtk_box_pack_start(GTK_BOX(app_box), menubar, FALSE, FALSE, 0);
// Start menu
gameMenu = gtk_menu_new();
gameMenu_game = gtk_menu_item_new_with_label("Game");
gameMenu_newGame = gtk_menu_item_new_with_label("New Game");
gameMenu_setup = gtk_menu_item_new_with_label("Setup");
gameMenu_options = gtk_menu_item_new_with_label("Options");
gameMenu_quit = gtk_menu_item_new_with_label("Quit");
sep = gtk_separator_menu_item_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM(gameMenu_game), gameMenu);
gtk_menu_shell_append(GTK_MENU_SHELL(menubar), gameMenu_game);
gtk_menu_shell_append(GTK_MENU_SHELL(gameMenu), gameMenu_newGame);
gtk_menu_shell_append(GTK_MENU_SHELL(gameMenu), gameMenu_setup);
gtk_menu_shell_append(GTK_MENU_SHELL(gameMenu), gameMenu_options);
gtk_menu_shell_append(GTK_MENU_SHELL(gameMenu), sep);
gtk_menu_shell_append(GTK_MENU_SHELL(gameMenu), gameMenu_quit);
gtk_widget_show_all(app_window);
// Signalhandlers
g_signal_connect(app_window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(gameMenu_newGame, "activate", G_CALLBACK(newGame), NULL);
g_signal_connect(gameMenu_setup, "activate", G_CALLBACK(openSetupDialog), NULL);
g_signal_connect(gameMenu_options, "activate", G_CALLBACK(openOptionDialog), NULL);
g_signal_connect(gameMenu_quit, "activate", G_CALLBACK(gtk_main_quit), NULL);
g_object_unref(app_icon);
}
GdkPixbuf *create_pixbuf(const gchar * filename) {
GdkPixbuf *pixbuf;
GError *error = NULL;
pixbuf = gdk_pixbuf_new_from_file(filename, &error);
if (!pixbuf) {
fprintf(stderr, "[!] %s\n", error->message);
g_error_free(error);
}
return pixbuf;
}
void newGame() {
fprintf(stdout, "[*] %s\n", "new game..");
}
void openSetupDialog() {
fprintf(stdout, "[*] %s\n", "open setup..");
}
void openOptionDialog() {
fprintf(stdout, "[*] %s\n", "open options..");
}