1 /*
2  * This file is part of d-dazzle.
3  *
4  * d-dazzle 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  * d-dazzle 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 d-dazzle; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 module dazzle.StateMachine;
20 
21 private import dazzle.c.functions;
22 public  import dazzle.c.types;
23 private import gio.ActionIF;
24 private import glib.ConstructionException;
25 private import glib.Str;
26 private import gobject.ObjectG;
27 private import gobject.Value;
28 private import gtk.BuildableIF;
29 private import gtk.BuildableT;
30 private import gtk.Widget;
31 
32 
33 /** */
34 public class StateMachine : ObjectG, BuildableIF
35 {
36 	/** the main Gtk struct */
37 	protected DzlStateMachine* dzlStateMachine;
38 
39 	/** Get the main Gtk struct */
40 	public DzlStateMachine* getStateMachineStruct(bool transferOwnership = false)
41 	{
42 		if (transferOwnership)
43 			ownedRef = false;
44 		return dzlStateMachine;
45 	}
46 
47 	/** the main Gtk struct as a void* */
48 	protected override void* getStruct()
49 	{
50 		return cast(void*)dzlStateMachine;
51 	}
52 
53 	/**
54 	 * Sets our main struct and passes it to the parent class.
55 	 */
56 	public this (DzlStateMachine* dzlStateMachine, bool ownedRef = false)
57 	{
58 		this.dzlStateMachine = dzlStateMachine;
59 		super(cast(GObject*)dzlStateMachine, ownedRef);
60 	}
61 
62 	// add the Buildable capabilities
63 	mixin BuildableT!(DzlStateMachine);
64 
65 
66 	/** */
67 	public static GType getType()
68 	{
69 		return dzl_state_machine_get_type();
70 	}
71 
72 	/** */
73 	public this()
74 	{
75 		auto p = dzl_state_machine_new();
76 
77 		if(p is null)
78 		{
79 			throw new ConstructionException("null returned by new");
80 		}
81 
82 		this(cast(DzlStateMachine*) p, true);
83 	}
84 
85 	/** */
86 	public static void buildableIfaceInit(GtkBuildableIface* iface)
87 	{
88 		dzl_state_machine_buildable_iface_init(iface);
89 	}
90 
91 	/** */
92 	public void addBinding(string state, void* sourceObject, string sourceProperty, void* targetObject, string targetProperty, GBindingFlags flags)
93 	{
94 		dzl_state_machine_add_binding(dzlStateMachine, Str.toStringz(state), sourceObject, Str.toStringz(sourceProperty), targetObject, Str.toStringz(targetProperty), flags);
95 	}
96 
97 	/** */
98 	public void addPropertyValist(string state, void* object, string property, void* varArgs)
99 	{
100 		dzl_state_machine_add_property_valist(dzlStateMachine, Str.toStringz(state), object, Str.toStringz(property), varArgs);
101 	}
102 
103 	/** */
104 	public void addPropertyv(string state, void* object, string property, Value value)
105 	{
106 		dzl_state_machine_add_propertyv(dzlStateMachine, Str.toStringz(state), object, Str.toStringz(property), (value is null) ? null : value.getValueStruct());
107 	}
108 
109 	/** */
110 	public void addStyle(string state, Widget widget, string style)
111 	{
112 		dzl_state_machine_add_style(dzlStateMachine, Str.toStringz(state), (widget is null) ? null : widget.getWidgetStruct(), Str.toStringz(style));
113 	}
114 
115 	/**
116 	 * Connects to the @detailed_signal of @source only when the current
117 	 * state of the state machine is @state.
118 	 *
119 	 * Params:
120 	 *     state = The state the signal connection should exist within
121 	 *     source = the source object to connect to
122 	 *     detailedSignal = The detailed signal of @source to connect.
123 	 *     callback = The callback to execute upon signal emission.
124 	 *     userData = The user data for @callback.
125 	 *     flags = signal connection flags.
126 	 */
127 	public void connectObject(string state, void* source, string detailedSignal, GCallback callback, void* userData, GConnectFlags flags)
128 	{
129 		dzl_state_machine_connect_object(dzlStateMachine, Str.toStringz(state), source, Str.toStringz(detailedSignal), callback, userData, flags);
130 	}
131 
132 	/**
133 	 * Creates a new #GAction with the name of @name.
134 	 *
135 	 * Setting the state of this action will toggle the state of the state machine.
136 	 * You should use g_variant_new_string() or similar to create the state.
137 	 *
138 	 * Params:
139 	 *     name = the name of the action.
140 	 *
141 	 * Returns: A newly created #GAction.
142 	 */
143 	public ActionIF createAction(string name)
144 	{
145 		auto p = dzl_state_machine_create_action(dzlStateMachine, Str.toStringz(name));
146 
147 		if(p is null)
148 		{
149 			return null;
150 		}
151 
152 		return ObjectG.getDObject!(ActionIF)(cast(GAction*) p, true);
153 	}
154 
155 	/**
156 	 * Gets the #DzlStateMachine:state property. This is the name of the
157 	 * current state of the machine.
158 	 *
159 	 * Returns: The current state of the machine.
160 	 */
161 	public string getState()
162 	{
163 		return Str.toString(dzl_state_machine_get_state(dzlStateMachine));
164 	}
165 
166 	/**
167 	 * Checks to see if the current state of the #DzlStateMachine matches @state.
168 	 *
169 	 * Params:
170 	 *     state = the name of the state to check
171 	 *
172 	 * Returns: %TRUE if @self is currently set to @state.
173 	 *
174 	 * Since: 3.28
175 	 */
176 	public bool isState(string state)
177 	{
178 		return dzl_state_machine_is_state(dzlStateMachine, Str.toStringz(state)) != 0;
179 	}
180 
181 	/**
182 	 * Sets the #DzlStateMachine:state property.
183 	 *
184 	 * Registered state transformations will be applied during the state
185 	 * transformation.
186 	 *
187 	 * If the transition results in a cyclic operation, the state will stop at
188 	 * the last state before the cycle was detected.
189 	 */
190 	public void setState(string state)
191 	{
192 		dzl_state_machine_set_state(dzlStateMachine, Str.toStringz(state));
193 	}
194 }