1use cairo;
6use glib;
7use glib::object::Cast;
8use glib::object::IsA;
9use glib::signal::connect_raw;
10use glib::signal::SignalHandlerId;
11use glib::translate::*;
12use glib::StaticType;
13use glib::Value;
14use glib_sys;
15use gobject_sys;
16use gtk_sys;
17use std::boxed::Box as Box_;
18use std::fmt;
19use std::mem::transmute;
20use Adjustment;
21use Buildable;
22use ResizeMode;
23use Widget;
24use WidgetPath;
25
26glib_wrapper! {
27 pub struct Container(Object<gtk_sys::GtkContainer, gtk_sys::GtkContainerClass, ContainerClass>) @extends Widget, @implements Buildable;
28
29 match fn {
30 get_type => || gtk_sys::gtk_container_get_type(),
31 }
32}
33
34pub const NONE_CONTAINER: Option<&Container> = None;
35
36pub trait ContainerExt: 'static {
37 fn add<P: IsA<Widget>>(&self, widget: &P);
38
39 fn check_resize(&self);
42
43 fn child_notify<P: IsA<Widget>>(&self, child: &P, child_property: &str);
48
49 fn child_type(&self) -> glib::types::Type;
57
58 fn forall<P: FnMut(&Widget)>(&self, callback: P);
59
60 fn foreach<P: FnMut(&Widget)>(&self, callback: P);
61
62 fn get_border_width(&self) -> u32;
63
64 fn get_children(&self) -> Vec<Widget>;
65
66 fn get_focus_child(&self) -> Option<Widget>;
70
71 fn get_focus_hadjustment(&self) -> Option<Adjustment>;
72
73 fn get_focus_vadjustment(&self) -> Option<Adjustment>;
74
75 fn get_path_for_child<P: IsA<Widget>>(&self, child: &P) -> Option<WidgetPath>;
76
77 fn propagate_draw<P: IsA<Widget>>(&self, child: &P, cr: &cairo::Context);
78
79 fn remove<P: IsA<Widget>>(&self, widget: &P);
80
81 fn set_border_width(&self, border_width: u32);
82
83 #[cfg_attr(feature = "v3_24", deprecated)]
84 fn set_focus_chain(&self, focusable_widgets: &[Widget]);
85
86 fn set_focus_child<P: IsA<Widget>>(&self, child: Option<&P>);
87
88 fn set_focus_hadjustment<P: IsA<Adjustment>>(&self, adjustment: &P);
89
90 fn set_focus_vadjustment<P: IsA<Adjustment>>(&self, adjustment: &P);
91
92 #[cfg_attr(feature = "v3_24", deprecated)]
93 fn unset_focus_chain(&self);
94
95 fn set_property_child(&self, child: Option<&Widget>);
96
97 fn get_property_resize_mode(&self) -> ResizeMode;
98
99 fn set_property_resize_mode(&self, resize_mode: ResizeMode);
100
101 fn connect_add<F: Fn(&Self, &Widget) + 'static>(&self, f: F) -> SignalHandlerId;
102
103 fn connect_check_resize<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
104
105 fn connect_remove<F: Fn(&Self, &Widget) + 'static>(&self, f: F) -> SignalHandlerId;
106
107 fn connect_set_focus_child<F: Fn(&Self, &Widget) + 'static>(&self, f: F) -> SignalHandlerId;
108
109 fn connect_property_border_width_notify<F: Fn(&Self) + 'static>(&self, f: F)
110 -> SignalHandlerId;
111
112 fn connect_property_child_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
113
114 fn connect_property_resize_mode_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
115}
116
117impl<O: IsA<Container>> ContainerExt for O {
118 fn add<P: IsA<Widget>>(&self, widget: &P) {
119 unsafe {
120 gtk_sys::gtk_container_add(
121 self.as_ref().to_glib_none().0,
122 widget.as_ref().to_glib_none().0,
123 );
124 }
125 }
126
127 fn check_resize(&self) {
132 unsafe {
133 gtk_sys::gtk_container_check_resize(self.as_ref().to_glib_none().0);
134 }
135 }
136
137 fn child_notify<P: IsA<Widget>>(&self, child: &P, child_property: &str) {
146 unsafe {
147 gtk_sys::gtk_container_child_notify(
148 self.as_ref().to_glib_none().0,
149 child.as_ref().to_glib_none().0,
150 child_property.to_glib_none().0,
151 );
152 }
153 }
154
155 fn child_type(&self) -> glib::types::Type {
169 unsafe {
170 from_glib(gtk_sys::gtk_container_child_type(
171 self.as_ref().to_glib_none().0,
172 ))
173 }
174 }
175
176 fn forall<P: FnMut(&Widget)>(&self, callback: P) {
177 let callback_data: P = callback;
178 unsafe extern "C" fn callback_func<P: FnMut(&Widget)>(
179 widget: *mut gtk_sys::GtkWidget,
180 data: glib_sys::gpointer,
181 ) {
182 let widget = from_glib_borrow(widget);
183 let callback: *mut P = data as *const _ as usize as *mut P;
184 (*callback)(&widget);
185 }
186 let callback = Some(callback_func::<P> as _);
187 let super_callback0: &P = &callback_data;
188 unsafe {
189 gtk_sys::gtk_container_forall(
190 self.as_ref().to_glib_none().0,
191 callback,
192 super_callback0 as *const _ as usize as *mut _,
193 );
194 }
195 }
196
197 fn foreach<P: FnMut(&Widget)>(&self, callback: P) {
198 let callback_data: P = callback;
199 unsafe extern "C" fn callback_func<P: FnMut(&Widget)>(
200 widget: *mut gtk_sys::GtkWidget,
201 data: glib_sys::gpointer,
202 ) {
203 let widget = from_glib_borrow(widget);
204 let callback: *mut P = data as *const _ as usize as *mut P;
205 (*callback)(&widget);
206 }
207 let callback = Some(callback_func::<P> as _);
208 let super_callback0: &P = &callback_data;
209 unsafe {
210 gtk_sys::gtk_container_foreach(
211 self.as_ref().to_glib_none().0,
212 callback,
213 super_callback0 as *const _ as usize as *mut _,
214 );
215 }
216 }
217
218 fn get_border_width(&self) -> u32 {
219 unsafe { gtk_sys::gtk_container_get_border_width(self.as_ref().to_glib_none().0) }
220 }
221
222 fn get_children(&self) -> Vec<Widget> {
223 unsafe {
224 FromGlibPtrContainer::from_glib_container(gtk_sys::gtk_container_get_children(
225 self.as_ref().to_glib_none().0,
226 ))
227 }
228 }
229
230 fn get_focus_child(&self) -> Option<Widget> {
235 unsafe {
236 from_glib_none(gtk_sys::gtk_container_get_focus_child(
237 self.as_ref().to_glib_none().0,
238 ))
239 }
240 }
241
242 fn get_focus_hadjustment(&self) -> Option<Adjustment> {
243 unsafe {
244 from_glib_none(gtk_sys::gtk_container_get_focus_hadjustment(
245 self.as_ref().to_glib_none().0,
246 ))
247 }
248 }
249
250 fn get_focus_vadjustment(&self) -> Option<Adjustment> {
251 unsafe {
252 from_glib_none(gtk_sys::gtk_container_get_focus_vadjustment(
253 self.as_ref().to_glib_none().0,
254 ))
255 }
256 }
257
258 fn get_path_for_child<P: IsA<Widget>>(&self, child: &P) -> Option<WidgetPath> {
259 unsafe {
260 from_glib_full(gtk_sys::gtk_container_get_path_for_child(
261 self.as_ref().to_glib_none().0,
262 child.as_ref().to_glib_none().0,
263 ))
264 }
265 }
266
267 fn propagate_draw<P: IsA<Widget>>(&self, child: &P, cr: &cairo::Context) {
268 unsafe {
269 gtk_sys::gtk_container_propagate_draw(
270 self.as_ref().to_glib_none().0,
271 child.as_ref().to_glib_none().0,
272 mut_override(cr.to_glib_none().0),
273 );
274 }
275 }
276
277 fn remove<P: IsA<Widget>>(&self, widget: &P) {
278 unsafe {
279 gtk_sys::gtk_container_remove(
280 self.as_ref().to_glib_none().0,
281 widget.as_ref().to_glib_none().0,
282 );
283 }
284 }
285
286 fn set_border_width(&self, border_width: u32) {
287 unsafe {
288 gtk_sys::gtk_container_set_border_width(self.as_ref().to_glib_none().0, border_width);
289 }
290 }
291
292 fn set_focus_chain(&self, focusable_widgets: &[Widget]) {
293 unsafe {
294 gtk_sys::gtk_container_set_focus_chain(
295 self.as_ref().to_glib_none().0,
296 focusable_widgets.to_glib_none().0,
297 );
298 }
299 }
300
301 fn set_focus_child<P: IsA<Widget>>(&self, child: Option<&P>) {
302 unsafe {
303 gtk_sys::gtk_container_set_focus_child(
304 self.as_ref().to_glib_none().0,
305 child.map(|p| p.as_ref()).to_glib_none().0,
306 );
307 }
308 }
309
310 fn set_focus_hadjustment<P: IsA<Adjustment>>(&self, adjustment: &P) {
311 unsafe {
312 gtk_sys::gtk_container_set_focus_hadjustment(
313 self.as_ref().to_glib_none().0,
314 adjustment.as_ref().to_glib_none().0,
315 );
316 }
317 }
318
319 fn set_focus_vadjustment<P: IsA<Adjustment>>(&self, adjustment: &P) {
320 unsafe {
321 gtk_sys::gtk_container_set_focus_vadjustment(
322 self.as_ref().to_glib_none().0,
323 adjustment.as_ref().to_glib_none().0,
324 );
325 }
326 }
327
328 fn unset_focus_chain(&self) {
329 unsafe {
330 gtk_sys::gtk_container_unset_focus_chain(self.as_ref().to_glib_none().0);
331 }
332 }
333
334 fn set_property_child(&self, child: Option<&Widget>) {
335 unsafe {
336 gobject_sys::g_object_set_property(
337 self.to_glib_none().0 as *mut gobject_sys::GObject,
338 b"child\0".as_ptr() as *const _,
339 Value::from(child).to_glib_none().0,
340 );
341 }
342 }
343
344 fn get_property_resize_mode(&self) -> ResizeMode {
345 unsafe {
346 let mut value = Value::from_type(<ResizeMode as StaticType>::static_type());
347 gobject_sys::g_object_get_property(
348 self.to_glib_none().0 as *mut gobject_sys::GObject,
349 b"resize-mode\0".as_ptr() as *const _,
350 value.to_glib_none_mut().0,
351 );
352 value.get().unwrap()
353 }
354 }
355
356 fn set_property_resize_mode(&self, resize_mode: ResizeMode) {
357 unsafe {
358 gobject_sys::g_object_set_property(
359 self.to_glib_none().0 as *mut gobject_sys::GObject,
360 b"resize-mode\0".as_ptr() as *const _,
361 Value::from(&resize_mode).to_glib_none().0,
362 );
363 }
364 }
365
366 fn connect_add<F: Fn(&Self, &Widget) + 'static>(&self, f: F) -> SignalHandlerId {
367 unsafe extern "C" fn add_trampoline<P, F: Fn(&P, &Widget) + 'static>(
368 this: *mut gtk_sys::GtkContainer,
369 object: *mut gtk_sys::GtkWidget,
370 f: glib_sys::gpointer,
371 ) where
372 P: IsA<Container>,
373 {
374 let f: &F = &*(f as *const F);
375 f(
376 &Container::from_glib_borrow(this).unsafe_cast(),
377 &from_glib_borrow(object),
378 )
379 }
380 unsafe {
381 let f: Box_<F> = Box_::new(f);
382 connect_raw(
383 self.as_ptr() as *mut _,
384 b"add\0".as_ptr() as *const _,
385 Some(transmute(add_trampoline::<Self, F> as usize)),
386 Box_::into_raw(f),
387 )
388 }
389 }
390
391 fn connect_check_resize<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
392 unsafe extern "C" fn check_resize_trampoline<P, F: Fn(&P) + 'static>(
393 this: *mut gtk_sys::GtkContainer,
394 f: glib_sys::gpointer,
395 ) where
396 P: IsA<Container>,
397 {
398 let f: &F = &*(f as *const F);
399 f(&Container::from_glib_borrow(this).unsafe_cast())
400 }
401 unsafe {
402 let f: Box_<F> = Box_::new(f);
403 connect_raw(
404 self.as_ptr() as *mut _,
405 b"check-resize\0".as_ptr() as *const _,
406 Some(transmute(check_resize_trampoline::<Self, F> as usize)),
407 Box_::into_raw(f),
408 )
409 }
410 }
411
412 fn connect_remove<F: Fn(&Self, &Widget) + 'static>(&self, f: F) -> SignalHandlerId {
413 unsafe extern "C" fn remove_trampoline<P, F: Fn(&P, &Widget) + 'static>(
414 this: *mut gtk_sys::GtkContainer,
415 object: *mut gtk_sys::GtkWidget,
416 f: glib_sys::gpointer,
417 ) where
418 P: IsA<Container>,
419 {
420 let f: &F = &*(f as *const F);
421 f(
422 &Container::from_glib_borrow(this).unsafe_cast(),
423 &from_glib_borrow(object),
424 )
425 }
426 unsafe {
427 let f: Box_<F> = Box_::new(f);
428 connect_raw(
429 self.as_ptr() as *mut _,
430 b"remove\0".as_ptr() as *const _,
431 Some(transmute(remove_trampoline::<Self, F> as usize)),
432 Box_::into_raw(f),
433 )
434 }
435 }
436
437 fn connect_set_focus_child<F: Fn(&Self, &Widget) + 'static>(&self, f: F) -> SignalHandlerId {
438 unsafe extern "C" fn set_focus_child_trampoline<P, F: Fn(&P, &Widget) + 'static>(
439 this: *mut gtk_sys::GtkContainer,
440 object: *mut gtk_sys::GtkWidget,
441 f: glib_sys::gpointer,
442 ) where
443 P: IsA<Container>,
444 {
445 let f: &F = &*(f as *const F);
446 f(
447 &Container::from_glib_borrow(this).unsafe_cast(),
448 &from_glib_borrow(object),
449 )
450 }
451 unsafe {
452 let f: Box_<F> = Box_::new(f);
453 connect_raw(
454 self.as_ptr() as *mut _,
455 b"set-focus-child\0".as_ptr() as *const _,
456 Some(transmute(set_focus_child_trampoline::<Self, F> as usize)),
457 Box_::into_raw(f),
458 )
459 }
460 }
461
462 fn connect_property_border_width_notify<F: Fn(&Self) + 'static>(
463 &self,
464 f: F,
465 ) -> SignalHandlerId {
466 unsafe extern "C" fn notify_border_width_trampoline<P, F: Fn(&P) + 'static>(
467 this: *mut gtk_sys::GtkContainer,
468 _param_spec: glib_sys::gpointer,
469 f: glib_sys::gpointer,
470 ) where
471 P: IsA<Container>,
472 {
473 let f: &F = &*(f as *const F);
474 f(&Container::from_glib_borrow(this).unsafe_cast())
475 }
476 unsafe {
477 let f: Box_<F> = Box_::new(f);
478 connect_raw(
479 self.as_ptr() as *mut _,
480 b"notify::border-width\0".as_ptr() as *const _,
481 Some(transmute(
482 notify_border_width_trampoline::<Self, F> as usize,
483 )),
484 Box_::into_raw(f),
485 )
486 }
487 }
488
489 fn connect_property_child_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
490 unsafe extern "C" fn notify_child_trampoline<P, F: Fn(&P) + 'static>(
491 this: *mut gtk_sys::GtkContainer,
492 _param_spec: glib_sys::gpointer,
493 f: glib_sys::gpointer,
494 ) where
495 P: IsA<Container>,
496 {
497 let f: &F = &*(f as *const F);
498 f(&Container::from_glib_borrow(this).unsafe_cast())
499 }
500 unsafe {
501 let f: Box_<F> = Box_::new(f);
502 connect_raw(
503 self.as_ptr() as *mut _,
504 b"notify::child\0".as_ptr() as *const _,
505 Some(transmute(notify_child_trampoline::<Self, F> as usize)),
506 Box_::into_raw(f),
507 )
508 }
509 }
510
511 fn connect_property_resize_mode_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
512 unsafe extern "C" fn notify_resize_mode_trampoline<P, F: Fn(&P) + 'static>(
513 this: *mut gtk_sys::GtkContainer,
514 _param_spec: glib_sys::gpointer,
515 f: glib_sys::gpointer,
516 ) where
517 P: IsA<Container>,
518 {
519 let f: &F = &*(f as *const F);
520 f(&Container::from_glib_borrow(this).unsafe_cast())
521 }
522 unsafe {
523 let f: Box_<F> = Box_::new(f);
524 connect_raw(
525 self.as_ptr() as *mut _,
526 b"notify::resize-mode\0".as_ptr() as *const _,
527 Some(transmute(notify_resize_mode_trampoline::<Self, F> as usize)),
528 Box_::into_raw(f),
529 )
530 }
531 }
532}
533
534impl fmt::Display for Container {
535 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
536 write!(f, "Container")
537 }
538}