gtkseparatortoolitem.c 8.54 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 26 27
#include "gtkseparatormenuitem.h"
#include "gtkseparatortoolitem.h"
#include "gtkintl.h"
28 29
#include "gtktoolbar.h"

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

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


58 59 60

static GObjectClass *parent_class = NULL;

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

GType
gtk_separator_tool_item_get_type (void)
{
  static GType type = 0;
72
  
73 74 75 76 77 78 79 80 81 82 83 84
  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 */
85
	  (GInstanceInitFunc) gtk_separator_tool_item_init,
86
	};
87
      
88 89 90 91 92 93
      type = g_type_register_static (GTK_TYPE_TOOL_ITEM,
				     "GtkSeparatorToolItem", &type_info, 0);
    }
  return type;
}

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

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

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

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

158 159 160 161 162 163 164 165 166 167 168 169
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;
}

170 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
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;
    }
}

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

227 228 229 230
static gboolean
gtk_separator_tool_item_expose (GtkWidget      *widget,
				GdkEventExpose *event)
{
231
  GtkToolbar *toolbar = NULL;
232

233 234 235 236 237
  if (widget->parent && GTK_IS_TOOLBAR (widget->parent))
    toolbar = GTK_TOOLBAR (widget->parent);

  _gtk_toolbar_paint_space_line (widget, toolbar,
				 &(event->area), &widget->allocation);
238
  
239
  return FALSE;
240 241
}

242 243 244 245 246 247 248 249 250
/**
 * gtk_separator_tool_item_new:
 * 
 * Create a new #GtkSeparatorToolItem
 * 
 * Return value: the new #GtkSeparatorToolItem
 * 
 * Since: 2.4
 **/
251 252 253 254
GtkToolItem *
gtk_separator_tool_item_new (void)
{
  GtkToolItem *self;
255
  
256 257 258 259 260
  self = g_object_new (GTK_TYPE_SEPARATOR_TOOL_ITEM,
		       NULL);
  
  return self;
}
261 262 263

/**
 * gtk_separator_tool_item_get_draw:
264
 * @item: a #GtkSeparatorToolItem 
265 266 267 268 269 270 271 272 273
 * 
 * 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
274
gtk_separator_tool_item_get_draw (GtkSeparatorToolItem *item)
275
{
276
  g_return_val_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item), FALSE);
277
  
278
  return item->priv->draw;
279 280 281 282
}

/**
 * gtk_separator_tool_item_set_draw:
283
 * @item: a #GtkSeparatorToolItem
284 285 286 287 288 289 290 291 292
 * @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
293
gtk_separator_tool_item_set_draw (GtkSeparatorToolItem *item,
294 295
				  gboolean              draw)
{
296
  g_return_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item));
297 298 299

  draw = draw != FALSE;

300
  if (draw != item->priv->draw)
301
    {
302
      item->priv->draw = draw;
303

304
      gtk_widget_queue_draw (GTK_WIDGET (item));
305

306
      g_object_notify (G_OBJECT (item), "draw");
307 308 309
    }
}