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