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