Skip to main content

gtk/
notebook.rs

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