From b2cab90dcb8e35b95bbe5fd159c642c6cb156d53 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 30 Jun 2014 22:51:15 +0200 Subject: [PATCH] Add preferences window with option to turn off maximized windows This is very limited right now. See README.md for other options to integrate here (and throughout the app). --- src/ag-app.c | 6 +++- src/ag-preferences.c | 80 ++++++++++++++++++++++++++++++------------- src/ag-preferences.h | 23 +++++++++++-- src/ag-preferences.ui | 59 +++++++++++++++++++++++++++++++ src/ag.gresource.xml | 1 + 5 files changed, 143 insertions(+), 26 deletions(-) create mode 100644 src/ag-preferences.ui diff --git a/src/ag-app.c b/src/ag-app.c index b84a823..fff9627 100644 --- a/src/ag-app.c +++ b/src/ag-app.c @@ -63,7 +63,11 @@ new_window_cb(GSimpleAction *action, GVariant *parameter, gpointer user_data) static void preferences_cb(GSimpleAction *action, GVariant *parameter, gpointer user_data) { - ag_preferences_show_dialog(); + AgApp *app = AG_APP(user_data); + GtkWindow *window; + + window = ag_app_peek_first_window(app); + ag_preferences_show_dialog(window); } static void diff --git a/src/ag-preferences.c b/src/ag-preferences.c index 7a80a77..5615d4a 100644 --- a/src/ag-preferences.c +++ b/src/ag-preferences.c @@ -1,43 +1,77 @@ -#include #include #include "ag-settings.h" +#include "ag-preferences.h" -typedef struct { - GtkWidget *dialog; +static GtkWidget *prefs_dialog = NULL; + +typedef struct _AgPreferencesPrivate { + GtkCheckButton *maximized; AgSettings *settings; -} AgPreferences; +} AgPreferencesPrivate; -static AgPreferences *prefs; +G_DEFINE_TYPE_WITH_PRIVATE(AgPreferences, ag_preferences, GTK_TYPE_DIALOG); static void -ag_preferences_init(void) +ag_preferences_finalize(GObject *gobject) { - GApplication *app; + AgPreferencesPrivate *priv; - if (prefs) { - return; - } + priv = ag_preferences_get_instance_private(AG_PREFERENCES(gobject)); - if ((app = g_application_get_default()) == NULL) { - g_warning("Cannot launch preferences: No default application found"); + g_clear_object(&priv->settings); - return; - } + G_OBJECT_CLASS(ag_preferences_parent_class)->finalize(gobject); +} - prefs = g_new0(AgPreferences, 1); - prefs->settings = ag_settings_get(); +static void +ag_preferences_response(GtkDialog *dlg, gint response_id) +{ + gtk_widget_destroy(GTK_WIDGET(dlg)); +} + +static void +ag_preferences_class_init(AgPreferencesClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS(klass); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass); + GtkDialogClass *dialog_class = GTK_DIALOG_CLASS(klass); + + object_class->finalize = ag_preferences_finalize; + dialog_class->response = ag_preferences_response; + + gtk_widget_class_set_template_from_resource(widget_class, "/eu/polonkai/gergely/astrognome/ag-preferences.ui"); + gtk_widget_class_bind_template_child_private(widget_class, AgPreferences, maximized); +} + +static void +ag_preferences_init(AgPreferences *prefs) +{ + AgPreferencesPrivate *priv; + GSettings *settings_window; + + priv = ag_preferences_get_instance_private(prefs); + gtk_widget_init_template(GTK_WIDGET(prefs)); + + priv->settings = ag_settings_get(); + + settings_window = ag_settings_peek_window_settings(priv->settings); + g_settings_bind(settings_window, "maximized", priv->maximized, "active", G_SETTINGS_BIND_DEFAULT); } void -ag_preferences_show_dialog(void) +ag_preferences_show_dialog(GtkWindow *parent) { - ag_preferences_init(); + g_return_if_fail(GTK_IS_WINDOW(parent)); - if (prefs->dialog != NULL) { - gtk_window_present(GTK_WINDOW(prefs->dialog)); - - return; + if (prefs_dialog == NULL) { + prefs_dialog = GTK_WIDGET(g_object_new(AG_TYPE_PREFERENCES, NULL)); + g_signal_connect(prefs_dialog, "destroy", G_CALLBACK(gtk_widget_destroyed), &prefs_dialog); } -} + if (parent != gtk_window_get_transient_for(GTK_WINDOW(prefs_dialog))) { + gtk_window_set_transient_for(GTK_WINDOW(prefs_dialog), parent); + } + + gtk_window_present(GTK_WINDOW(prefs_dialog)); +} diff --git a/src/ag-preferences.h b/src/ag-preferences.h index adfd3ab..686d9e7 100644 --- a/src/ag-preferences.h +++ b/src/ag-preferences.h @@ -5,9 +5,28 @@ G_BEGIN_DECLS -void ag_preferences_show_dialog(void); +#define AG_TYPE_PREFERENCES (ag_preferences_get_type()) +#define AG_PREFERENCES(o) (G_TYPE_CHECK_INSTANCE_CAST((o), AG_TYPE_PREFERENCES, AgPreferences)) +#define AG_PREFERENCES_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), AG_TYPE_PREFERENCES, AgPreferencesClass)) +#define AG_IS_PREFERENCES(o) (G_TYPE_CHECK_INSTANCE_TYPE((o), AG_TYPE_PREFERENCES)) +#define AG_IS_PREFERENCES_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE((k), AG_TYPE_PREFERENCES)) +#define AG_PREFERENCES_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS((o), AG_TYPE_PREFERENCES, AgPreferencesClass)) + +typedef struct _AgPreferences AgPreferences; +typedef struct _AgPreferencesClass AgPreferencesClass; + +struct _AgPreferences { + GtkDialog parent; +}; + +struct _AgPreferencesClass { + GtkDialogClass parent_class; +}; + +GType ag_preferences_get_type(void); + +void ag_preferences_show_dialog(GtkWindow *parent); G_END_DECLS #endif /* __AG_PREFERENCES_H__ */ - diff --git a/src/ag-preferences.ui b/src/ag-preferences.ui new file mode 100644 index 0000000..1f5a2a7 --- /dev/null +++ b/src/ag-preferences.ui @@ -0,0 +1,59 @@ + + + + + + diff --git a/src/ag.gresource.xml b/src/ag.gresource.xml index 6466ba3..61f3916 100644 --- a/src/ag.gresource.xml +++ b/src/ag.gresource.xml @@ -2,5 +2,6 @@ astrognome.ui + ag-preferences.ui