gio/auto/
action.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::ffi;
6use glib::{
7    prelude::*,
8    signal::{SignalHandlerId, connect_raw},
9    translate::*,
10};
11use std::boxed::Box as Box_;
12
13glib::wrapper! {
14    /// `GAction` represents a single named action.
15    ///
16    /// The main interface to an action is that it can be activated with
17    /// [`ActionExt::activate()`][crate::prelude::ActionExt::activate()]. This results in the 'activate' signal being
18    /// emitted. An activation has a `GVariant` parameter (which may be
19    /// `NULL`). The correct type for the parameter is determined by a static
20    /// parameter type (which is given at construction time).
21    ///
22    /// An action may optionally have a state, in which case the state may be
23    /// set with [`ActionExt::change_state()`][crate::prelude::ActionExt::change_state()]. This call takes a [type@GLib.Variant]. The
24    /// correct type for the state is determined by a static state type
25    /// (which is given at construction time).
26    ///
27    /// The state may have a hint associated with it, specifying its valid
28    /// range.
29    ///
30    /// `GAction` is merely the interface to the concept of an action, as
31    /// described above.  Various implementations of actions exist, including
32    /// [`SimpleAction`][crate::SimpleAction].
33    ///
34    /// In all cases, the implementing class is responsible for storing the
35    /// name of the action, the parameter type, the enabled state, the optional
36    /// state type and the state and emitting the appropriate signals when these
37    /// change. The implementor is responsible for filtering calls to
38    /// [`ActionExt::activate()`][crate::prelude::ActionExt::activate()] and [`ActionExt::change_state()`][crate::prelude::ActionExt::change_state()]
39    /// for type safety and for the state being enabled.
40    ///
41    /// Probably the only useful thing to do with a `GAction` is to put it
42    /// inside of a [`SimpleActionGroup`][crate::SimpleActionGroup].
43    ///
44    /// ## Properties
45    ///
46    ///
47    /// #### `enabled`
48    ///  If @action is currently enabled.
49    ///
50    /// If the action is disabled then calls to [`ActionExt::activate()`][crate::prelude::ActionExt::activate()] and
51    /// [`ActionExt::change_state()`][crate::prelude::ActionExt::change_state()] have no effect.
52    ///
53    /// Readable
54    ///
55    ///
56    /// #### `name`
57    ///  The name of the action.  This is mostly meaningful for identifying
58    /// the action once it has been added to a [type@Gio.ActionGroup]. It is immutable.
59    ///
60    /// Readable
61    ///
62    ///
63    /// #### `parameter-type`
64    ///  The type of the parameter that must be given when activating the
65    /// action. This is immutable, and may be `NULL` if no parameter is needed when
66    /// activating the action.
67    ///
68    /// Readable
69    ///
70    ///
71    /// #### `state`
72    ///  The state of the action, or `NULL` if the action is stateless.
73    ///
74    /// Readable
75    ///
76    ///
77    /// #### `state-type`
78    ///  The [type@GLib.VariantType] of the state that the action has, or `NULL` if the
79    /// action is stateless. This is immutable.
80    ///
81    /// Readable
82    ///
83    /// # Implements
84    ///
85    /// [`ActionExt`][trait@crate::prelude::ActionExt]
86    #[doc(alias = "GAction")]
87    pub struct Action(Interface<ffi::GAction, ffi::GActionInterface>);
88
89    match fn {
90        type_ => || ffi::g_action_get_type(),
91    }
92}
93
94impl Action {
95    pub const NONE: Option<&'static Action> = None;
96
97    /// Checks if @action_name is valid.
98    ///
99    /// @action_name is valid if it consists only of alphanumeric characters,
100    /// plus `-` and `.`.  The empty string is not a valid action name.
101    ///
102    /// It is an error to call this function with a non-UTF-8 @action_name.
103    /// @action_name must not be `NULL`.
104    /// ## `action_name`
105    /// a potential action name
106    ///
107    /// # Returns
108    ///
109    /// `TRUE` if @action_name is valid
110    #[doc(alias = "g_action_name_is_valid")]
111    pub fn name_is_valid(action_name: &str) -> bool {
112        unsafe { from_glib(ffi::g_action_name_is_valid(action_name.to_glib_none().0)) }
113    }
114
115    /// Parses a detailed action name into its separate name and target
116    /// components.
117    ///
118    /// Detailed action names can have three formats.
119    ///
120    /// The first format is used to represent an action name with no target
121    /// value and consists of just an action name containing no whitespace
122    /// nor the characters `:`, `(` or `)`.  For example: `app.action`.
123    ///
124    /// The second format is used to represent an action with a target value
125    /// that is a non-empty string consisting only of alphanumerics, plus `-`
126    /// and `.`.  In that case, the action name and target value are
127    /// separated by a double colon (`::`).  For example:
128    /// `app.action::target`.
129    ///
130    /// The third format is used to represent an action with any type of
131    /// target value, including strings.  The target value follows the action
132    /// name, surrounded in parens.  For example: `app.action(42)`.  The
133    /// target value is parsed using `GLib::Variant::parse()`.  If a tuple-typed
134    /// value is desired, it must be specified in the same way, resulting in
135    /// two sets of parens, for example: `app.action((1,2,3))`.  A string
136    /// target can be specified this way as well: `app.action('target')`.
137    /// For strings, this third format must be used if target value is
138    /// empty or contains characters other than alphanumerics, `-` and `.`.
139    ///
140    /// If this function returns `TRUE`, a non-`NULL` value is guaranteed to be returned
141    /// in @action_name (if a pointer is passed in). A `NULL` value may still be
142    /// returned in @target_value, as the @detailed_name may not contain a target.
143    ///
144    /// If returned, the [type@GLib.Variant] in @target_value is guaranteed to not be floating.
145    /// ## `detailed_name`
146    /// a detailed action name
147    ///
148    /// # Returns
149    ///
150    /// `TRUE` if successful, else `FALSE` with @error set
151    ///
152    /// ## `action_name`
153    /// the action name
154    ///
155    /// ## `target_value`
156    /// the target value,
157    ///   or `NULL` for no target
158    #[doc(alias = "g_action_parse_detailed_name")]
159    pub fn parse_detailed_name(
160        detailed_name: &str,
161    ) -> Result<(glib::GString, Option<glib::Variant>), glib::Error> {
162        unsafe {
163            let mut action_name = std::ptr::null_mut();
164            let mut target_value = std::ptr::null_mut();
165            let mut error = std::ptr::null_mut();
166            let is_ok = ffi::g_action_parse_detailed_name(
167                detailed_name.to_glib_none().0,
168                &mut action_name,
169                &mut target_value,
170                &mut error,
171            );
172            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
173            if error.is_null() {
174                Ok((from_glib_full(action_name), from_glib_full(target_value)))
175            } else {
176                Err(from_glib_full(error))
177            }
178        }
179    }
180
181    /// Formats a detailed action name from @action_name and @target_value.
182    ///
183    /// It is an error to call this function with an invalid action name.
184    ///
185    /// This function is the opposite of [`parse_detailed_name()`][Self::parse_detailed_name()].
186    /// It will produce a string that can be parsed back to the @action_name
187    /// and @target_value by that function.
188    ///
189    /// See that function for the types of strings that will be printed by
190    /// this function.
191    /// ## `action_name`
192    /// a valid action name
193    /// ## `target_value`
194    /// a [type@GLib.Variant] target value, or `NULL`
195    ///
196    /// # Returns
197    ///
198    /// a detailed format string
199    #[doc(alias = "g_action_print_detailed_name")]
200    pub fn print_detailed_name(
201        action_name: &str,
202        target_value: Option<&glib::Variant>,
203    ) -> glib::GString {
204        unsafe {
205            from_glib_full(ffi::g_action_print_detailed_name(
206                action_name.to_glib_none().0,
207                target_value.to_glib_none().0,
208            ))
209        }
210    }
211}
212
213/// Trait containing all [`struct@Action`] methods.
214///
215/// # Implementors
216///
217/// [`Action`][struct@crate::Action], [`PropertyAction`][struct@crate::PropertyAction], [`SimpleAction`][struct@crate::SimpleAction]
218pub trait ActionExt: IsA<Action> + 'static {
219    /// Activates the action.
220    ///
221    /// @parameter must be the correct type of parameter for the action (ie:
222    /// the parameter type given at construction time).  If the parameter
223    /// type was `NULL` then @parameter must also be `NULL`.
224    ///
225    /// If the @parameter [type@GLib.Variant] is floating, it is consumed.
226    /// ## `parameter`
227    /// the parameter to the activation
228    #[doc(alias = "g_action_activate")]
229    fn activate(&self, parameter: Option<&glib::Variant>) {
230        unsafe {
231            ffi::g_action_activate(self.as_ref().to_glib_none().0, parameter.to_glib_none().0);
232        }
233    }
234
235    /// Request for the state of @self to be changed to @value.
236    ///
237    /// The action must be stateful and @value must be of the correct type.
238    /// See [`state_type()`][Self::state_type()].
239    ///
240    /// This call merely requests a change.  The action may refuse to change
241    /// its state or may change its state to something other than @value.
242    /// See [`state_hint()`][Self::state_hint()].
243    ///
244    /// If the @value [type@GLib.Variant] is floating, it is consumed.
245    /// ## `value`
246    /// the new state
247    #[doc(alias = "g_action_change_state")]
248    fn change_state(&self, value: &glib::Variant) {
249        unsafe {
250            ffi::g_action_change_state(self.as_ref().to_glib_none().0, value.to_glib_none().0);
251        }
252    }
253
254    /// Checks if @self is currently enabled.
255    ///
256    /// An action must be enabled in order to be activated or in order to
257    /// have its state changed from outside callers.
258    ///
259    /// # Returns
260    ///
261    /// whether the action is enabled
262    #[doc(alias = "g_action_get_enabled")]
263    #[doc(alias = "get_enabled")]
264    #[doc(alias = "enabled")]
265    fn is_enabled(&self) -> bool {
266        unsafe { from_glib(ffi::g_action_get_enabled(self.as_ref().to_glib_none().0)) }
267    }
268
269    /// Queries the name of @self.
270    ///
271    /// # Returns
272    ///
273    /// the name of the action
274    #[doc(alias = "g_action_get_name")]
275    #[doc(alias = "get_name")]
276    fn name(&self) -> glib::GString {
277        unsafe { from_glib_none(ffi::g_action_get_name(self.as_ref().to_glib_none().0)) }
278    }
279
280    /// Queries the type of the parameter that must be given when activating
281    /// @self.
282    ///
283    /// When activating the action using [`activate()`][Self::activate()], the
284    /// [type@GLib.Variant] given to that function must be of the type returned by
285    /// this function.
286    ///
287    /// In the case that this function returns `NULL`, you must not give any
288    /// [type@GLib.Variant], but `NULL` instead.
289    ///
290    /// # Returns
291    ///
292    /// the parameter type
293    #[doc(alias = "g_action_get_parameter_type")]
294    #[doc(alias = "get_parameter_type")]
295    #[doc(alias = "parameter-type")]
296    fn parameter_type(&self) -> Option<glib::VariantType> {
297        unsafe {
298            from_glib_none(ffi::g_action_get_parameter_type(
299                self.as_ref().to_glib_none().0,
300            ))
301        }
302    }
303
304    /// Queries the current state of @self.
305    ///
306    /// If the action is not stateful then `NULL` will be returned.  If the
307    /// action is stateful then the type of the return value is the type
308    /// given by [`state_type()`][Self::state_type()].
309    ///
310    /// The return value (if non-`NULL`) should be freed with
311    /// `GLib::Variant::unref()` when it is no longer required.
312    ///
313    /// # Returns
314    ///
315    /// the current state of the action
316    #[doc(alias = "g_action_get_state")]
317    #[doc(alias = "get_state")]
318    fn state(&self) -> Option<glib::Variant> {
319        unsafe { from_glib_full(ffi::g_action_get_state(self.as_ref().to_glib_none().0)) }
320    }
321
322    /// Requests a hint about the valid range of values for the state of
323    /// @self.
324    ///
325    /// If `NULL` is returned it either means that the action is not stateful
326    /// or that there is no hint about the valid range of values for the
327    /// state of the action.
328    ///
329    /// If a [type@GLib.Variant] array is returned then each item in the array is a
330    /// possible value for the state.  If a [type@GLib.Variant] pair (ie: two-tuple) is
331    /// returned then the tuple specifies the inclusive lower and upper bound
332    /// of valid values for the state.
333    ///
334    /// In any case, the information is merely a hint.  It may be possible to
335    /// have a state value outside of the hinted range and setting a value
336    /// within the range may fail.
337    ///
338    /// The return value (if non-`NULL`) should be freed with
339    /// `GLib::Variant::unref()` when it is no longer required.
340    ///
341    /// # Returns
342    ///
343    /// the state range hint
344    #[doc(alias = "g_action_get_state_hint")]
345    #[doc(alias = "get_state_hint")]
346    fn state_hint(&self) -> Option<glib::Variant> {
347        unsafe { from_glib_full(ffi::g_action_get_state_hint(self.as_ref().to_glib_none().0)) }
348    }
349
350    /// Queries the type of the state of @self.
351    ///
352    /// If the action is stateful (e.g. created with
353    /// [`SimpleAction::new_stateful()`][crate::SimpleAction::new_stateful()]) then this function returns the
354    /// [type@GLib.VariantType] of the state.  This is the type of the initial value
355    /// given as the state. All calls to [`change_state()`][Self::change_state()] must give a
356    /// [type@GLib.Variant] of this type and [`state()`][Self::state()] will return a
357    /// [type@GLib.Variant] of the same type.
358    ///
359    /// If the action is not stateful (e.g. created with [`SimpleAction::new()`][crate::SimpleAction::new()])
360    /// then this function will return `NULL`. In that case, [`state()`][Self::state()]
361    /// will return `NULL` and you must not call [`change_state()`][Self::change_state()].
362    ///
363    /// # Returns
364    ///
365    /// the state type, if the action is stateful
366    #[doc(alias = "g_action_get_state_type")]
367    #[doc(alias = "get_state_type")]
368    #[doc(alias = "state-type")]
369    fn state_type(&self) -> Option<glib::VariantType> {
370        unsafe { from_glib_none(ffi::g_action_get_state_type(self.as_ref().to_glib_none().0)) }
371    }
372
373    #[doc(alias = "enabled")]
374    fn connect_enabled_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
375        unsafe extern "C" fn notify_enabled_trampoline<P: IsA<Action>, F: Fn(&P) + 'static>(
376            this: *mut ffi::GAction,
377            _param_spec: glib::ffi::gpointer,
378            f: glib::ffi::gpointer,
379        ) {
380            unsafe {
381                let f: &F = &*(f as *const F);
382                f(Action::from_glib_borrow(this).unsafe_cast_ref())
383            }
384        }
385        unsafe {
386            let f: Box_<F> = Box_::new(f);
387            connect_raw(
388                self.as_ptr() as *mut _,
389                c"notify::enabled".as_ptr() as *const _,
390                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
391                    notify_enabled_trampoline::<Self, F> as *const (),
392                )),
393                Box_::into_raw(f),
394            )
395        }
396    }
397
398    #[doc(alias = "name")]
399    fn connect_name_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
400        unsafe extern "C" fn notify_name_trampoline<P: IsA<Action>, F: Fn(&P) + 'static>(
401            this: *mut ffi::GAction,
402            _param_spec: glib::ffi::gpointer,
403            f: glib::ffi::gpointer,
404        ) {
405            unsafe {
406                let f: &F = &*(f as *const F);
407                f(Action::from_glib_borrow(this).unsafe_cast_ref())
408            }
409        }
410        unsafe {
411            let f: Box_<F> = Box_::new(f);
412            connect_raw(
413                self.as_ptr() as *mut _,
414                c"notify::name".as_ptr() as *const _,
415                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
416                    notify_name_trampoline::<Self, F> as *const (),
417                )),
418                Box_::into_raw(f),
419            )
420        }
421    }
422
423    #[doc(alias = "parameter-type")]
424    fn connect_parameter_type_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
425        unsafe extern "C" fn notify_parameter_type_trampoline<
426            P: IsA<Action>,
427            F: Fn(&P) + 'static,
428        >(
429            this: *mut ffi::GAction,
430            _param_spec: glib::ffi::gpointer,
431            f: glib::ffi::gpointer,
432        ) {
433            unsafe {
434                let f: &F = &*(f as *const F);
435                f(Action::from_glib_borrow(this).unsafe_cast_ref())
436            }
437        }
438        unsafe {
439            let f: Box_<F> = Box_::new(f);
440            connect_raw(
441                self.as_ptr() as *mut _,
442                c"notify::parameter-type".as_ptr() as *const _,
443                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
444                    notify_parameter_type_trampoline::<Self, F> as *const (),
445                )),
446                Box_::into_raw(f),
447            )
448        }
449    }
450
451    #[doc(alias = "state")]
452    fn connect_state_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
453        unsafe extern "C" fn notify_state_trampoline<P: IsA<Action>, F: Fn(&P) + 'static>(
454            this: *mut ffi::GAction,
455            _param_spec: glib::ffi::gpointer,
456            f: glib::ffi::gpointer,
457        ) {
458            unsafe {
459                let f: &F = &*(f as *const F);
460                f(Action::from_glib_borrow(this).unsafe_cast_ref())
461            }
462        }
463        unsafe {
464            let f: Box_<F> = Box_::new(f);
465            connect_raw(
466                self.as_ptr() as *mut _,
467                c"notify::state".as_ptr() as *const _,
468                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
469                    notify_state_trampoline::<Self, F> as *const (),
470                )),
471                Box_::into_raw(f),
472            )
473        }
474    }
475
476    #[doc(alias = "state-type")]
477    fn connect_state_type_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
478        unsafe extern "C" fn notify_state_type_trampoline<P: IsA<Action>, F: Fn(&P) + 'static>(
479            this: *mut ffi::GAction,
480            _param_spec: glib::ffi::gpointer,
481            f: glib::ffi::gpointer,
482        ) {
483            unsafe {
484                let f: &F = &*(f as *const F);
485                f(Action::from_glib_borrow(this).unsafe_cast_ref())
486            }
487        }
488        unsafe {
489            let f: Box_<F> = Box_::new(f);
490            connect_raw(
491                self.as_ptr() as *mut _,
492                c"notify::state-type".as_ptr() as *const _,
493                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
494                    notify_state_type_trampoline::<Self, F> as *const (),
495                )),
496                Box_::into_raw(f),
497            )
498        }
499    }
500}
501
502impl<O: IsA<Action>> ActionExt for O {}