[balsa/gtk4: 275/294] file-chooser-button: Replacement for Gtk functions




commit b2627cf379d11a49f6c991ebe753b6c4afdbb65b
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Mon Dec 14 14:48:22 2020 -0500

    file-chooser-button: Replacement for Gtk functions
    
    new file:   libbalsa/file-chooser-button.c
    new file:   libbalsa/file-chooser-button.h
    modified:   libbalsa/meson.build
    modified:   libbalsa/Makefile.am
    modified:   po/POTFILES.in

 libbalsa/Makefile.am           |   2 +
 libbalsa/file-chooser-button.c | 180 +++++++++++++++++++++++++++++++++++++++++
 libbalsa/file-chooser-button.h |  39 +++++++++
 libbalsa/meson.build           |   2 +
 po/POTFILES.in                 |   1 +
 5 files changed, 224 insertions(+)
---
diff --git a/libbalsa/Makefile.am b/libbalsa/Makefile.am
index b272d4269..00c9d3044 100644
--- a/libbalsa/Makefile.am
+++ b/libbalsa/Makefile.am
@@ -36,6 +36,8 @@ libbalsa_a_SOURCES =          \
        body.h                  \
        completion.c            \
        completion.h            \
+        file-chooser-button.c   \
+        file-chooser-button.h   \
        files.c                 \
        files.h                 \
        filter-error.c          \
diff --git a/libbalsa/file-chooser-button.c b/libbalsa/file-chooser-button.c
new file mode 100644
index 000000000..993f0f761
--- /dev/null
+++ b/libbalsa/file-chooser-button.c
@@ -0,0 +1,180 @@
+/* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
+/* Balsa E-Mail Client
+ *
+ * Copyright (C) 1997-2020 Peter Bloomfield
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
+# include "config.h"
+#endif                          /* HAVE_CONFIG_H */
+
+#include "file-chooser-button.h"
+#include <glib/gi18n.h>
+
+/*
+ * libbalsa_file_chooser_button
+ *
+ * Replacement in Gtk4 for GtkFileChooserButton
+ */
+
+#define LIBBALSA_FILE_CHOOSER_BUTTON_KEY "libbalsa-file-chooser-button"
+
+typedef struct {
+    GtkWidget           *button;
+    char                *title;
+    GtkFileChooserAction action;
+    GFile               *file;
+    GCallback            response_cb;
+    gpointer             response_data;
+    GtkWidget           *dialog;
+} libbalsa_file_chooser_button_data;
+
+static void
+libbalsa_file_chooser_button_free(gpointer user_data)
+{
+    libbalsa_file_chooser_button_data *data = user_data;
+
+    g_free(data->title);
+    g_object_unref(data->file);
+    g_free(data);
+}
+
+static void
+libbalsa_file_chooser_button_response(GtkDialog *dialog,
+                                      int        response_id,
+                                      gpointer   user_data)
+{
+    libbalsa_file_chooser_button_data *data = user_data;
+
+    gtk_widget_hide(GTK_WIDGET(dialog));
+
+    if (response_id == GTK_RESPONSE_ACCEPT) {
+        GFile *file;
+        char *basename;
+
+        file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(dialog));
+        g_set_object(&data->file, file);
+        basename = g_file_get_basename(file);
+        g_object_unref(file);
+
+        gtk_button_set_label(GTK_BUTTON(data->button), basename);
+        g_free(basename);
+
+        if (data->response_cb != NULL)
+            ((GFunc) data->response_cb)(data->button, data->response_data);
+    }
+
+    gtk_window_destroy(GTK_WINDOW(dialog));
+    data->dialog = NULL;
+}
+
+static void
+libbalsa_file_chooser_button_clicked(GtkButton *button,
+                                     gpointer   user_data)
+{
+    libbalsa_file_chooser_button_data *data = user_data;
+
+    if (data->dialog == NULL) {
+        data->dialog =
+            gtk_file_chooser_dialog_new(data->title,
+                                        GTK_WINDOW(gtk_widget_get_root(GTK_WIDGET(button))),
+                                        data->action,
+                                        _("_Cancel"), GTK_RESPONSE_CANCEL,
+                                        _("_Select"), GTK_RESPONSE_ACCEPT,
+                                        NULL);
+        g_signal_connect(data->dialog, "response",
+                         G_CALLBACK(libbalsa_file_chooser_button_response), data);
+    }
+
+    gtk_file_chooser_set_file(GTK_FILE_CHOOSER(data->dialog), data->file, NULL);
+
+    gtk_widget_show(data->dialog);
+}
+
+static GtkWidget *
+libbalsa_file_chooser_button_from_data(libbalsa_file_chooser_button_data *data)
+{
+    data->file = g_file_new_for_path("");
+
+    data->button = gtk_button_new();
+    g_object_set_data_full(G_OBJECT(data->button), LIBBALSA_FILE_CHOOSER_BUTTON_KEY, data,
+                           libbalsa_file_chooser_button_free);
+    g_signal_connect(data->button, "clicked", G_CALLBACK(libbalsa_file_chooser_button_clicked), data);
+
+    return data->button;
+}
+
+GtkWidget *
+libbalsa_file_chooser_button_new(const char          *title,
+                                 GtkFileChooserAction action,
+                                 GCallback            response_cb,
+                                 gpointer             response_data)
+{
+    libbalsa_file_chooser_button_data *data;
+
+    data = g_new0(libbalsa_file_chooser_button_data, 1);
+    data->title = g_strdup(title);
+    data->action = action;
+    data->response_cb = response_cb;
+    data->response_data = response_data;
+
+    return libbalsa_file_chooser_button_from_data(data);
+}
+
+GtkWidget *
+libbalsa_file_chooser_button_new_with_dialog(GtkWidget *dialog)
+{
+    libbalsa_file_chooser_button_data *data;
+
+    data = g_new0(libbalsa_file_chooser_button_data, 1);
+    data->dialog = dialog;
+
+    return libbalsa_file_chooser_button_from_data(data);
+}
+
+void
+libbalsa_file_chooser_button_set_file(GtkWidget *button, GFile *file)
+{
+    libbalsa_file_chooser_button_data *data;
+    char *basename;
+
+    g_return_if_fail(GTK_IS_BUTTON(button));
+    g_return_if_fail(G_IS_FILE(file));
+
+    data = g_object_get_data(G_OBJECT(button), LIBBALSA_FILE_CHOOSER_BUTTON_KEY);
+
+    g_return_if_fail(data != NULL);
+
+    basename = g_file_get_basename(file);
+    gtk_button_set_label(GTK_BUTTON(data->button), basename);
+    g_free(basename);
+
+    g_set_object(&data->file, file);
+}
+
+GFile *
+libbalsa_file_chooser_button_get_file(GtkWidget *button)
+{
+    libbalsa_file_chooser_button_data *data;
+
+    g_return_val_if_fail(GTK_IS_BUTTON(button), NULL);
+
+    data = g_object_get_data(G_OBJECT(button), LIBBALSA_FILE_CHOOSER_BUTTON_KEY);
+
+    g_return_val_if_fail(data != NULL, NULL);
+
+    return g_object_ref(data->file);
+}
diff --git a/libbalsa/file-chooser-button.h b/libbalsa/file-chooser-button.h
new file mode 100644
index 000000000..8ca529bd0
--- /dev/null
+++ b/libbalsa/file-chooser-button.h
@@ -0,0 +1,39 @@
+/* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
+/* Balsa E-Mail Client
+ *
+ * Copyright (C) 1997-2016 Stuart Parmenter and others,
+ *                         See the file AUTHORS for a list.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option) 
+ * any later version.
+ *  
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of 
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
+ * GNU General Public License for more details.
+ *  
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef __FILE_CHOOSER_BUTTON_H__
+#define __FILE_CHOOSER_BUTTON_H__
+
+#ifndef BALSA_VERSION
+# error "Include config.h before this file."
+#endif
+
+#include <gtk/gtk.h>
+
+GtkWidget *libbalsa_file_chooser_button_new            (const char          *title,
+                                                        GtkFileChooserAction action,
+                                                        GCallback            response_cb,
+                                                        gpointer             response_data);
+GtkWidget *libbalsa_file_chooser_button_new_with_dialog(GtkWidget *dialog);
+void       libbalsa_file_chooser_button_set_file       (GtkWidget           *button,
+                                                        GFile               *file);
+GFile     *libbalsa_file_chooser_button_get_file       (GtkWidget           *button);
+
+#endif                          /* __FILE_CHOOSER_BUTTON_H__ */
diff --git a/libbalsa/meson.build b/libbalsa/meson.build
index 8abc1824b..a1076e51e 100644
--- a/libbalsa/meson.build
+++ b/libbalsa/meson.build
@@ -33,6 +33,8 @@ libbalsa_a_sources = [
   'body.h',
   'completion.c',
   'completion.h',
+  'file-chooser-button.c',
+  'file-chooser-button.h',
   'files.c',
   'files.h',
   'filter-error.c',
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 54c71749d..351fed879 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -14,6 +14,7 @@ libbalsa/address.c
 libbalsa/address-view.c
 libbalsa/autocrypt.c
 libbalsa/body.c
+libbalsa/file-chooser-button.c
 libbalsa/filter.c
 libbalsa/filter-error.c
 libbalsa/filter-file.c


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]