gtk4/
notebook.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::translate::*;
4use libc::c_int;
5
6use crate::{ffi, prelude::*, Notebook, Widget};
7
8impl Notebook {
9    /// Appends a page to @self.
10    /// ## `child`
11    /// the [`Widget`][crate::Widget] to use as the contents of the page
12    /// ## `tab_label`
13    /// the [`Widget`][crate::Widget] to be used as the label
14    ///   for the page, or [`None`] to use the default label, “page N”
15    ///
16    /// # Returns
17    ///
18    /// the index (starting from 0) of the appended
19    ///   page in the notebook, or -1 if function fails
20    #[doc(alias = "gtk_notebook_append_page")]
21    pub fn append_page(
22        &self,
23        child: &impl IsA<Widget>,
24        tab_label: Option<&impl IsA<Widget>>,
25    ) -> u32 {
26        unsafe {
27            let ret = ffi::gtk_notebook_append_page(
28                self.to_glib_none().0,
29                child.as_ref().to_glib_none().0,
30                tab_label.map(|p| p.as_ref()).to_glib_none().0,
31            );
32            debug_assert!(ret >= 0);
33            ret as u32
34        }
35    }
36
37    /// Appends a page to @self, specifying the widget to use as the
38    /// label in the popup menu.
39    /// ## `child`
40    /// the [`Widget`][crate::Widget] to use as the contents of the page
41    /// ## `tab_label`
42    /// the [`Widget`][crate::Widget] to be used as the label
43    ///   for the page, or [`None`] to use the default label, “page N”
44    /// ## `menu_label`
45    /// the widget to use as a label for the
46    ///   page-switch menu, if that is enabled. If [`None`], and @tab_label
47    ///   is a [`Label`][crate::Label] or [`None`], then the menu label will be a newly
48    ///   created label with the same text as @tab_label; if @tab_label
49    ///   is not a [`Label`][crate::Label], @menu_label must be specified if the
50    ///   page-switch menu is to be used.
51    ///
52    /// # Returns
53    ///
54    /// the index (starting from 0) of the appended
55    ///   page in the notebook, or -1 if function fails
56    #[doc(alias = "gtk_notebook_append_page_menu")]
57    pub fn append_page_menu(
58        &self,
59        child: &impl IsA<Widget>,
60        tab_label: Option<&impl IsA<Widget>>,
61        menu_label: Option<&impl IsA<Widget>>,
62    ) -> u32 {
63        unsafe {
64            let ret = ffi::gtk_notebook_append_page_menu(
65                self.to_glib_none().0,
66                child.as_ref().to_glib_none().0,
67                tab_label.map(|p| p.as_ref()).to_glib_none().0,
68                menu_label.map(|p| p.as_ref()).to_glib_none().0,
69            );
70            debug_assert!(ret >= 0);
71            ret as u32
72        }
73    }
74
75    /// Returns the page number of the current page.
76    ///
77    /// # Returns
78    ///
79    /// the index (starting from 0) of the current
80    ///   page in the notebook. If the notebook has no pages,
81    ///   then -1 will be returned.
82    #[doc(alias = "gtk_notebook_get_current_page")]
83    #[doc(alias = "get_current_page")]
84    pub fn current_page(&self) -> Option<u32> {
85        unsafe {
86            let ret = ffi::gtk_notebook_get_current_page(self.to_glib_none().0);
87            if ret >= 0 {
88                Some(ret as u32)
89            } else {
90                None
91            }
92        }
93    }
94
95    /// Gets the number of pages in a notebook.
96    ///
97    /// # Returns
98    ///
99    /// the number of pages in the notebook
100    #[doc(alias = "gtk_notebook_get_n_pages")]
101    #[doc(alias = "get_n_pages")]
102    pub fn n_pages(&self) -> u32 {
103        unsafe {
104            let ret = ffi::gtk_notebook_get_n_pages(self.to_glib_none().0);
105            debug_assert!(ret >= 0);
106            ret as u32
107        }
108    }
109
110    /// Returns the child widget contained in page number @page_num.
111    /// ## `page_num`
112    /// the index of a page in the notebook, or -1
113    ///   to get the last page
114    ///
115    /// # Returns
116    ///
117    /// the child widget, or [`None`] if @page_num
118    /// is out of bounds
119    #[doc(alias = "gtk_notebook_get_nth_page")]
120    #[doc(alias = "get_nth_page")]
121    pub fn nth_page(&self, page_num: Option<u32>) -> Option<Widget> {
122        unsafe {
123            from_glib_none(ffi::gtk_notebook_get_nth_page(
124                self.to_glib_none().0,
125                page_num.map_or(-1, |n| n as c_int),
126            ))
127        }
128    }
129
130    /// Insert a page into @self at the given position.
131    /// ## `child`
132    /// the [`Widget`][crate::Widget] to use as the contents of the page
133    /// ## `tab_label`
134    /// the [`Widget`][crate::Widget] to be used as the label
135    ///   for the page, or [`None`] to use the default label, “page N”
136    /// ## `position`
137    /// the index (starting at 0) at which to insert the page,
138    ///   or -1 to append the page after all other pages
139    ///
140    /// # Returns
141    ///
142    /// the index (starting from 0) of the inserted
143    ///   page in the notebook, or -1 if function fails
144    #[doc(alias = "gtk_notebook_insert_page")]
145    pub fn insert_page(
146        &self,
147        child: &impl IsA<Widget>,
148        tab_label: Option<&impl IsA<Widget>>,
149        position: Option<u32>,
150    ) -> u32 {
151        unsafe {
152            let ret = ffi::gtk_notebook_insert_page(
153                self.to_glib_none().0,
154                child.as_ref().to_glib_none().0,
155                tab_label.map(|p| p.as_ref()).to_glib_none().0,
156                position.map_or(-1, |n| n as c_int),
157            );
158            debug_assert!(ret >= 0);
159            ret as u32
160        }
161    }
162
163    /// Insert a page into @self at the given position, specifying
164    /// the widget to use as the label in the popup menu.
165    /// ## `child`
166    /// the [`Widget`][crate::Widget] to use as the contents of the page
167    /// ## `tab_label`
168    /// the [`Widget`][crate::Widget] to be used as the label
169    ///   for the page, or [`None`] to use the default label, “page N”
170    /// ## `menu_label`
171    /// the widget to use as a label for the
172    ///   page-switch menu, if that is enabled. If [`None`], and @tab_label
173    ///   is a [`Label`][crate::Label] or [`None`], then the menu label will be a newly
174    ///   created label with the same text as @tab_label; if @tab_label
175    ///   is not a [`Label`][crate::Label], @menu_label must be specified if the
176    ///   page-switch menu is to be used.
177    /// ## `position`
178    /// the index (starting at 0) at which to insert the page,
179    ///   or -1 to append the page after all other pages.
180    ///
181    /// # Returns
182    ///
183    /// the index (starting from 0) of the inserted
184    ///   page in the notebook
185    #[doc(alias = "gtk_notebook_insert_page_menu")]
186    pub fn insert_page_menu(
187        &self,
188        child: &impl IsA<Widget>,
189        tab_label: Option<&impl IsA<Widget>>,
190        menu_label: Option<&impl IsA<Widget>>,
191        position: Option<u32>,
192    ) -> u32 {
193        unsafe {
194            let ret = ffi::gtk_notebook_insert_page_menu(
195                self.to_glib_none().0,
196                child.as_ref().to_glib_none().0,
197                tab_label.map(|p| p.as_ref()).to_glib_none().0,
198                menu_label.map(|p| p.as_ref()).to_glib_none().0,
199                position.map_or(-1, |n| n as c_int),
200            );
201            debug_assert!(ret >= 0);
202            ret as u32
203        }
204    }
205    /// Finds the index of the page which contains the given child
206    /// widget.
207    /// ## `child`
208    /// a [`Widget`][crate::Widget]
209    ///
210    /// # Returns
211    ///
212    /// the index of the page containing @child, or
213    ///   -1 if @child is not in the notebook
214    #[doc(alias = "gtk_notebook_page_num")]
215    pub fn page_num(&self, child: &impl IsA<Widget>) -> Option<u32> {
216        unsafe {
217            let ret =
218                ffi::gtk_notebook_page_num(self.to_glib_none().0, child.as_ref().to_glib_none().0);
219            if ret >= 0 {
220                Some(ret as u32)
221            } else {
222                None
223            }
224        }
225    }
226    /// Prepends a page to @self.
227    /// ## `child`
228    /// the [`Widget`][crate::Widget] to use as the contents of the page
229    /// ## `tab_label`
230    /// the [`Widget`][crate::Widget] to be used as the label
231    ///   for the page, or [`None`] to use the default label, “page N”
232    ///
233    /// # Returns
234    ///
235    /// the index (starting from 0) of the prepended
236    ///   page in the notebook, or -1 if function fails
237    #[doc(alias = "gtk_notebook_prepend_page")]
238    pub fn prepend_page(
239        &self,
240        child: &impl IsA<Widget>,
241        tab_label: Option<&impl IsA<Widget>>,
242    ) -> u32 {
243        unsafe {
244            let ret = ffi::gtk_notebook_prepend_page(
245                self.to_glib_none().0,
246                child.as_ref().to_glib_none().0,
247                tab_label.map(|p| p.as_ref()).to_glib_none().0,
248            );
249            debug_assert!(ret >= 0);
250            ret as u32
251        }
252    }
253
254    /// Prepends a page to @self, specifying the widget to use as the
255    /// label in the popup menu.
256    /// ## `child`
257    /// the [`Widget`][crate::Widget] to use as the contents of the page
258    /// ## `tab_label`
259    /// the [`Widget`][crate::Widget] to be used as the label
260    ///   for the page, or [`None`] to use the default label, “page N”
261    /// ## `menu_label`
262    /// the widget to use as a label for the
263    ///   page-switch menu, if that is enabled. If [`None`], and @tab_label
264    ///   is a [`Label`][crate::Label] or [`None`], then the menu label will be a newly
265    ///   created label with the same text as @tab_label; if @tab_label
266    ///   is not a [`Label`][crate::Label], @menu_label must be specified if the
267    ///   page-switch menu is to be used.
268    ///
269    /// # Returns
270    ///
271    /// the index (starting from 0) of the prepended
272    ///   page in the notebook, or -1 if function fails
273    #[doc(alias = "gtk_notebook_prepend_page_menu")]
274    pub fn prepend_page_menu(
275        &self,
276        child: &impl IsA<Widget>,
277        tab_label: Option<&impl IsA<Widget>>,
278        menu_label: Option<&impl IsA<Widget>>,
279    ) -> u32 {
280        unsafe {
281            let ret = ffi::gtk_notebook_prepend_page_menu(
282                self.to_glib_none().0,
283                child.as_ref().to_glib_none().0,
284                tab_label.map(|p| p.as_ref()).to_glib_none().0,
285                menu_label.map(|p| p.as_ref()).to_glib_none().0,
286            );
287            debug_assert!(ret >= 0);
288            ret as u32
289        }
290    }
291
292    /// Removes a page from the notebook given its index
293    /// in the notebook.
294    /// ## `page_num`
295    /// the index of a notebook page, starting
296    ///   from 0. If -1, the last page will be removed.
297    #[doc(alias = "gtk_notebook_remove_page")]
298    pub fn remove_page(&self, page_num: Option<u32>) {
299        unsafe {
300            ffi::gtk_notebook_remove_page(
301                self.to_glib_none().0,
302                page_num.map_or(-1, |n| n as c_int),
303            );
304        }
305    }
306
307    /// Reorders the page containing @child, so that it appears in position
308    /// @position.
309    ///
310    /// If @position is greater than or equal to the number of children in
311    /// the list or negative, @child will be moved to the end of the list.
312    /// ## `child`
313    /// the child to move
314    /// ## `position`
315    /// the new position, or -1 to move to the end
316    #[doc(alias = "gtk_notebook_reorder_child")]
317    pub fn reorder_child(&self, child: &impl IsA<Widget>, position: Option<u32>) {
318        unsafe {
319            ffi::gtk_notebook_reorder_child(
320                self.to_glib_none().0,
321                child.as_ref().to_glib_none().0,
322                position.map_or(-1, |n| n as c_int),
323            );
324        }
325    }
326
327    /// Switches to the page number @page_num.
328    ///
329    /// Note that due to historical reasons, GtkNotebook refuses
330    /// to switch to a page unless the child widget is visible.
331    /// Therefore, it is recommended to show child widgets before
332    /// adding them to a notebook.
333    /// ## `page_num`
334    /// index of the page to switch to, starting from 0.
335    ///   If negative, the last page will be used. If greater
336    ///   than the number of pages in the notebook, nothing
337    ///   will be done.
338    #[doc(alias = "gtk_notebook_set_current_page")]
339    pub fn set_current_page(&self, page_num: Option<u32>) {
340        unsafe {
341            ffi::gtk_notebook_set_current_page(
342                self.to_glib_none().0,
343                page_num.map_or(-1, |n| n as c_int),
344            );
345        }
346    }
347}