gtkseparatortoolitem.c 8.69 KB
Newer Older
1 2
/* gtkseparatortoolitem.c
 *
Anders Carlsson's avatar
Anders Carlsson committed
3
 * Copyright (C) 2002 Anders Carlsson <andersca@gnome.org>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * Copyright (C) 2002 James Henstridge <james@daa.com.au>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

22 23
#undef GTK_DISABLE_DEPRECATED

24
#include <config.h>
25
#include "gtkalias.h"
26 27 28
#include "gtkseparatormenuitem.h"
#include "gtkseparatortoolitem.h"
#include "gtkintl.h"
29 30
#include "gtktoolbar.h"

31 32
#define MENU_ID "gtk-separator-tool-item-menu-id"

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
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);
56
static gint     get_space_size                            (GtkToolItem               *tool_item);
57 58


59 60 61

static GObjectClass *parent_class = NULL;

62 63 64 65 66 67
#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;
};
68 69 70 71 72

GType
gtk_separator_tool_item_get_type (void)
{
  static GType type = 0;
73
  
74 75 76 77 78 79 80 81 82 83 84 85
  if (!type)
    {
      static const GTypeInfo type_info =
	{
	  sizeof (GtkSeparatorToolItemClass),
	  (GBaseInitFunc) 0,
	  (GBaseFinalizeFunc) 0,
	  (GClassInitFunc) gtk_separator_tool_item_class_init,
	  (GClassFinalizeFunc) 0,
	  NULL,
	  sizeof (GtkSeparatorToolItem),
	  0, /* n_preallocs */
86
	  (GInstanceInitFunc) gtk_separator_tool_item_init,
87
	};
88
      
89 90 91 92 93 94
      type = g_type_register_static (GTK_TYPE_TOOL_ITEM,
				     "GtkSeparatorToolItem", &type_info, 0);
    }
  return type;
}

95 96 97
static gint
get_space_size (GtkToolItem *tool_item)
{
98
  gint space_size = _gtk_toolbar_get_default_space_size();
99 100 101 102 103 104 105 106
  GtkWidget *parent = GTK_WIDGET (tool_item)->parent;
  
  if (GTK_IS_TOOLBAR (parent))
    {
      gtk_widget_style_get (parent,
			    "space_size", &space_size,
			    NULL);
    }
107
  
108 109
  return space_size;
}
110 111 112 113

static void
gtk_separator_tool_item_class_init (GtkSeparatorToolItemClass *class)
{
114
  GObjectClass *object_class;
115 116
  GtkContainerClass *container_class;
  GtkToolItemClass *toolitem_class;
117
  GtkWidgetClass *widget_class;
118
  
119
  parent_class = g_type_class_peek_parent (class);
120
  object_class = (GObjectClass *)class;
121 122
  container_class = (GtkContainerClass *)class;
  toolitem_class = (GtkToolItemClass *)class;
123
  widget_class = (GtkWidgetClass *)class;
124

125 126
  object_class->set_property = gtk_separator_tool_item_set_property;
  object_class->get_property = gtk_separator_tool_item_get_property;
127
  widget_class->size_request = gtk_separator_tool_item_size_request;
128
  widget_class->expose_event = gtk_separator_tool_item_expose;
129
  toolitem_class->create_menu_proxy = gtk_separator_tool_item_create_menu_proxy;
130
  
131
  container_class->add = gtk_separator_tool_item_add;
132 133 134 135
  
  g_object_class_install_property (object_class,
				   PROP_DRAW,
				   g_param_spec_boolean ("draw",
136 137
							 P_("Draw"),
							 P_("Whether the separator is drawn, or just blank"),
138 139 140 141 142 143 144 145 146 147 148 149
							 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;
150 151 152 153 154 155
}

static void
gtk_separator_tool_item_add (GtkContainer *container,
			     GtkWidget    *child)
{
156
  g_warning ("attempt to add a child to an GtkSeparatorToolItem");
157 158
}

159 160 161 162 163 164 165 166 167 168 169 170
static gboolean
gtk_separator_tool_item_create_menu_proxy (GtkToolItem *item)
{
  GtkWidget *menu_item = NULL;
  
  menu_item = gtk_separator_menu_item_new();
  
  gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
  
  return TRUE;
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
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;
    }
}

209 210 211 212 213 214
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);
215
  
216 217 218 219 220 221 222 223 224 225 226 227
  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    {
      requisition->width = get_space_size (item);
      requisition->height = 1;
    }
  else
    {
      requisition->height = get_space_size (item);
      requisition->width = 1;
    }
}

228 229 230 231
static gboolean
gtk_separator_tool_item_expose (GtkWidget      *widget,
				GdkEventExpose *event)
{
232
  GtkToolbar *toolbar = NULL;
233 234
  GtkSeparatorToolItemPrivate *priv =
      GTK_SEPARATOR_TOOL_ITEM_GET_PRIVATE (widget);
235

236 237 238 239
  if (priv->draw)
    {
      if (widget->parent && GTK_IS_TOOLBAR (widget->parent))
	toolbar = GTK_TOOLBAR (widget->parent);
240

241 242 243
      _gtk_toolbar_paint_space_line (widget, toolbar,
				     &(event->area), &widget->allocation);
    }
244
  
245
  return FALSE;
246 247
}

248 249 250 251 252 253 254 255 256
/**
 * gtk_separator_tool_item_new:
 * 
 * Create a new #GtkSeparatorToolItem
 * 
 * Return value: the new #GtkSeparatorToolItem
 * 
 * Since: 2.4
 **/
257 258 259 260
GtkToolItem *
gtk_separator_tool_item_new (void)
{
  GtkToolItem *self;
261
  
262 263 264 265 266
  self = g_object_new (GTK_TYPE_SEPARATOR_TOOL_ITEM,
		       NULL);
  
  return self;
}
267 268 269

/**
 * gtk_separator_tool_item_get_draw:
270
 * @item: a #GtkSeparatorToolItem 
271 272 273 274 275 276 277 278 279
 * 
 * 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
280
gtk_separator_tool_item_get_draw (GtkSeparatorToolItem *item)
281
{
282
  g_return_val_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item), FALSE);
283
  
284
  return item->priv->draw;
285 286 287 288
}

/**
 * gtk_separator_tool_item_set_draw:
289
 * @item: a #GtkSeparatorToolItem
290 291 292 293 294 295 296 297 298
 * @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
299
gtk_separator_tool_item_set_draw (GtkSeparatorToolItem *item,
300 301
				  gboolean              draw)
{
302
  g_return_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item));
303 304 305

  draw = draw != FALSE;

306
  if (draw != item->priv->draw)
307
    {
308
      item->priv->draw = draw;
309

310
      gtk_widget_queue_draw (GTK_WIDGET (item));
311

312
      g_object_notify (G_OBJECT (item), "draw");
313 314 315
    }
}