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