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.Avatar;
20 
21 private import glib.ConstructionException;
22 private import glib.Str;
23 private import gobject.ObjectG;
24 private import gtk.BuildableIF;
25 private import gtk.BuildableT;
26 private import gtk.DrawingArea;
27 private import gtk.Widget;
28 private import handy.c.functions;
29 public  import handy.c.types;
30 
31 
32 /** */
33 public class Avatar : DrawingArea
34 {
35 	/** the main Gtk struct */
36 	protected HdyAvatar* hdyAvatar;
37 
38 	/** Get the main Gtk struct */
39 	public HdyAvatar* getAvatarStruct(bool transferOwnership = false)
40 	{
41 		if (transferOwnership)
42 			ownedRef = false;
43 		return hdyAvatar;
44 	}
45 
46 	/** the main Gtk struct as a void* */
47 	protected override void* getStruct()
48 	{
49 		return cast(void*)hdyAvatar;
50 	}
51 
52 	/**
53 	 * Sets our main struct and passes it to the parent class.
54 	 */
55 	public this (HdyAvatar* hdyAvatar, bool ownedRef = false)
56 	{
57 		this.hdyAvatar = hdyAvatar;
58 		super(cast(GtkDrawingArea*)hdyAvatar, ownedRef);
59 	}
60 
61 
62 	/** */
63 	public static GType getType()
64 	{
65 		return hdy_avatar_get_type();
66 	}
67 
68 	/**
69 	 * Creates a new #HdyAvatar.
70 	 *
71 	 * Params:
72 	 *     size = The size of the avatar
73 	 *     text = The text used to generate the color and initials if
74 	 *         @show_initials is %TRUE. The color is selected at random if @text is empty.
75 	 *     showInitials = whether to show the initials or the fallback icon on
76 	 *         top of the color generated based on @text.
77 	 *
78 	 * Returns: the newly created #HdyAvatar
79 	 *
80 	 * Throws: ConstructionException GTK+ fails to create the object.
81 	 */
82 	public this(int size, string text, bool showInitials)
83 	{
84 		auto __p = hdy_avatar_new(size, Str.toStringz(text), showInitials);
85 
86 		if(__p is null)
87 		{
88 			throw new ConstructionException("null returned by new");
89 		}
90 
91 		this(cast(HdyAvatar*) __p);
92 	}
93 
94 	/**
95 	 * Gets the name of the icon in the icon theme to use when the icon should be
96 	 * displayed.
97 	 *
98 	 * Returns: the name of the icon from the icon theme.
99 	 *
100 	 * Since: 1.0
101 	 */
102 	public string getIconName()
103 	{
104 		return Str.toString(hdy_avatar_get_icon_name(hdyAvatar));
105 	}
106 
107 	/**
108 	 * Returns whether initials are used for the fallback or the icon.
109 	 *
110 	 * Returns: %TRUE if the initials are used for the fallback.
111 	 */
112 	public bool getShowInitials()
113 	{
114 		return hdy_avatar_get_show_initials(hdyAvatar) != 0;
115 	}
116 
117 	/**
118 	 * Returns the size of the avatar.
119 	 *
120 	 * Returns: the size of the avatar.
121 	 */
122 	public int getSize()
123 	{
124 		return hdy_avatar_get_size(hdyAvatar);
125 	}
126 
127 	/**
128 	 * Get the text used to generate the fallback initials and color
129 	 *
130 	 * Returns: returns the text used to generate
131 	 *     the fallback initials. This is the internal string used by
132 	 *     the #HdyAvatar, and must not be modified.
133 	 */
134 	public string getText()
135 	{
136 		return Str.toString(hdy_avatar_get_text(hdyAvatar));
137 	}
138 
139 	/**
140 	 * Sets the name of the icon in the icon theme to use when the icon should be
141 	 * displayed.
142 	 * If no name is set, the avatar-default-symbolic icon will be used.
143 	 * If the name doesn't match a valid icon, it is an error and no icon will be
144 	 * displayed.
145 	 * If the icon theme is changed, the image will be updated automatically.
146 	 *
147 	 * Params:
148 	 *     iconName = the name of the icon from the icon theme
149 	 *
150 	 * Since: 1.0
151 	 */
152 	public void setIconName(string iconName)
153 	{
154 		hdy_avatar_set_icon_name(hdyAvatar, Str.toStringz(iconName));
155 	}
156 
157 	/**
158 	 * A callback which is called when the custom image need to be reloaded for some
159 	 * reason (e.g. scale-factor changes).
160 	 *
161 	 * Params:
162 	 *     loadImage = callback to set a custom image
163 	 *     userData = user data passed to @load_image
164 	 *     destroy = destroy notifier for @user_data
165 	 */
166 	public void setImageLoadFunc(HdyAvatarImageLoadFunc loadImage, void* userData, GDestroyNotify destroy)
167 	{
168 		hdy_avatar_set_image_load_func(hdyAvatar, loadImage, userData, destroy);
169 	}
170 
171 	/**
172 	 * Sets whether the initials should be shown on the fallback avatar or the icon.
173 	 *
174 	 * Params:
175 	 *     showInitials = whether the initials should be shown on the fallback avatar
176 	 *         or the icon.
177 	 */
178 	public void setShowInitials(bool showInitials)
179 	{
180 		hdy_avatar_set_show_initials(hdyAvatar, showInitials);
181 	}
182 
183 	/**
184 	 * Sets the size of the avatar.
185 	 *
186 	 * Params:
187 	 *     size = The size to be used for the avatar
188 	 */
189 	public void setSize(int size)
190 	{
191 		hdy_avatar_set_size(hdyAvatar, size);
192 	}
193 
194 	/**
195 	 * Set the text used to generate the fallback initials color
196 	 *
197 	 * Params:
198 	 *     text = the text used to get the initials and color
199 	 */
200 	public void setText(string text)
201 	{
202 		hdy_avatar_set_text(hdyAvatar, Str.toStringz(text));
203 	}
204 }