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 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 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 int[string] connectedSignals; 190 191 void delegate(Variant, SimpleAction)[] onActivateListeners; 192 /** 193 * Indicates that the action was just activated. 194 * 195 * @parameter will always be of the expected type. In the event that 196 * an incorrect type was given, no signal will be emitted. 197 * 198 * Since GLib 2.40, if no handler is connected to this signal then the 199 * default behaviour for boolean-stated actions with a %NULL parameter 200 * type is to toggle them via the #GSimpleAction::change-state signal. 201 * For stateful actions where the state type is equal to the parameter 202 * type, the default is to forward them directly to 203 * #GSimpleAction::change-state. This should allow almost all users 204 * of #GSimpleAction to connect only one handler or the other. 205 * 206 * Params: 207 * parameter = the parameter to the activation 208 * 209 * Since: 2.28 210 */ 211 void addOnActivate(void delegate(Variant, SimpleAction) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 212 { 213 if ( "activate" !in connectedSignals ) 214 { 215 Signals.connectData( 216 this, 217 "activate", 218 cast(GCallback)&callBackActivate, 219 cast(void*)this, 220 null, 221 connectFlags); 222 connectedSignals["activate"] = 1; 223 } 224 onActivateListeners ~= dlg; 225 } 226 extern(C) static void callBackActivate(GSimpleAction* simpleactionStruct, GVariant* parameter, SimpleAction _simpleaction) 227 { 228 foreach ( void delegate(Variant, SimpleAction) dlg; _simpleaction.onActivateListeners ) 229 { 230 dlg(new Variant(parameter), _simpleaction); 231 } 232 } 233 234 void delegate(Variant, SimpleAction)[] onChangeStateListeners; 235 /** 236 * Indicates that the action just received a request to change its 237 * state. 238 * 239 * @value will always be of the correct state type. In the event that 240 * an incorrect type was given, no signal will be emitted. 241 * 242 * If no handler is connected to this signal then the default 243 * behaviour is to call g_simple_action_set_state() to set the state 244 * to the requested value. If you connect a signal handler then no 245 * default action is taken. If the state should change then you must 246 * call g_simple_action_set_state() from the handler. 247 * 248 * An example of a 'change-state' handler: 249 * |[<!-- language="C" --> 250 * static void 251 * change_volume_state (GSimpleAction *action, 252 * GVariant *value, 253 * gpointer user_data) 254 * { 255 * gint requested; 256 * 257 * requested = g_variant_get_int32 (value); 258 * 259 * // Volume only goes from 0 to 10 260 * if (0 <= requested && requested <= 10) 261 * g_simple_action_set_state (action, value); 262 * } 263 * ]| 264 * 265 * The handler need not set the state to the requested value. 266 * It could set it to any value at all, or take some other action. 267 * 268 * Params: 269 * value = the requested value for the state 270 * 271 * Since: 2.30 272 */ 273 void addOnChangeState(void delegate(Variant, SimpleAction) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 274 { 275 if ( "change-state" !in connectedSignals ) 276 { 277 Signals.connectData( 278 this, 279 "change-state", 280 cast(GCallback)&callBackChangeState, 281 cast(void*)this, 282 null, 283 connectFlags); 284 connectedSignals["change-state"] = 1; 285 } 286 onChangeStateListeners ~= dlg; 287 } 288 extern(C) static void callBackChangeState(GSimpleAction* simpleactionStruct, GVariant* value, SimpleAction _simpleaction) 289 { 290 foreach ( void delegate(Variant, SimpleAction) dlg; _simpleaction.onChangeStateListeners ) 291 { 292 dlg(new Variant(value), _simpleaction); 293 } 294 } 295 }