]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkseparatortoolitem.c
Make sure to include <config.h> (#137001)
[~andy/gtk] / gtk / gtkseparatortoolitem.c
index 34015b888b2f20fe648fcc6ee045bfd6c4d4a7f6..e3682114721a07ea69707bf298b4b1a18f04c2d6 100644 (file)
@@ -1,6 +1,6 @@
 /* gtkseparatortoolitem.c
  *
- * Copyright (C) 2002 Anders Carlsson <andersca@codefactory.se>
+ * Copyright (C) 2002 Anders Carlsson <andersca@gnome.org>
  * Copyright (C) 2002 James Henstridge <james@daa.com.au>
  *
  * This library is free software; you can redistribute it and/or
 
 #undef GTK_DISABLE_DEPRECATED
 
+#include <config.h>
 #include "gtkseparatormenuitem.h"
 #include "gtkseparatortoolitem.h"
 #include "gtkintl.h"
 #include "gtktoolbar.h"
 
-/* note: keep in sync with DEFAULT_SPACE_SIZE and DEFAULT_SPACE_STYLE in gtktoolbar.c */
-#define DEFAULT_SPACE_SIZE 4
-#define DEFAULT_SPACE_STYLE GTK_TOOLBAR_SPACE_LINE
+#define MENU_ID "gtk-separator-tool-item-menu-id"
 
-#define SPACE_LINE_DIVISION 10
-#define SPACE_LINE_START    3
-#define SPACE_LINE_END      7
+enum {
+  PROP_0,
+  PROP_DRAW
+};
+
+static void     gtk_separator_tool_item_class_init        (GtkSeparatorToolItemClass *class);
+static void    gtk_separator_tool_item_init              (GtkSeparatorToolItem      *separator_item,
+                                                          GtkSeparatorToolItemClass *class);
+static gboolean gtk_separator_tool_item_create_menu_proxy (GtkToolItem               *item);
+static void     gtk_separator_tool_item_set_property      (GObject                   *object,
+                                                          guint                      prop_id,
+                                                          const GValue              *value,
+                                                          GParamSpec                *pspec);
+static void     gtk_separator_tool_item_get_property       (GObject                   *object,
+                                                          guint                      prop_id,
+                                                          GValue                    *value,
+                                                          GParamSpec                *pspec);
+static void     gtk_separator_tool_item_size_request      (GtkWidget                 *widget,
+                                                          GtkRequisition            *requisition);
+static gboolean gtk_separator_tool_item_expose            (GtkWidget                 *widget,
+                                                          GdkEventExpose            *event);
+static void     gtk_separator_tool_item_add               (GtkContainer              *container,
+                                                          GtkWidget                 *child);
+static gint     get_space_size                            (GtkToolItem               *tool_item);
 
-#define MENU_ID "gtk-separator-tool-item-menu-id"
 
-static void                 gtk_separator_tool_item_class_init (GtkSeparatorToolItemClass *class);
-static gboolean             gtk_separator_tool_item_create_menu_proxy (GtkToolItem *item);
-static void                gtk_separator_tool_item_size_request (GtkWidget      *widget,
-                                                                 
-                                                                 GtkRequisition *requisition);
-static gboolean             gtk_separator_tool_item_expose     (GtkWidget                 *widget,
-                                                               GdkEventExpose            *event);
-static void                 gtk_separator_tool_item_add        (GtkContainer              *container,
-                                                               GtkWidget                 *child);
-static GtkToolbarSpaceStyle get_space_style                    (GtkToolItem               *tool_item);
-static gint                 get_space_size                     (GtkToolItem               *tool_item);
 
 static GObjectClass *parent_class = NULL;
 
+#define GTK_SEPARATOR_TOOL_ITEM_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_SEPARATOR_TOOL_ITEM, GtkSeparatorToolItemPrivate))
+
+struct _GtkSeparatorToolItemPrivate
+{
+  guint draw : 1;
+};
 
 GType
 gtk_separator_tool_item_get_type (void)
 {
   static GType type = 0;
-
+  
   if (!type)
     {
       static const GTypeInfo type_info =
@@ -68,35 +82,19 @@ gtk_separator_tool_item_get_type (void)
          NULL,
          sizeof (GtkSeparatorToolItem),
          0, /* n_preallocs */
-         (GInstanceInitFunc) NULL,
+         (GInstanceInitFunc) gtk_separator_tool_item_init,
        };
-
+      
       type = g_type_register_static (GTK_TYPE_TOOL_ITEM,
                                     "GtkSeparatorToolItem", &type_info, 0);
     }
   return type;
 }
 
-static GtkToolbarSpaceStyle
-get_space_style (GtkToolItem *tool_item)
-{
-  GtkToolbarSpaceStyle space_style = DEFAULT_SPACE_STYLE;
-  GtkWidget *parent = GTK_WIDGET (tool_item)->parent;
-
-  if (GTK_IS_TOOLBAR (parent))
-    {
-      gtk_widget_style_get (parent,
-                           "space_style", &space_style,
-                           NULL);
-    }
-
-  return space_style;  
-}
-
 static gint
 get_space_size (GtkToolItem *tool_item)
 {
-  gint space_size = DEFAULT_SPACE_SIZE;
+  gint space_size = _gtk_toolbar_get_default_space_size();
   GtkWidget *parent = GTK_WIDGET (tool_item)->parent;
   
   if (GTK_IS_TOOLBAR (parent))
@@ -105,34 +103,56 @@ get_space_size (GtkToolItem *tool_item)
                            "space_size", &space_size,
                            NULL);
     }
-
+  
   return space_size;
 }
 
 static void
 gtk_separator_tool_item_class_init (GtkSeparatorToolItemClass *class)
 {
+  GObjectClass *object_class;
   GtkContainerClass *container_class;
   GtkToolItemClass *toolitem_class;
   GtkWidgetClass *widget_class;
-
+  
   parent_class = g_type_class_peek_parent (class);
+  object_class = (GObjectClass *)class;
   container_class = (GtkContainerClass *)class;
   toolitem_class = (GtkToolItemClass *)class;
   widget_class = (GtkWidgetClass *)class;
 
+  object_class->set_property = gtk_separator_tool_item_set_property;
+  object_class->get_property = gtk_separator_tool_item_get_property;
   widget_class->size_request = gtk_separator_tool_item_size_request;
   widget_class->expose_event = gtk_separator_tool_item_expose;
   toolitem_class->create_menu_proxy = gtk_separator_tool_item_create_menu_proxy;
-
+  
   container_class->add = gtk_separator_tool_item_add;
+  
+  g_object_class_install_property (object_class,
+                                  PROP_DRAW,
+                                  g_param_spec_boolean ("draw",
+                                                        P_("Draw"),
+                                                        P_("Whether the separator is drawn, or just blank"),
+                                                        TRUE,
+                                                        G_PARAM_READWRITE));
+  
+  g_type_class_add_private (object_class, sizeof (GtkSeparatorToolItemPrivate));
+}
+
+static void
+gtk_separator_tool_item_init (GtkSeparatorToolItem      *separator_item,
+                             GtkSeparatorToolItemClass *class)
+{
+  separator_item->priv = GTK_SEPARATOR_TOOL_ITEM_GET_PRIVATE (separator_item);
+  separator_item->priv->draw = TRUE;
 }
 
 static void
 gtk_separator_tool_item_add (GtkContainer *container,
                             GtkWidget    *child)
 {
-  g_warning("attempt to add a child to an GtkSeparatorToolItem");
+  g_warning ("attempt to add a child to an GtkSeparatorToolItem");
 }
 
 static gboolean
@@ -147,13 +167,51 @@ gtk_separator_tool_item_create_menu_proxy (GtkToolItem *item)
   return TRUE;
 }
 
+static void
+gtk_separator_tool_item_set_property (GObject      *object,
+                                     guint         prop_id,
+                                     const GValue *value,
+                                     GParamSpec   *pspec)
+{
+  GtkSeparatorToolItem *item = GTK_SEPARATOR_TOOL_ITEM (object);
+  
+  switch (prop_id)
+    {
+    case PROP_DRAW:
+      gtk_separator_tool_item_set_draw (item, g_value_get_boolean (value));
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+gtk_separator_tool_item_get_property (GObject      *object,
+                                     guint         prop_id,
+                                     GValue       *value,
+                                     GParamSpec   *pspec)
+{
+  GtkSeparatorToolItem *item = GTK_SEPARATOR_TOOL_ITEM (object);
+  
+  switch (prop_id)
+    {
+    case PROP_DRAW:
+      g_value_set_boolean (value, gtk_separator_tool_item_get_draw (item));
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
 static void
 gtk_separator_tool_item_size_request (GtkWidget      *widget,
                                      GtkRequisition *requisition)
 {
   GtkToolItem *item = GTK_TOOL_ITEM (widget);
   GtkOrientation orientation = gtk_tool_item_get_orientation (item);
-
+  
   if (orientation == GTK_ORIENTATION_HORIZONTAL)
     {
       requisition->width = get_space_size (item);
@@ -170,44 +228,15 @@ static gboolean
 gtk_separator_tool_item_expose (GtkWidget      *widget,
                                GdkEventExpose *event)
 {
-  GtkToolItem *tool_item = GTK_TOOL_ITEM (widget);
-  gint space_size;
-  GtkAllocation *allocation;
-  GtkOrientation orientation;
-  GdkRectangle *area;
+  GtkToolbar *toolbar = NULL;
 
-  if (get_space_style (tool_item) == GTK_TOOLBAR_SPACE_LINE)
-    {
-      space_size = get_space_size (tool_item);
-      allocation = &(widget->allocation);
-      orientation = gtk_tool_item_get_orientation (tool_item);
-      area = &(event->area);
+  if (widget->parent && GTK_IS_TOOLBAR (widget->parent))
+    toolbar = GTK_TOOLBAR (widget->parent);
 
-      if (orientation == GTK_ORIENTATION_HORIZONTAL)
-       {
-         gtk_paint_vline (widget->style, widget->window,
-                          GTK_WIDGET_STATE (widget), area, widget,
-                          "separator_tool_item",
-                          allocation->y + allocation->height *
-                          SPACE_LINE_START / SPACE_LINE_DIVISION,
-                          allocation->y + allocation->height *
-                          SPACE_LINE_END / SPACE_LINE_DIVISION,
-                          allocation->x + (space_size - widget->style->xthickness) / 2);
-       }
-      else if (orientation == GTK_ORIENTATION_VERTICAL)
-       {
-         gtk_paint_hline (widget->style, widget->window,
-                          GTK_WIDGET_STATE (widget), area, widget,
-                          "separator_tool_item",
-                          allocation->x + allocation->width *
-                          SPACE_LINE_START / SPACE_LINE_DIVISION,
-                          allocation->x + allocation->width *
-                          SPACE_LINE_END / SPACE_LINE_DIVISION,
-                          allocation->y + (space_size - widget->style->ythickness) / 2);
-       }
-    }
+  _gtk_toolbar_paint_space_line (widget, toolbar,
+                                &(event->area), &widget->allocation);
   
-  return TRUE;
+  return FALSE;
 }
 
 /**
@@ -223,9 +252,58 @@ GtkToolItem *
 gtk_separator_tool_item_new (void)
 {
   GtkToolItem *self;
-
+  
   self = g_object_new (GTK_TYPE_SEPARATOR_TOOL_ITEM,
                       NULL);
   
   return self;
 }
+
+/**
+ * gtk_separator_tool_item_get_draw:
+ * @item: a #GtkSeparatorToolItem 
+ * 
+ * Returns whether @separator_tool_item is drawn as a
+ * line, or just blank. See gtk_separator_tool_item_set_draw().
+ * 
+ * Return value: #TRUE if @separator_tool_item is drawn as a line, or just blank.
+ * 
+ * Since: 2.4
+ **/
+gboolean
+gtk_separator_tool_item_get_draw (GtkSeparatorToolItem *item)
+{
+  g_return_val_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item), FALSE);
+  
+  return item->priv->draw;
+}
+
+/**
+ * gtk_separator_tool_item_set_draw:
+ * @item: a #GtkSeparatorToolItem
+ * @draw: whether @separator_tool_item is drawn as a vertical iln
+ * 
+ * When @separator_tool_items is drawn as a vertical line, or just blank.
+ * Setting this #FALSE along with gtk_tool_item_set_expand() is useful
+ * to create an item that forces following items to the end of the toolbar.
+ * 
+ * Since: 2.4
+ **/
+void
+gtk_separator_tool_item_set_draw (GtkSeparatorToolItem *item,
+                                 gboolean              draw)
+{
+  g_return_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item));
+
+  draw = draw != FALSE;
+
+  if (draw != item->priv->draw)
+    {
+      item->priv->draw = draw;
+
+      gtk_widget_queue_draw (GTK_WIDGET (item));
+
+      g_object_notify (G_OBJECT (item), "draw");
+    }
+}
+