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