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