node-editor: Port to async dialog API

This commit is contained in:
Matthias Clasen 2022-10-28 20:43:37 -04:00
parent d620197ae1
commit e7de4c1f99

View File

@ -548,45 +548,36 @@ node_editor_window_load (NodeEditorWindow *self,
} }
static void static void
open_response_cb (GtkWidget *dialog, open_response_cb (GObject *source,
int response, GAsyncResult *result,
NodeEditorWindow *self) void *user_data)
{ {
gtk_widget_hide (dialog); GtkFileDialog *dialog = GTK_FILE_DIALOG (source);
NodeEditorWindow *self = user_data;
GFile *file;
if (response == GTK_RESPONSE_ACCEPT) file = gtk_file_dialog_open_finish (dialog, result, NULL);
if (file)
{ {
GFile *file;
file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
node_editor_window_load (self, file); node_editor_window_load (self, file);
g_object_unref (file); g_object_unref (file);
} }
gtk_window_destroy (GTK_WINDOW (dialog));
} }
static void static void
show_open_filechooser (NodeEditorWindow *self) show_open_filechooser (NodeEditorWindow *self)
{ {
GtkWidget *dialog; GtkFileDialog *dialog;
GFile *cwd;
dialog = gtk_file_chooser_dialog_new ("Open node file", dialog = gtk_file_dialog_new ();
GTK_WINDOW (self), gtk_file_dialog_set_title (dialog, "Open node file");
GTK_FILE_CHOOSER_ACTION_OPEN, cwd = g_file_new_for_path (".");
"_Cancel", GTK_RESPONSE_CANCEL, gtk_file_dialog_set_current_folder (dialog, cwd);
"_Load", GTK_RESPONSE_ACCEPT,
NULL);
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
GFile *cwd = g_file_new_for_path (".");
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), cwd, NULL);
g_object_unref (cwd); g_object_unref (cwd);
gtk_file_dialog_open (dialog, GTK_WINDOW (self),
g_signal_connect (dialog, "response", G_CALLBACK (open_response_cb), self); NULL, NULL, open_response_cb, self);
gtk_widget_show (dialog); g_object_unref (dialog);
} }
static void static void
@ -597,21 +588,22 @@ open_cb (GtkWidget *button,
} }
static void static void
save_response_cb (GtkWidget *dialog, save_response_cb (GObject *source,
int response, GAsyncResult *result,
NodeEditorWindow *self) void *user_data)
{ {
gtk_widget_hide (dialog); GtkFileDialog *dialog = GTK_FILE_DIALOG (source);
NodeEditorWindow *self = user_data;
GFile *file;
if (response == GTK_RESPONSE_ACCEPT) file = gtk_file_dialog_save_finish (dialog, result, NULL);
if (file)
{ {
GFile *file;
char *text; char *text;
GError *error = NULL; GError *error = NULL;
text = get_current_text (self->text_buffer); text = get_current_text (self->text_buffer);
file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
g_file_replace_contents (file, text, strlen (text), g_file_replace_contents (file, text, strlen (text),
NULL, FALSE, NULL, FALSE,
G_FILE_CREATE_NONE, G_FILE_CREATE_NONE,
@ -620,49 +612,40 @@ save_response_cb (GtkWidget *dialog,
&error); &error);
if (error != NULL) if (error != NULL)
{ {
GtkWidget *message_dialog; GtkAlertDialog *alert;
message_dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (self))), alert = gtk_alert_dialog_new ("Saving failed");
GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT, gtk_alert_dialog_set_detail (alert, error->message);
GTK_MESSAGE_INFO, gtk_alert_dialog_show (alert,
GTK_BUTTONS_OK, GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (self))));
"Saving failed"); g_object_unref (alert);
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (message_dialog),
"%s", error->message);
g_signal_connect (message_dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
gtk_widget_show (message_dialog);
g_error_free (error); g_error_free (error);
} }
g_free (text); g_free (text);
g_object_unref (file); g_object_unref (file);
} }
gtk_window_destroy (GTK_WINDOW (dialog));
} }
static void static void
save_cb (GtkWidget *button, save_cb (GtkWidget *button,
NodeEditorWindow *self) NodeEditorWindow *self)
{ {
GtkWidget *dialog; GtkFileDialog *dialog;
GFile *cwd;
dialog = gtk_file_chooser_dialog_new ("Save node", dialog = gtk_file_dialog_new ();
GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (button))), gtk_file_dialog_set_title (dialog, "Save node");
GTK_FILE_CHOOSER_ACTION_SAVE, cwd = g_file_new_for_path (".");
"_Cancel", GTK_RESPONSE_CANCEL, gtk_file_dialog_set_current_folder (dialog, cwd);
"_Save", GTK_RESPONSE_ACCEPT,
NULL);
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
GFile *cwd = g_file_new_for_path (".");
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), cwd, NULL);
g_object_unref (cwd); g_object_unref (cwd);
gtk_file_dialog_save (dialog,
g_signal_connect (dialog, "response", G_CALLBACK (save_response_cb), self); GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (button))),
gtk_widget_show (dialog); NULL,
"demo.node",
NULL,
save_response_cb, self);
g_object_unref (dialog);
} }
static GdkTexture * static GdkTexture *
@ -724,34 +707,29 @@ create_cairo_texture (NodeEditorWindow *self)
} }
static void static void
export_image_response_cb (GtkWidget *dialog, export_image_response_cb (GObject *source,
int response, GAsyncResult *result,
GdkTexture *texture) void *user_data)
{ {
gtk_widget_hide (dialog); GtkFileDialog *dialog = GTK_FILE_DIALOG (source);
GdkTexture *texture = user_data;
GFile *file;
if (response == GTK_RESPONSE_ACCEPT) file = gtk_file_dialog_save_finish (dialog, result, NULL);
if (file)
{ {
GFile *file;
file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
if (!gdk_texture_save_to_png (texture, g_file_peek_path (file))) if (!gdk_texture_save_to_png (texture, g_file_peek_path (file)))
{ {
GtkWidget *message_dialog; GtkAlertDialog *alert;
message_dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_window_get_transient_for (GTK_WINDOW (dialog))), alert = gtk_alert_dialog_new ("Exporting to image failed");
GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT, gtk_alert_dialog_show (alert, GTK_WINDOW (gtk_window_get_transient_for (GTK_WINDOW (dialog))));
GTK_MESSAGE_INFO, g_object_unref (alert);
GTK_BUTTONS_OK,
"Exporting to image failed");
g_signal_connect (message_dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
gtk_widget_show (message_dialog);
} }
g_object_unref (file); g_object_unref (file);
} }
gtk_window_destroy (GTK_WINDOW (dialog));
g_object_unref (texture); g_object_unref (texture);
} }
@ -760,25 +738,24 @@ export_image_cb (GtkWidget *button,
NodeEditorWindow *self) NodeEditorWindow *self)
{ {
GdkTexture *texture; GdkTexture *texture;
GtkWidget *dialog; GtkFileDialog *dialog;
texture = create_texture (self); texture = create_texture (self);
if (texture == NULL) if (texture == NULL)
return; return;
dialog = gtk_file_chooser_dialog_new ("", dialog = gtk_file_dialog_new ();
GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (button))), gtk_file_dialog_set_title (dialog, "");
GTK_FILE_CHOOSER_ACTION_SAVE, gtk_file_dialog_save (dialog,
"_Cancel", GTK_RESPONSE_CANCEL, GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (button))),
"_Save", GTK_RESPONSE_ACCEPT, NULL,
NULL); "example.png",
NULL,
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT); export_image_response_cb, texture);
gtk_window_set_modal (GTK_WINDOW (dialog), TRUE); g_object_unref (dialog);
g_signal_connect (dialog, "response", G_CALLBACK (export_image_response_cb), texture);
gtk_widget_show (dialog);
} }
static void static void
clip_image_cb (GtkWidget *button, clip_image_cb (GtkWidget *button,
NodeEditorWindow *self) NodeEditorWindow *self)