1 /*
2  * This file is part of d-handy.
3  *
4  * d-handy 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-handy 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-handy; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 module handy.ValueObject;
20 
21 private import glib.ConstructionException;
22 private import glib.Str;
23 private import gobject.ObjectG;
24 private import gobject.Value;
25 private import handy.c.functions;
26 public  import handy.c.types;
27 
28 
29 /** */
30 public class ValueObject : ObjectG
31 {
32 	/** the main Gtk struct */
33 	protected HdyValueObject* hdyValueObject;
34 
35 	/** Get the main Gtk struct */
36 	public HdyValueObject* getValueObjectStruct(bool transferOwnership = false)
37 	{
38 		if (transferOwnership)
39 			ownedRef = false;
40 		return hdyValueObject;
41 	}
42 
43 	/** the main Gtk struct as a void* */
44 	protected override void* getStruct()
45 	{
46 		return cast(void*)hdyValueObject;
47 	}
48 
49 	/**
50 	 * Sets our main struct and passes it to the parent class.
51 	 */
52 	public this (HdyValueObject* hdyValueObject, bool ownedRef = false)
53 	{
54 		this.hdyValueObject = hdyValueObject;
55 		super(cast(GObject*)hdyValueObject, ownedRef);
56 	}
57 
58 
59 	/** */
60 	public static GType getType()
61 	{
62 		return hdy_value_object_get_type();
63 	}
64 
65 	/**
66 	 * Create a new #HdyValueObject.
67 	 *
68 	 * Params:
69 	 *     value = the #GValue to store
70 	 *
71 	 * Returns: a new #HdyValueObject
72 	 *
73 	 * Since: 0.0.8
74 	 *
75 	 * Throws: ConstructionException GTK+ fails to create the object.
76 	 */
77 	public this(Value value)
78 	{
79 		auto p = hdy_value_object_new((value is null) ? null : value.getValueStruct());
80 
81 		if(p is null)
82 		{
83 			throw new ConstructionException("null returned by new");
84 		}
85 
86 		this(cast(HdyValueObject*) p, true);
87 	}
88 
89 	/**
90 	 * Creates a new #HdyValueObject. This is a convenience method to create a
91 	 * #HdyValueObject that stores a string.
92 	 *
93 	 * Params:
94 	 *     string_ = the string to store
95 	 *
96 	 * Returns: a new #HdyValueObject
97 	 *
98 	 * Since: 0.0.8
99 	 *
100 	 * Throws: ConstructionException GTK+ fails to create the object.
101 	 */
102 	public this(string string_)
103 	{
104 		auto p = hdy_value_object_new_string(Str.toStringz(string_));
105 
106 		if(p is null)
107 		{
108 			throw new ConstructionException("null returned by new_string");
109 		}
110 
111 		this(cast(HdyValueObject*) p, true);
112 	}
113 
114 	/**
115 	 * Creates a new #HdyValueObject. This is a convenience method to create a
116 	 * #HdyValueObject that stores a string taking ownership of it.
117 	 *
118 	 * Params:
119 	 *     string_ = the string to store
120 	 *
121 	 * Returns: a new #HdyValueObject
122 	 *
123 	 * Since: 0.0.8
124 	 *
125 	 * Throws: ConstructionException GTK+ fails to create the object.
126 	 */
127 	public this(string string_)
128 	{
129 		auto p = hdy_value_object_new_take_string(Str.toStringz(string_));
130 
131 		if(p is null)
132 		{
133 			throw new ConstructionException("null returned by new_take_string");
134 		}
135 
136 		this(cast(HdyValueObject*) p, true);
137 	}
138 
139 	/**
140 	 * Copy data from the contained #GValue into @dest.
141 	 *
142 	 * Params:
143 	 *     dest = #GValue with correct type to copy into
144 	 *
145 	 * Since: 0.0.8
146 	 */
147 	public void copyValue(Value dest)
148 	{
149 		hdy_value_object_copy_value(hdyValueObject, (dest is null) ? null : dest.getValueStruct());
150 	}
151 
152 	/**
153 	 * Returns a copy of the contained string if the value is of type
154 	 * #G_TYPE_STRING.
155 	 *
156 	 * Returns: a copy of the contained string
157 	 *
158 	 * Since: 0.0.8
159 	 */
160 	public string dupString()
161 	{
162 		auto retStr = hdy_value_object_dup_string(hdyValueObject);
163 
164 		scope(exit) Str.freeString(retStr);
165 		return Str.toString(retStr);
166 	}
167 
168 	/**
169 	 * Returns the contained string if the value is of type #G_TYPE_STRING.
170 	 *
171 	 * Returns: the contained string
172 	 *
173 	 * Since: 0.0.8
174 	 */
175 	public string getString()
176 	{
177 		return Str.toString(hdy_value_object_get_string(hdyValueObject));
178 	}
179 
180 	/**
181 	 * Return the contained value.
182 	 *
183 	 * Returns: the contained #GValue
184 	 *
185 	 * Since: 0.0.8
186 	 */
187 	public Value getValue()
188 	{
189 		auto p = hdy_value_object_get_value(hdyValueObject);
190 
191 		if(p is null)
192 		{
193 			return null;
194 		}
195 
196 		return ObjectG.getDObject!(Value)(cast(GValue*) p);
197 	}
198 }