Skip to content

Instantly share code, notes, and snippets.

@nowl
Created March 12, 2011 06:06
Show Gist options
  • Save nowl/867078 to your computer and use it in GitHub Desktop.
Save nowl/867078 to your computer and use it in GitHub Desktop.
Opening a file dialog with gtk
#include <gtk/gtk.h>
static gboolean
menu_close_program(GtkWidget *widget,
GdkEvent *event,
gpointer data)
{
gtk_main_quit();
return TRUE;
}
static void
button_press_cb(GtkWidget *src,
gpointer *data)
{
GtkWidget *file_dlg = gtk_file_chooser_dialog_new("Select file",
NULL,
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
NULL);
if( gtk_dialog_run(GTK_DIALOG(file_dlg)) == GTK_RESPONSE_ACCEPT )
{
char *filename;
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_dlg));
printf("selected file %s\n", filename);
g_free (filename);
}
gtk_widget_destroy(file_dlg);
}
int
main(int argc, char *argv[])
{
GtkWidget *main_window;
GtkWidget *sizer_main;
GtkWidget *button;
gtk_init(&argc, &argv);
main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(main_window), "test");
g_signal_connect_swapped(main_window, "delete-event",
G_CALLBACK(menu_close_program),
main_window);
button = gtk_button_new_with_label("press me");
g_signal_connect(button, "clicked",
G_CALLBACK(button_press_cb),
NULL);
sizer_main = gtk_vbox_new(TRUE, 5);
gtk_box_pack_start(GTK_BOX(sizer_main), button, FALSE, FALSE, 0);
gtk_container_add(GTK_CONTAINER(main_window), sizer_main);
gtk_widget_show_all(main_window);
gtk_main();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment