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