gtkradiotoolbutton.c 7.55 KB
Newer Older
1 2
/* gtkradiotoolbutton.c
 *
3
 * Copyright (C) 2002 Anders Carlsson <andersca@gnome.og>
4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (C) 2002 James Henstridge <james@daa.com.au>
 * Copyright (C) 2003 Soeren Sandmann <sandmann@daimi.au.dk>
 *
 * 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
Javier Jardón's avatar
Javier Jardón committed
18
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 20
 */

21
#include "config.h"
22 23 24
#include "gtkradiotoolbutton.h"
#include "gtkradiobutton.h"
#include "gtkintl.h"
25
#include "gtkprivate.h"
26

27

28 29 30 31 32 33 34 35 36 37 38 39 40
/**
 * SECTION:gtkradiotoolbutton
 * @Short_description: A toolbar item that contains a radio button
 * @Title: GtkRadioToolButton
 * @See_also: #GtkToolbar, #GtkToolButton
 *
 * A #GtkRadioToolButton is a #GtkToolItem that contains a radio button,
 * that is, a button that is part of a group of toggle buttons where only
 * one button can be active at a time.
 *
 * Use gtk_radio_tool_button_new() to create a new
 * #GtkRadioToolButton. Use gtk_radio_tool_button_new_from_widget() to
 * create a new #GtkRadioToolButton that is part of the same group as an
41
 * existing #GtkRadioToolButton.
42 43 44
 */


45 46 47 48 49 50 51 52 53
enum {
  PROP_0,
  PROP_GROUP
};

static void gtk_radio_tool_button_set_property (GObject         *object,
						guint            prop_id,
						const GValue    *value,
						GParamSpec      *pspec);
54

Matthias Clasen's avatar
Matthias Clasen committed
55
G_DEFINE_TYPE (GtkRadioToolButton, gtk_radio_tool_button, GTK_TYPE_TOGGLE_TOOL_BUTTON)
56 57 58 59

static void
gtk_radio_tool_button_class_init (GtkRadioToolButtonClass *klass)
{
60
  GObjectClass *object_class;
61 62
  GtkToolButtonClass *toolbutton_class;

63
  object_class = (GObjectClass *)klass;
64
  toolbutton_class = (GtkToolButtonClass *)klass;
65 66

  object_class->set_property = gtk_radio_tool_button_set_property;
67 68
  
  toolbutton_class->button_type = GTK_TYPE_RADIO_BUTTON;  
69 70 71 72 73 74 75 76 77 78 79

  /**
   * GtkRadioToolButton:group:
   *
   * Sets a new group for a radio tool button.
   *
   * Since: 2.4
   */
  g_object_class_install_property (object_class,
				   PROP_GROUP,
				   g_param_spec_object ("group",
80 81
							P_("Group"),
							P_("The radio tool button whose group this button belongs to."),
82
							GTK_TYPE_RADIO_TOOL_BUTTON,
83
							GTK_PARAM_WRITABLE));
84

85 86 87 88 89
}

static void
gtk_radio_tool_button_init (GtkRadioToolButton *button)
{
90 91
  GtkToolButton *tool_button = GTK_TOOL_BUTTON (button);
  gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button)), FALSE);
92 93
}

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
static void
gtk_radio_tool_button_set_property (GObject         *object,
				    guint            prop_id,
				    const GValue    *value,
				    GParamSpec      *pspec)
{
  GtkRadioToolButton *button;

  button = GTK_RADIO_TOOL_BUTTON (object);

  switch (prop_id)
    {
    case PROP_GROUP:
      {
	GtkRadioToolButton *arg;
	GSList *slist = NULL;
	if (G_VALUE_HOLDS_OBJECT (value)) 
	  {
	    arg = GTK_RADIO_TOOL_BUTTON (g_value_get_object (value));
	    if (arg)
	      slist = gtk_radio_tool_button_get_group (arg);
	    gtk_radio_tool_button_set_group (button, slist);
	  }
      }
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

125 126
/**
 * gtk_radio_tool_button_new:
127
 * @group: (allow-none) (element-type GtkRadioButton): An
128
 *   existing radio button group, or %NULL if you are creating a new group
129 130 131
 * 
 * Creates a new #GtkRadioToolButton, adding it to @group.
 * 
132
 * Returns: The new #GtkRadioToolButton
133 134 135
 * 
 * Since: 2.4
 **/
136 137 138 139 140 141 142 143 144 145 146 147 148
GtkToolItem *
gtk_radio_tool_button_new (GSList *group)
{
  GtkRadioToolButton *button;
  
  button = g_object_new (GTK_TYPE_RADIO_TOOL_BUTTON,
			 NULL);

  gtk_radio_tool_button_set_group (button, group);
  
  return GTK_TOOL_ITEM (button);
}

149 150
/**
 * gtk_radio_tool_button_new_from_stock:
151 152
 * @group: (allow-none) (element-type GtkRadioButton): an existing radio button
 *   group, or %NULL if you are creating a new group
153 154 155 156 157 158
 * @stock_id: the name of a stock item
 * 
 * Creates a new #GtkRadioToolButton, adding it to @group. 
 * The new #GtkRadioToolButton will contain an icon and label from the
 * stock item indicated by @stock_id.
 * 
159
 * Returns: The new #GtkRadioToolButton
160 161
 * 
 * Since: 2.4
162 163
 *
 * Deprecated: 3.10: Use gtk_radio_tool_button_new() instead.
164
 **/
165 166 167 168 169 170 171 172 173
GtkToolItem *
gtk_radio_tool_button_new_from_stock (GSList      *group,
				      const gchar *stock_id)
{
  GtkRadioToolButton *button;

  g_return_val_if_fail (stock_id != NULL, NULL);
  
  button = g_object_new (GTK_TYPE_RADIO_TOOL_BUTTON,
174
			 "stock-id", stock_id,
175 176 177 178 179 180 181 182
			 NULL);


  gtk_radio_tool_button_set_group (button, group);
  
  return GTK_TOOL_ITEM (button);
}

183
/**
184
 * gtk_radio_tool_button_new_from_widget: (constructor)
185
 * @group: (allow-none): An existing #GtkRadioToolButton, or %NULL
186
 *
187
 * Creates a new #GtkRadioToolButton adding it to the same group as @gruup
188
 *
189
 * Returns: (transfer none): The new #GtkRadioToolButton
190
 *
191 192
 * Since: 2.4
 **/
193
GtkToolItem *
194
gtk_radio_tool_button_new_from_widget (GtkRadioToolButton *group)
195 196 197
{
  GSList *list = NULL;
  
198
  g_return_val_if_fail (group == NULL || GTK_IS_RADIO_TOOL_BUTTON (group), NULL);
199

200
  if (group != NULL)
201 202
    list = gtk_radio_tool_button_get_group (GTK_RADIO_TOOL_BUTTON (group));
  
203
  return gtk_radio_tool_button_new (list);
204 205
}

206
/**
207
 * gtk_radio_tool_button_new_with_stock_from_widget: (constructor)
208
 * @group: (allow-none): An existing #GtkRadioToolButton.
209 210
 * @stock_id: the name of a stock item
 *
211 212 213
 * Creates a new #GtkRadioToolButton adding it to the same group as @group.
 * The new #GtkRadioToolButton will contain an icon and label from the
 * stock item indicated by @stock_id.
214
 *
215
 * Returns: (transfer none): A new #GtkRadioToolButton
216
 *
217
 * Since: 2.4
218 219
 *
 * Deprecated: 3.10: gtk_radio_tool_button_new_from_widget
220
 **/
221
GtkToolItem *
222 223
gtk_radio_tool_button_new_with_stock_from_widget (GtkRadioToolButton *group,
						  const gchar        *stock_id)
224 225
{
  GSList *list = NULL;
226 227
  GtkToolItem *item;

228
  g_return_val_if_fail (group == NULL || GTK_IS_RADIO_TOOL_BUTTON (group), NULL);
229

230
  if (group != NULL)
231
    list = gtk_radio_tool_button_get_group (group);
232 233 234 235 236 237

  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
  item = gtk_radio_tool_button_new_from_stock (list, stock_id);
  G_GNUC_END_IGNORE_DEPRECATIONS;

  return item;
238 239
}

240 241 242 243 244 245
static GtkRadioButton *
get_radio_button (GtkRadioToolButton *button)
{
  return GTK_RADIO_BUTTON (_gtk_tool_button_get_button (GTK_TOOL_BUTTON (button)));
}

246 247 248 249 250
/**
 * gtk_radio_tool_button_get_group:
 * @button: a #GtkRadioToolButton
 *
 * Returns the radio button group @button belongs to.
251
 *
252
 * Returns: (transfer none) (element-type GtkRadioButton): The group @button belongs to.
253
 *
254
 * Since: 2.4
255
 */
256 257 258 259 260
GSList *
gtk_radio_tool_button_get_group (GtkRadioToolButton *button)
{
  g_return_val_if_fail (GTK_IS_RADIO_TOOL_BUTTON (button), NULL);

261
  return gtk_radio_button_get_group (get_radio_button (button));
262 263
}

264 265 266
/**
 * gtk_radio_tool_button_set_group:
 * @button: a #GtkRadioToolButton
267
 * @group: (element-type GtkRadioButton) (allow-none): an existing radio button group, or %NULL
268 269 270 271 272
 * 
 * Adds @button to @group, removing it from the group it belonged to before.
 * 
 * Since: 2.4
 **/
273 274 275 276 277 278
void
gtk_radio_tool_button_set_group (GtkRadioToolButton *button,
				 GSList             *group)
{
  g_return_if_fail (GTK_IS_RADIO_TOOL_BUTTON (button));

279
  gtk_radio_button_set_group (get_radio_button (button), group);
280
}