New abstract base class: PsppireWindowBase 20120204030502/pspp
authorJohn Darrington <john@darrington.wattle.id.au>
Sat, 4 Feb 2012 07:44:05 +0000 (08:44 +0100)
committerJohn Darrington <john@darrington.wattle.id.au>
Sat, 4 Feb 2012 07:44:05 +0000 (08:44 +0100)
Created a new abstract base class for windows/dialogs and moved the
psppire_conf manipulation to there.  Should fix problems reported by bojo42
where the window geometry wasn't always correctly saved.

src/ui/gui/automake.mk
src/ui/gui/psppire-dialog.c
src/ui/gui/psppire-window-base.c [new file with mode: 0644]
src/ui/gui/psppire-window-base.h [new file with mode: 0644]
src/ui/gui/psppire-window.c

index 770a5a9a012635dff906690f47ec351a57252714..560d44af835ea58582dc410611dd5754ef1ef9ef 100644 (file)
@@ -252,6 +252,8 @@ src_ui_gui_psppire_SOURCES = \
        src/ui/gui/psppire-vbuttonbox.h \
        src/ui/gui/psppire-window.c \
        src/ui/gui/psppire-window.h \
+       src/ui/gui/psppire-window-base.c \
+       src/ui/gui/psppire-window-base.h \
        src/ui/gui/psppire-window-register.c \
        src/ui/gui/psppire-window-register.h \
        src/ui/gui/rank-dialog.c \
index c365ab4b8eb47b2d237c9875db4eb92f2664435a..1e4216175b41e1a536a4d64decbca52a89911afc 100644 (file)
 #include "psppire-dialog.h"
 #include "psppire-buttonbox.h"
 #include "psppire-selector.h"
-#include "psppire-conf.h"
 #include <string.h>
 #include "builder-wrapper.h"
 #include "help-menu.h"
 
+#include "psppire-window-base.h"
+
 static void psppire_dialog_class_init          (PsppireDialogClass *);
 static void psppire_dialog_init                (PsppireDialog      *);
 
@@ -69,7 +70,7 @@ psppire_dialog_get_type (void)
        NULL
       };
 
-      dialog_type = g_type_register_static (GTK_TYPE_WINDOW,
+      dialog_type = g_type_register_static (PSPPIRE_TYPE_WINDOW_BASE,
                                            "PsppireDialog", &dialog_info, 0);
 
       g_type_add_interface_static (dialog_type,
@@ -314,37 +315,6 @@ delete_event_callback (GtkWidget *w, GdkEvent *e, gpointer data)
 }
 
 
-static gboolean
-configure_event_callback (GtkDialog *dialog,
-                         GdkEvent *event, gpointer data)
-{
-  const gchar *base;
-
-  PsppireConf *conf = psppire_conf_new ();
-
-  if ( ! gtk_widget_get_mapped (GTK_WIDGET (dialog)))
-    return FALSE;
-
-  base = gtk_buildable_get_name (GTK_BUILDABLE (dialog));
-
-  psppire_conf_save_window_geometry (conf, base, GTK_WINDOW (dialog));
-
-  return FALSE;
-}
-
-
-static void
-on_realize (GtkWindow *dialog, gpointer data)
-{
-  PsppireConf *conf = psppire_conf_new ();
-
-  const gchar *base = gtk_buildable_get_name (GTK_BUILDABLE (dialog));
-
-  psppire_conf_set_window_geometry (conf, base, dialog);
-}
-
-
-
 static void
 psppire_dialog_init (PsppireDialog *dialog)
 {
@@ -366,15 +336,6 @@ psppire_dialog_init (PsppireDialog *dialog)
                    G_CALLBACK (delete_event_callback),
                    dialog);
 
-  g_signal_connect (dialog, "configure-event",
-                   G_CALLBACK (configure_event_callback),
-                   dialog);
-
-  g_signal_connect (dialog, "realize",
-                   G_CALLBACK (on_realize),
-                   dialog);
-
-
   gtk_window_set_type_hint (GTK_WINDOW (dialog),
        GDK_WINDOW_TYPE_HINT_DIALOG);
 
diff --git a/src/ui/gui/psppire-window-base.c b/src/ui/gui/psppire-window-base.c
new file mode 100644 (file)
index 0000000..29c0fd7
--- /dev/null
@@ -0,0 +1,106 @@
+/* PSPPIRE - a graphical user interface for PSPP.
+   Copyright (C) 2012  Free Software Foundation
+
+   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 3 of the License, 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 <http://www.gnu.org/licenses/>. */
+
+/*
+ This is an abstract base class upon which all (well almost all) windows in
+ psppire are based.   The exceptions are transient windows such as the 
+ splash screen and popups.
+
+ It currently provides the feature where the window's geometry "persists"
+ so that she gets the windows appearing in her favourite size/shape/position.
+*/
+
+
+#include <config.h>
+
+#include "psppire-window-base.h"
+#include "psppire-conf.h"
+#include <string.h>
+
+#include <gtk/gtk.h>
+
+static void psppire_window_base_class_init    (PsppireWindowBaseClass *class);
+static void psppire_window_base_init          (PsppireWindowBase      *window);
+
+G_DEFINE_ABSTRACT_TYPE (PsppireWindowBase, psppire_window_base, GTK_TYPE_WINDOW);
+
+
+/* Obtain a string identifying this window.
+
+   If the window has a name, we use that.
+   Otherwise we fall back on the class name.
+ */
+static const char *
+get_window_id (GtkWidget *wb)
+{
+  const gchar *name = NULL;
+      
+  g_object_get (wb, "name", &name, NULL);
+
+  if (NULL == name || 0 == strcmp ("", name))
+    name = G_OBJECT_TYPE_NAME (wb);
+
+  return name;
+}
+
+/*
+  On realization, we read the desired geometry from the config, and set the
+  window accordingly.
+ */
+static void
+realize (GtkWidget *wb)
+{
+  PsppireConf *conf = psppire_conf_new ();
+
+  psppire_conf_set_window_geometry (conf, get_window_id (wb), GTK_WINDOW (wb));
+
+  if (GTK_WIDGET_CLASS (psppire_window_base_parent_class)->realize)
+    return GTK_WIDGET_CLASS (psppire_window_base_parent_class)->realize (wb) ;
+}
+
+
+/*
+  When the window is resized/repositioned, write the new geometry to the config.
+*/
+static gboolean
+configure_event (GtkWidget *wb, GdkEventConfigure *event)
+{
+  if (gtk_widget_get_mapped (wb))
+    {
+      PsppireConf *conf = psppire_conf_new ();
+
+      psppire_conf_save_window_geometry (conf, get_window_id (wb), GTK_WINDOW (wb));
+    }
+
+  if (GTK_WIDGET_CLASS (psppire_window_base_parent_class)->configure_event)
+    return GTK_WIDGET_CLASS (psppire_window_base_parent_class)->configure_event (wb, event) ;
+
+  return FALSE;
+}
+
+static void 
+psppire_window_base_class_init    (PsppireWindowBaseClass *class)
+{
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
+  widget_class->configure_event = configure_event;
+  widget_class->realize = realize;
+}
+
+static void 
+psppire_window_base_init          (PsppireWindowBase      *window)
+{
+}
+
diff --git a/src/ui/gui/psppire-window-base.h b/src/ui/gui/psppire-window-base.h
new file mode 100644 (file)
index 0000000..5453bb7
--- /dev/null
@@ -0,0 +1,73 @@
+/* PSPPIRE - a graphical user interface for PSPP.
+   Copyright (C) 2012  Free Software Foundation
+
+   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 3 of the License, 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 <http://www.gnu.org/licenses/>. */
+
+/*
+ This is an abstract base class upon which all (well almost all) windows in
+ psppire are based.   The exceptions are transient windows such as the 
+ splash screen and popups.
+*/
+
+#ifndef __PSPPIRE_WINDOW_BASE_H__
+#define __PSPPIRE_WINDOW_BASE_H__
+
+
+#include <glib.h>
+#include <glib-object.h>
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+
+#define PSPPIRE_TYPE_WINDOW_BASE            (psppire_window_base_get_type ())
+
+#define PSPPIRE_WINDOW_BASE(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+    PSPPIRE_TYPE_WINDOW_BASE, PsppireWindowBase))
+
+#define PSPPIRE_WINDOW_BASE_CLASS(class)    (G_TYPE_CHECK_CLASS_CAST ((class), \
+    PSPPIRE_TYPE_WINDOW_BASE, PsppireWindowBaseClass))
+
+#define PSPPIRE_IS_WINDOW_BASE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+    PSPPIRE_TYPE_WINDOW_BASE))
+
+#define PSPPIRE_IS_WINDOW_BASE_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), \
+    PSPPIRE_TYPE_WINDOW_BASE))
+
+
+typedef struct _PsppireWindowBase       PsppireWindowBase;
+typedef struct _PsppireWindowBaseClass  PsppireWindowBaseClass;
+
+
+struct _PsppireWindowBase
+{
+  GtkWindow parent;
+
+  /* <private> */
+};
+
+
+struct _PsppireWindowBaseClass
+{
+  GtkWindowClass parent_class;
+};
+
+
+
+GType      psppire_window_base_get_type        (void);
+GType      psppire_window_base_model_get_type        (void);
+
+G_END_DECLS
+
+#endif /* __PSPPIRE_WINDOW_BASE_H__ */
index 4eb274b5284805b3cfc8742499d11c1409cd7a26..4c7fcf57dc2040c32357e9e371bbad8f6339d75d 100644 (file)
@@ -17,6 +17,7 @@
 #include <config.h>
 
 #include "psppire-window.h"
+#include "psppire-window-base.h"
 
 #include <gtk/gtk.h>
 
@@ -32,7 +33,6 @@
 #include "data/dataset.h"
 
 #include "helper.h"
-#include "psppire-conf.h"
 #include "psppire-data-window.h"
 #include "psppire-encoding-selector.h"
 #include "psppire-syntax-window.h"
@@ -67,7 +67,7 @@ psppire_window_get_type (void)
       };
 
       psppire_window_type =
-       g_type_register_static (GTK_TYPE_WINDOW, "PsppireWindow",
+       g_type_register_static (PSPPIRE_TYPE_WINDOW_BASE, "PsppireWindow",
                                &psppire_window_info, G_TYPE_FLAG_ABSTRACT);
     }
 
@@ -229,17 +229,6 @@ psppire_window_get_property (GObject         *object,
 }
 
 
-static void
-on_realize (GtkWindow *window, gpointer data)
-{
-  PsppireConf *conf = psppire_conf_new ();
-
-  const gchar *base = G_OBJECT_TYPE_NAME (window);
-
-  psppire_conf_set_window_geometry (conf, base, window);
-}
-
-
 static void
 psppire_window_finalize (GObject *object)
 {
@@ -423,13 +412,6 @@ on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
 {
   PsppireWindowRegister *reg = psppire_window_register_new ();
 
-  const gchar *base = G_OBJECT_TYPE_NAME (w);
-
-  PsppireConf *conf = psppire_conf_new ();
-
-  psppire_conf_save_window_geometry (conf, base, GTK_WINDOW (w));
-
-
   if ( w->dirty )
     {
       gint response = psppire_window_query_save (w);
@@ -490,9 +472,6 @@ psppire_window_init (PsppireWindow *window)
   g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
 
   g_object_set (window, "icon-name", "pspp", NULL);
-
-  g_signal_connect (window, "realize",
-                   G_CALLBACK (on_realize), window);
 }
 
 /*