1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module gtk.RadioMenuItem; 26 27 private import glib.ConstructionException; 28 private import glib.ListSG; 29 private import glib.Str; 30 private import gobject.ObjectG; 31 private import gobject.Signals; 32 private import gtk.CheckMenuItem; 33 private import gtk.Widget; 34 private import gtk.c.functions; 35 public import gtk.c.types; 36 public import gtkc.gtktypes; 37 private import std.algorithm; 38 39 40 /** 41 * A radio menu item is a check menu item that belongs to a group. At each 42 * instant exactly one of the radio menu items from a group is selected. 43 * 44 * The group list does not need to be freed, as each #GtkRadioMenuItem will 45 * remove itself and its list item when it is destroyed. 46 * 47 * The correct way to create a group of radio menu items is approximatively 48 * this: 49 * 50 * ## How to create a group of radio menu items. 51 * 52 * |[<!-- language="C" --> 53 * GSList *group = NULL; 54 * GtkWidget *item; 55 * gint i; 56 * 57 * for (i = 0; i < 5; i++) 58 * { 59 * item = gtk_radio_menu_item_new_with_label (group, "This is an example"); 60 * group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item)); 61 * if (i == 1) 62 * gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), TRUE); 63 * } 64 * ]| 65 * 66 * # CSS nodes 67 * 68 * |[<!-- language="plain" --> 69 * menuitem 70 * ├── radio.left 71 * ╰── <child> 72 * ]| 73 * 74 * GtkRadioMenuItem has a main CSS node with name menuitem, and a subnode 75 * with name radio, which gets the .left or .right style class. 76 */ 77 public class RadioMenuItem : CheckMenuItem 78 { 79 /** the main Gtk struct */ 80 protected GtkRadioMenuItem* gtkRadioMenuItem; 81 82 /** Get the main Gtk struct */ 83 public GtkRadioMenuItem* getRadioMenuItemStruct(bool transferOwnership = false) 84 { 85 if (transferOwnership) 86 ownedRef = false; 87 return gtkRadioMenuItem; 88 } 89 90 /** the main Gtk struct as a void* */ 91 protected override void* getStruct() 92 { 93 return cast(void*)gtkRadioMenuItem; 94 } 95 96 protected override void setStruct(GObject* obj) 97 { 98 gtkRadioMenuItem = cast(GtkRadioMenuItem*)obj; 99 super.setStruct(obj); 100 } 101 102 /** 103 * Sets our main struct and passes it to the parent class. 104 */ 105 public this (GtkRadioMenuItem* gtkRadioMenuItem, bool ownedRef = false) 106 { 107 this.gtkRadioMenuItem = gtkRadioMenuItem; 108 super(cast(GtkCheckMenuItem*)gtkRadioMenuItem, ownedRef); 109 } 110 111 /** 112 * Creates a new GtkRadioMenuItem whose child is a simple GtkLabel. 113 * The new GtkRadioMenuItem is added to the same group as group. 114 * If mnemonic is true the label will be 115 * created using gtk_label_new_with_mnemonic(), so underscores in label 116 * indicate the mnemonic for the menu item. 117 * Since 2.4 118 * Params: 119 * group = an existing GtkRadioMenuItem 120 * label = the text for the label 121 * Throws: ConstructionException GTK+ fails to create the object. 122 */ 123 public this (RadioMenuItem radioMenuItem, string label, bool mnemonic=true) 124 { 125 GtkRadioMenuItem* p; 126 127 if ( mnemonic ) 128 { 129 // GtkWidget* gtk_radio_menu_item_new_with_mnemonic_from_widget (GtkRadioMenuItem *group, const gchar *label); 130 p = cast(GtkRadioMenuItem*)gtk_radio_menu_item_new_with_mnemonic_from_widget( 131 radioMenuItem.getRadioMenuItemStruct(), Str.toStringz(label)); 132 } 133 else 134 { 135 // GtkWidget* gtk_radio_menu_item_new_with_label_from_widget (GtkRadioMenuItem *group, const gchar *label); 136 p = cast(GtkRadioMenuItem*)gtk_radio_menu_item_new_with_label_from_widget( 137 radioMenuItem.getRadioMenuItemStruct(), Str.toStringz(label)); 138 } 139 140 if(p is null) 141 { 142 throw new ConstructionException("null returned by gtk_radio_menu_item_new_with_"); 143 } 144 145 this(p); 146 } 147 148 /** 149 * Creates a new GtkRadioMenuItem whose child is a simple GtkLabel. 150 * Params: 151 * group = the group to which the radio menu item is to be attached 152 * label = the text for the label 153 * mnemonic = if true the label 154 * will be created using gtk_label_new_with_mnemonic(), so underscores 155 * in label indicate the mnemonic for the menu item. 156 * Throws: ConstructionException GTK+ fails to create the object. 157 */ 158 public this (ListSG group, string label, bool mnemonic=true) 159 { 160 GtkRadioMenuItem* p; 161 162 if ( mnemonic ) 163 { 164 // GtkWidget* gtk_radio_menu_item_new_with_mnemonic (GSList *group, const gchar *label); 165 p = cast(GtkRadioMenuItem*)gtk_radio_menu_item_new_with_mnemonic( 166 group is null ? null : group.getListSGStruct(), Str.toStringz(label)); 167 } 168 else 169 { 170 // GtkWidget* gtk_radio_menu_item_new_with_label (GSList *group, const gchar *label); 171 p = cast(GtkRadioMenuItem*)gtk_radio_menu_item_new_with_label( 172 group is null ? null : group.getListSGStruct(), Str.toStringz(label)); 173 } 174 175 if(p is null) 176 { 177 throw new ConstructionException("null returned by gtk_radio_menu_item_new_with_"); 178 } 179 180 this(p); 181 } 182 183 /** 184 */ 185 186 /** */ 187 public static GType getType() 188 { 189 return gtk_radio_menu_item_get_type(); 190 } 191 192 /** 193 * Creates a new #GtkRadioMenuItem. 194 * 195 * Params: 196 * group = the group to which the 197 * radio menu item is to be attached, or %NULL 198 * 199 * Returns: a new #GtkRadioMenuItem 200 * 201 * Throws: ConstructionException GTK+ fails to create the object. 202 */ 203 public this(ListSG group) 204 { 205 auto p = gtk_radio_menu_item_new((group is null) ? null : group.getListSGStruct()); 206 207 if(p is null) 208 { 209 throw new ConstructionException("null returned by new"); 210 } 211 212 this(cast(GtkRadioMenuItem*) p); 213 } 214 215 /** 216 * Creates a new #GtkRadioMenuItem adding it to the same group as @group. 217 * 218 * Params: 219 * group = An existing #GtkRadioMenuItem 220 * 221 * Returns: The new #GtkRadioMenuItem 222 * 223 * Since: 2.4 224 * 225 * Throws: ConstructionException GTK+ fails to create the object. 226 */ 227 public this(RadioMenuItem group) 228 { 229 auto p = gtk_radio_menu_item_new_from_widget((group is null) ? null : group.getRadioMenuItemStruct()); 230 231 if(p is null) 232 { 233 throw new ConstructionException("null returned by new_from_widget"); 234 } 235 236 this(cast(GtkRadioMenuItem*) p); 237 } 238 239 /** 240 * Returns the group to which the radio menu item belongs, as a #GList of 241 * #GtkRadioMenuItem. The list belongs to GTK+ and should not be freed. 242 * 243 * Returns: the group 244 * of @radio_menu_item 245 */ 246 public ListSG getGroup() 247 { 248 auto p = gtk_radio_menu_item_get_group(gtkRadioMenuItem); 249 250 if(p is null) 251 { 252 return null; 253 } 254 255 return new ListSG(cast(GSList*) p); 256 } 257 258 /** 259 * Joins a #GtkRadioMenuItem object to the group of another #GtkRadioMenuItem 260 * object. 261 * 262 * This function should be used by language bindings to avoid the memory 263 * manangement of the opaque #GSList of gtk_radio_menu_item_get_group() 264 * and gtk_radio_menu_item_set_group(). 265 * 266 * A common way to set up a group of #GtkRadioMenuItem instances is: 267 * 268 * |[ 269 * GtkRadioMenuItem *last_item = NULL; 270 * 271 * while ( ...more items to add... ) 272 * { 273 * GtkRadioMenuItem *radio_item; 274 * 275 * radio_item = gtk_radio_menu_item_new (...); 276 * 277 * gtk_radio_menu_item_join_group (radio_item, last_item); 278 * last_item = radio_item; 279 * } 280 * ]| 281 * 282 * Params: 283 * groupSource = a #GtkRadioMenuItem whose group we are 284 * joining, or %NULL to remove the @radio_menu_item from its current 285 * group 286 * 287 * Since: 3.18 288 */ 289 public void joinGroup(RadioMenuItem groupSource) 290 { 291 gtk_radio_menu_item_join_group(gtkRadioMenuItem, (groupSource is null) ? null : groupSource.getRadioMenuItemStruct()); 292 } 293 294 /** 295 * Sets the group of a radio menu item, or changes it. 296 * 297 * Params: 298 * group = the new group, or %NULL. 299 */ 300 public void setGroup(ListSG group) 301 { 302 gtk_radio_menu_item_set_group(gtkRadioMenuItem, (group is null) ? null : group.getListSGStruct()); 303 } 304 305 protected class OnGroupChangedDelegateWrapper 306 { 307 void delegate(RadioMenuItem) dlg; 308 gulong handlerId; 309 310 this(void delegate(RadioMenuItem) dlg) 311 { 312 this.dlg = dlg; 313 onGroupChangedListeners ~= this; 314 } 315 316 void remove(OnGroupChangedDelegateWrapper source) 317 { 318 foreach(index, wrapper; onGroupChangedListeners) 319 { 320 if (wrapper.handlerId == source.handlerId) 321 { 322 onGroupChangedListeners[index] = null; 323 onGroupChangedListeners = std.algorithm.remove(onGroupChangedListeners, index); 324 break; 325 } 326 } 327 } 328 } 329 OnGroupChangedDelegateWrapper[] onGroupChangedListeners; 330 331 /** */ 332 gulong addOnGroupChanged(void delegate(RadioMenuItem) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 333 { 334 auto wrapper = new OnGroupChangedDelegateWrapper(dlg); 335 wrapper.handlerId = Signals.connectData( 336 this, 337 "group-changed", 338 cast(GCallback)&callBackGroupChanged, 339 cast(void*)wrapper, 340 cast(GClosureNotify)&callBackGroupChangedDestroy, 341 connectFlags); 342 return wrapper.handlerId; 343 } 344 345 extern(C) static void callBackGroupChanged(GtkRadioMenuItem* radiomenuitemStruct, OnGroupChangedDelegateWrapper wrapper) 346 { 347 wrapper.dlg(wrapper.outer); 348 } 349 350 extern(C) static void callBackGroupChangedDestroy(OnGroupChangedDelegateWrapper wrapper, GClosure* closure) 351 { 352 wrapper.remove(wrapper); 353 } 354 }