Skip to main content

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