[Glade-devel] [patch, glade3] rework widget creation



--=-Tln+9DChDSI2zkjKPrHb
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi!

The attached patch is a rework of how a GladeWidget is created. 
After the patch the code path to create a widget IMHO makes more sense
and matches better the one of widget deletion: the fundamental change is
that galde_command_create calls glade_widget_new_from_class to create a
widget and then adds it to the project instead of being called by it.

The patch isn't small and touches various files, so I'm going to explain
the changes in detail below.

* glade-command.c: glade_command_create now takes the widget class and
the placeholder it should replace as args; it creates the widget with
glade_widget_new_from_class. Toplevel widget are created with
glade_command_create_toplevel.

* glade-placeholder.c: the placeholder (if present) is passed to
glade_command_create and then is passed on till create_execute which
takes care of replacing it, this way we can delete
glade_placeholder_replace_widget and simply call glade_command_create.

* glade-widget.c: glade_widget_new now only take the widget class as
argument; glade_new_from_class takes also a project arg so special
casing for toplevels (which don't have parent->project) is not needed
anymore. I also deleted glade_widget_new_from_class_name since it was
not used anywhere and a bad idea to begin with (if ever needed just get
the class from its name and then call glade_widget_new_from_class).

* glade_project_window.c: use glade_command_create_toplevel

* glade_property.c: fix up the caller of glade_widget_new_from_class (I
was a little unsure about this little change since I am not familiar
with property.c, but I noticed that even if before widget->project was
not explicitly passed to the called function, there was a
g_return_if_fail (GLADE_IS_PROJECT (parent->project))... )


It seems to work here, but at the moment adding widget to vbox etc it's
broken so the patch isn't tested a lot, if you see any problems let me
know.

Sorry for being so verbose, I hope it all makes sense and it doesn't
conflict with your current work.


ciao
        paolo

--=-Tln+9DChDSI2zkjKPrHb
Content-Disposition: attachment; filename=create-rework.patch
Content-Type: text/x-patch; name=create-rework.patch; charset=UTF-8
Content-Transfer-Encoding: 7bit

diff -upr gnome2/glade3/ChangeLog glade3/ChangeLog
--- gnome2/glade3/ChangeLog     2003-05-22 17:24:15.000000000 +0200
+++ glade3/ChangeLog    2003-05-23 18:02:22.000000000 +0200
@@ -1,3 +1,14 @@
+2003-05-23  Paolo Borelli <pborelli katamail com>
+
+       * src/glade-command.[ch]: rework the create/delete command; now it
+       calls glade_widget_new_from_class instead of being called by it.
+       Introduce glade_command_create_toplevel.
+       * src/glade-widget.[ch]: cleanup widget creation.
+       * src/glade-project-window.c: update for the above.
+       * src/glade-property.c: ditto.
+       * src/glade-placeholder: remove glade_placeholder_replace_widget,
+       use glade_command_create_widget instead.
+
 2003-05-18  Joaquin Cuenca Abela  <e98cuenc yahoo com>
 
        * src/glade-placeholder.c: Made glade_holder_replace independent of
diff -upr gnome2/glade3/src/glade-command.c glade3/src/glade-command.c
--- gnome2/glade3/src/glade-command.c   2003-05-22 17:24:15.000000000 +0200
+++ glade3/src/glade-command.c  2003-05-23 18:05:24.000000000 +0200
@@ -616,20 +616,19 @@ static gboolean
 glade_command_create_execute (GladeCommandCreateDelete *me)
 {
        GladeWidget *widget = me->widget;
-       GladeWidget *parent = widget->parent;
-       
-       if (me->placeholder) {
-               parent = glade_placeholder_get_parent (me->placeholder);
-
-               if (me->widget->parent->class->placeholder_replace)
-                       me->widget->parent->class->placeholder_replace (GTK_WIDGET (me->placeholder), 
widget->widget, widget->parent->widget);
+       GladePlaceholder *placeholder = me->placeholder;
 
+       if (placeholder) {
+               if (widget->parent->class->placeholder_replace)
+                       widget->parent->class->placeholder_replace (GTK_WIDGET (placeholder),
+                                                                   widget->widget,
+                                                                   widget->parent->widget);
                me->placeholder = NULL;
        }
 
-       if (parent)
-               parent->children = g_list_prepend (parent->children, widget);
-       
+       if (widget->parent)
+               widget->parent->children = g_list_prepend (widget->parent->children, widget);
+
        glade_project_selection_clear (widget->project, FALSE);
        glade_project_add_widget (widget->project, widget);
 
@@ -645,16 +644,13 @@ static gboolean
 glade_command_delete_execute (GladeCommandCreateDelete *me)
 {
        GladeWidget *widget = me->widget;
-       GladeWidget *parent;
 
        g_return_val_if_fail (widget != NULL, TRUE);
 
        glade_project_selection_remove (widget, TRUE);
        glade_project_remove_widget (widget);
 
-       parent = widget->parent;
-
-       if (parent) {
+       if (widget->parent != NULL) {
                gtk_widget_ref (widget->widget);
                me->placeholder = glade_widget_replace_with_placeholder (widget);
        } else {
@@ -675,7 +671,7 @@ glade_command_create_delete_execute (Gla
 {
        GladeCommandCreateDelete* me = (GladeCommandCreateDelete*) cmd;
        gboolean retval;
-       
+
        if (me->create)
                retval = glade_command_create_execute (me);
        else
@@ -705,39 +701,69 @@ glade_command_create_delete_collapse (Gl
        g_return_if_reached ();
 }
 
+/**
+ * Placeholder arg may be NULL for toplevel widgets
+ */
 static void
-glade_command_create_delete_common (GladeWidget *widget, gboolean create)
+glade_command_create_delete_common (GladeWidget *widget,
+                                   GladePlaceholder *placeholder,
+                                   gboolean create)
 {
        GladeCommandCreateDelete *me;
        GladeCommand *cmd;
-       GladeProject *project;
-       
-       me = (GladeCommandCreateDelete*) g_object_new (GLADE_COMMAND_CREATE_DELETE_TYPE, NULL);
-       cmd = (GladeCommand*) me;
-       
-       project = glade_project_window_get_project ();
-       
-       me->widget = widget;
-       me->create = create;
-       me->placeholder = NULL;
+
+       me = (GladeCommandCreateDelete*) g_object_new (GLADE_COMMAND_CREATE_DELETE_TYPE, NULL);
+       cmd = (GladeCommand*) me;
+
+       me->widget = widget;
+       me->create = create;
+       me->placeholder = placeholder;
        cmd->description = g_strdup_printf (_("%s %s"), create ? "Create" : "Delete", widget->name);
-       
+
        g_debug(("Pushing: %s\n", cmd->description));
 
        glade_command_create_delete_execute (GLADE_COMMAND (me));
-       glade_command_push_undo(project, GLADE_COMMAND (me));
+       glade_command_push_undo(widget->project, GLADE_COMMAND (me));
 }
 
 void
 glade_command_delete (GladeWidget *widget)
 {
-       glade_command_create_delete_common (widget, FALSE);
+       g_return_if_fail (GLADE_IS_WIDGET (widget));
+
+       glade_command_create_delete_common (widget, NULL, FALSE);
 }
 
 void
-glade_command_create (GladeWidget *widget)
+glade_command_create (GladeWidgetClass *class, GladePlaceholder *placeholder)
 {
-       glade_command_create_delete_common (widget, TRUE);
+       GladeWidget *widget;
+       GladeWidget *parent;
+
+       g_return_if_fail (GLADE_IS_WIDGET_CLASS (class));
+       g_return_if_fail (placeholder != NULL);
+
+       parent = glade_placeholder_get_parent (placeholder);
+       g_return_if_fail (parent != NULL);
+
+       widget = glade_widget_new_from_class (class, parent->project, parent);
+       glade_widget_set_default_packing_options (widget);
+
+       glade_command_create_delete_common (widget, placeholder, TRUE);
+}
+
+void
+glade_command_create_toplevel (GladeWidgetClass *class, GladeProject *project)
+{
+       GladeWidget *widget;
+
+       g_return_if_fail (GLADE_IS_WIDGET_CLASS (class));
+       g_return_if_fail (GLADE_IS_PROJECT (project));
+
+       widget = glade_widget_new_from_class (class, project, NULL);
+       widget->project = project;
+
+       glade_command_create_delete_common (widget, NULL, TRUE);
 }
 
 /**
diff -upr gnome2/glade3/src/glade-command.h glade3/src/glade-command.h
--- gnome2/glade3/src/glade-command.h   2003-05-22 17:24:15.000000000 +0200
+++ glade3/src/glade-command.h  2003-05-23 16:38:38.000000000 +0200
@@ -5,17 +5,23 @@
 #include <glib-object.h>
 #include "glade-widget.h"
 
+G_BEGIN_DECLS
+
 void glade_command_undo (void);
 void glade_command_redo (void);
-const gchar* glade_command_get_description (GList *l);
 
-void glade_command_set_property (GObject *obj, const gchar* name, const GValue* value);
-void glade_command_set_name (GladeWidget *obj, const gchar* name);
+const gchar *glade_command_get_description (GList *l);
+
+void glade_command_set_property (GObject *obj, const gchar *name, const GValue *value);
+void glade_command_set_name (GladeWidget *obj, const gchar *name);
 
 void glade_command_delete (GladeWidget *widget);
-void glade_command_create (GladeWidget *widget);
+void glade_command_create (GladeWidgetClass *class, GladePlaceholder *placeholder);
+void glade_command_create_toplevel (GladeWidgetClass *class, GladeProject *project);
 
 void glade_command_cut   (GladeWidget *widget);
 void glade_command_paste (GladeWidget *widget, GladePlaceholder *placeholder);
 
+G_END_DECLS
+
 #endif /* GLADE_COMMAND_H */
diff -upr gnome2/glade3/src/glade-placeholder.c glade3/src/glade-placeholder.c
--- gnome2/glade3/src/glade-placeholder.c       2003-05-22 17:24:15.000000000 +0200
+++ glade3/src/glade-placeholder.c      2003-05-23 16:41:46.000000000 +0200
@@ -141,26 +141,6 @@ glade_placeholder_replace_notebook (GtkW
        gtk_notebook_set_current_page (notebook, page_num);
 }
 
-static void
-glade_placeholder_replace_widget (GladePlaceholder *placeholder, GladeWidgetClass *class, GladeProject 
*project)
-{
-       GladeWidget *parent;
-       GladeWidget *widget;
-
-       parent = glade_placeholder_get_parent (placeholder);
-       g_return_if_fail (parent != NULL);
-
-       widget = glade_widget_new_from_class (class, parent);
-       if (widget == NULL)
-               return;
-
-       glade_placeholder_replace (placeholder, parent, widget);
-
-       glade_widget_set_default_packing_options (widget);
-       
-       glade_project_selection_set (widget, TRUE);
-}
-
 void
 glade_placeholder_add_methods_to_class (GladeWidgetClass *class)
 {
@@ -269,7 +249,7 @@ glade_placeholder_on_button_press_event 
                         * A widget type is selected in the palette.
                         * Add a new widget of that type.
                         */
-                       glade_placeholder_replace_widget (placeholder, gpw->add_class, project);
+                       glade_command_create (gpw->add_class, placeholder);
                        glade_project_window_set_add_class (gpw, NULL);
                        gpw->active_placeholder = NULL;
                } else {
diff -upr gnome2/glade3/src/glade-project-window.c glade3/src/glade-project-window.c
--- gnome2/glade3/src/glade-project-window.c    2003-05-22 17:24:15.000000000 +0200
+++ glade3/src/glade-project-window.c   2003-05-23 17:29:10.000000000 +0200
@@ -441,14 +441,11 @@ static void
 gpw_palette_button_clicked (GladePalette *palette, GladeWidgetClass *class)
 {
        GladeProjectWindow *gpw;
-       GladeProject *project;
 
        gpw = glade_project_window_get ();
 
        if (GLADE_WIDGET_CLASS_TOPLEVEL (class)) {
-               project = gpw->project;
-               g_return_if_fail (project != NULL);
-               glade_widget_new_toplevel (project, class);
+               glade_command_create_toplevel (class, gpw->project);
                glade_project_window_set_add_class (gpw, NULL);
        } else {
                glade_project_window_set_add_class (gpw, class);
Binary files gnome2/glade3/src/glade-project-window.o and glade3/src/glade-project-window.o differ
diff -upr gnome2/glade3/src/glade-property.c glade3/src/glade-property.c
--- gnome2/glade3/src/glade-property.c  2003-05-22 17:24:15.000000000 +0200
+++ glade3/src/glade-property.c 2003-05-23 17:27:35.000000000 +0200
@@ -136,6 +136,7 @@ glade_property_new_from_class (GladeProp
 
        if (class->type == GLADE_PROPERTY_TYPE_OBJECT) {
                property->child = glade_widget_new_from_class (class->child,
+                                                              widget->project,
                                                               widget);
                return property;
        }
diff -upr gnome2/glade3/src/glade-widget.c glade3/src/glade-widget.c
--- gnome2/glade3/src/glade-widget.c    2003-05-22 17:24:16.000000000 +0200
+++ glade3/src/glade-widget.c   2003-05-23 17:20:55.000000000 +0200
@@ -60,24 +60,21 @@ glade_widget_new_name (GladeProject *pro
 
 /**
  * glade_widget_new:
- * @project: The GladeProject this widget belongs to
  * @class: The GladeWidgeClass of the GladeWidget
- * @gtk_widget: The "view" of the GladeWidget
- * @name: The unique name, this is visible name to the user
  * 
  * Allocates a new GladeWidget structure and fills in the required memebers.
  * 
  * Return Value: 
  **/
 static GladeWidget *
-glade_widget_new (GladeWidgetClass *class, GladeProject *project)
+glade_widget_new (GladeWidgetClass *class)
 {
        GladeWidget *widget;
 
        widget = g_new0 (GladeWidget, 1);
        widget->name     = NULL;
        widget->widget   = NULL;
-       widget->project  = project;
+       widget->project  = NULL;
        widget->class    = class;
        widget->properties = glade_property_list_new_from_widget_class (class, widget);
        widget->parent   = NULL;
@@ -765,18 +762,18 @@ glade_widget_connect_signals (GladeWidge
 }
 
 static GladeWidget *
-glade_widget_new_full (GladeProject *project,
-                      GladeWidgetClass *class,
+glade_widget_new_full (GladeWidgetClass *class,
+                      GladeProject *project,
                       GladeWidget *parent)
 {
        GladeWidget *widget;
 
-       g_return_val_if_fail (GLADE_IS_PROJECT (project), NULL);
        g_return_val_if_fail (GLADE_IS_WIDGET_CLASS (class), NULL);
 
-       widget = glade_widget_new (class, project);
+       widget = glade_widget_new (class);
        widget->name    = glade_widget_new_name (project, class);
        widget->parent  = parent;
+       widget->project = project;
 
        glade_packing_add_properties (widget);
        glade_widget_create_gtk_widget (widget);
@@ -920,9 +917,9 @@ glade_widget_query_properties (GladeWidg
 }
 
 /**
- * glade_widget_new_from_class_full:
- * @project: 
+ * glade_widget_new_from_class:
  * @class:
+ * @project:
  * @parent: the parent of the new widget, should be NULL for toplevel widgets
  * 
  * Creates a new GladeWidget from a given class. Takes care of registering
@@ -931,10 +928,10 @@ glade_widget_query_properties (GladeWidg
  * 
  * Return Value: A newly creatred GladeWidget, NULL on user cancel or error    
  **/
-static GladeWidget *
-glade_widget_new_from_class_full (GladeWidgetClass *class,
-                                 GladeProject *project,
-                                 GladeWidget *parent)
+GladeWidget *
+glade_widget_new_from_class (GladeWidgetClass *class,
+                            GladeProject *project,
+                            GladeWidget *parent)
 {
        GladePropertyQueryResult *result = NULL;
        GladeWidget *widget;
@@ -948,52 +945,20 @@ glade_widget_new_from_class_full (GladeW
                        return NULL;
        }
 
-       widget = glade_widget_new_full (project, class, parent);
+       widget = glade_widget_new_full (class, project, parent);
 
        /* If we are a container, add the placeholders */
        if (GLADE_WIDGET_CLASS_ADD_PLACEHOLDER (class))
                glade_placeholder_add_with_result (class, widget, result);
-       
+
        if (result) 
                glade_property_query_result_destroy (result);
 
-       /* glade_command_create does a gtk_widget_show, so we should do it
-        * after we've set the options, as sometimes these can't be set
-        * if the widget is visible (for instance, on a GtkWindow) */
        glade_widget_set_default_options (widget);
-       glade_command_create (widget);
-       
-       return widget;
-}
-
-GladeWidget *
-glade_widget_new_from_class (GladeWidgetClass *class, GladeWidget *parent)
-{
-       GladeProject *project;
-
-       g_return_val_if_fail (GLADE_IS_WIDGET_CLASS (class), NULL);
-       g_return_val_if_fail (GLADE_IS_WIDGET (parent), NULL);
-       g_return_val_if_fail (GLADE_IS_PROJECT (parent->project), NULL);
-
-       project = parent->project;
-
-       return glade_widget_new_from_class_full (class, project, parent);
-}
-       
-GladeWidget *
-glade_widget_new_toplevel (GladeProject *project, GladeWidgetClass *class)
-{
-       GladeWidget *widget;
-       
-       g_return_val_if_fail (GLADE_IS_PROJECT (project), NULL);
-       g_return_val_if_fail (GLADE_IS_WIDGET_CLASS (class), NULL);
 
-       widget = glade_widget_new_from_class_full (class, project, NULL);
-       
        return widget;
 }
 
-
 const gchar *
 glade_widget_get_name (GladeWidget *widget)
 {
@@ -1006,8 +971,6 @@ glade_widget_get_class (GladeWidget *wid
        return widget->class;
 }
 
-
-
 /**
  * glade_widget_get_property_from_list:
  * @list: The list of GladeProperty
@@ -1050,7 +1013,6 @@ glade_widget_get_property_from_list (GLi
        return property;
 }
 
-
 /**
  * glade_widget_get_property_from_class:
  * @widget: 
@@ -1200,7 +1162,7 @@ glade_widget_clone (GladeWidget *widget)
        /*
         * This should be enough to clone.
         */
-       clone = glade_widget_new (widget->class, NULL);
+       clone = glade_widget_new (widget->class);
        clone->name = glade_widget_new_name (widget->project, widget->class);
        clone->project = widget->project;
        glade_widget_create_gtk_widget (clone);
@@ -1226,36 +1188,6 @@ glade_widget_replace_with_placeholder (G
        return placeholder;
 }
 
-/**
- * glade_widget_new_from_class_name:
- * @class_name: 
- * @parent: 
- * 
- * Given a class name, it creates a GladeWidget
- * 
- * Return Value: the newly created GladeWidget, NULL on error
- **/
-GladeWidget *
-glade_widget_new_from_class_name (const gchar *name,
-                                 GladeWidget *parent)
-{
-       GladeWidgetClass *class;
-       GladeWidget *widget;
-
-       g_return_val_if_fail (name != NULL, NULL);
-       g_return_val_if_fail (GLADE_IS_WIDGET (parent), NULL);
-
-       class = glade_widget_class_get_by_name (name);
-       if (class == NULL)
-               return NULL;
-
-       widget = glade_widget_new_from_class (class, parent);
-
-       return widget;
-}
-       
-
-
 GladeXmlNode *
 glade_widget_write (GladeXmlContext *context, GladeWidget *widget)
 {
@@ -1388,7 +1320,7 @@ glade_widget_new_from_node_real (GladeXm
        if (!class)
                return NULL;
        
-       widget = glade_widget_new_full (project, class, parent);
+       widget = glade_widget_new_full (class, project, parent);
 
        child = glade_xml_node_get_children (node);
        for (; child != NULL; child = glade_xml_node_next (child)) {
diff -upr gnome2/glade3/src/glade-widget.h glade3/src/glade-widget.h
--- gnome2/glade3/src/glade-widget.h    2003-05-22 17:24:16.000000000 +0200
+++ glade3/src/glade-widget.h   2003-05-23 17:21:43.000000000 +0200
@@ -12,7 +12,7 @@ G_BEGIN_DECLS
  */
 
 struct _GladeWidget {
-       
+
        GladeWidgetClass *class; /* The class of the widget.
                                  * [see glade-widget-class.h ]
                                  */
@@ -67,12 +67,10 @@ gboolean glade_widget_create_gtk_widget 
 void glade_widget_set_contents (GladeWidget *widget);
 void glade_widget_connect_signals (GladeWidget *widget);
 
-GladeWidget * glade_widget_new_toplevel (GladeProject *project,
-                                        GladeWidgetClass *class);
 GladeWidget * glade_widget_new_from_class (GladeWidgetClass *class,
-                                          GladeWidget *widget);
-GladeWidget * glade_widget_new_from_class_name (const gchar *class_name,
-                                               GladeWidget *parent);
+                                          GladeProject *project,
+                                          GladeWidget *parent);
+
 void glade_widget_set_default_packing_options (GladeWidget *widget);
 
 

--=-Tln+9DChDSI2zkjKPrHb--





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