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