gio/auto/menu_model.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::{MenuAttributeIter, MenuLinkIter, 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 ///
16 ///
17 /// Notice that the separators visible in the [example](#an-example-menu)
18 /// appear nowhere in the [menu model](#a-menu-example). This is because
19 /// separators are not explicitly represented in the menu model. Instead,
20 /// a separator is inserted between any two non-empty sections of a menu.
21 /// Section items can have labels just like any other item. In that case,
22 /// a display system may show a section header instead of a separator.
23 ///
24 /// The motivation for this abstract model of application controls is
25 /// that modern user interfaces tend to make these controls available
26 /// outside the application. Examples include global menus, jumplists,
27 /// dash boards, etc. To support such uses, it is necessary to ‘export’
28 /// information about actions and their representation in menus, which
29 /// is exactly what the action group exporter and the menu model exporter do for
30 /// [`ActionGroup`][crate::ActionGroup] and [`MenuModel`][crate::MenuModel]. The client-side
31 /// counterparts to make use of the exported information are
32 /// [`DBusActionGroup`][crate::DBusActionGroup] and [`DBusMenuModel`][crate::DBusMenuModel].
33 ///
34 /// The API of `GMenuModel` is very generic, with iterators for the
35 /// attributes and links of an item, see
36 /// [`MenuModelExt::iterate_item_attributes()`][crate::prelude::MenuModelExt::iterate_item_attributes()] and
37 /// [`MenuModelExt::iterate_item_links()`][crate::prelude::MenuModelExt::iterate_item_links()]. The ‘standard’ attributes and
38 /// link types have predefined names: `G_MENU_ATTRIBUTE_LABEL`,
39 /// `G_MENU_ATTRIBUTE_ACTION`, `G_MENU_ATTRIBUTE_TARGET`, `G_MENU_LINK_SECTION`
40 /// and `G_MENU_LINK_SUBMENU`.
41 ///
42 /// Items in a `GMenuModel` represent active controls if they refer to
43 /// an action that can get activated when the user interacts with the
44 /// menu item. The reference to the action is encoded by the string ID
45 /// in the `G_MENU_ATTRIBUTE_ACTION` attribute. An action ID uniquely
46 /// identifies an action in an action group. Which action group(s) provide
47 /// actions depends on the context in which the menu model is used.
48 /// E.g. when the model is exported as the application menu of a
49 /// [`GtkApplication`](https://docs.gtk.org/gtk4/class.Application.html),
50 /// actions can be application-wide or window-specific (and thus come from
51 /// two different action groups). By convention, the application-wide actions
52 /// have names that start with `app.`, while the names of window-specific
53 /// actions start with `win.`.
54 ///
55 /// While a wide variety of stateful actions is possible, the following
56 /// is the minimum that is expected to be supported by all users of exported
57 /// menu information:
58 ///
59 /// - an action with no parameter type and no state
60 /// - an action with no parameter type and boolean state
61 /// - an action with string parameter type and string state
62 ///
63 /// ## Stateless
64 ///
65 /// A stateless action typically corresponds to an ordinary menu item.
66 ///
67 /// Selecting such a menu item will activate the action (with no parameter).
68 ///
69 /// ## Boolean State
70 ///
71 /// An action with a boolean state will most typically be used with a ‘toggle’
72 /// or ‘switch’ menu item. The state can be set directly, but activating the
73 /// action (with no parameter) results in the state being toggled.
74 ///
75 /// Selecting a toggle menu item will activate the action. The menu item should
76 /// be rendered as ‘checked’ when the state is true.
77 ///
78 /// ## String Parameter and State
79 ///
80 /// Actions with string parameters and state will most typically be used to
81 /// represent an enumerated choice over the items available for a group of
82 /// radio menu items. Activating the action with a string parameter is
83 /// equivalent to setting that parameter as the state.
84 ///
85 /// Radio menu items, in addition to being associated with the action, will
86 /// have a target value. Selecting that menu item will result in activation
87 /// of the action with the target value as the parameter. The menu item should
88 /// be rendered as ‘selected’ when the state of the action is equal to the
89 /// target value of the menu item.
90 ///
91 /// This is an Abstract Base Class, you cannot instantiate it.
92 ///
93 /// ## Signals
94 ///
95 ///
96 /// #### `items-changed`
97 /// Emitted when a change has occurred to the menu.
98 ///
99 /// The only changes that can occur to a menu is that items are removed
100 /// or added. Items may not change (except by being removed and added
101 /// back in the same location). This signal is capable of describing
102 /// both of those changes (at the same time).
103 ///
104 /// The signal means that starting at the index @position, @removed
105 /// items were removed and @added items were added in their place. If
106 /// @removed is zero then only items were added. If @added is zero
107 /// then only items were removed.
108 ///
109 /// As an example, if the menu contains items a, b, c, d (in that
110 /// order) and the signal (2, 1, 3) occurs then the new composition of
111 /// the menu will be a, b, \_, \_, \_, d (with each _ representing some
112 /// new item).
113 ///
114 /// Signal handlers may query the model (particularly the added items)
115 /// and expect to see the results of the modification that is being
116 /// reported. The signal is emitted after the modification.
117 ///
118 ///
119 ///
120 /// # Implements
121 ///
122 /// [`MenuModelExt`][trait@crate::prelude::MenuModelExt], [`trait@glib::ObjectExt`]
123 #[doc(alias = "GMenuModel")]
124 pub struct MenuModel(Object<ffi::GMenuModel, ffi::GMenuModelClass>);
125
126 match fn {
127 type_ => || ffi::g_menu_model_get_type(),
128 }
129}
130
131impl MenuModel {
132 pub const NONE: Option<&'static MenuModel> = None;
133}
134
135/// Trait containing all [`struct@MenuModel`] methods.
136///
137/// # Implementors
138///
139/// [`DBusMenuModel`][struct@crate::DBusMenuModel], [`MenuModel`][struct@crate::MenuModel], [`Menu`][struct@crate::Menu]
140pub trait MenuModelExt: IsA<MenuModel> + 'static {
141 //#[doc(alias = "g_menu_model_get_item_attribute")]
142 //#[doc(alias = "get_item_attribute")]
143 //fn is_item_attribute(&self, item_index: i32, attribute: &str, format_string: &str, : /*Unknown conversion*//*Unimplemented*/Basic: VarArgs) -> bool {
144 // unsafe { TODO: call ffi:g_menu_model_get_item_attribute() }
145 //}
146
147 /// Queries the item at position @item_index in @self for the attribute
148 /// specified by @attribute.
149 ///
150 /// If @expected_type is non-[`None`] then it specifies the expected type of
151 /// the attribute. If it is [`None`] then any type will be accepted.
152 ///
153 /// If the attribute exists and matches @expected_type (or if the
154 /// expected type is unspecified) then the value is returned.
155 ///
156 /// If the attribute does not exist, or does not match the expected type
157 /// then [`None`] is returned.
158 /// ## `item_index`
159 /// the index of the item
160 /// ## `attribute`
161 /// the attribute to query
162 /// ## `expected_type`
163 /// the expected type of the attribute, or
164 /// [`None`]
165 ///
166 /// # Returns
167 ///
168 /// the value of the attribute
169 #[doc(alias = "g_menu_model_get_item_attribute_value")]
170 #[doc(alias = "get_item_attribute_value")]
171 fn item_attribute_value(
172 &self,
173 item_index: i32,
174 attribute: &str,
175 expected_type: Option<&glib::VariantTy>,
176 ) -> Option<glib::Variant> {
177 unsafe {
178 from_glib_full(ffi::g_menu_model_get_item_attribute_value(
179 self.as_ref().to_glib_none().0,
180 item_index,
181 attribute.to_glib_none().0,
182 expected_type.to_glib_none().0,
183 ))
184 }
185 }
186
187 /// Queries the item at position @item_index in @self for the link
188 /// specified by @link.
189 ///
190 /// If the link exists, the linked #GMenuModel is returned. If the link
191 /// does not exist, [`None`] is returned.
192 /// ## `item_index`
193 /// the index of the item
194 /// ## `link`
195 /// the link to query
196 ///
197 /// # Returns
198 ///
199 /// the linked #GMenuModel, or [`None`]
200 #[doc(alias = "g_menu_model_get_item_link")]
201 #[doc(alias = "get_item_link")]
202 #[must_use]
203 fn item_link(&self, item_index: i32, link: &str) -> Option<MenuModel> {
204 unsafe {
205 from_glib_full(ffi::g_menu_model_get_item_link(
206 self.as_ref().to_glib_none().0,
207 item_index,
208 link.to_glib_none().0,
209 ))
210 }
211 }
212
213 /// Query the number of items in @self.
214 ///
215 /// # Returns
216 ///
217 /// the number of items
218 #[doc(alias = "g_menu_model_get_n_items")]
219 #[doc(alias = "get_n_items")]
220 fn n_items(&self) -> i32 {
221 unsafe { ffi::g_menu_model_get_n_items(self.as_ref().to_glib_none().0) }
222 }
223
224 /// Queries if @self is mutable.
225 ///
226 /// An immutable #GMenuModel will never emit the #GMenuModel::items-changed
227 /// signal. Consumers of the model may make optimisations accordingly.
228 ///
229 /// # Returns
230 ///
231 /// [`true`] if the model is mutable (ie: "items-changed" may be
232 /// emitted).
233 #[doc(alias = "g_menu_model_is_mutable")]
234 fn is_mutable(&self) -> bool {
235 unsafe { from_glib(ffi::g_menu_model_is_mutable(self.as_ref().to_glib_none().0)) }
236 }
237
238 /// Requests emission of the #GMenuModel::items-changed signal on @self.
239 ///
240 /// This function should never be called except by #GMenuModel
241 /// subclasses. Any other calls to this function will very likely lead
242 /// to a violation of the interface of the model.
243 ///
244 /// The implementation should update its internal representation of the
245 /// menu before emitting the signal. The implementation should further
246 /// expect to receive queries about the new state of the menu (and
247 /// particularly added menu items) while signal handlers are running.
248 ///
249 /// The implementation must dispatch this call directly from a mainloop
250 /// entry and not in response to calls -- particularly those from the
251 /// #GMenuModel API. Said another way: the menu must not change while
252 /// user code is running without returning to the mainloop.
253 /// ## `position`
254 /// the position of the change
255 /// ## `removed`
256 /// the number of items removed
257 /// ## `added`
258 /// the number of items added
259 #[doc(alias = "g_menu_model_items_changed")]
260 fn items_changed(&self, position: i32, removed: i32, added: i32) {
261 unsafe {
262 ffi::g_menu_model_items_changed(
263 self.as_ref().to_glib_none().0,
264 position,
265 removed,
266 added,
267 );
268 }
269 }
270
271 /// Creates a #GMenuAttributeIter to iterate over the attributes of
272 /// the item at position @item_index in @self.
273 ///
274 /// You must free the iterator with g_object_unref() when you are done.
275 /// ## `item_index`
276 /// the index of the item
277 ///
278 /// # Returns
279 ///
280 /// a new #GMenuAttributeIter
281 #[doc(alias = "g_menu_model_iterate_item_attributes")]
282 fn iterate_item_attributes(&self, item_index: i32) -> MenuAttributeIter {
283 unsafe {
284 from_glib_full(ffi::g_menu_model_iterate_item_attributes(
285 self.as_ref().to_glib_none().0,
286 item_index,
287 ))
288 }
289 }
290
291 /// Creates a #GMenuLinkIter to iterate over the links of the item at
292 /// position @item_index in @self.
293 ///
294 /// You must free the iterator with g_object_unref() when you are done.
295 /// ## `item_index`
296 /// the index of the item
297 ///
298 /// # Returns
299 ///
300 /// a new #GMenuLinkIter
301 #[doc(alias = "g_menu_model_iterate_item_links")]
302 fn iterate_item_links(&self, item_index: i32) -> MenuLinkIter {
303 unsafe {
304 from_glib_full(ffi::g_menu_model_iterate_item_links(
305 self.as_ref().to_glib_none().0,
306 item_index,
307 ))
308 }
309 }
310
311 /// Emitted when a change has occurred to the menu.
312 ///
313 /// The only changes that can occur to a menu is that items are removed
314 /// or added. Items may not change (except by being removed and added
315 /// back in the same location). This signal is capable of describing
316 /// both of those changes (at the same time).
317 ///
318 /// The signal means that starting at the index @position, @removed
319 /// items were removed and @added items were added in their place. If
320 /// @removed is zero then only items were added. If @added is zero
321 /// then only items were removed.
322 ///
323 /// As an example, if the menu contains items a, b, c, d (in that
324 /// order) and the signal (2, 1, 3) occurs then the new composition of
325 /// the menu will be a, b, \_, \_, \_, d (with each _ representing some
326 /// new item).
327 ///
328 /// Signal handlers may query the model (particularly the added items)
329 /// and expect to see the results of the modification that is being
330 /// reported. The signal is emitted after the modification.
331 /// ## `position`
332 /// the position of the change
333 /// ## `removed`
334 /// the number of items removed
335 /// ## `added`
336 /// the number of items added
337 #[doc(alias = "items-changed")]
338 fn connect_items_changed<F: Fn(&Self, i32, i32, i32) + 'static>(
339 &self,
340 f: F,
341 ) -> SignalHandlerId {
342 unsafe extern "C" fn items_changed_trampoline<
343 P: IsA<MenuModel>,
344 F: Fn(&P, i32, i32, i32) + 'static,
345 >(
346 this: *mut ffi::GMenuModel,
347 position: std::ffi::c_int,
348 removed: std::ffi::c_int,
349 added: std::ffi::c_int,
350 f: glib::ffi::gpointer,
351 ) {
352 unsafe {
353 let f: &F = &*(f as *const F);
354 f(
355 MenuModel::from_glib_borrow(this).unsafe_cast_ref(),
356 position,
357 removed,
358 added,
359 )
360 }
361 }
362 unsafe {
363 let f: Box_<F> = Box_::new(f);
364 connect_raw(
365 self.as_ptr() as *mut _,
366 c"items-changed".as_ptr(),
367 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
368 items_changed_trampoline::<Self, F> as *const (),
369 )),
370 Box_::into_raw(f),
371 )
372 }
373 }
374}
375
376impl<O: IsA<MenuModel>> MenuModelExt for O {}