native_windows_gui2/controls/
handle_from_control.rs

1use super::{Button, CheckBox, ControlHandle, ImageFrame, Label, RadioButton, TextInput, Window};
2use std::convert::From;
3
4#[allow(unused)]
5use std::fmt::Display;
6
7macro_rules! handles {
8    ($control:ty) => {
9        #[allow(deprecated)]
10        impl From<&$control> for ControlHandle {
11            fn from(control: &$control) -> Self {
12                control.handle
13            }
14        }
15
16        #[allow(deprecated)]
17        impl From<&mut $control> for ControlHandle {
18            fn from(control: &mut $control) -> Self {
19                control.handle
20            }
21        }
22
23        #[allow(deprecated)]
24        impl PartialEq<ControlHandle> for $control {
25            fn eq(&self, other: &ControlHandle) -> bool {
26                self.handle == *other
27            }
28        }
29
30        #[allow(deprecated)]
31        impl PartialEq<$control> for ControlHandle {
32            fn eq(&self, other: &$control) -> bool {
33                *self == other.handle
34            }
35        }
36    };
37}
38
39/**
40Automatically implements the functionnalities required to process an external struct as a NWG control
41
42```rust
43#[macro_use] extern crate native_windows_gui2 as nwg;
44
45pub struct TestControl {
46    edit: nwg::TextInput,
47    custom_data: String,
48}
49
50subclass_control!(TestControl, TextInput, edit);
51```
52*/
53#[macro_export]
54macro_rules! subclass_control {
55    ($ty:ident, $base_type:ident, $field: ident) => {
56        impl ::std::ops::Deref for $ty {
57            type Target = $crate::$base_type;
58            fn deref(&self) -> &$crate::$base_type {
59                &self.$field
60            }
61        }
62
63        impl ::std::ops::DerefMut for $ty {
64            fn deref_mut(&mut self) -> &mut Self::Target {
65                &mut self.$field
66            }
67        }
68
69        impl Into<$crate::ControlHandle> for &$ty {
70            fn into(self) -> $crate::ControlHandle {
71                self.$field.handle.clone()
72            }
73        }
74
75        impl Into<$crate::ControlHandle> for &mut $ty {
76            fn into(self) -> $crate::ControlHandle {
77                self.$field.handle.clone()
78            }
79        }
80
81        impl PartialEq<$ty> for $crate::ControlHandle {
82            fn eq(&self, other: &$ty) -> bool {
83                *self == other.$field.handle
84            }
85        }
86    };
87}
88
89handles!(Window);
90handles!(Button);
91handles!(ImageFrame);
92handles!(Label);
93handles!(CheckBox);
94handles!(RadioButton);
95handles!(TextInput);
96
97#[cfg(feature = "textbox")]
98use super::TextBox;
99
100#[cfg(feature = "textbox")]
101handles!(TextBox);
102
103#[cfg(feature = "status-bar")]
104use super::StatusBar;
105
106#[cfg(feature = "status-bar")]
107handles!(StatusBar);
108
109#[cfg(feature = "tooltip")]
110use super::Tooltip;
111
112#[cfg(feature = "tooltip")]
113handles!(Tooltip);
114
115#[cfg(feature = "trackbar")]
116use super::TrackBar;
117
118#[cfg(feature = "trackbar")]
119handles!(TrackBar);
120
121#[cfg(feature = "menu")]
122use super::{Menu, MenuItem, MenuSeparator};
123
124#[cfg(feature = "menu")]
125handles!(Menu);
126#[cfg(feature = "menu")]
127handles!(MenuItem);
128#[cfg(feature = "menu")]
129handles!(MenuSeparator);
130
131#[cfg(feature = "combobox")]
132use super::ComboBox;
133
134#[cfg(feature = "combobox")]
135impl<D: Display + Default> From<&ComboBox<D>> for ControlHandle {
136    fn from(control: &ComboBox<D>) -> Self {
137        control.handle
138    }
139}
140
141#[cfg(feature = "combobox")]
142impl<D: Display + Default> PartialEq<ControlHandle> for ComboBox<D> {
143    fn eq(&self, other: &ControlHandle) -> bool {
144        self.handle == *other
145    }
146}
147
148#[cfg(feature = "combobox")]
149impl<D: Display + Default> PartialEq<ComboBox<D>> for ControlHandle {
150    fn eq(&self, other: &ComboBox<D>) -> bool {
151        *self == other.handle
152    }
153}
154
155#[cfg(feature = "listbox")]
156use super::ListBox;
157
158#[cfg(feature = "listbox")]
159impl<D: Display + Default> From<&ListBox<D>> for ControlHandle {
160    fn from(control: &ListBox<D>) -> Self {
161        control.handle
162    }
163}
164
165#[cfg(feature = "listbox")]
166impl<D: Display + Default> PartialEq<ControlHandle> for ListBox<D> {
167    fn eq(&self, other: &ControlHandle) -> bool {
168        self.handle == *other
169    }
170}
171
172#[cfg(feature = "listbox")]
173impl<D: Display + Default> PartialEq<ListBox<D>> for ControlHandle {
174    fn eq(&self, other: &ListBox<D>) -> bool {
175        *self == other.handle
176    }
177}
178
179#[cfg(feature = "tabs")]
180use super::{Tab, TabsContainer};
181
182#[cfg(feature = "tabs")]
183handles!(TabsContainer);
184
185#[cfg(feature = "tabs")]
186handles!(Tab);
187
188#[cfg(feature = "datetime-picker")]
189use super::DatePicker;
190
191#[cfg(feature = "datetime-picker")]
192handles!(DatePicker);
193
194#[cfg(feature = "progress-bar")]
195use super::ProgressBar;
196
197#[cfg(feature = "progress-bar")]
198handles!(ProgressBar);
199
200#[cfg(feature = "tree-view")]
201use super::TreeView;
202
203#[cfg(feature = "tree-view")]
204handles!(TreeView);
205
206#[cfg(feature = "tray-notification")]
207use super::TrayNotification;
208
209#[cfg(feature = "tray-notification")]
210handles!(TrayNotification);
211
212#[cfg(feature = "message-window")]
213use super::MessageWindow;
214
215#[cfg(feature = "message-window")]
216handles!(MessageWindow);
217
218#[cfg(feature = "timer")]
219#[allow(deprecated)]
220use super::Timer;
221
222#[cfg(feature = "timer")]
223handles!(Timer);
224
225#[cfg(feature = "animation-timer")]
226use super::AnimationTimer;
227
228#[cfg(feature = "animation-timer")]
229handles!(AnimationTimer);
230
231#[cfg(feature = "notice")]
232use super::Notice;
233
234#[cfg(feature = "notice")]
235handles!(Notice);
236
237#[cfg(feature = "list-view")]
238use super::ListView;
239
240#[cfg(feature = "list-view")]
241handles!(ListView);
242
243#[cfg(feature = "extern-canvas")]
244use super::ExternCanvas;
245
246#[cfg(feature = "extern-canvas")]
247handles!(ExternCanvas);
248
249#[cfg(feature = "frame")]
250use super::Frame;
251
252#[cfg(feature = "frame")]
253handles!(Frame);
254
255#[cfg(feature = "rich-textbox")]
256use super::RichTextBox;
257
258#[cfg(feature = "rich-textbox")]
259handles!(RichTextBox);
260
261#[cfg(feature = "rich-textbox")]
262use super::RichLabel;
263
264#[cfg(feature = "rich-textbox")]
265handles!(RichLabel);
266
267#[cfg(feature = "scroll-bar")]
268use super::ScrollBar;
269
270#[cfg(feature = "scroll-bar")]
271handles!(ScrollBar);
272
273#[cfg(feature = "number-select")]
274use super::NumberSelect;
275
276#[cfg(feature = "number-select")]
277handles!(NumberSelect);
278
279#[cfg(feature = "plotting")]
280use super::Plotters;
281
282#[cfg(feature = "plotting")]
283handles!(Plotters);