Skip to main content

gtk4/auto/
icon_theme.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{IconLookupFlags, IconPaintable, TextDirection, ffi};
6use glib::{
7    object::ObjectType as _,
8    prelude::*,
9    signal::{SignalHandlerId, connect_raw},
10    translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15    /// s much more
16    /// efficient to use the standard icon theme of the [`Widget`][crate::Widget] so that the
17    /// icon information is shared with other people looking up icons.
18    ///
19    /// **⚠️ The following code is in c ⚠️**
20    ///
21    /// ```c
22    /// GtkIconTheme *icon_theme;
23    /// GtkIconPaintable *icon;
24    /// GdkPaintable *paintable;
25    ///
26    /// icon_theme = gtk_icon_theme_get_for_display (gtk_widget_get_display (my_widget));
27    /// icon = gtk_icon_theme_lookup_icon (icon_theme,
28    ///                                    "my-icon-name", // icon name
29    ///                                    48, // icon size
30    ///                                    1,  // scale
31    ///                                    0,  // flags);
32    /// paintable = GDK_PAINTABLE (icon);
33    /// // Use the paintable
34    /// g_object_unref (icon);
35    /// ```
36    ///
37    /// ## Properties
38    ///
39    ///
40    /// #### `display`
41    ///  The display that this icon theme object is attached to.
42    ///
43    /// Readable | Writable
44    ///
45    ///
46    /// #### `icon-names`
47    ///  The icon names that are supported by the icon theme.
48    ///
49    /// Readable
50    ///
51    ///
52    /// #### `resource-path`
53    ///  Resource paths that will be looked at when looking for icons,
54    /// similar to search paths.
55    ///
56    /// The resources are considered as part of the hicolor icon theme
57    /// and must be located in subdirectories that are defined in the
58    /// hicolor icon theme, such as `@path/16x16/actions/run.png`.
59    /// Icons that are directly placed in the resource path instead
60    /// of a subdirectory are also considered as ultimate fallback.
61    ///
62    /// Readable | Writable
63    ///
64    ///
65    /// #### `search-path`
66    ///  s home directory.)
67    ///
68    /// Readable | Writable
69    ///
70    ///
71    /// #### `theme-name`
72    ///  The name of the icon theme that is being used.
73    ///
74    /// Unless set to a different value, this will be the value of
75    /// the `GtkSettings:gtk-icon-theme-name` property of the [`Settings`][crate::Settings]
76    /// object associated to the display of the icontheme object.
77    ///
78    /// Readable | Writable
79    ///
80    /// ## Signals
81    ///
82    ///
83    /// #### `changed`
84    ///  Emitted when the icon theme changes.
85    ///
86    /// This can happen because current icon theme is switched or
87    /// because GTK detects that a change has occurred in the
88    /// contents of the current icon theme.
89    ///
90    ///
91    ///
92    /// # Implements
93    ///
94    /// [`trait@glib::ObjectExt`]
95    #[doc(alias = "GtkIconTheme")]
96    pub struct IconTheme(Object<ffi::GtkIconTheme>);
97
98    match fn {
99        type_ => || ffi::gtk_icon_theme_get_type(),
100    }
101}
102
103impl IconTheme {
104    /// ll want to use
105    /// [`for_display()`][Self::for_display()] rather than creating
106    /// a new icon theme object for scratch.
107    ///
108    /// # Returns
109    ///
110    /// the newly created [`IconTheme`][crate::IconTheme] object.
111    #[doc(alias = "gtk_icon_theme_new")]
112    pub fn new() -> IconTheme {
113        assert_initialized_main_thread!();
114        unsafe { from_glib_full(ffi::gtk_icon_theme_new()) }
115    }
116
117    // rustdoc-stripper-ignore-next
118    /// Creates a new builder-pattern struct instance to construct [`IconTheme`] objects.
119    ///
120    /// This method returns an instance of [`IconThemeBuilder`](crate::builders::IconThemeBuilder) which can be used to create [`IconTheme`] objects.
121    pub fn builder() -> IconThemeBuilder {
122        IconThemeBuilder::new()
123    }
124
125    /// Adds a resource path that will be looked at when looking
126    /// for icons, similar to search paths.
127    ///
128    /// See [`set_resource_path()`][Self::set_resource_path()].
129    ///
130    /// This function should be used to make application-specific icons
131    /// available as part of the icon theme.
132    /// ## `path`
133    /// a resource path
134    #[doc(alias = "gtk_icon_theme_add_resource_path")]
135    pub fn add_resource_path(&self, path: &str) {
136        unsafe {
137            ffi::gtk_icon_theme_add_resource_path(self.to_glib_none().0, path.to_glib_none().0);
138        }
139    }
140
141    /// Appends a directory to the search path.
142    ///
143    /// See [`set_search_path()`][Self::set_search_path()].
144    /// ## `path`
145    /// directory name to append to the icon path
146    #[doc(alias = "gtk_icon_theme_add_search_path")]
147    pub fn add_search_path(&self, path: impl AsRef<std::path::Path>) {
148        unsafe {
149            ffi::gtk_icon_theme_add_search_path(
150                self.to_glib_none().0,
151                path.as_ref().to_glib_none().0,
152            );
153        }
154    }
155
156    /// Returns the display that the [`IconTheme`][crate::IconTheme] object was
157    /// created for.
158    ///
159    /// # Returns
160    ///
161    /// the display of @icon_theme
162    #[doc(alias = "gtk_icon_theme_get_display")]
163    #[doc(alias = "get_display")]
164    pub fn display(&self) -> Option<gdk::Display> {
165        unsafe { from_glib_none(ffi::gtk_icon_theme_get_display(self.to_glib_none().0)) }
166    }
167
168    /// Lists the names of icons in the current icon theme.
169    ///
170    /// # Returns
171    ///
172    /// a string array
173    ///   holding the names of all the icons in the theme. You must
174    ///   free the array using g_strfreev().
175    #[doc(alias = "gtk_icon_theme_get_icon_names")]
176    #[doc(alias = "get_icon_names")]
177    #[doc(alias = "icon-names")]
178    pub fn icon_names(&self) -> Vec<glib::GString> {
179        unsafe {
180            FromGlibPtrContainer::from_glib_full(ffi::gtk_icon_theme_get_icon_names(
181                self.to_glib_none().0,
182            ))
183        }
184    }
185
186    /// Gets the current resource path.
187    ///
188    /// See [`set_resource_path()`][Self::set_resource_path()].
189    ///
190    /// # Returns
191    ///
192    ///
193    ///   A list of resource paths
194    #[doc(alias = "gtk_icon_theme_get_resource_path")]
195    #[doc(alias = "get_resource_path")]
196    #[doc(alias = "resource-path")]
197    pub fn resource_path(&self) -> Vec<glib::GString> {
198        unsafe {
199            FromGlibPtrContainer::from_glib_full(ffi::gtk_icon_theme_get_resource_path(
200                self.to_glib_none().0,
201            ))
202        }
203    }
204
205    /// Gets the current search path.
206    ///
207    /// See [`set_search_path()`][Self::set_search_path()].
208    ///
209    /// # Returns
210    ///
211    ///
212    ///   a list of icon theme path directories
213    #[doc(alias = "gtk_icon_theme_get_search_path")]
214    #[doc(alias = "get_search_path")]
215    #[doc(alias = "search-path")]
216    pub fn search_path(&self) -> Vec<std::path::PathBuf> {
217        unsafe {
218            FromGlibPtrContainer::from_glib_full(ffi::gtk_icon_theme_get_search_path(
219                self.to_glib_none().0,
220            ))
221        }
222    }
223
224    /// Gets the current icon theme name.
225    ///
226    /// # Returns
227    ///
228    /// the current icon theme name,
229    #[doc(alias = "gtk_icon_theme_get_theme_name")]
230    #[doc(alias = "get_theme_name")]
231    #[doc(alias = "theme-name")]
232    pub fn theme_name(&self) -> glib::GString {
233        unsafe { from_glib_full(ffi::gtk_icon_theme_get_theme_name(self.to_glib_none().0)) }
234    }
235
236    /// Checks whether an icon theme includes an icon
237    /// for a particular `GIcon`.
238    /// ## `gicon`
239    /// a `GIcon`
240    ///
241    /// # Returns
242    ///
243    /// [`true`] if @self includes an icon for @gicon
244    #[cfg(feature = "v4_2")]
245    #[cfg_attr(docsrs, doc(cfg(feature = "v4_2")))]
246    #[doc(alias = "gtk_icon_theme_has_gicon")]
247    pub fn has_gicon(&self, gicon: &impl IsA<gio::Icon>) -> bool {
248        unsafe {
249            from_glib(ffi::gtk_icon_theme_has_gicon(
250                self.to_glib_none().0,
251                gicon.as_ref().to_glib_none().0,
252            ))
253        }
254    }
255
256    /// Checks whether an icon theme includes an icon
257    /// for a particular name.
258    /// ## `icon_name`
259    /// the name of an icon
260    ///
261    /// # Returns
262    ///
263    /// [`true`] if @self includes an
264    ///  icon for @icon_name.
265    #[doc(alias = "gtk_icon_theme_has_icon")]
266    pub fn has_icon(&self, icon_name: &str) -> bool {
267        unsafe {
268            from_glib(ffi::gtk_icon_theme_has_icon(
269                self.to_glib_none().0,
270                icon_name.to_glib_none().0,
271            ))
272        }
273    }
274
275    /// Looks up a icon for a desired size and window scale.
276    ///
277    /// The icon can then be rendered by using it as a [`gdk::Paintable`][crate::gdk::Paintable],
278    /// or you can get information such as the filename and size.
279    /// ## `icon`
280    /// the `GIcon` to look up
281    /// ## `size`
282    /// desired icon size, in application pixels
283    /// ## `scale`
284    /// the desired scale
285    /// ## `direction`
286    /// text direction the icon will be displayed in
287    /// ## `flags`
288    /// flags modifying the behavior of the icon lookup
289    ///
290    /// # Returns
291    ///
292    /// a [`IconPaintable`][crate::IconPaintable] containing
293    ///   information about the icon. Unref with g_object_unref()
294    #[doc(alias = "gtk_icon_theme_lookup_by_gicon")]
295    pub fn lookup_by_gicon(
296        &self,
297        icon: &impl IsA<gio::Icon>,
298        size: i32,
299        scale: i32,
300        direction: TextDirection,
301        flags: IconLookupFlags,
302    ) -> IconPaintable {
303        unsafe {
304            from_glib_full(ffi::gtk_icon_theme_lookup_by_gicon(
305                self.to_glib_none().0,
306                icon.as_ref().to_glib_none().0,
307                size,
308                scale,
309                direction.into_glib(),
310                flags.into_glib(),
311            ))
312        }
313    }
314
315    /// Looks up a named icon for a desired size and window scale,
316    /// returning a [`IconPaintable`][crate::IconPaintable].
317    ///
318    /// The icon can then be rendered by using it as a [`gdk::Paintable`][crate::gdk::Paintable],
319    /// or you can get information such as the filename and size.
320    ///
321    /// If the available @icon_name is not available and @fallbacks are
322    /// provided, they will be tried in order.
323    ///
324    /// If no matching icon is found, then a paintable that renders the
325    /// "missing icon" icon is returned. If you need to do something else
326    /// for missing icons you need to use [`has_icon()`][Self::has_icon()].
327    ///
328    /// Note that you probably want to listen for icon theme changes and
329    /// update the icon. This is usually done by overriding the
330    /// GtkWidgetClass.css-changed() function.
331    /// ## `icon_name`
332    /// the name of the icon to lookup
333    /// ## `fallbacks`
334    /// fallback names
335    /// ## `size`
336    /// desired icon size, in application pixels
337    /// ## `scale`
338    /// the window scale this will be displayed on
339    /// ## `direction`
340    /// text direction the icon will be displayed in
341    /// ## `flags`
342    /// flags modifying the behavior of the icon lookup
343    ///
344    /// # Returns
345    ///
346    /// a [`IconPaintable`][crate::IconPaintable] object
347    ///   containing the icon.
348    #[doc(alias = "gtk_icon_theme_lookup_icon")]
349    pub fn lookup_icon(
350        &self,
351        icon_name: &str,
352        fallbacks: &[&str],
353        size: i32,
354        scale: i32,
355        direction: TextDirection,
356        flags: IconLookupFlags,
357    ) -> IconPaintable {
358        unsafe {
359            from_glib_full(ffi::gtk_icon_theme_lookup_icon(
360                self.to_glib_none().0,
361                icon_name.to_glib_none().0,
362                fallbacks.to_glib_none().0,
363                size,
364                scale,
365                direction.into_glib(),
366                flags.into_glib(),
367            ))
368        }
369    }
370
371    /// Sets the resource paths that will be looked at when
372    /// looking for icons, similar to search paths.
373    ///
374    /// The resources are considered as part of the hicolor icon theme
375    /// and must be located in subdirectories that are defined in the
376    /// hicolor icon theme, such as `@path/16x16/actions/run.png`
377    /// or `@path/scalable/actions/run.svg`.
378    ///
379    /// Icons that are directly placed in the resource path instead
380    /// of a subdirectory are also considered as ultimate fallback,
381    /// but they are treated like unthemed icons.
382    /// ## `path`
383    ///
384    ///   NULL-terminated array of resource paths
385    ///   that are searched for icons
386    #[doc(alias = "gtk_icon_theme_set_resource_path")]
387    #[doc(alias = "resource-path")]
388    pub fn set_resource_path(&self, path: &[&str]) {
389        unsafe {
390            ffi::gtk_icon_theme_set_resource_path(self.to_glib_none().0, path.to_glib_none().0);
391        }
392    }
393
394    /// t found either in the current
395    /// icon theme or the default icon theme, and an image file with
396    /// the right name is found directly in one of the elements of
397    /// @path, then that image will be used for the icon name.
398    /// (This is legacy feature, and new icons should be put
399    /// into the fallback icon theme, which is called hicolor,
400    /// rather than directly on the icon path.)
401    /// ## `path`
402    /// NULL-terminated
403    ///   array of directories that are searched for icon themes
404    #[doc(alias = "gtk_icon_theme_set_search_path")]
405    #[doc(alias = "search-path")]
406    pub fn set_search_path(&self, path: &[&std::path::Path]) {
407        unsafe {
408            ffi::gtk_icon_theme_set_search_path(self.to_glib_none().0, path.to_glib_none().0);
409        }
410    }
411
412    /// Sets the name of the icon theme that the [`IconTheme`][crate::IconTheme] object uses
413    /// overriding system configuration.
414    ///
415    /// This function cannot be called on the icon theme objects returned
416    /// from [`for_display()`][Self::for_display()].
417    /// ## `theme_name`
418    /// name of icon theme to use instead of
419    ///   configured theme, or [`None`] to unset a previously set custom theme
420    #[doc(alias = "gtk_icon_theme_set_theme_name")]
421    #[doc(alias = "theme-name")]
422    pub fn set_theme_name(&self, theme_name: Option<&str>) {
423        unsafe {
424            ffi::gtk_icon_theme_set_theme_name(self.to_glib_none().0, theme_name.to_glib_none().0);
425        }
426    }
427
428    /// The display that this icon theme object is attached to.
429    pub fn set_display<P: IsA<gdk::Display>>(&self, display: Option<&P>) {
430        ObjectExt::set_property(self, "display", display)
431    }
432
433    /// Gets the icon theme object associated with @display.
434    ///
435    /// If this function has not previously been called for the given
436    /// display, a new icon theme object will be created and associated
437    /// with the display. Icon theme objects are fairly expensive to create,
438    /// so using this function is usually a better choice than calling
439    /// [`new()`][Self::new()] and setting the display yourself; by using
440    /// this function a single icon theme object will be shared between users.
441    /// ## `display`
442    /// a [`gdk::Display`][crate::gdk::Display]
443    ///
444    /// # Returns
445    ///
446    /// A unique [`IconTheme`][crate::IconTheme] associated with
447    ///   the given display. This icon theme is associated with the display
448    ///   and can be used as long as the display is open. Do not ref or unref it.
449    #[doc(alias = "gtk_icon_theme_get_for_display")]
450    #[doc(alias = "get_for_display")]
451    pub fn for_display(display: &impl IsA<gdk::Display>) -> IconTheme {
452        assert_initialized_main_thread!();
453        unsafe {
454            from_glib_none(ffi::gtk_icon_theme_get_for_display(
455                display.as_ref().to_glib_none().0,
456            ))
457        }
458    }
459
460    /// Emitted when the icon theme changes.
461    ///
462    /// This can happen because current icon theme is switched or
463    /// because GTK detects that a change has occurred in the
464    /// contents of the current icon theme.
465    #[doc(alias = "changed")]
466    pub fn connect_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
467        unsafe extern "C" fn changed_trampoline<F: Fn(&IconTheme) + 'static>(
468            this: *mut ffi::GtkIconTheme,
469            f: glib::ffi::gpointer,
470        ) {
471            unsafe {
472                let f: &F = &*(f as *const F);
473                f(&from_glib_borrow(this))
474            }
475        }
476        unsafe {
477            let f: Box_<F> = Box_::new(f);
478            connect_raw(
479                self.as_ptr() as *mut _,
480                c"changed".as_ptr(),
481                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
482                    changed_trampoline::<F> as *const (),
483                )),
484                Box_::into_raw(f),
485            )
486        }
487    }
488
489    #[doc(alias = "display")]
490    pub fn connect_display_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
491        unsafe extern "C" fn notify_display_trampoline<F: Fn(&IconTheme) + 'static>(
492            this: *mut ffi::GtkIconTheme,
493            _param_spec: glib::ffi::gpointer,
494            f: glib::ffi::gpointer,
495        ) {
496            unsafe {
497                let f: &F = &*(f as *const F);
498                f(&from_glib_borrow(this))
499            }
500        }
501        unsafe {
502            let f: Box_<F> = Box_::new(f);
503            connect_raw(
504                self.as_ptr() as *mut _,
505                c"notify::display".as_ptr(),
506                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
507                    notify_display_trampoline::<F> as *const (),
508                )),
509                Box_::into_raw(f),
510            )
511        }
512    }
513
514    #[doc(alias = "icon-names")]
515    pub fn connect_icon_names_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
516        unsafe extern "C" fn notify_icon_names_trampoline<F: Fn(&IconTheme) + 'static>(
517            this: *mut ffi::GtkIconTheme,
518            _param_spec: glib::ffi::gpointer,
519            f: glib::ffi::gpointer,
520        ) {
521            unsafe {
522                let f: &F = &*(f as *const F);
523                f(&from_glib_borrow(this))
524            }
525        }
526        unsafe {
527            let f: Box_<F> = Box_::new(f);
528            connect_raw(
529                self.as_ptr() as *mut _,
530                c"notify::icon-names".as_ptr(),
531                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
532                    notify_icon_names_trampoline::<F> as *const (),
533                )),
534                Box_::into_raw(f),
535            )
536        }
537    }
538
539    #[doc(alias = "resource-path")]
540    pub fn connect_resource_path_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
541        unsafe extern "C" fn notify_resource_path_trampoline<F: Fn(&IconTheme) + 'static>(
542            this: *mut ffi::GtkIconTheme,
543            _param_spec: glib::ffi::gpointer,
544            f: glib::ffi::gpointer,
545        ) {
546            unsafe {
547                let f: &F = &*(f as *const F);
548                f(&from_glib_borrow(this))
549            }
550        }
551        unsafe {
552            let f: Box_<F> = Box_::new(f);
553            connect_raw(
554                self.as_ptr() as *mut _,
555                c"notify::resource-path".as_ptr(),
556                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
557                    notify_resource_path_trampoline::<F> as *const (),
558                )),
559                Box_::into_raw(f),
560            )
561        }
562    }
563
564    #[doc(alias = "search-path")]
565    pub fn connect_search_path_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
566        unsafe extern "C" fn notify_search_path_trampoline<F: Fn(&IconTheme) + 'static>(
567            this: *mut ffi::GtkIconTheme,
568            _param_spec: glib::ffi::gpointer,
569            f: glib::ffi::gpointer,
570        ) {
571            unsafe {
572                let f: &F = &*(f as *const F);
573                f(&from_glib_borrow(this))
574            }
575        }
576        unsafe {
577            let f: Box_<F> = Box_::new(f);
578            connect_raw(
579                self.as_ptr() as *mut _,
580                c"notify::search-path".as_ptr(),
581                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
582                    notify_search_path_trampoline::<F> as *const (),
583                )),
584                Box_::into_raw(f),
585            )
586        }
587    }
588
589    #[doc(alias = "theme-name")]
590    pub fn connect_theme_name_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
591        unsafe extern "C" fn notify_theme_name_trampoline<F: Fn(&IconTheme) + 'static>(
592            this: *mut ffi::GtkIconTheme,
593            _param_spec: glib::ffi::gpointer,
594            f: glib::ffi::gpointer,
595        ) {
596            unsafe {
597                let f: &F = &*(f as *const F);
598                f(&from_glib_borrow(this))
599            }
600        }
601        unsafe {
602            let f: Box_<F> = Box_::new(f);
603            connect_raw(
604                self.as_ptr() as *mut _,
605                c"notify::theme-name".as_ptr(),
606                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
607                    notify_theme_name_trampoline::<F> as *const (),
608                )),
609                Box_::into_raw(f),
610            )
611        }
612    }
613}
614
615impl Default for IconTheme {
616    fn default() -> Self {
617        Self::new()
618    }
619}
620
621// rustdoc-stripper-ignore-next
622/// A [builder-pattern] type to construct [`IconTheme`] objects.
623///
624/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
625#[must_use = "The builder must be built to be used"]
626pub struct IconThemeBuilder {
627    builder: glib::object::ObjectBuilder<'static, IconTheme>,
628}
629
630impl IconThemeBuilder {
631    fn new() -> Self {
632        Self {
633            builder: glib::object::Object::builder(),
634        }
635    }
636
637    /// The display that this icon theme object is attached to.
638    pub fn display(self, display: &impl IsA<gdk::Display>) -> Self {
639        Self {
640            builder: self.builder.property("display", display.clone().upcast()),
641        }
642    }
643
644    /// Resource paths that will be looked at when looking for icons,
645    /// similar to search paths.
646    ///
647    /// The resources are considered as part of the hicolor icon theme
648    /// and must be located in subdirectories that are defined in the
649    /// hicolor icon theme, such as `@path/16x16/actions/run.png`.
650    /// Icons that are directly placed in the resource path instead
651    /// of a subdirectory are also considered as ultimate fallback.
652    pub fn resource_path(self, resource_path: impl Into<glib::StrV>) -> Self {
653        Self {
654            builder: self.builder.property("resource-path", resource_path.into()),
655        }
656    }
657
658    /// s home directory.)
659    pub fn search_path(self, search_path: impl Into<glib::StrV>) -> Self {
660        Self {
661            builder: self.builder.property("search-path", search_path.into()),
662        }
663    }
664
665    /// The name of the icon theme that is being used.
666    ///
667    /// Unless set to a different value, this will be the value of
668    /// the `GtkSettings:gtk-icon-theme-name` property of the [`Settings`][crate::Settings]
669    /// object associated to the display of the icontheme object.
670    pub fn theme_name(self, theme_name: impl Into<glib::GString>) -> Self {
671        Self {
672            builder: self.builder.property("theme-name", theme_name.into()),
673        }
674    }
675
676    // rustdoc-stripper-ignore-next
677    /// Build the [`IconTheme`].
678    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
679    pub fn build(self) -> IconTheme {
680        assert_initialized_main_thread!();
681        self.builder.build()
682    }
683}