1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::any::Any;
use crate::decl::*;
use crate::gui::{privs::*, *};
use crate::prelude::*;
/// Switches between raw and dialog implementations.
///
/// Hierarchy: `BaseWnd` -> `(Raw|Dlg)Base` -> `(Raw|Dlg)Control` -> `WindowControl`.
#[derive(Clone)]
enum RawDlg {
Raw(RawControl),
Dlg(DlgControl),
}
/// An user child window, which can handle events. Can be programmatically
/// created or load a dialog resource from a `.res` file.
#[derive(Clone)]
pub struct WindowControl(RawDlg);
unsafe impl Send for WindowControl {}
impl AsRef<BaseWnd> for WindowControl {
fn as_ref(&self) -> &BaseWnd {
match &self.0 {
RawDlg::Raw(r) => r.raw_base().base(),
RawDlg::Dlg(d) => d.dlg_base().base(),
}
}
}
impl GuiWindow for WindowControl {
fn hwnd(&self) -> &HWND {
self.as_ref().hwnd()
}
fn as_any(&self) -> &dyn Any {
self
}
}
impl GuiParent for WindowControl {}
impl GuiControl for WindowControl {
fn ctrl_id(&self) -> u16 {
match &self.0 {
RawDlg::Raw(r) => r.ctrl_id(),
RawDlg::Dlg(d) => d.ctrl_id(),
}
}
}
impl WindowControl {
/// Instantiates a new `WindowControl` object, to be created internally with
/// [`HWND::CreateWindowEx`](crate::HWND::CreateWindowEx).
///
/// # Panics
///
/// Panics if the parent window was already created – that is, you cannot
/// dynamically create a `WindowControl` in an event closure.
#[must_use]
pub fn new(parent: &(impl GuiParent + 'static), opts: WindowControlOpts) -> Self {
if *parent.hwnd() != HWND::NULL {
panic!("Cannot create a custom child control after the parent window is created.");
}
Self(RawDlg::Raw(RawControl::new(parent, opts)))
}
/// Instantiates a new `WindowControl` object, to be loaded from a dialog
/// resource with
/// [`HINSTANCE::CreateDialogParam`](crate::HINSTANCE::CreateDialogParam).
///
/// If the parent window is a dialog, position is in Dialog Template Units;
/// otherwise in pixels, which will be multiplied to match current system
/// DPI.
///
/// # Panics
///
/// Panics if the parent dialog was already created – that is, you cannot
/// dynamically create a `WindowControl` in an event closure.
///
/// Panics if the creation process fails.
#[must_use]
pub fn new_dlg(
parent: &(impl GuiParent + 'static),
dlg_id: u16,
position: (i32, i32),
resize_behavior: (Horz, Vert),
ctrl_id: Option<u16>,
) -> Self {
if *parent.hwnd() != HWND::NULL {
panic!("Cannot create a custom child control after the parent window is created.");
}
Self(RawDlg::Dlg(DlgControl::new(parent, dlg_id, position, resize_behavior, ctrl_id)))
}
/// Exposes methods to handle window messages.
///
/// # Panics
///
/// Panics if the window is already created. Events must be set before
/// window creation.
#[must_use]
pub fn on(&self) -> &impl GuiEventsParent {
self.as_ref().on()
}
}