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 gio.SimpleAction; 26 27 private import gio.ActionIF; 28 private import gio.ActionT; 29 private import gio.c.functions; 30 public import gio.c.types; 31 private import glib.ConstructionException; 32 private import glib.Str; 33 private import glib.Variant; 34 private import glib.VariantType; 35 private import gobject.ObjectG; 36 private import gobject.Signals; 37 public import gtkc.giotypes; 38 private import std.algorithm; 39 40 41 /** 42 * A #GSimpleAction is the obvious simple implementation of the #GAction 43 * interface. This is the easiest way to create an action for purposes of 44 * adding it to a #GSimpleActionGroup. 45 * 46 * See also #GtkAction. 47 */ 48 public class SimpleAction : ObjectG, ActionIF 49 { 50 /** the main Gtk struct */ 51 protected GSimpleAction* gSimpleAction; 52 53 /** Get the main Gtk struct */ 54 public GSimpleAction* getSimpleActionStruct(bool transferOwnership = false) 55 { 56 if (transferOwnership) 57 ownedRef = false; 58 return gSimpleAction; 59 } 60 61 /** the main Gtk struct as a void* */ 62 protected override void* getStruct() 63 { 64 return cast(void*)gSimpleAction; 65 } 66 67 /** 68 * Sets our main struct and passes it to the parent class. 69 */ 70 public this (GSimpleAction* gSimpleAction, bool ownedRef = false) 71 { 72 this.gSimpleAction = gSimpleAction; 73 super(cast(GObject*)gSimpleAction, ownedRef); 74 } 75 76 // add the Action capabilities 77 mixin ActionT!(GSimpleAction); 78 79 80 /** */ 81 public static GType getType() 82 { 83 return g_simple_action_get_type(); 84 } 85 86 /** 87 * Creates a new action. 88 * 89 * The created action is stateless. See g_simple_action_new_stateful(). 90 * 91 * Params: 92 * name = the name of the action 93 * parameterType = the type of parameter to the activate function 94 * 95 * Returns: a new #GSimpleAction 96 * 97 * Since: 2.28 98 * 99 * Throws: ConstructionException GTK+ fails to create the object. 100 */ 101 public this(string name, VariantType parameterType) 102 { 103 auto p = g_simple_action_new(Str.toStringz(name), (parameterType is null) ? null : parameterType.getVariantTypeStruct()); 104 105 if(p is null) 106 { 107 throw new ConstructionException("null returned by new"); 108 } 109 110 this(cast(GSimpleAction*) p, true); 111 } 112 113 /** 114 * Creates a new stateful action. 115 * 116 * @state is the initial state of the action. All future state values 117 * must have the same #GVariantType as the initial state. 118 * 119 * If the @state GVariant is floating, it is consumed. 120 * 121 * Params: 122 * name = the name of the action 123 * parameterType = the type of the parameter to the activate function 124 * state = the initial state of the action 125 * 126 * Returns: a new #GSimpleAction 127 * 128 * Since: 2.28 129 * 130 * Throws: ConstructionException GTK+ fails to create the object. 131 */ 132 public this(string name, VariantType parameterType, Variant state) 133 { 134 auto p = g_simple_action_new_stateful(Str.toStringz(name), (parameterType is null) ? null : parameterType.getVariantTypeStruct(), (state is null) ? null : state.getVariantStruct()); 135 136 if(p is null) 137 { 138 throw new ConstructionException("null returned by new_stateful"); 139 } 140 141 this(cast(GSimpleAction*) p, true); 142 } 143 144 /** 145 * Sets the action as enabled or not. 146 * 147 * An action must be enabled in order to be activated or in order to 148 * have its state changed from outside callers. 149 * 150 * This should only be called by the implementor of the action. Users 151 * of the action should not attempt to modify its enabled flag. 152 * 153 * Params: 154 * enabled = whether the action is enabled 155 * 156 * Since: 2.28 157 */ 158 public void setEnabled(bool enabled) 159 { 160 g_simple_action_set_enabled(gSimpleAction, enabled); 161 } 162 163 /** 164 * Sets the state of the action. 165 * 166 * This directly updates the 'state' property to the given value. 167 * 168 * This should only be called by the implementor of the action. Users 169 * of the action should not attempt to directly modify the 'state' 170 * property. Instead, they should call g_action_change_state() to 171 * request the change. 172 * 173 * If the @value GVariant is floating, it is consumed. 174 * 175 * Params: 176 * value = the new #GVariant for the state 177 * 178 * Since: 2.30 179 */ 180 public void setState(Variant value) 181 { 182 g_simple_action_set_state(gSimpleAction, (value is null) ? null : value.getVariantStruct()); 183 } 184 185 /** 186 * Sets the state hint for the action. 187 * 188 * See g_action_get_state_hint() for more information about 189 * action state hints. 190 * 191 * Params: 192 * stateHint = a #GVariant representing the state hint 193 * 194 * Since: 2.44 195 */ 196 public void setStateHint(Variant stateHint) 197 { 198 g_simple_action_set_state_hint(gSimpleAction, (stateHint is null) ? null : stateHint.getVariantStruct()); 199 } 200 201 protected class OnActivateDelegateWrapper 202 { 203 void delegate(Variant, SimpleAction) dlg; 204 gulong handlerId; 205 206 this(void delegate(Variant, SimpleAction) dlg) 207 { 208 this.dlg = dlg; 209 onActivateListeners ~= this; 210 } 211 212 void remove(OnActivateDelegateWrapper source) 213 { 214 foreach(index, wrapper; onActivateListeners) 215 { 216 if (wrapper.handlerId == source.handlerId) 217 { 218 onActivateListeners[index] = null; 219 onActivateListeners = std.algorithm.remove(onActivateListeners, index); 220 break; 221 } 222 } 223 } 224 } 225 OnActivateDelegateWrapper[] onActivateListeners; 226 227 /** 228 * Indicates that the action was just activated. 229 * 230 * @parameter will always be of the expected type. In the event that 231 * an incorrect type was given, no signal will be emitted. 232 * 233 * Since GLib 2.40, if no handler is connected to this signal then the 234 * default behaviour for boolean-stated actions with a %NULL parameter 235 * type is to toggle them via the #GSimpleAction::change-state signal. 236 * For stateful actions where the state type is equal to the parameter 237 * type, the default is to forward them directly to 238 * #GSimpleAction::change-state. This should allow almost all users 239 * of #GSimpleAction to connect only one handler or the other. 240 * 241 * Params: 242 * parameter = the parameter to the activation 243 * 244 * Since: 2.28 245 */ 246 gulong addOnActivate(void delegate(Variant, SimpleAction) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 247 { 248 auto wrapper = new OnActivateDelegateWrapper(dlg); 249 wrapper.handlerId = Signals.connectData( 250 this, 251 "activate", 252 cast(GCallback)&callBackActivate, 253 cast(void*)wrapper, 254 cast(GClosureNotify)&callBackActivateDestroy, 255 connectFlags); 256 return wrapper.handlerId; 257 } 258 259 extern(C) static void callBackActivate(GSimpleAction* simpleactionStruct, GVariant* parameter, OnActivateDelegateWrapper wrapper) 260 { 261 wrapper.dlg(new Variant(parameter), wrapper.outer); 262 } 263 264 extern(C) static void callBackActivateDestroy(OnActivateDelegateWrapper wrapper, GClosure* closure) 265 { 266 wrapper.remove(wrapper); 267 } 268 269 protected class OnChangeStateDelegateWrapper 270 { 271 void delegate(Variant, SimpleAction) dlg; 272 gulong handlerId; 273 274 this(void delegate(Variant, SimpleAction) dlg) 275 { 276 this.dlg = dlg; 277 onChangeStateListeners ~= this; 278 } 279 280 void remove(OnChangeStateDelegateWrapper source) 281 { 282 foreach(index, wrapper; onChangeStateListeners) 283 { 284 if (wrapper.handlerId == source.handlerId) 285 { 286 onChangeStateListeners[index] = null; 287 onChangeStateListeners = std.algorithm.remove(onChangeStateListeners, index); 288 break; 289 } 290 } 291 } 292 } 293 OnChangeStateDelegateWrapper[] onChangeStateListeners; 294 295 /** 296 * Indicates that the action just received a request to change its 297 * state. 298 * 299 * @value will always be of the correct state type. In the event that 300 * an incorrect type was given, no signal will be emitted. 301 * 302 * If no handler is connected to this signal then the default 303 * behaviour is to call g_simple_action_set_state() to set the state 304 * to the requested value. If you connect a signal handler then no 305 * default action is taken. If the state should change then you must 306 * call g_simple_action_set_state() from the handler. 307 * 308 * An example of a 'change-state' handler: 309 * |[<!-- language="C" --> 310 * static void 311 * change_volume_state (GSimpleAction *action, 312 * GVariant *value, 313 * gpointer user_data) 314 * { 315 * gint requested; 316 * 317 * requested = g_variant_get_int32 (value); 318 * 319 * // Volume only goes from 0 to 10 320 * if (0 <= requested && requested <= 10) 321 * g_simple_action_set_state (action, value); 322 * } 323 * ]| 324 * 325 * The handler need not set the state to the requested value. 326 * It could set it to any value at all, or take some other action. 327 * 328 * Params: 329 * value = the requested value for the state 330 * 331 * Since: 2.30 332 */ 333 gulong addOnChangeState(void delegate(Variant, SimpleAction) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 334 { 335 auto wrapper = new OnChangeStateDelegateWrapper(dlg); 336 wrapper.handlerId = Signals.connectData( 337 this, 338 "change-state", 339 cast(GCallback)&callBackChangeState, 340 cast(void*)wrapper, 341 cast(GClosureNotify)&callBackChangeStateDestroy, 342 connectFlags); 343 return wrapper.handlerId; 344 } 345 346 extern(C) static void callBackChangeState(GSimpleAction* simpleactionStruct, GVariant* value, OnChangeStateDelegateWrapper wrapper) 347 { 348 wrapper.dlg(new Variant(value), wrapper.outer); 349 } 350 351 extern(C) static void callBackChangeStateDestroy(OnChangeStateDelegateWrapper wrapper, GClosure* closure) 352 { 353 wrapper.remove(wrapper); 354 } 355 }