Rationalizing widget default styles



The following patch rationalizes various problems with default
styles and widgets.

 * It removes:

    gtk_widget_set_default_style()
    gtk_widget_push_style()
    gtk_widget_pop_style()

   I consider these functions harmful, because if _anything_ is
   set in the RC file (a theme, a font, etc.) for that widget,
   then they will be ignored. We waste a fair bit of effort
   and object data tracking the default style when the widget
   was created -- something that is not useful in 99% of cases.

 * It makes the canonical way to remove a style set with
   gtk_widget_set_style (widget, style) -  
   gtk_widget_set_style (widget, NULL). I believe this is the
   way of unsetting the style for a widget that would make
   the most sense to someone familiar with the GTK+ API.

 * It makes the functions:

    gtk_widget_set_rc_style (widget)
    gtk_widget_restore_default_style (widget)

   Deprecated aliases for gtk_widget_set_style (widget, NULL);

   Previously:
    
    - gtk_widget_set_rc_style() did was basically equivalent
      to (in my proposal):

          gtk_widget_set_style (widget, NULL);
          gtk_widget_ensure_style (widget);

      I don't think it was meant to be public at all, but somehow
      was.

    - gtk_widget_restore_default_style() was a completely broken
      function that did approximately what I'm proposing
      for gtk_widget_set_style (widget, NULL); it undid
      the effect of gtk_widget_set_style(), but didn't properly
      handle getting the style from the RC file.

Possible alternates for the above:

 * Make push_style(), pop_style() deprecated noops rather than 
   removing them. But since people might have been counting
   on them to work (and they would have, in a very controlled
   environment) I think just removing them is better.

 * Not make the gtk_widget_set_style (widget, NULL) change
   and instead fix gtk_widget_restore_default_style() to
   work properly.

Note that the patch is mostly - and very little +; it is a considerable
simplification to this code. 

Regards,
                                        Owen

Index: gtkwidget.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkwidget.c,v
retrieving revision 1.228
diff -u -r1.228 gtkwidget.c
--- gtkwidget.c	2001/06/24 15:34:47	1.228
+++ gtkwidget.c	2001/06/26 23:42:53
@@ -190,12 +190,12 @@
 static gboolean		gtk_widget_real_focus			(GtkWidget        *widget,
 								 GtkDirectionType  direction);
 static GdkColormap*	gtk_widget_peek_colormap		(void);
-static GtkStyle*	gtk_widget_peek_style			(void);
 static PangoContext*	gtk_widget_peek_pango_context		(GtkWidget	  *widget);
 static void		gtk_widget_reparent_container_child	(GtkWidget	  *widget,
 								 gpointer          client_data);
 static void		gtk_widget_propagate_state		(GtkWidget	  *widget,
 								 GtkStateData 	  *data);
+static void             gtk_widget_reset_rc_style               (GtkWidget        *widget);
 static void		gtk_widget_set_style_internal		(GtkWidget	  *widget,
 								 GtkStyle	  *style,
 								 gboolean	   initial_emission);
@@ -221,7 +221,6 @@
 static GdkColormap     *default_colormap = NULL;
 static GtkStyle        *gtk_default_style = NULL;
 static GSList          *colormap_stack = NULL;
-static GSList          *style_stack = NULL;
 static guint            composite_child_stack = 0;
 static GtkTextDirection gtk_default_direction = GTK_TEXT_DIR_LTR;
 static GParamSpecPool  *style_property_spec_pool = NULL;
@@ -230,7 +229,6 @@
 static GQuark		quark_event_mask = 0;
 static GQuark		quark_extension_event_mode = 0;
 static GQuark		quark_parent_window = 0;
-static GQuark		quark_saved_default_style = 0;
 static GQuark		quark_shape_info = 0;
 static GQuark		quark_colormap = 0;
 static GQuark		quark_pango_context = 0;
@@ -301,7 +299,6 @@
   quark_event_mask = g_quark_from_static_string ("gtk-event-mask");
   quark_extension_event_mode = g_quark_from_static_string ("gtk-extension-event-mode");
   quark_parent_window = g_quark_from_static_string ("gtk-parent-window");
-  quark_saved_default_style = g_quark_from_static_string ("gtk-saved-default-style");
   quark_shape_info = g_quark_from_static_string ("gtk-shape-info");
   quark_colormap = g_quark_from_static_string ("gtk-colormap");
   quark_pango_context = g_quark_from_static_string ("gtk-pango-context");
@@ -1265,7 +1262,7 @@
 			(composite_child_stack ? GTK_COMPOSITE_CHILD : 0) |
 			GTK_DOUBLE_BUFFERED);
 
-  widget->style = gtk_widget_peek_style ();
+  widget->style = gtk_widget_get_default_style ();
   gtk_style_ref (widget->style);
   
   colormap = gtk_widget_peek_colormap ();
@@ -3192,7 +3189,7 @@
   widget->name = g_strdup (name);
 
   if (GTK_WIDGET_RC_STYLE (widget))
-    gtk_widget_set_rc_style (widget);
+    gtk_widget_reset_rc_style (widget);
 
   g_object_notify (G_OBJECT (widget), "name");
 }
@@ -3439,7 +3436,8 @@
 /**
  * gtk_widget_set_style:
  * @widget: a #GtkWidget
- * @style: a #GtkStyle
+ * @style: a #GtkStyle, or %NULL to remove the effect of a previous
+ *         gtk_widget_set_style and go back to the default style
  *
  * Sets the #GtkStyle for a widget (widget->style). You probably don't
  * want to use this function; it interacts badly with themes, because
@@ -3451,26 +3449,25 @@
 gtk_widget_set_style (GtkWidget *widget,
 		      GtkStyle	*style)
 {
-  GtkStyle *default_style;
-  gboolean initial_emission;
-
   g_return_if_fail (widget != NULL);
   g_return_if_fail (GTK_IS_WIDGET (widget));
-  g_return_if_fail (style != NULL);
-
-  initial_emission = !GTK_WIDGET_RC_STYLE (widget) && !GTK_WIDGET_USER_STYLE (widget);
 
-  GTK_WIDGET_UNSET_FLAGS (widget, GTK_RC_STYLE);
-  GTK_PRIVATE_SET_FLAG (widget, GTK_USER_STYLE);
+  if (style)
+    {
+      gboolean initial_emission;
 
-  default_style = gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_saved_default_style);
-  if (!default_style)
+      initial_emission = !GTK_WIDGET_RC_STYLE (widget) && !GTK_WIDGET_USER_STYLE (widget);
+      
+      GTK_WIDGET_UNSET_FLAGS (widget, GTK_RC_STYLE);
+      GTK_PRIVATE_SET_FLAG (widget, GTK_USER_STYLE);
+      
+      gtk_widget_set_style_internal (widget, style, initial_emission);
+    }
+  else
     {
-      gtk_style_ref (widget->style);
-      gtk_object_set_data_by_id (GTK_OBJECT (widget), quark_saved_default_style, widget->style);
+      if (GTK_WIDGET_USER_STYLE (widget))
+	gtk_widget_reset_rc_style (widget);
     }
-
-  gtk_widget_set_style_internal (widget, style, initial_emission);
 }
 
 /**
@@ -3491,19 +3488,15 @@
 
   if (!GTK_WIDGET_USER_STYLE (widget) &&
       !GTK_WIDGET_RC_STYLE (widget))
-    gtk_widget_set_rc_style (widget);
+    gtk_widget_reset_rc_style (widget);
 }
 
-/**
- * gtk_widget_set_rc_style:
- * @widget: a #GtkWidget
- * 
- * 
+/* Look up the RC style for this widget, unsetting any user style that
+ * may be in effect currently
  **/
-void
-gtk_widget_set_rc_style (GtkWidget *widget)
+static void
+gtk_widget_reset_rc_style (GtkWidget *widget)
 {
-  GtkStyle *saved_style;
   GtkStyle *new_style;
   gboolean initial_emission;
   
@@ -3515,52 +3508,12 @@
   GTK_PRIVATE_UNSET_FLAG (widget, GTK_USER_STYLE);
   GTK_WIDGET_SET_FLAGS (widget, GTK_RC_STYLE);
 
-  saved_style = gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_saved_default_style);
   new_style = gtk_rc_get_style (widget);
-  if (new_style)
-    {
-      if (!saved_style)
-	{
-	  gtk_style_ref (widget->style);
-	  gtk_object_set_data_by_id (GTK_OBJECT (widget), quark_saved_default_style, widget->style);
-	}
-      gtk_widget_set_style_internal (widget, new_style, initial_emission);
-    }
-  else
-    {
-      if (saved_style)
-	{
-	  g_assert (initial_emission == FALSE); /* FIXME: remove this line */
-
-	  gtk_object_remove_data_by_id (GTK_OBJECT (widget), quark_saved_default_style);
-	  gtk_widget_set_style_internal (widget, saved_style, initial_emission);
-	  gtk_style_unref (saved_style);
-	}
-      else
-	{
-	  if (initial_emission)
-	    gtk_widget_set_style_internal (widget, widget->style, TRUE);
-	}
-    }
-}
-
-void
-gtk_widget_restore_default_style (GtkWidget *widget)
-{
-  GtkStyle *default_style;
-
-  g_return_if_fail (widget != NULL);
-  g_return_if_fail (GTK_IS_WIDGET (widget));
-
-  GTK_PRIVATE_UNSET_FLAG (widget, GTK_USER_STYLE);
+  if (!new_style)
+    new_style = gtk_widget_get_default_style ();
 
-  default_style = gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_saved_default_style);
-  if (default_style)
-    {
-      gtk_object_remove_data_by_id (GTK_OBJECT (widget), quark_saved_default_style);
-      gtk_widget_set_style_internal (widget, default_style, FALSE);
-      gtk_style_unref (default_style);
-    }
+  if (initial_emission || new_style != widget->style)
+    gtk_widget_set_style_internal (widget, widget->style, TRUE);
 }
 
 /**
@@ -3624,7 +3577,7 @@
    */
   
   if (GTK_WIDGET_RC_STYLE (widget))
-    gtk_widget_set_rc_style (widget);
+    gtk_widget_reset_rc_style (widget);
 }
 
 /**
@@ -3903,7 +3856,7 @@
 			      gpointer	 client_data)
 {
   if (GTK_WIDGET_RC_STYLE (widget))
-    gtk_widget_set_rc_style (widget);
+    gtk_widget_reset_rc_style (widget);
   
   if (GTK_IS_CONTAINER (widget))
     gtk_container_forall (GTK_CONTAINER (widget),
@@ -3948,19 +3901,6 @@
   gtk_widget_set_style_recurse (widget, NULL);
 }
 
-void
-gtk_widget_set_default_style (GtkStyle *style)
-{
-  if (style != gtk_default_style)
-     {
-       if (gtk_default_style)
-	 gtk_style_unref (gtk_default_style);
-       gtk_default_style = style;
-       if (gtk_default_style)
-	 gtk_style_ref (gtk_default_style);
-     }
-}
-
 GtkStyle*
 gtk_widget_get_default_style (void)
 {
@@ -3973,38 +3913,6 @@
   return gtk_default_style;
 }
 
-void
-gtk_widget_push_style (GtkStyle *style)
-{
-  g_return_if_fail (style != NULL);
-
-  gtk_style_ref (style);
-  style_stack = g_slist_prepend (style_stack, style);
-}
-
-static GtkStyle*
-gtk_widget_peek_style (void)
-{
-  if (style_stack)
-    return (GtkStyle*) style_stack->data;
-  else
-    return gtk_widget_get_default_style ();
-}
-
-void
-gtk_widget_pop_style (void)
-{
-  GSList *tmp;
-  
-  if (style_stack)
-    {
-      tmp = style_stack;
-      style_stack = style_stack->next;
-      gtk_style_unref ((GtkStyle*) tmp->data);
-      g_slist_free_1 (tmp);
-    }
-}
-
 static PangoContext *
 gtk_widget_peek_pango_context (GtkWidget *widget)
 {
@@ -4670,6 +4578,20 @@
   return gdk_colormap_get_visual (gtk_widget_get_colormap (widget));
 }
 
+/**
+ * gtk_widget_get_settings:
+ * @widget: a #GtkWidget
+ * 
+ * Get the settings object holding the settings (global property
+ * settings, RC file information, etc) used for this widget.
+ * 
+ * Return value: the relevant #GtkSettings object
+ **/
+GtkSettings*
+gtk_widget_get_settings (GtkWidget *widget)
+{
+  return gtk_settings_get_default ();
+}
 
 /**
  * gtk_widget_set_colormap:
@@ -5114,7 +5036,6 @@
 gtk_widget_real_destroy (GtkObject *object)
 {
   GtkWidget *widget;
-  GtkStyle *saved_style;
 
   /* gtk_object_destroy() will already hold a refcount on object
    */
@@ -5123,15 +5044,8 @@
   gtk_grab_remove (widget);
   gtk_selection_remove_all (widget);
   
-  saved_style = gtk_object_get_data_by_id (object, quark_saved_default_style);
-  if (saved_style)
-    {
-      gtk_style_unref (saved_style);
-      gtk_object_remove_data_by_id (object, quark_saved_default_style);
-    }
-
   gtk_style_unref (widget->style);
-  widget->style = gtk_widget_peek_style ();
+  widget->style = gtk_widget_get_default_style ();
   gtk_style_ref (widget->style);
 
   GTK_OBJECT_CLASS (parent_class)->destroy (object);
@@ -5144,18 +5058,10 @@
   GtkWidgetAuxInfo *aux_info;
   gint *events;
   GdkExtensionMode *mode;
-  GtkStyle *saved_style;
   GtkAccessible *accessible;
   
   gtk_grab_remove (widget);
   gtk_selection_remove_all (widget);
-
-  saved_style = gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_saved_default_style);
-  if (saved_style)
-    {
-      gtk_style_unref (saved_style);
-      gtk_object_remove_data_by_id (GTK_OBJECT (widget), quark_saved_default_style);
-    }
 
   gtk_style_unref (widget->style);
   widget->style = NULL;
Index: gtkwidget.h
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkwidget.h,v
retrieving revision 1.112
diff -u -r1.112 gtkwidget.h
--- gtkwidget.h	2001/06/24 15:34:48	1.112
+++ gtkwidget.h	2001/06/26 23:42:53
@@ -32,6 +32,7 @@
 #include <gtk/gtkobject.h>
 #include <gtk/gtkadjustment.h>
 #include <gtk/gtkstyle.h>
+#include <gtk/gtksettings.h>
 #include <atk/atkobject.h>
 
 #ifdef __cplusplus
@@ -565,7 +566,9 @@
 GdkColormap* gtk_widget_get_colormap	(GtkWidget	*widget);
 GdkVisual*   gtk_widget_get_visual	(GtkWidget	*widget);
 
+GtkSettings* gtk_widget_get_settings    (GtkWidget      *widget);
 
+
 /* Accessibility support */
 AtkObject*       gtk_widget_get_accessible               (GtkWidget          *widget);
 
@@ -594,10 +597,8 @@
  */
 void	   gtk_widget_set_style		(GtkWidget	*widget,
 					 GtkStyle	*style);
-void	   gtk_widget_set_rc_style	(GtkWidget	*widget);
 void	   gtk_widget_ensure_style	(GtkWidget	*widget);
 GtkStyle*  gtk_widget_get_style		(GtkWidget	*widget);
-void	   gtk_widget_restore_default_style (GtkWidget	*widget);
 
 void        gtk_widget_modify_style       (GtkWidget            *widget,
 					   GtkRcStyle           *style);
@@ -617,6 +618,11 @@
 void        gtk_widget_modify_font        (GtkWidget            *widget,
 					   PangoFontDescription *font_desc);
 
+#ifndef GTK_DISABLE_DEPRECATED
+#define gtk_widget_set_rc_style(widget)          (gtk_widget_set_style (widget, NULL))
+#define gtk_widget_restore_default_style(widget) (gtk_widget_set_style (widget, NULL))
+#endif
+
 PangoContext *gtk_widget_create_pango_context (GtkWidget   *widget);
 PangoContext *gtk_widget_get_pango_context    (GtkWidget   *widget);
 PangoLayout  *gtk_widget_create_pango_layout  (GtkWidget   *widget,
@@ -641,11 +647,9 @@
  * This will override the values that got set by the
  * gtk_widget_set_default_* () functions.
  */
-void	     gtk_widget_push_style	     (GtkStyle	 *style);
 void	     gtk_widget_push_colormap	     (GdkColormap *cmap);
 void	     gtk_widget_push_composite_child (void);
 void	     gtk_widget_pop_composite_child  (void);
-void	     gtk_widget_pop_style	     (void);
 void	     gtk_widget_pop_colormap	     (void);
 
 /* widget style properties


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