[Glade-devel] [patch, glade3] decouple glade-project from glade-project-window



--=-Qbp+OqgZSYGTQ3ZrZ8S/
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi!

The attached patch decouples completely glade-project from
glade-project-window, to the point that glade-project.c does not need to
include glade-project-window.h anymore. I think that this is a good
thing if we want that the glade-project object can be used in other
apps. Beside is a good cleanup since things that got moved really belong
to gpw (updating the menu, the title etc).

The patch consist of the following:
- move away from project things which belonged to gpw; a side effect of
this is that some of these functions could be made private to gpw (eg
refresh_title). In particular note that we don't have anymore open/save
and open_from_file/save_to_file, but just open/save.
- cleanups in glade_project_window; some functions now are private to
this file; I also changed the api of some functions like add_project to
not take gpw as an argument since all the callers simply do gpw =
glade_project_window_get () and then pass it as an argument.
- fix main.c for the above changes.


ciao
        paolo

--=-Qbp+OqgZSYGTQ3ZrZ8S/
Content-Disposition: attachment; filename=decouple_prj-gpw.patch
Content-Type: text/x-patch; name=decouple_prj-gpw.patch; charset=UTF-8
Content-Transfer-Encoding: 7bit

diff -pur gnome2/glade3/ChangeLog glade3/ChangeLog
--- gnome2/glade3/ChangeLog     2003-05-15 09:32:17.000000000 +0200
+++ glade3/ChangeLog    2003-05-15 18:47:10.000000000 +0200
@@ -1,3 +1,11 @@
+2003-05-15  Paolo Borelli  <pborelli katamail com>
+
+       * src/glade-project.[ch]: decouple the project object from
+       glade-project-window.
+       * src/glade-project-window.[ch]: move here the things removed
+       from glade-project. Slightly change some APIs.
+       * src/main.c: update for the above.
+
 2003-05-14  Joaquin Cuenca Abela  <e98cuenc yahoo com>
 
        * src/glade-project-window.[ch]: Make the undo/redo toolbar items
diff -pur gnome2/glade3/src/glade-project.c glade3/src/glade-project.c
--- gnome2/glade3/src/glade-project.c   2003-05-09 07:54:00.000000000 +0200
+++ glade3/src/glade-project.c  2003-05-15 18:11:28.000000000 +0200
@@ -28,7 +28,6 @@
 
 #include "glade.h"
 #include "glade-project.h"
-#include "glade-project-window.h"
 #include "glade-widget.h"
 #include "glade-widget-class.h"
 #include "glade-xml-utils.h"
@@ -37,7 +36,6 @@
 
 static void glade_project_class_init (GladeProjectClass * klass);
 static void glade_project_init (GladeProject *project);
-static void glade_project_destroy (GtkObject *object);
 
 enum
 {
@@ -119,11 +117,8 @@ glade_project_class_init (GladeProjectCl
        klass->remove_widget = NULL;
        klass->widget_name_changed = NULL;
        klass->selection_changed = NULL;
-       
-       object_class->destroy = glade_project_destroy;
 }
 
-
 static void
 glade_project_init (GladeProject * project)
 {
@@ -132,65 +127,6 @@ glade_project_init (GladeProject * proje
        project->selection = NULL;
        project->undo_stack = NULL;
        project->prev_redo_item = NULL;
-
-       /* Setup the /Project menu item */
-       project->entry.accelerator = NULL;
-       project->entry.callback = glade_project_window_set_project_cb;
-       project->entry.callback_action = 0;
-       project->entry.item_type = g_strdup ("<Item>");
-}
-
-static void
-glade_project_destroy (GtkObject *object)
-{
-       GladeProject *project;
-
-       project = GLADE_PROJECT (object);
-
-}
-
-static GladeProject *
-glade_project_check_previously_loaded (const gchar *path)
-{
-       GladeProjectWindow *gpw;
-       GladeProject *project;
-       GList *list;
-
-       gpw = glade_project_window_get ();
-       list = gpw->projects;
-
-       for (; list; list = list->next) {
-               project = GLADE_PROJECT (list->data);
-
-               if (project->path != NULL && !strcmp (project->path, path))
-                       return project;
-       }
-
-       return NULL;
-}
-
-static void
-glade_project_update_menu_path (GladeProject *project)
-{
-       g_free (project->entry.path);
-
-       project->entry.path = g_strdup_printf ("/Project/%s", project->name);
-}
-
-static void
-glade_project_refresh_menu_item (GladeProject *project)
-{
-       GladeProjectWindow *gpw;
-       GtkWidget *label;
-
-       gpw = glade_project_window_get ();
-       label = gtk_bin_get_child (GTK_BIN (gtk_item_factory_get_item (gpw->item_factory, 
project->entry.path)));
-
-       /* Change the menu item's label */
-       gtk_label_set_text (GTK_LABEL (label), project->name);
-
-       /* Update the path entry, for future changes. */
-       glade_project_update_menu_path (project);
 }
 
 GladeProject *
@@ -204,17 +140,9 @@ glade_project_new (gboolean untitled)
        if (untitled)
                project->name = g_strdup_printf ("Untitled %i", i++);
 
-       glade_project_update_menu_path (project);
-
        return project;
 }
 
-static void
-glade_project_set_changed (GladeProject *project, gboolean changed)
-{
-       project->changed = changed;
-}
-
 void
 glade_project_selection_changed (GladeProject *project)
 {
@@ -255,7 +183,7 @@ glade_project_add_widget (GladeProject *
        g_return_if_fail (GTK_IS_OBJECT (project));
 
        glade_project_add_widget_real (project, widget);
-       glade_project_set_changed (project, TRUE);
+       project->changed = TRUE;
 }
 
 /**
@@ -293,11 +221,10 @@ glade_project_remove_widget (GladeWidget
        g_return_if_fail (widget != NULL);
 
        project = widget->project;
-       
-       glade_project_remove_widget_real (project, widget);
 
+       glade_project_remove_widget_real (project, widget);
        glade_project_selection_changed (project);
-       glade_project_set_changed (project, TRUE);
+       project->changed = TRUE;
 }
        
 void
@@ -310,7 +237,7 @@ glade_project_widget_name_changed (Glade
        gtk_signal_emit (GTK_OBJECT (project),
                         glade_project_signals [WIDGET_NAME_CHANGED], widget);
        
-       glade_project_set_changed (project, TRUE);
+       project->changed = TRUE;
 }
 
 /**
@@ -378,18 +305,6 @@ glade_project_new_widget_name (GladeProj
        return NULL;
 }
 
-
-
-
-
-
-
-
-
-
-
-
-
 void
 glade_project_selection_clear (GladeProject *project, gboolean emit_signal)
 {
@@ -564,8 +479,15 @@ glade_project_new_from_node (GladeXmlNod
        return project; 
 }
 
-static GladeProject *
-glade_project_open_from_file (const gchar *path)
+/**
+ * glade_project_open:
+ * @path: 
+ * 
+ * Open a project at the given path.
+ * On success returns the opened project else NULL.
+ **/
+GladeProject *
+glade_project_open (const gchar *path)
 {
        GladeXmlContext *context;
        GladeXmlDoc *doc;
@@ -582,15 +504,20 @@ glade_project_open_from_file (const gcha
                project->path = g_strdup_printf ("%s", path);
                g_free (project->name);
                project->name = g_path_get_basename (project->path);
-               /* Setup the menu item to be shown in the /Project menu. */
-               glade_project_update_menu_path (project);
        }
 
        return project;
 }
 
-static gboolean
-glade_project_save_to_file (GladeProject *project, const gchar *full_path)
+/**
+ * glade_project_save:
+ * @project:
+ * @path 
+ * 
+ * Save the project to the given path. Returns TRUE on success.
+ **/
+gboolean
+glade_project_save (GladeProject *project, const gchar *path)
 {
        GladeXmlContext *context;
        GladeXmlNode *root;
@@ -609,14 +536,14 @@ glade_project_save_to_file (GladeProject
                return FALSE;
 
        glade_xml_doc_set_root (xml_doc, root);
-       ret = glade_xml_doc_save (xml_doc, full_path);
+       ret = glade_xml_doc_save (xml_doc, path);
        glade_xml_doc_free (xml_doc);
 
        if (ret < 0)
                return FALSE;
 
        g_free (project->path);
-       project->path = g_strdup_printf ("%s", full_path);
+       project->path = g_strdup_printf ("%s", path);
        g_free (project->name);
        project->name = g_path_get_basename (project->path);
 
@@ -625,65 +552,3 @@ glade_project_save_to_file (GladeProject
        return TRUE;
 }
 
-/**
- * glade_project_open:
- * @path: 
- * 
- * Open a project at the given path. Returns TRUE on success.
- **/
-gboolean
-glade_project_open (const gchar *path)
-{
-       GladeProjectWindow *gpw;
-       GladeProject *project;
-
-       g_return_val_if_fail (path != NULL, FALSE);
-
-       gpw = glade_project_window_get ();
-
-       /* If the project is previously loaded, don't re-load */
-       if ((project = glade_project_check_previously_loaded (path)) != NULL) {
-               glade_project_window_set_project (gpw, project);
-               return TRUE;
-       }
-
-       project = glade_project_open_from_file (path);
-       if (!project) {
-               glade_util_ui_warn (_("Could not open project."));
-               return FALSE;
-       }
-
-       glade_project_window_add_project (gpw, project);
-       return TRUE;
-}
-
-/**
- * glade_project_save:
- * @project:
- * @path 
- * 
- * Save the project to the given path. Returns TRUE on success.
- **/
-gboolean
-glade_project_save (GladeProject *project, const gchar *path)
-{
-       GladeProjectWindow *gpw;
-
-       g_return_val_if_fail (GLADE_IS_PROJECT (project), FALSE);
-       g_return_val_if_fail (path != NULL, FALSE);
-
-       gpw = glade_project_window_get ();
-
-       if (!glade_project_save_to_file (project, path)) {
-               glade_util_ui_warn (_("Invalid file name"));
-               return FALSE;
-       }
-
-       glade_project_refresh_menu_item (project);
-       glade_project_window_refresh_title (gpw);       
-       glade_util_flash_message (gpw->statusbar_actions_context_id,
-                                 _("Project '%s' saved"), project->name);
-
-       return TRUE;
-}
-
diff -pur gnome2/glade3/src/glade-project.h glade3/src/glade-project.h
--- gnome2/glade3/src/glade-project.h   2003-05-05 14:11:36.000000000 +0200
+++ glade3/src/glade-project.h  2003-05-15 18:10:59.000000000 +0200
@@ -61,11 +61,8 @@ guint glade_project_get_type (void);
 
 GladeProject *glade_project_new (gboolean untitled);
 
-
-/* Project operations */
+GladeProject *glade_project_open (const gchar *path);
 gboolean glade_project_save (GladeProject *project, const gchar *path);
-gboolean glade_project_open (const gchar *path);
-
 
 /* Widget related stuff */
 void glade_project_remove_widget (GladeWidget *widget);
diff -pur gnome2/glade3/src/glade-project-window.c glade3/src/glade-project-window.c
--- gnome2/glade3/src/glade-project-window.c    2003-05-15 09:32:23.000000000 +0200
+++ glade3/src/glade-project-window.c   2003-05-15 18:39:19.000000000 +0200
@@ -25,6 +25,8 @@
 #include <config.h>
 #endif
 
+#include <string.h>
+
 #include "glade.h"
 #include "glade-palette.h"
 #include "glade-editor.h"
@@ -43,16 +45,47 @@
 #include <gdk/gdkkeysyms.h>
 #include <gtk/gtkstock.h>
 
+static void
+gpw_refresh_title (GladeProjectWindow *gpw)
+{
+       gchar *title;
+
+       if (gpw->project)
+               title = g_strdup_printf ("glade3 - %s", gpw->project->name);
+       else
+               title = g_strdup_printf ("glade3");
+
+       gtk_window_set_title (GTK_WINDOW (gpw->window), title);
+       g_free (title);
+}
 
 static void
-gpw_new_cb (void)
+gpw_refresh_project_entry (GladeProject *project)
 {
        GladeProjectWindow *gpw;
-       GladeProject *project;
+       GtkWidget *item;
+       GtkWidget *label;
 
-       project = glade_project_new (TRUE);
        gpw = glade_project_window_get ();
-       glade_project_window_add_project (gpw, project);
+
+       item = gtk_item_factory_get_item (gpw->item_factory, project->entry.path);
+       label = gtk_bin_get_child (GTK_BIN (item));
+
+       /* Change the menu item's label */
+       gtk_label_set_text (GTK_LABEL (label), project->name);
+
+       /* Update the path entry, for future changes. */
+       g_free (project->entry.path);
+       project->entry.path = g_strdup_printf ("/Project/%s", project->name);
+}
+
+static void
+gpw_new_cb (void)
+{
+       GladeProject *project;
+ 
+       project = glade_project_new (TRUE);
+       glade_project_window_add_project (project);
 }
 
 static void
@@ -60,6 +93,7 @@ gpw_on_open_filesel_ok (GtkWidget *widge
 {
        GtkWidget *filesel;
        const gchar *path;
+       GladeProject *project;
 
        filesel = gtk_widget_get_toplevel (widget);
 
@@ -70,7 +104,13 @@ gpw_on_open_filesel_ok (GtkWidget *widge
        if (!path)
                return;
 
-       glade_project_open (path);
+       project = glade_project_open (path);
+       if (!project) {
+               glade_util_ui_warn (_("Could not open project."));
+               return;
+       }
+
+       glade_project_window_add_project (project);
 }
 
 static void
@@ -94,6 +134,7 @@ gpw_on_save_filesel_ok (GtkWidget *widge
 {
        GtkWidget *filesel;
        const gchar *path;
+       GladeProjectWindow *gpw;
 
        filesel = gtk_widget_get_toplevel (widget);
 
@@ -104,7 +145,17 @@ gpw_on_save_filesel_ok (GtkWidget *widge
        if (!path)
                return;
 
-       glade_project_save (project, path);
+       gpw = glade_project_window_get ();
+
+       if (!glade_project_save (project, path)) {
+               glade_util_ui_warn (_("Invalid file name"));
+               return;
+       }
+
+       gpw_refresh_project_entry (project);
+       gpw_refresh_title (gpw);
+       glade_util_flash_message (gpw->statusbar_actions_context_id,
+                                 _("Project '%s' saved"), project->name);
 }
 
 static void
@@ -220,6 +271,7 @@ gpw_close_cb (void)
        GladeProjectWindow *gpw;
        GladeProject *project;
        gboolean close;
+       gchar *item_path;
        GList *list;
 
        gpw = glade_project_window_get ();
@@ -234,7 +286,8 @@ gpw_close_cb (void)
                                return;
        }
 
-       gtk_item_factory_delete_entry (gpw->item_factory, &(project->entry));
+       item_path = g_strdup_printf ("/Project/%s", project->name);
+       gtk_item_factory_delete_item (gpw->item_factory, item_path);
 
        for (list = project->widgets; list; list = list->next) {
                GtkWidget *widget;
@@ -255,11 +308,11 @@ gpw_close_cb (void)
                        glade_project_view_set_project (view, NULL);
                }
                gpw->project = NULL;
-               glade_project_window_refresh_title (gpw);
+               gpw_refresh_title (gpw);
                return;
        }
        
-       glade_project_window_set_project (gpw, gpw->projects->data);
+       glade_project_window_set_project (gpw->projects->data);
 }
 
 static void
@@ -1029,13 +1082,16 @@ glade_project_window_selection_changed_c
 }
 
 void
-glade_project_window_set_project (GladeProjectWindow *gpw, GladeProject *project)
+glade_project_window_set_project (GladeProject *project)
 {
+       GladeProjectWindow *gpw;
        GladeProjectView *view;
        GList *list;
 
        g_return_if_fail (GLADE_IS_PROJECT (project));
 
+       gpw = glade_project_window_get ();
+
        if (g_list_find (gpw->projects, project) == NULL) {
                g_warning ("Could not set project because it could not "
                           " be found in the gpw->project list\n");
@@ -1043,7 +1099,7 @@ glade_project_window_set_project (GladeP
        }
        
        gpw->project = project;
-       glade_project_window_refresh_title (gpw);
+       gpw_refresh_title (gpw);
 
        list = gpw->views;
        for (; list != NULL; list = list->next) {
@@ -1059,15 +1115,6 @@ glade_project_window_set_project (GladeP
        glade_project_selection_changed (project);
 }
 
-void
-glade_project_window_set_project_cb (GladeProject *project)
-{
-       GladeProjectWindow *gpw;
-
-       gpw = glade_project_window_get ();
-       glade_project_window_set_project (gpw, project);
-}
-               
 static void
 gpw_widget_name_changed_cb (GladeProject *project, GladeWidget *widget, GladeEditor *editor)
 {
@@ -1075,16 +1122,35 @@ gpw_widget_name_changed_cb (GladeProject
 }
 
 void
-glade_project_window_add_project (GladeProjectWindow *gpw, GladeProject *project)
-{
-       g_return_if_fail (GLADE_IS_PROJECT_WINDOW (gpw));
-       g_return_if_fail (GLADE_IS_PROJECT (project));
-       
-       gpw->projects = g_list_prepend (gpw->projects, project);
+glade_project_window_add_project (GladeProject *project)
+ {
+       GladeProjectWindow *gpw;
+       GList *list;
+
+       g_return_if_fail (GLADE_IS_PROJECT (project));
+
+       gpw = glade_project_window_get ();
+
+       /* If the project was previously loaded, don't re-load */
+       for (list = gpw->projects; list; list = list->next) {
+               GladeProject *cur_project = GLADE_PROJECT (list->data);
+
+               if (cur_project->path != NULL && !strcmp (cur_project->path, project->path)) {
+                       glade_project_window_set_project (cur_project);
+                       return;
+               }
+       }
+
+       gpw->projects = g_list_prepend (gpw->projects, project);
+ 
+       /* Add the project in the /Project menu. */
+       project->entry.path = g_strdup_printf ("/Project/%s", project->name);
+       project->entry.accelerator = NULL;
+       project->entry.callback = glade_project_window_set_project;
+       project->entry.callback_action = 0;
+       project->entry.item_type = g_strdup ("<Item>");
 
-       /* Add the project in the /Project menu. */
-       gtk_item_factory_create_item (gpw->item_factory, &(project->entry),
-                                     project, 1);
+       gtk_item_factory_create_item (gpw->item_factory, &(project->entry), project, 1);
 
        /* connect the widget_changed_name signal to the editor, so that changes to the widget
         * name external to the properties editor (as when the user undo a widget name change)
@@ -1092,7 +1158,7 @@ glade_project_window_add_project (GladeP
        g_signal_connect (G_OBJECT (project), "widget_name_changed",
                          G_CALLBACK (gpw_widget_name_changed_cb), gpw->editor);
 
-       glade_project_window_set_project (gpw, project);
+       glade_project_window_set_project (project);
 }
 
 void
@@ -1130,8 +1196,6 @@ glade_project_window_change_menu_label (
 void
 glade_project_window_refresh_undo_redo (GladeProjectWindow *gpw)
 {
-       GtkWidget *undo_widget;
-       GtkWidget *redo_widget;
        GList *prev_redo_item;
        GList *undo_item;
        GList *redo_item;
@@ -1140,9 +1204,6 @@ glade_project_window_refresh_undo_redo (
        GladeProject *project;
        
        g_return_if_fail (GLADE_IS_PROJECT_WINDOW (gpw));
-       
-       undo_widget = gtk_item_factory_get_item (gpw->item_factory, "<main>/Edit/Undo");
-       redo_widget = gtk_item_factory_get_item (gpw->item_factory, "<main>/Edit/Redo");
 
        project = gpw->project;
        if (project == NULL) {
@@ -1165,20 +1226,6 @@ glade_project_window_refresh_undo_redo (
 }
 
 void
-glade_project_window_refresh_title (GladeProjectWindow *gpw)
-{
-       gchar *title;
-
-       if (gpw->project)
-               title = g_strdup_printf ("glade3 - %s", gpw->project->name);
-       else
-               title = g_strdup_printf ("glade3");
-
-       gtk_window_set_title (GTK_WINDOW (gpw->window), title);
-       g_free (title);
-}
-
-void
 glade_project_window_set_add_class (GladeProjectWindow *gpw, GladeWidgetClass *class)
 {
        gpw->add_class = class;
@@ -1199,8 +1246,12 @@ glade_project_window_get_project (void)
 }
 
 void
-glade_project_window_show_all (GladeProjectWindow *gpw)
+glade_project_window_show_all ()
 {
+       GladeProjectWindow *gpw;
+
+       gpw = glade_project_window_get ();
+
        gtk_widget_show_all (gpw->window);
        gpw_show_palette (gpw);
        gpw_show_editor (gpw);
diff -pur gnome2/glade3/src/glade-project-window.h glade3/src/glade-project-window.h
--- gnome2/glade3/src/glade-project-window.h    2003-05-15 09:32:23.000000000 +0200
+++ glade3/src/glade-project-window.h   2003-05-15 18:38:34.000000000 +0200
@@ -47,7 +47,7 @@ struct _GladeProjectWindow
                                               * placeholder
                                               */
 
-       GList *views;  /* A list of GladeProjectView item */
+       GList *views;    /* A list of GladeProjectView item */
        GList *projects; /* The list of Projects */
 
        guint project_selection_changed_signal;
@@ -57,16 +57,14 @@ struct _GladeProjectWindow
 GladeProjectWindow * glade_project_window_new (GList *catalogs);
 GladeProjectWindow * glade_project_window_get ();
 GladeProject *       glade_project_window_get_project ();
-void                 glade_project_window_show_all (GladeProjectWindow *gpw);
+
+void glade_project_window_show_all ();
 
 void glade_project_window_set_add_class       (GladeProjectWindow *gpw, GladeWidgetClass *class);
 
-void glade_project_window_set_project_cb (GladeProject *project);
+void glade_project_window_set_project         (GladeProject *project);
 
-void glade_project_window_set_project         (GladeProjectWindow *project_window,
-                                              GladeProject       *project);
-void glade_project_window_add_project         (GladeProjectWindow *gpw,
-                                              GladeProject *project);
+void glade_project_window_add_project         (GladeProject *project);
 
 void glade_project_window_refresh_undo_redo   (GladeProjectWindow *gpw);
 void glade_project_window_refresh_title       (GladeProjectWindow *gpw);
diff -pur gnome2/glade3/src/main.c glade3/src/main.c
--- gnome2/glade3/src/main.c    2003-05-15 09:32:34.000000000 +0200
+++ glade3/src/main.c   2003-05-15 18:39:32.000000000 +0200
@@ -109,7 +109,6 @@ glade_init ()
 int
 main (int argc, char *argv[])
 {
-       GladeProjectWindow *gpw;
        GList *files = NULL;
 #ifdef G_OS_UNIX
        poptContext popt_context;
@@ -141,17 +140,18 @@ main (int argc, char *argv[])
                return 0;
        }
 
-       gpw = glade_project_window_get ();
-       glade_project_window_show_all (gpw);
+       glade_project_window_show_all ();
 
        if (files) {
                for (; files != NULL; files = files->next) {
-                       glade_project_open (files->data);
+                       GladeProject *project;
+                       project = glade_project_open (files->data);
+                       glade_project_window_add_project (project);
                }
        } else {
                GladeProject *project;
                project = glade_project_new (TRUE);
-               glade_project_window_add_project (gpw, project);
+               glade_project_window_add_project (project);
        }
                        
        gtk_main ();

--=-Qbp+OqgZSYGTQ3ZrZ8S/--





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