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 gtk.IconTheme;
26 
27 private import gdk.Display;
28 private import gio.IconIF;
29 private import glib.ConstructionException;
30 private import glib.Str;
31 private import glib.c.functions;
32 private import gobject.ObjectG;
33 private import gobject.Signals;
34 private import gtk.IconPaintable;
35 private import gtk.c.functions;
36 public  import gtk.c.types;
37 private import std.algorithm;
38 
39 
40 /**
41  * `GtkIconTheme` provides a facility for loading themed icons.
42  * 
43  * The main reason for using a name rather than simply providing a filename
44  * is to allow different icons to be used depending on what “icon theme” is
45  * selected by the user. The operation of icon themes on Linux and Unix
46  * follows the [Icon Theme Specification](http://www.freedesktop.org/Standards/icon-theme-spec)
47  * There is a fallback icon theme, named `hicolor`, where applications
48  * should install their icons, but additional icon themes can be installed
49  * as operating system vendors and users choose.
50  * 
51  * In many cases, named themes are used indirectly, via [class@Gtk.Image]
52  * rather than directly, but looking up icons directly is also simple. The
53  * `GtkIconTheme` object acts as a database of all the icons in the current
54  * theme. You can create new `GtkIconTheme` objects, but it’s much more
55  * efficient to use the standard icon theme of the `GtkWidget` so that the
56  * icon information is shared with other people looking up icons.
57  * 
58  * ```c
59  * GtkIconTheme *icon_theme;
60  * GtkIconPaintable *icon;
61  * GdkPaintable *paintable;
62  * 
63  * icon_theme = gtk_icon_theme_get_for_display (gtk_widget_get_display (my_widget));
64  * icon = gtk_icon_theme_lookup_icon (icon_theme,
65  * "my-icon-name", // icon name
66  * 48, // icon size
67  * 1,  // scale
68  * 0,  // flags);
69  * paintable = GDK_PAINTABLE (icon);
70  * // Use the paintable
71  * g_object_unref (icon);
72  * ```
73  */
74 public class IconTheme : ObjectG
75 {
76 	/** the main Gtk struct */
77 	protected GtkIconTheme* gtkIconTheme;
78 
79 	/** Get the main Gtk struct */
80 	public GtkIconTheme* getIconThemeStruct(bool transferOwnership = false)
81 	{
82 		if (transferOwnership)
83 			ownedRef = false;
84 		return gtkIconTheme;
85 	}
86 
87 	/** the main Gtk struct as a void* */
88 	protected override void* getStruct()
89 	{
90 		return cast(void*)gtkIconTheme;
91 	}
92 
93 	/**
94 	 * Sets our main struct and passes it to the parent class.
95 	 */
96 	public this (GtkIconTheme* gtkIconTheme, bool ownedRef = false)
97 	{
98 		this.gtkIconTheme = gtkIconTheme;
99 		super(cast(GObject*)gtkIconTheme, ownedRef);
100 	}
101 
102 
103 	/** */
104 	public static GType getType()
105 	{
106 		return gtk_icon_theme_get_type();
107 	}
108 
109 	/**
110 	 * Creates a new icon theme object.
111 	 *
112 	 * Icon theme objects are used to lookup up an icon by name
113 	 * in a particular icon theme. Usually, you’ll want to use
114 	 * [func@Gtk.IconTheme.get_for_display] rather than creating
115 	 * a new icon theme object for scratch.
116 	 *
117 	 * Returns: the newly created `GtkIconTheme` object.
118 	 *
119 	 * Throws: ConstructionException GTK+ fails to create the object.
120 	 */
121 	public this()
122 	{
123 		auto __p = gtk_icon_theme_new();
124 
125 		if(__p is null)
126 		{
127 			throw new ConstructionException("null returned by new");
128 		}
129 
130 		this(cast(GtkIconTheme*) __p, true);
131 	}
132 
133 	/**
134 	 * Gets the icon theme object associated with @display.
135 	 *
136 	 * If this function has not previously been called for the given
137 	 * display, a new icon theme object will be created and associated
138 	 * with the display. Icon theme objects are fairly expensive to create,
139 	 * so using this function is usually a better choice than calling
140 	 * [ctor@Gtk.IconTheme.new] and setting the display yourself; by using
141 	 * this function a single icon theme object will be shared between users.
142 	 *
143 	 * Params:
144 	 *     display = a `GdkDisplay`
145 	 *
146 	 * Returns: A unique `GtkIconTheme` associated with
147 	 *     the given display. This icon theme is associated with the display
148 	 *     and can be used as long as the display is open. Do not ref or unref it.
149 	 */
150 	public static IconTheme getForDisplay(Display display)
151 	{
152 		auto __p = gtk_icon_theme_get_for_display((display is null) ? null : display.getDisplayStruct());
153 
154 		if(__p is null)
155 		{
156 			return null;
157 		}
158 
159 		return ObjectG.getDObject!(IconTheme)(cast(GtkIconTheme*) __p);
160 	}
161 
162 	/**
163 	 * Adds a resource path that will be looked at when looking
164 	 * for icons, similar to search paths.
165 	 *
166 	 * See [method@Gtk.IconTheme.set_resource_path].
167 	 *
168 	 * This function should be used to make application-specific icons
169 	 * available as part of the icon theme.
170 	 *
171 	 * Params:
172 	 *     path = a resource path
173 	 */
174 	public void addResourcePath(string path)
175 	{
176 		gtk_icon_theme_add_resource_path(gtkIconTheme, Str.toStringz(path));
177 	}
178 
179 	/**
180 	 * Appends a directory to the search path.
181 	 *
182 	 * See [method@Gtk.IconTheme.set_search_path].
183 	 *
184 	 * Params:
185 	 *     path = directory name to append to the icon path
186 	 */
187 	public void addSearchPath(string path)
188 	{
189 		gtk_icon_theme_add_search_path(gtkIconTheme, Str.toStringz(path));
190 	}
191 
192 	/**
193 	 * Returns the display that the `GtkIconTheme` object was
194 	 * created for.
195 	 *
196 	 * Returns: the display of @icon_theme
197 	 */
198 	public Display getDisplay()
199 	{
200 		auto __p = gtk_icon_theme_get_display(gtkIconTheme);
201 
202 		if(__p is null)
203 		{
204 			return null;
205 		}
206 
207 		return ObjectG.getDObject!(Display)(cast(GdkDisplay*) __p);
208 	}
209 
210 	/**
211 	 * Lists the names of icons in the current icon theme.
212 	 *
213 	 * Returns: a string array
214 	 *     holding the names of all the icons in the theme. You must
215 	 *     free the array using g_strfreev().
216 	 */
217 	public string[] getIconNames()
218 	{
219 		auto retStr = gtk_icon_theme_get_icon_names(gtkIconTheme);
220 
221 		scope(exit) Str.freeStringArray(retStr);
222 		return Str.toStringArray(retStr);
223 	}
224 
225 	/**
226 	 * Returns an array of integers describing the sizes at which
227 	 * the icon is available without scaling.
228 	 *
229 	 * A size of -1 means that the icon is available in a scalable
230 	 * format. The array is zero-terminated.
231 	 *
232 	 * Params:
233 	 *     iconName = the name of an icon
234 	 *
235 	 * Returns: A newly
236 	 *     allocated array describing the sizes at which the icon is
237 	 *     available. The array should be freed with g_free() when it is no
238 	 *     longer needed.
239 	 */
240 	public int[] getIconSizes(string iconName)
241 	{
242 		auto __p = gtk_icon_theme_get_icon_sizes(gtkIconTheme, Str.toStringz(iconName));
243 
244 		return __p[0 .. getArrayLength(__p)];
245 	}
246 
247 	/**
248 	 * Gets the current resource path.
249 	 *
250 	 * See [method@Gtk.IconTheme.set_resource_path].
251 	 *
252 	 * Returns: A list of resource paths or %NULL.
253 	 *     The returned value should be freed with g_strfreev().
254 	 */
255 	public string[] getResourcePath()
256 	{
257 		auto retStr = gtk_icon_theme_get_resource_path(gtkIconTheme);
258 
259 		scope(exit) Str.freeStringArray(retStr);
260 		return Str.toStringArray(retStr);
261 	}
262 
263 	/**
264 	 * Gets the current search path.
265 	 *
266 	 * See [method@Gtk.IconTheme.set_search_path].
267 	 *
268 	 * Returns: a list of icon theme path directories or %NULL.
269 	 *     The returned value should be freed with g_strfreev().
270 	 */
271 	public string[] getSearchPath()
272 	{
273 		auto retStr = gtk_icon_theme_get_search_path(gtkIconTheme);
274 
275 		scope(exit) Str.freeStringArray(retStr);
276 		return Str.toStringArray(retStr);
277 	}
278 
279 	/**
280 	 * Gets the current icon theme name.
281 	 *
282 	 * Returns (transfer full): the current icon theme name,
283 	 */
284 	public string getThemeName()
285 	{
286 		auto retStr = gtk_icon_theme_get_theme_name(gtkIconTheme);
287 
288 		scope(exit) Str.freeString(retStr);
289 		return Str.toString(retStr);
290 	}
291 
292 	/**
293 	 * Checks whether an icon theme includes an icon
294 	 * for a particular `GIcon`.
295 	 *
296 	 * Params:
297 	 *     gicon = a `GIcon`
298 	 *
299 	 * Returns: %TRUE if @self includes an icon for @gicon
300 	 */
301 	public bool hasGicon(IconIF gicon)
302 	{
303 		return gtk_icon_theme_has_gicon(gtkIconTheme, (gicon is null) ? null : gicon.getIconStruct()) != 0;
304 	}
305 
306 	/**
307 	 * Checks whether an icon theme includes an icon
308 	 * for a particular name.
309 	 *
310 	 * Params:
311 	 *     iconName = the name of an icon
312 	 *
313 	 * Returns: %TRUE if @self includes an
314 	 *     icon for @icon_name.
315 	 */
316 	public bool hasIcon(string iconName)
317 	{
318 		return gtk_icon_theme_has_icon(gtkIconTheme, Str.toStringz(iconName)) != 0;
319 	}
320 
321 	/**
322 	 * Looks up a icon for a desired size and window scale.
323 	 *
324 	 * The icon can then be rendered by using it as a `GdkPaintable`,
325 	 * or you can get information such as the filename and size.
326 	 *
327 	 * Params:
328 	 *     icon = the `GIcon` to look up
329 	 *     size = desired icon size
330 	 *     scale = the desired scale
331 	 *     direction = text direction the icon will be displayed in
332 	 *     flags = flags modifying the behavior of the icon lookup
333 	 *
334 	 * Returns: a `GtkIconPaintable` containing
335 	 *     information about the icon. Unref with g_object_unref()
336 	 */
337 	public IconPaintable lookupByGicon(IconIF icon, int size, int scale, GtkTextDirection direction, GtkIconLookupFlags flags)
338 	{
339 		auto __p = gtk_icon_theme_lookup_by_gicon(gtkIconTheme, (icon is null) ? null : icon.getIconStruct(), size, scale, direction, flags);
340 
341 		if(__p is null)
342 		{
343 			return null;
344 		}
345 
346 		return ObjectG.getDObject!(IconPaintable)(cast(GtkIconPaintable*) __p, true);
347 	}
348 
349 	/**
350 	 * Looks up a named icon for a desired size and window scale,
351 	 * returning a `GtkIconPaintable`.
352 	 *
353 	 * The icon can then be rendered by using it as a `GdkPaintable`,
354 	 * or you can get information such as the filename and size.
355 	 *
356 	 * If the available @icon_name is not available and @fallbacks are
357 	 * provided, they will be tried in order.
358 	 *
359 	 * If no matching icon is found, then a paintable that renders the
360 	 * "missing icon" icon is returned. If you need to do something else
361 	 * for missing icons you need to use [method@Gtk.IconTheme.has_icon].
362 	 *
363 	 * Note that you probably want to listen for icon theme changes and
364 	 * update the icon. This is usually done by overriding the
365 	 * GtkWidgetClass.css-changed() function.
366 	 *
367 	 * Params:
368 	 *     iconName = the name of the icon to lookup
369 	 *     size = desired icon size.
370 	 *     scale = the window scale this will be displayed on
371 	 *     direction = text direction the icon will be displayed in
372 	 *     flags = flags modifying the behavior of the icon lookup
373 	 *
374 	 * Returns: a `GtkIconPaintable` object
375 	 *     containing the icon.
376 	 */
377 	public IconPaintable lookupIcon(string iconName, string[] fallbacks, int size, int scale, GtkTextDirection direction, GtkIconLookupFlags flags)
378 	{
379 		auto __p = gtk_icon_theme_lookup_icon(gtkIconTheme, Str.toStringz(iconName), Str.toStringzArray(fallbacks), size, scale, direction, flags);
380 
381 		if(__p is null)
382 		{
383 			return null;
384 		}
385 
386 		return ObjectG.getDObject!(IconPaintable)(cast(GtkIconPaintable*) __p, true);
387 	}
388 
389 	/**
390 	 * Sets the resource paths that will be looked at when
391 	 * looking for icons, similar to search paths.
392 	 *
393 	 * The resources are considered as part of the hicolor icon theme
394 	 * and must be located in subdirectories that are defined in the
395 	 * hicolor icon theme, such as `@path/16x16/actions/run.png`
396 	 * or `@path/scalable/actions/run.svg`.
397 	 *
398 	 * Icons that are directly placed in the resource path instead
399 	 * of a subdirectory are also considered as ultimate fallback,
400 	 * but they are treated like unthemed icons.
401 	 *
402 	 * Params:
403 	 *     path = NULL-terminated array of resource paths
404 	 *         that are searched for icons
405 	 */
406 	public void setResourcePath(string path)
407 	{
408 		gtk_icon_theme_set_resource_path(gtkIconTheme, Str.toStringz(path));
409 	}
410 
411 	/**
412 	 * Sets the search path for the icon theme object.
413 	 *
414 	 * When looking for an icon theme, GTK will search for a subdirectory
415 	 * of one or more of the directories in @path with the same name
416 	 * as the icon theme containing an index.theme file. (Themes from
417 	 * multiple of the path elements are combined to allow themes to be
418 	 * extended by adding icons in the user’s home directory.)
419 	 *
420 	 * In addition if an icon found isn’t found either in the current
421 	 * icon theme or the default icon theme, and an image file with
422 	 * the right name is found directly in one of the elements of
423 	 * @path, then that image will be used for the icon name.
424 	 * (This is legacy feature, and new icons should be put
425 	 * into the fallback icon theme, which is called hicolor,
426 	 * rather than directly on the icon path.)
427 	 *
428 	 * Params:
429 	 *     path = NULL-terminated
430 	 *         array of directories that are searched for icon themes
431 	 */
432 	public void setSearchPath(string[] path)
433 	{
434 		gtk_icon_theme_set_search_path(gtkIconTheme, Str.toStringzArray(path));
435 	}
436 
437 	/**
438 	 * Sets the name of the icon theme that the `GtkIconTheme` object uses
439 	 * overriding system configuration.
440 	 *
441 	 * This function cannot be called on the icon theme objects returned
442 	 * from [type_func@Gtk.IconTheme.get_for_display].
443 	 *
444 	 * Params:
445 	 *     themeName = name of icon theme to use instead of
446 	 *         configured theme, or %NULL to unset a previously set custom theme
447 	 */
448 	public void setThemeName(string themeName)
449 	{
450 		gtk_icon_theme_set_theme_name(gtkIconTheme, Str.toStringz(themeName));
451 	}
452 
453 	/**
454 	 * Emitted when the icon theme changes.
455 	 *
456 	 * This can happen becuase current icon theme is switched or
457 	 * because GTK detects that a change has occurred in the
458 	 * contents of the current icon theme.
459 	 */
460 	gulong addOnChanged(void delegate(IconTheme) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
461 	{
462 		return Signals.connect(this, "changed", dlg, connectFlags ^ ConnectFlags.SWAPPED);
463 	}
464 }