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