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