1#[cfg(feature = "v4_12")]
6#[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
7use crate::ScrollInfo;
8use crate::{
9 ffi, Accessible, AccessibleRole, Adjustment, Align, Buildable, ConstraintTarget, LayoutManager,
10 Overflow, Scrollable, ScrollablePolicy, Widget,
11};
12use glib::{
13 prelude::*,
14 signal::{connect_raw, SignalHandlerId},
15 translate::*,
16};
17use std::boxed::Box as Box_;
18
19glib::wrapper! {
20 #[doc(alias = "GtkViewport")]
21 pub struct Viewport(Object<ffi::GtkViewport>) @extends Widget, @implements Accessible, Buildable, ConstraintTarget, Scrollable;
22
23 match fn {
24 type_ => || ffi::gtk_viewport_get_type(),
25 }
26}
27
28impl Viewport {
29 #[doc(alias = "gtk_viewport_new")]
30 pub fn new(
31 hadjustment: Option<&impl IsA<Adjustment>>,
32 vadjustment: Option<&impl IsA<Adjustment>>,
33 ) -> Viewport {
34 assert_initialized_main_thread!();
35 unsafe {
36 Widget::from_glib_none(ffi::gtk_viewport_new(
37 hadjustment.map(|p| p.as_ref()).to_glib_none().0,
38 vadjustment.map(|p| p.as_ref()).to_glib_none().0,
39 ))
40 .unsafe_cast()
41 }
42 }
43
44 pub fn builder() -> ViewportBuilder {
49 ViewportBuilder::new()
50 }
51
52 #[doc(alias = "gtk_viewport_get_child")]
53 #[doc(alias = "get_child")]
54 pub fn child(&self) -> Option<Widget> {
55 unsafe { from_glib_none(ffi::gtk_viewport_get_child(self.to_glib_none().0)) }
56 }
57
58 #[doc(alias = "gtk_viewport_get_scroll_to_focus")]
59 #[doc(alias = "get_scroll_to_focus")]
60 #[doc(alias = "scroll-to-focus")]
61 pub fn is_scroll_to_focus(&self) -> bool {
62 unsafe { from_glib(ffi::gtk_viewport_get_scroll_to_focus(self.to_glib_none().0)) }
63 }
64
65 #[cfg(feature = "v4_12")]
66 #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
67 #[doc(alias = "gtk_viewport_scroll_to")]
68 pub fn scroll_to(&self, descendant: &impl IsA<Widget>, scroll: Option<ScrollInfo>) {
69 unsafe {
70 ffi::gtk_viewport_scroll_to(
71 self.to_glib_none().0,
72 descendant.as_ref().to_glib_none().0,
73 scroll.into_glib_ptr(),
74 );
75 }
76 }
77
78 #[doc(alias = "gtk_viewport_set_child")]
79 #[doc(alias = "child")]
80 pub fn set_child(&self, child: Option<&impl IsA<Widget>>) {
81 unsafe {
82 ffi::gtk_viewport_set_child(
83 self.to_glib_none().0,
84 child.map(|p| p.as_ref()).to_glib_none().0,
85 );
86 }
87 }
88
89 #[doc(alias = "gtk_viewport_set_scroll_to_focus")]
90 #[doc(alias = "scroll-to-focus")]
91 pub fn set_scroll_to_focus(&self, scroll_to_focus: bool) {
92 unsafe {
93 ffi::gtk_viewport_set_scroll_to_focus(
94 self.to_glib_none().0,
95 scroll_to_focus.into_glib(),
96 );
97 }
98 }
99
100 #[doc(alias = "child")]
101 pub fn connect_child_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
102 unsafe extern "C" fn notify_child_trampoline<F: Fn(&Viewport) + 'static>(
103 this: *mut ffi::GtkViewport,
104 _param_spec: glib::ffi::gpointer,
105 f: glib::ffi::gpointer,
106 ) {
107 let f: &F = &*(f as *const F);
108 f(&from_glib_borrow(this))
109 }
110 unsafe {
111 let f: Box_<F> = Box_::new(f);
112 connect_raw(
113 self.as_ptr() as *mut _,
114 c"notify::child".as_ptr() as *const _,
115 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
116 notify_child_trampoline::<F> as *const (),
117 )),
118 Box_::into_raw(f),
119 )
120 }
121 }
122
123 #[doc(alias = "scroll-to-focus")]
124 pub fn connect_scroll_to_focus_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
125 unsafe extern "C" fn notify_scroll_to_focus_trampoline<F: Fn(&Viewport) + 'static>(
126 this: *mut ffi::GtkViewport,
127 _param_spec: glib::ffi::gpointer,
128 f: glib::ffi::gpointer,
129 ) {
130 let f: &F = &*(f as *const F);
131 f(&from_glib_borrow(this))
132 }
133 unsafe {
134 let f: Box_<F> = Box_::new(f);
135 connect_raw(
136 self.as_ptr() as *mut _,
137 c"notify::scroll-to-focus".as_ptr() as *const _,
138 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
139 notify_scroll_to_focus_trampoline::<F> as *const (),
140 )),
141 Box_::into_raw(f),
142 )
143 }
144 }
145}
146
147impl Default for Viewport {
148 fn default() -> Self {
149 glib::object::Object::new::<Self>()
150 }
151}
152
153#[must_use = "The builder must be built to be used"]
158pub struct ViewportBuilder {
159 builder: glib::object::ObjectBuilder<'static, Viewport>,
160}
161
162impl ViewportBuilder {
163 fn new() -> Self {
164 Self {
165 builder: glib::object::Object::builder(),
166 }
167 }
168
169 pub fn child(self, child: &impl IsA<Widget>) -> Self {
170 Self {
171 builder: self.builder.property("child", child.clone().upcast()),
172 }
173 }
174
175 pub fn scroll_to_focus(self, scroll_to_focus: bool) -> Self {
176 Self {
177 builder: self.builder.property("scroll-to-focus", scroll_to_focus),
178 }
179 }
180
181 pub fn can_focus(self, can_focus: bool) -> Self {
182 Self {
183 builder: self.builder.property("can-focus", can_focus),
184 }
185 }
186
187 pub fn can_target(self, can_target: bool) -> Self {
188 Self {
189 builder: self.builder.property("can-target", can_target),
190 }
191 }
192
193 pub fn css_classes(self, css_classes: impl Into<glib::StrV>) -> Self {
194 Self {
195 builder: self.builder.property("css-classes", css_classes.into()),
196 }
197 }
198
199 pub fn css_name(self, css_name: impl Into<glib::GString>) -> Self {
200 Self {
201 builder: self.builder.property("css-name", css_name.into()),
202 }
203 }
204
205 pub fn cursor(self, cursor: &gdk::Cursor) -> Self {
206 Self {
207 builder: self.builder.property("cursor", cursor.clone()),
208 }
209 }
210
211 pub fn focus_on_click(self, focus_on_click: bool) -> Self {
212 Self {
213 builder: self.builder.property("focus-on-click", focus_on_click),
214 }
215 }
216
217 pub fn focusable(self, focusable: bool) -> Self {
218 Self {
219 builder: self.builder.property("focusable", focusable),
220 }
221 }
222
223 pub fn halign(self, halign: Align) -> Self {
224 Self {
225 builder: self.builder.property("halign", halign),
226 }
227 }
228
229 pub fn has_tooltip(self, has_tooltip: bool) -> Self {
230 Self {
231 builder: self.builder.property("has-tooltip", has_tooltip),
232 }
233 }
234
235 pub fn height_request(self, height_request: i32) -> Self {
236 Self {
237 builder: self.builder.property("height-request", height_request),
238 }
239 }
240
241 pub fn hexpand(self, hexpand: bool) -> Self {
242 Self {
243 builder: self.builder.property("hexpand", hexpand),
244 }
245 }
246
247 pub fn hexpand_set(self, hexpand_set: bool) -> Self {
248 Self {
249 builder: self.builder.property("hexpand-set", hexpand_set),
250 }
251 }
252
253 pub fn layout_manager(self, layout_manager: &impl IsA<LayoutManager>) -> Self {
254 Self {
255 builder: self
256 .builder
257 .property("layout-manager", layout_manager.clone().upcast()),
258 }
259 }
260
261 #[cfg(feature = "v4_18")]
262 #[cfg_attr(docsrs, doc(cfg(feature = "v4_18")))]
263 pub fn limit_events(self, limit_events: bool) -> Self {
264 Self {
265 builder: self.builder.property("limit-events", limit_events),
266 }
267 }
268
269 pub fn margin_bottom(self, margin_bottom: i32) -> Self {
270 Self {
271 builder: self.builder.property("margin-bottom", margin_bottom),
272 }
273 }
274
275 pub fn margin_end(self, margin_end: i32) -> Self {
276 Self {
277 builder: self.builder.property("margin-end", margin_end),
278 }
279 }
280
281 pub fn margin_start(self, margin_start: i32) -> Self {
282 Self {
283 builder: self.builder.property("margin-start", margin_start),
284 }
285 }
286
287 pub fn margin_top(self, margin_top: i32) -> Self {
288 Self {
289 builder: self.builder.property("margin-top", margin_top),
290 }
291 }
292
293 pub fn name(self, name: impl Into<glib::GString>) -> Self {
294 Self {
295 builder: self.builder.property("name", name.into()),
296 }
297 }
298
299 pub fn opacity(self, opacity: f64) -> Self {
300 Self {
301 builder: self.builder.property("opacity", opacity),
302 }
303 }
304
305 pub fn overflow(self, overflow: Overflow) -> Self {
306 Self {
307 builder: self.builder.property("overflow", overflow),
308 }
309 }
310
311 pub fn receives_default(self, receives_default: bool) -> Self {
312 Self {
313 builder: self.builder.property("receives-default", receives_default),
314 }
315 }
316
317 pub fn sensitive(self, sensitive: bool) -> Self {
318 Self {
319 builder: self.builder.property("sensitive", sensitive),
320 }
321 }
322
323 pub fn tooltip_markup(self, tooltip_markup: impl Into<glib::GString>) -> Self {
324 Self {
325 builder: self
326 .builder
327 .property("tooltip-markup", tooltip_markup.into()),
328 }
329 }
330
331 pub fn tooltip_text(self, tooltip_text: impl Into<glib::GString>) -> Self {
332 Self {
333 builder: self.builder.property("tooltip-text", tooltip_text.into()),
334 }
335 }
336
337 pub fn valign(self, valign: Align) -> Self {
338 Self {
339 builder: self.builder.property("valign", valign),
340 }
341 }
342
343 pub fn vexpand(self, vexpand: bool) -> Self {
344 Self {
345 builder: self.builder.property("vexpand", vexpand),
346 }
347 }
348
349 pub fn vexpand_set(self, vexpand_set: bool) -> Self {
350 Self {
351 builder: self.builder.property("vexpand-set", vexpand_set),
352 }
353 }
354
355 pub fn visible(self, visible: bool) -> Self {
356 Self {
357 builder: self.builder.property("visible", visible),
358 }
359 }
360
361 pub fn width_request(self, width_request: i32) -> Self {
362 Self {
363 builder: self.builder.property("width-request", width_request),
364 }
365 }
366
367 pub fn accessible_role(self, accessible_role: AccessibleRole) -> Self {
368 Self {
369 builder: self.builder.property("accessible-role", accessible_role),
370 }
371 }
372
373 pub fn hadjustment(self, hadjustment: &impl IsA<Adjustment>) -> Self {
374 Self {
375 builder: self
376 .builder
377 .property("hadjustment", hadjustment.clone().upcast()),
378 }
379 }
380
381 pub fn hscroll_policy(self, hscroll_policy: ScrollablePolicy) -> Self {
382 Self {
383 builder: self.builder.property("hscroll-policy", hscroll_policy),
384 }
385 }
386
387 pub fn vadjustment(self, vadjustment: &impl IsA<Adjustment>) -> Self {
388 Self {
389 builder: self
390 .builder
391 .property("vadjustment", vadjustment.clone().upcast()),
392 }
393 }
394
395 pub fn vscroll_policy(self, vscroll_policy: ScrollablePolicy) -> Self {
396 Self {
397 builder: self.builder.property("vscroll-policy", vscroll_policy),
398 }
399 }
400
401 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
404 pub fn build(self) -> Viewport {
405 assert_initialized_main_thread!();
406 self.builder.build()
407 }
408}