-
-
Save X-m7/4c047bfc2a87844d94588759145114ba to your computer and use it in GitHub Desktop.
GTK4 file picker test program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <gtk/gtk.h> | |
/* | |
* GTK4 file picker sample | |
* Compile with gcc -o fpicker-gtk fpicker-gtk.c `pkg-config --cflags --libs gtk4` | |
*/ | |
static void | |
on_open_response (GtkDialog *dialog, | |
int response) | |
{ | |
if (response == GTK_RESPONSE_ACCEPT) | |
{ | |
GtkFileChooser *chooser = GTK_FILE_CHOOSER (dialog); | |
g_autoptr(GFile) file = gtk_file_chooser_get_file (chooser); | |
} | |
gtk_window_destroy (GTK_WINDOW (dialog)); | |
} | |
static void | |
file_handle (GtkWidget *widget, gpointer data) | |
{ | |
GtkWidget *dialog; | |
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN; | |
dialog = gtk_file_chooser_dialog_new ("Open File", | |
GTK_WINDOW(gtk_widget_get_parent(widget)), | |
action, | |
"_Cancel", | |
GTK_RESPONSE_CANCEL, | |
"_Open", | |
GTK_RESPONSE_ACCEPT, | |
NULL); | |
gtk_widget_show (dialog); | |
g_signal_connect (dialog, "response", | |
G_CALLBACK (on_open_response), | |
NULL); | |
} | |
/* | |
* Portal | |
*/ | |
static void | |
on_open_response_portal (GtkNativeDialog *native, int response) | |
{ | |
if (response == GTK_RESPONSE_ACCEPT) | |
{ | |
GtkFileChooser *chooser = GTK_FILE_CHOOSER (native); | |
GFile *file = gtk_file_chooser_get_file (chooser); | |
g_object_unref (file); | |
} | |
g_object_unref (native); | |
} | |
static void | |
file_handle_portal (GtkWidget *widget, gpointer data) | |
{ | |
GtkFileChooserNative *native; | |
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN; | |
native = gtk_file_chooser_native_new ("Open File", | |
GTK_WINDOW(gtk_widget_get_parent(widget)), | |
action, | |
"_Open", | |
"_Cancel"); | |
g_signal_connect (native, "response", G_CALLBACK (on_open_response_portal), NULL); | |
gtk_native_dialog_show (GTK_NATIVE_DIALOG(native)); | |
} | |
/* | |
* Main | |
*/ | |
static void | |
activate (GtkApplication *app, | |
gpointer user_data) | |
{ | |
GtkWidget *window; | |
GtkWidget *button; | |
window = gtk_application_window_new (app); | |
gtk_window_set_title (GTK_WINDOW (window), "Window"); | |
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200); | |
button = gtk_button_new_with_label ("Hello World"); | |
g_signal_connect (button, "clicked", G_CALLBACK (file_handle), NULL); //switch here between GTK4 and portal/native | |
gtk_window_set_child (GTK_WINDOW (window), button); | |
gtk_window_present (GTK_WINDOW (window)); | |
} | |
int | |
main (int argc, | |
char **argv) | |
{ | |
GtkApplication *app; | |
int status; | |
app = gtk_application_new ("org.gtk.example", 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment