[Glade-devel] GladePalette patch



--Boundary_(ID_wqTpbZlA2ZrNUxGezyDL7w)
Content-type: text/plain
Content-transfer-encoding: 7BIT

Hey ho!

So, here's another patch. It basically changes the GladePalette's
catalog selection implementation. Instead of using individual radio
buttons, I used a GtkComboBox. The reason behind this decision is that
since glade3 has extensible catalog support, we can have more than 4
catalogs in the future. And having individual radio button for each
would ... well would just not be right.

Also, I took the oppurtunity to cleanup the code a bit. Here I would
suggest a little programming tip . Whenever using
g_object_[get/set]_data() functions, for the paramenter name's string,
don't use a string, but use a all caps constant (or a #define, smae
thing). Because if you mis-type it or change it, the compiler will catch
it for you. Plus, if someone decides to change the string he/she must
find all the places in the code where to change and that can be tedious.

Archit

P.S. just incase you are wondering why they curious hello (hey ho). in
my emails, I started thinking the same too, since I usually just wrote
it without thinking about it. Anyway I came with a plausible reason, its
the christmas spirit in me :-)

--Boundary_(ID_wqTpbZlA2ZrNUxGezyDL7w)
Content-type: text/x-patch; name=glade3-palette-cleanup-and-change.diff;
 charset=UTF-8
Content-transfer-encoding: 7BIT
Content-disposition: attachment; filename=glade3-palette-cleanup-and-change.diff

? depcomp
? install-sh
? stamp-h1
Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/glade3/ChangeLog,v
retrieving revision 1.325
diff -u -r1.325 ChangeLog
--- ChangeLog   16 Dec 2004 21:24:02 -0000      1.325
+++ ChangeLog   19 Dec 2004 01:08:55 -0000
@@ -1,3 +1,11 @@
+2004-12-18  Archit Baweja  <bighead users sourceforge net>
+
+       * src/glade-palette.c (glade_palette_widget_table_create): cleanup
+       and use glade_palette_create_widget_class_button.
+       (glade_palette_create_widget_class_button): new function
+       (glade_palette_init): use a GtkComboBox for catalog names instead of
+       individual radio buttons. All callbacks and callers changed.
+
 2004-12-16  Archit Baweja  <bighead users sourceforge net>
 
        * widgets/gtk-obsolete.xml: added entry for GtkFileSelection
Index: src/glade-command.c
===================================================================
RCS file: /cvs/gnome/glade3/src/glade-command.c,v
retrieving revision 1.34
diff -u -r1.34 glade-command.c
--- src/glade-command.c 5 Nov 2004 19:44:21 -0000       1.34
+++ src/glade-command.c 19 Dec 2004 01:08:55 -0000
@@ -306,7 +306,7 @@
        GladeCommandSetProperty* me = (GladeCommandSetProperty*) cmd;
        GValue new_value = { 0, };
 
-       g_return_val_if_fail (me != NULL, TRUE);
+       g_return_val_if_fail (me != NULL, FALSE);
 
        g_value_init (&new_value, G_VALUE_TYPE (me->arg_value));
        g_value_copy (me->arg_value, &new_value);
Index: src/glade-palette.c
===================================================================
RCS file: /cvs/gnome/glade3/src/glade-palette.c,v
retrieving revision 1.27
diff -u -r1.27 glade-palette.c
--- src/glade-palette.c 18 Nov 2004 06:14:01 -0000      1.27
+++ src/glade-palette.c 19 Dec 2004 01:08:56 -0000
@@ -33,52 +33,18 @@
 #include "glade-widget-class.h"
 
 
-static void glade_palette_class_init (GladePaletteClass *class);
-static void glade_palette_init (GladePalette *glade_palette);
-
 enum
 {
        TOGGLED,
        LAST_SIGNAL
 };
 
+const char *GLADE_PALETTE_BUTTON_CLASS_DATA = "user";
+
 static guint glade_palette_signals[LAST_SIGNAL] = {0};
 
 static GtkWindowClass *parent_class = NULL;
 
-/**
- * glade_palette_get_type:
- *
- * Creates the typecode for the #GladePalette object type.
- *
- * Returns: the typecode for the #GladePalette object type
- */
-GType
-glade_palette_get_type (void)
-{
-       static GType type = 0;
-
-       if (!type)
-       {
-               static const GTypeInfo info =
-               {
-                       sizeof (GladePaletteClass),
-                       (GBaseInitFunc) NULL,
-                       (GBaseFinalizeFunc) NULL,
-                       (GClassInitFunc) glade_palette_class_init,
-                       (GClassFinalizeFunc) NULL,
-                       NULL,
-                       sizeof (GladePalette),
-                       0,
-                       (GInstanceInitFunc) glade_palette_init
-               };
-
-               type = g_type_register_static (GTK_TYPE_VBOX, "GladePalette", &info, 0);
-       }
-
-       return type;
-}
-
 static void
 glade_palette_class_init (GladePaletteClass *class)
 {
@@ -102,6 +68,7 @@
 static void
 glade_palette_on_button_toggled (GtkWidget *button, GladePalette *palette)
 {
+
        GladeWidgetClass *class;
 
        g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
@@ -120,7 +87,8 @@
        }
        else
        {
-               class = g_object_get_data (G_OBJECT (button), "user");
+               class = g_object_get_data (G_OBJECT (button),
+                                          GLADE_PALETTE_BUTTON_CLASS_DATA);
                g_return_if_fail (GLADE_IS_WIDGET_CLASS (class));
 
                palette->current = class;
@@ -131,6 +99,17 @@
        g_signal_emit (G_OBJECT (palette), glade_palette_signals[TOGGLED], 0);
 }
 
+static void
+glade_palette_on_catalog_selector_changed (GtkWidget *combo_box,
+                                          GladePalette *palette)
+{
+       /* find out which catalog was selected. */
+       gint page = gtk_combo_box_get_active (GTK_COMBO_BOX (palette->catalog_selector));
+
+       /* Select that catalog in the notebook. */
+       gtk_notebook_set_current_page (GTK_NOTEBOOK (palette->notebook), page);
+}
+
 static GtkWidget *
 glade_palette_selector_new (GladePalette *palette)
 {
@@ -166,109 +145,85 @@
        return hbox;    
 }
 
-static gboolean
-glade_palette_attach_icon (GladePalette *palette,
-                          GtkWidget *table,
-                          GList *list,
-                          gint i,
-                          gint visual_pos,
-                          gint cols)
+
+/**
+ * glade_palette_create_widget_class_button:
+ * @palette:           the palette to add to.
+ * @widget_class:       the widget class who's button we're adding.
+ * 
+ * Creates a GtkHBox with an icon for the corresponding @widget_class and
+ * the name.
+ * 
+ * Return Value: the GtkHBox
+ **/
+static GtkWidget *
+glade_palette_create_widget_class_button (GladePalette *palette, 
+                                         GladeWidgetClass *widget_class)
 {
-       GladeWidgetClass *class;
-       GtkWidget *button;
-       gint x, y;
+       GtkWidget *label;
+       GtkWidget *radio;
+       GtkWidget *hbox;
 
-       class = g_list_nth_data (list, i);
-       g_return_val_if_fail (class != NULL, FALSE);
+       label = gtk_label_new (widget_class->palette_name);
+       radio = gtk_radio_button_new (palette->widgets_button_group);
 
-       if (!class->in_palette)
-               return FALSE;
+       g_object_set_data (G_OBJECT (radio), GLADE_PALETTE_BUTTON_CLASS_DATA,
+                          widget_class);
+       palette->widgets_button_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio));
+       gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (radio), FALSE);
+       gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+
+       hbox = gtk_hbox_new (FALSE, 2);
+       gtk_box_pack_start (GTK_BOX (hbox), widget_class->icon,
+                           FALSE, FALSE, 0);
+       gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 1);
 
-       button = gtk_radio_button_new (palette->widgets_button_group);
-       gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (button), FALSE);
-       palette->widgets_button_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (button));
-       gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
-       gtk_container_add (GTK_CONTAINER (button), class->icon);
-       glade_util_widget_set_tooltip (button, class->generic_name);
-
-       /* store the widget class in the button */
-       g_object_set_data (G_OBJECT (button), "user", class);
-       g_signal_connect (G_OBJECT (button), "toggled",
-                         G_CALLBACK (glade_palette_on_button_toggled), palette);
+       gtk_button_set_relief (GTK_BUTTON (radio), GTK_RELIEF_NONE);
+       gtk_container_add (GTK_CONTAINER (radio), hbox);
 
-       x = (visual_pos % cols);
-       y = (gint) (visual_pos / cols);
-       gtk_table_attach (GTK_TABLE (table),
-                         button,
-                         x, x + 1,
-                         y, y + 1,
-                         0, 0,
-                         0, 0);
-       return TRUE;
+       g_signal_connect (G_OBJECT (radio), "toggled",
+                         G_CALLBACK (glade_palette_on_button_toggled), palette);
+       return radio;
 }
 
 static GtkWidget *
 glade_palette_widget_table_create (GladePalette *palette, GladeCatalog *catalog)
 {
        GList *list;
-       GtkVBox *vbox;
-       GtkHBox *hbox;
-       GtkLabel *label;
-       GtkRadioButton *radio;
-       GtkScrolledWindow *scrolled_window;
-       int nb_rows = 0;
-       int row = 0;
+       GtkWidget *sw;
+       GtkWidget *vbox;
 
-       list = glade_catalog_get_widget_classes (catalog);
-       while (list)
-       {
-               GladeWidgetClass *widget_class = (GladeWidgetClass*) list->data;
-               if (widget_class->in_palette)
-                       nb_rows++;
-
-               list = list->next;
-       }
-
-       vbox = GTK_VBOX (gtk_vbox_new (FALSE, 0));
+       vbox = gtk_vbox_new (FALSE, 0);
 
        list = glade_catalog_get_widget_classes (catalog);
-       while (list)
-       {
-               GladeWidgetClass *widget_class = (GladeWidgetClass*) list->data;
-               if (widget_class->in_palette)
-               {
-                       label = GTK_LABEL (gtk_label_new (widget_class->palette_name));
-                       radio = GTK_RADIO_BUTTON (gtk_radio_button_new (palette->widgets_button_group));
 
-                       g_object_set_data (G_OBJECT (radio), "user", list->data);
-                       palette->widgets_button_group = gtk_radio_button_get_group (radio);
-                       gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (radio), FALSE);
-                       gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
-
-                       hbox = GTK_HBOX (gtk_hbox_new (FALSE, 2));
-                       gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET (widget_class->icon), FALSE, FALSE, 0);
-                       gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET (label), TRUE, TRUE, 1);
-
-                       gtk_button_set_relief (GTK_BUTTON (radio), GTK_RELIEF_NONE);
-                       gtk_container_add (GTK_CONTAINER (radio), GTK_WIDGET (hbox));
-
-                       g_signal_connect (G_OBJECT (radio), "toggled",
-                                         G_CALLBACK (glade_palette_on_button_toggled), palette);
+       /* Go through all the widget classes in this catalog. */
+       for (; list; list = list->next)
+       {
+               GladeWidgetClass *gwidget_class = GLADE_WIDGET_CLASS (list->data);
 
-                       gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (radio), FALSE, FALSE, 0);
-                       row++;
+               /*
+                * If the widget class wants to be in the palette (I don't
+                * know why a widget class wouldn't want to be, but whatever..
+                */
+               if (gwidget_class->in_palette)
+               {
+                       GtkWidget *button = glade_palette_create_widget_class_button (palette, gwidget_class);
+                       gtk_box_pack_start (GTK_BOX (vbox), button,
+                                           FALSE, FALSE, 0);
                }
-
-               list = list->next;
        }
 
-       scrolled_window = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
-       gtk_scrolled_window_set_policy (scrolled_window, GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
-       gtk_scrolled_window_add_with_viewport (scrolled_window, GTK_WIDGET (vbox));
-       gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_NONE);
-       gtk_widget_set_size_request (GTK_WIDGET (scrolled_window), -1, 400);
+       /* Add it in a scrolled window. */
+       sw = gtk_scrolled_window_new (NULL, NULL);
+       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
+                                       GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
+       gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (sw), vbox);
+       gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), 
+                                            GTK_SHADOW_NONE);
+       gtk_widget_set_size_request (GTK_WIDGET (sw), -1, 400);
 
-       return GTK_WIDGET (scrolled_window);
+       return sw;
 }
 
 static void
@@ -276,10 +231,11 @@
 {
        GtkWidget *selector_hbox;
        GtkWidget *widget;
+       GtkWidget *combo_box;
 
        palette->current = NULL;
        palette->widgets_button_group = NULL;
-       palette->sections_button_group = NULL;
+       palette->catalog_selector = NULL;
 
        /* Selector */
        selector_hbox = glade_palette_selector_new (palette);
@@ -289,10 +245,16 @@
        widget = gtk_hseparator_new ();
        gtk_box_pack_start (GTK_BOX (palette), widget, FALSE, TRUE, 3);
 
-       /* Groups */
-       palette->groups_vbox = gtk_vbox_new (FALSE, 0);
-       gtk_box_pack_start (GTK_BOX (palette), palette->groups_vbox, FALSE, TRUE, 0);
-       
+       /* The catalog selector menu. */
+       combo_box = gtk_combo_box_new_text ();
+       gtk_box_pack_start (GTK_BOX (palette), combo_box, FALSE, FALSE, 0);
+       g_signal_connect (G_OBJECT (combo_box), "changed", 
+                         G_CALLBACK (glade_palette_on_catalog_selector_changed),
+                         palette);
+
+       gtk_widget_show (combo_box);
+       palette->catalog_selector = combo_box;
+
        /* Separator */
        widget = gtk_hseparator_new ();
        gtk_box_pack_start (GTK_BOX (palette), widget, FALSE, TRUE, 3);
@@ -306,30 +268,17 @@
        palette->nb_sections = 0;
 }
 
-static gint
-glade_palette_on_catalog_button_toggled (GtkWidget *button,
-                                        GladePalette *palette)
-{
-       gint page;
-
-       page = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (button), "page"));
-       gtk_notebook_set_current_page (GTK_NOTEBOOK (palette->notebook), page);
-
-       return TRUE;
-}
-
 /**
  * glade_palette_append_catalog:
  * @palette: a #GladePalette
  * @catalog: a #GladeCatalog
  * 
- * TODO: write me
+ * Append @catalog to the @palette.
  */
 void
 glade_palette_append_catalog (GladePalette *palette, GladeCatalog *catalog)
 {
        gint page;
-       GtkWidget *button;
        GtkWidget *widget;
 
        g_return_if_fail (GLADE_IS_PALETTE (palette));
@@ -337,15 +286,9 @@
 
        page = palette->nb_sections++;
 
-       /* Add the button */ 
-       button = gtk_radio_button_new_with_label (palette->sections_button_group, catalog->title);
-       palette->sections_button_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (button));
-       gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (button), FALSE);
-       g_object_set_data (G_OBJECT (button), "page", GINT_TO_POINTER (page));
-       g_signal_connect (G_OBJECT (button), "toggled",
-                         G_CALLBACK (glade_palette_on_catalog_button_toggled),
-                         palette);
-       gtk_box_pack_start (GTK_BOX (palette->groups_vbox), button, FALSE, TRUE, 0);
+       /* Add the catalog's title to the GtkComboBox */
+       gtk_combo_box_append_text (GTK_COMBO_BOX (palette->catalog_selector),
+                                  catalog->title);
 
        /* Add the section */
        widget = glade_palette_widget_table_create (palette, catalog);
@@ -355,12 +298,45 @@
 }
 
 /**
+ * glade_palette_get_type:
+ *
+ * Creates the typecode for the #GladePalette object type.
+ *
+ * Returns: the typecode for the #GladePalette object type
+ */
+GType
+glade_palette_get_type (void)
+{
+       static GType type = 0;
+
+       if (!type)
+       {
+               static const GTypeInfo info =
+               {
+                       sizeof (GladePaletteClass),
+                       (GBaseInitFunc) NULL,
+                       (GBaseFinalizeFunc) NULL,
+                       (GClassInitFunc) glade_palette_class_init,
+                       (GClassFinalizeFunc) NULL,
+                       NULL,
+                       sizeof (GladePalette),
+                       0,
+                       (GInstanceInitFunc) glade_palette_init
+               };
+
+               type = g_type_register_static (GTK_TYPE_VBOX, "GladePalette", &info, 0);
+       }
+
+       return type;
+}
+
+/**
  * glade_palette_new:
  * @catalogs:
  * 
- * TODO: write me
+ * Create a new palette
  *
- * Returns:
+ * Returns: the new GladePalette
  */
 GladePalette *
 glade_palette_new (GList *catalogs)
@@ -374,6 +350,7 @@
        for (list = catalogs; list; list = list->next)
                glade_palette_append_catalog (palette, GLADE_CATALOG (list->data));
 
+       gtk_combo_box_set_active (GTK_COMBO_BOX (palette->catalog_selector), 0);
        return palette;
 }
 
Index: src/glade-palette.h
===================================================================
RCS file: /cvs/gnome/glade3/src/glade-palette.h,v
retrieving revision 1.13
diff -u -r1.13 glade-palette.h
--- src/glade-palette.h 28 Nov 2003 16:45:14 -0000      1.13
+++ src/glade-palette.h 19 Dec 2004 01:08:56 -0000
@@ -31,7 +31,7 @@
                                    * selector button is pressed.
                                    */
 
-       GtkWidget *selector; /* The selevtor is a button that is
+       GtkWidget *selector; /* The selector is a button that is
                              * clicked to cancel the add widget action.
                              * This sets our cursor back to the "select
                              * widget" mode. This button is part of the
@@ -44,14 +44,15 @@
                            * class is selected
                            */
 
-       GtkWidget *groups_vbox; /* The vbox that contains the titles of the sections */
 
        GtkWidget *notebook; /* Where we store the different catalogs */
 
        gint nb_sections; /* The number of sections in this palette */
-       GSList *sections_button_group; /* Each section of the palette has
-                                       * a button. This is the button_group_list
-                                       */
+
+       GtkWidget *catalog_selector; /* The combo_box with the names of the
+                                     * various catalogs so the user can select
+                                     * it.
+                                     */
 
        GSList *widgets_button_group; /* Each widget in a section has a button
                                       * in the palette. This is the button

--Boundary_(ID_wqTpbZlA2ZrNUxGezyDL7w)--




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