gtkseparatortoolitem.c 7.93 KB
Newer Older
1 2
/* gtkseparatortoolitem.c
 *
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
#include "gtktoolbar.h"
29
#include "gtkprivate.h"
30
#include "gtkalias.h"
31

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

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
enum {
  PROP_0,
  PROP_DRAW
};

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);
54
static gint     get_space_size                            (GtkToolItem               *tool_item);
55 56


57

58 59 60 61 62 63
#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;
};
64

Matthias Clasen's avatar
Matthias Clasen committed
65
G_DEFINE_TYPE (GtkSeparatorToolItem, gtk_separator_tool_item, GTK_TYPE_TOOL_ITEM)
66

67 68 69
static gint
get_space_size (GtkToolItem *tool_item)
{
70
  gint space_size = _gtk_toolbar_get_default_space_size();
71 72 73 74 75
  GtkWidget *parent = GTK_WIDGET (tool_item)->parent;
  
  if (GTK_IS_TOOLBAR (parent))
    {
      gtk_widget_style_get (parent,
76
			    "space-size", &space_size,
77 78
			    NULL);
    }
79
  
80 81
  return space_size;
}
82 83 84 85

static void
gtk_separator_tool_item_class_init (GtkSeparatorToolItemClass *class)
{
86
  GObjectClass *object_class;
87 88
  GtkContainerClass *container_class;
  GtkToolItemClass *toolitem_class;
89
  GtkWidgetClass *widget_class;
90 91
  
  object_class = (GObjectClass *)class;
92 93
  container_class = (GtkContainerClass *)class;
  toolitem_class = (GtkToolItemClass *)class;
94
  widget_class = (GtkWidgetClass *)class;
95

96 97
  object_class->set_property = gtk_separator_tool_item_set_property;
  object_class->get_property = gtk_separator_tool_item_get_property;
98
  widget_class->size_request = gtk_separator_tool_item_size_request;
99
  widget_class->expose_event = gtk_separator_tool_item_expose;
100
  toolitem_class->create_menu_proxy = gtk_separator_tool_item_create_menu_proxy;
101
  
102
  container_class->add = gtk_separator_tool_item_add;
103 104 105 106
  
  g_object_class_install_property (object_class,
				   PROP_DRAW,
				   g_param_spec_boolean ("draw",
107 108
							 P_("Draw"),
							 P_("Whether the separator is drawn, or just blank"),
109
							 TRUE,
110
							 GTK_PARAM_READWRITE));
111 112 113 114 115
  
  g_type_class_add_private (object_class, sizeof (GtkSeparatorToolItemPrivate));
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
116
gtk_separator_tool_item_init (GtkSeparatorToolItem      *separator_item)
117 118 119
{
  separator_item->priv = GTK_SEPARATOR_TOOL_ITEM_GET_PRIVATE (separator_item);
  separator_item->priv->draw = TRUE;
120 121 122 123 124 125
}

static void
gtk_separator_tool_item_add (GtkContainer *container,
			     GtkWidget    *child)
{
126
  g_warning ("attempt to add a child to an GtkSeparatorToolItem");
127 128
}

129 130 131 132 133 134 135 136 137 138 139 140
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;
}

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
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;
    }
}

179 180 181 182 183 184
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);
185
  
186 187 188 189 190 191 192 193 194 195 196 197
  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    {
      requisition->width = get_space_size (item);
      requisition->height = 1;
    }
  else
    {
      requisition->height = get_space_size (item);
      requisition->width = 1;
    }
}

198 199 200 201
static gboolean
gtk_separator_tool_item_expose (GtkWidget      *widget,
				GdkEventExpose *event)
{
202
  GtkToolbar *toolbar = NULL;
203 204
  GtkSeparatorToolItemPrivate *priv =
      GTK_SEPARATOR_TOOL_ITEM_GET_PRIVATE (widget);
205

206 207
  if (priv->draw)
    {
208
      if (GTK_IS_TOOLBAR (widget->parent))
209
	toolbar = GTK_TOOLBAR (widget->parent);
210

211 212 213
      _gtk_toolbar_paint_space_line (widget, toolbar,
				     &(event->area), &widget->allocation);
    }
214
  
215
  return FALSE;
216 217
}

218 219 220 221 222 223 224 225 226
/**
 * gtk_separator_tool_item_new:
 * 
 * Create a new #GtkSeparatorToolItem
 * 
 * Return value: the new #GtkSeparatorToolItem
 * 
 * Since: 2.4
 **/
227 228 229 230
GtkToolItem *
gtk_separator_tool_item_new (void)
{
  GtkToolItem *self;
231
  
232 233 234 235 236
  self = g_object_new (GTK_TYPE_SEPARATOR_TOOL_ITEM,
		       NULL);
  
  return self;
}
237 238 239

/**
 * gtk_separator_tool_item_get_draw:
240
 * @item: a #GtkSeparatorToolItem 
241 242 243 244 245 246 247 248 249
 * 
 * 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
250
gtk_separator_tool_item_get_draw (GtkSeparatorToolItem *item)
251
{
252
  g_return_val_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item), FALSE);
253
  
254
  return item->priv->draw;
255 256 257 258
}

/**
 * gtk_separator_tool_item_set_draw:
259
 * @item: a #GtkSeparatorToolItem
Matthias Clasen's avatar
Matthias Clasen committed
260
 * @draw: whether @separator_tool_item is drawn as a vertical line
261 262 263 264 265 266 267 268
 * 
 * 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
269
gtk_separator_tool_item_set_draw (GtkSeparatorToolItem *item,
270 271
				  gboolean              draw)
{
272
  g_return_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item));
273 274 275

  draw = draw != FALSE;

276
  if (draw != item->priv->draw)
277
    {
278
      item->priv->draw = draw;
279

280
      gtk_widget_queue_draw (GTK_WIDGET (item));
281

282
      g_object_notify (G_OBJECT (item), "draw");
283 284 285
    }
}

286 287
#define __GTK_SEPARATOR_TOOL_ITEM_C__
#include "gtkaliasdef.c"