[Howto] GTK+ FileChooser-dialog example

posted in: computer | 0

GTK+ 3 example: open filechooser-dialog with 2 buttons, select file, click open, print filename to console, clear filename-memory, close filechooser-dialog

GtkWidget       *fc_dialog;
GtkFileChooser *fc;
char *filename;

fc_dialog = gtk_file_chooser_dialog_new("Title", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, "Abort", GTK_RESPONSE_CANCEL, "Open", GTK_RESPONSE_ACCEPT, NULL);

gtk_widget_show(fc_dialog);

if (gtk_dialog_run(GTK_DIALOG(fc_dialog)) == GTK_RESPONSE_ACCEPT){
fc = GTK_FILE_CHOOSER(fc_dialog);
filename = gtk_file_chooser_get_filename(fc);
g_print("%s", filename);
g_free (filename);
gtk_widget_destroy(fc_dialog);}
else {
gtk_widget_destroy(fc_dialog);
}