example: Use declared callbacks where possible

Replace manual signal connections with signal handlers
declared in the ui file, where possible.
This commit is contained in:
Matthias Clasen
2013-07-27 20:33:08 -04:00
parent 3bb059b6e8
commit c9ce98714d
15 changed files with 116 additions and 80 deletions

View File

@@ -4,68 +4,76 @@
#include "exampleappwin.h"
#include "exampleappprefs.h"
struct ExampleAppPrefs {
GtkDialog parent;
struct _ExampleAppPrefs
{
GtkDialog parent;
};
struct ExampleAppPrefsClass {
GtkDialogClass parent_class;
struct _ExampleAppPrefsClass
{
GtkDialogClass parent_class;
};
typedef struct ExampleAppPrefsPrivate ExampleAppPrefsPrivate;
struct ExampleAppPrefsPrivate {
GSettings *settings;
GtkWidget *font;
GtkWidget *transition;
GtkWidget *close;
typedef struct _ExampleAppPrefsPrivate ExampleAppPrefsPrivate;
struct _ExampleAppPrefsPrivate
{
GSettings *settings;
GtkWidget *font;
GtkWidget *transition;
};
G_DEFINE_TYPE_WITH_PRIVATE(ExampleAppPrefs, example_app_prefs, GTK_TYPE_DIALOG)
static void
preferences_closed (GtkWidget *button)
{
gtk_widget_destroy (gtk_widget_get_toplevel (button));
}
static void
example_app_prefs_init (ExampleAppPrefs *prefs)
{
ExampleAppPrefsPrivate *priv;
ExampleAppPrefsPrivate *priv;
priv = example_app_prefs_get_instance_private (prefs);
gtk_widget_init_template (GTK_WIDGET (prefs));
priv->settings = g_settings_new ("org.gtk.exampleapp");
priv = example_app_prefs_get_instance_private (prefs);
gtk_widget_init_template (GTK_WIDGET (prefs));
priv->settings = g_settings_new ("org.gtk.exampleapp");
g_settings_bind (priv->settings, "font",
priv->font, "font",
G_SETTINGS_BIND_DEFAULT);
g_settings_bind (priv->settings, "transition",
priv->transition, "active-id",
G_SETTINGS_BIND_DEFAULT);
g_signal_connect_swapped (priv->close, "clicked",
G_CALLBACK (gtk_widget_destroy), prefs);
g_settings_bind (priv->settings, "font",
priv->font, "font",
G_SETTINGS_BIND_DEFAULT);
g_settings_bind (priv->settings, "transition",
priv->transition, "active-id",
G_SETTINGS_BIND_DEFAULT);
}
static void
example_app_prefs_dispose (GObject *object)
{
ExampleAppPrefsPrivate *priv;
ExampleAppPrefsPrivate *priv;
priv = example_app_prefs_get_instance_private (EXAMPLE_APP_PREFS (object));
g_clear_object (&priv->settings);
priv = example_app_prefs_get_instance_private (EXAMPLE_APP_PREFS (object));
g_clear_object (&priv->settings);
G_OBJECT_CLASS (example_app_prefs_parent_class)->dispose (object);
G_OBJECT_CLASS (example_app_prefs_parent_class)->dispose (object);
}
static void
example_app_prefs_class_init (ExampleAppPrefsClass *class)
{
G_OBJECT_CLASS (class)->dispose = example_app_prefs_dispose;
G_OBJECT_CLASS (class)->dispose = example_app_prefs_dispose;
gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class),
"/org/gtk/exampleapp/prefs.ui");
gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class), ExampleAppPrefs, font);
gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class), ExampleAppPrefs, transition);
gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class), ExampleAppPrefs, close);
gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class),
"/org/gtk/exampleapp/prefs.ui");
gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class), ExampleAppPrefs, font);
gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class), ExampleAppPrefs, transition);
gtk_widget_class_bind_template_callback (GTK_WIDGET_CLASS (class), preferences_closed);
}
ExampleAppPrefs *
example_app_prefs_new (ExampleAppWindow *win)
{
return g_object_new (EXAMPLE_APP_PREFS_TYPE, "transient-for", win, NULL);
return g_object_new (EXAMPLE_APP_PREFS_TYPE, "transient-for", win, NULL);
}

View File

@@ -9,8 +9,8 @@
#define EXAMPLE_APP_PREFS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EXAMPLE_APP_PREFS_TYPE, ExampleAppPrefs))
typedef struct ExampleAppPrefs ExampleAppPrefs;
typedef struct ExampleAppPrefsClass ExampleAppPrefsClass;
typedef struct _ExampleAppPrefs ExampleAppPrefs;
typedef struct _ExampleAppPrefsClass ExampleAppPrefsClass;
GType example_app_prefs_get_type (void);

View File

@@ -69,6 +69,7 @@
<property name="visible">True</property>
<child>
<object class="GtkButton" id="close">
<signal name="clicked" handler="preferences_closed"/>
<property name="visible">True</property>
<property name="label">_Close</property>
<property name="use-underline">True</property>