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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use std::any::TypeId;
use std::hash::Hash;
use winapi::HWND;
use ui::Ui;
use controls::{Control, ControlT, AnyHandle};
use error::Error;
use events::Event;
const WINDOW_CLASS_NAME: &'static str = "NWG_BUILTIN_WINDOW";
pub struct WindowT<S: Clone+Into<String>> {
pub title: S,
pub position: (i32, i32),
pub size: (u32, u32),
pub resizable: bool,
pub visible: bool,
pub disabled: bool,
pub exit_on_close: bool
}
impl<S: Clone+Into<String>, ID: Hash+Clone> ControlT<ID> for WindowT<S> {
fn type_id(&self) -> TypeId { TypeId::of::<Window>() }
fn events(&self) -> Vec<Event> {
vec![Event::Destroyed, Event::KeyDown, Event::KeyUp, Event::Char, Event::Closed]
}
#[allow(unused_variables)]
fn build(&self, ui: &Ui<ID>) -> Result<Box<Control>, Error> {
unsafe{
if let Err(e) = build_sysclass() { return Err(e); }
match build_window(&self) {
Ok(h) => { Ok( Box::new(Window{handle: h}) as Box<Control> ) },
Err(e) => Err(e)
}
}
}
}
#[allow(dead_code)]
pub struct Window {
handle: HWND,
}
impl Window {
pub fn close(&self) {
use user32::PostMessageW;
use winapi::WM_CLOSE;
unsafe{ PostMessageW(self.handle, WM_CLOSE, 0, 0) };
}
}
impl Control for Window {
fn handle(&self) -> AnyHandle {
AnyHandle::HWND(self.handle)
}
fn free(&mut self) {
use user32::DestroyWindow;
unsafe{ DestroyWindow(self.handle) };
}
}
use winapi::{UINT, WPARAM, LPARAM, LRESULT};
#[allow(unused_variables)]
unsafe extern "system" fn window_sysproc(hwnd: HWND, msg: UINT, w: WPARAM, l: LPARAM) -> LRESULT {
use winapi::{WM_CREATE, WM_CLOSE};
use user32::{DefWindowProcW, PostQuitMessage, ShowWindow};
use low::window_helper::get_window_long;
let handled = match msg {
WM_CREATE => true,
WM_CLOSE => {
ShowWindow(hwnd, 0);
let exit_on_close = get_window_long(hwnd) & 0x01 == 1;
if exit_on_close {
PostQuitMessage(0);
}
true
}
_ => false
};
if handled {
0
} else {
DefWindowProcW(hwnd, msg, w, l)
}
}
#[inline(always)]
unsafe fn build_sysclass() -> Result<(), Error> {
use low::window_helper::{SysclassParams, build_sysclass};
let params = SysclassParams{ class_name: WINDOW_CLASS_NAME, sysproc: Some(window_sysproc) };
if let Err(e) = build_sysclass(params) {
Err(Error::System(e))
} else {
Ok(())
}
}
#[inline(always)]
unsafe fn build_window<S: Clone+Into<String>>(t: &WindowT<S>) -> Result<HWND, Error> {
use low::window_helper::{WindowParams, build_window, set_window_long};
use winapi::{DWORD, WS_VISIBLE, WS_DISABLED, WS_OVERLAPPEDWINDOW, WS_CAPTION, WS_OVERLAPPED, WS_MINIMIZEBOX, WS_MAXIMIZEBOX, WS_SYSMENU};
let fixed_window: DWORD = WS_SYSMENU | WS_CAPTION | WS_OVERLAPPED | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
let flags: DWORD =
if t.visible { WS_VISIBLE } else { 0 } |
if t.disabled { WS_DISABLED } else { 0 } |
if !t.resizable { fixed_window } else { WS_OVERLAPPEDWINDOW } ;
let params = WindowParams {
title: t.title.clone().into(),
class_name: WINDOW_CLASS_NAME,
position: t.position.clone(),
size: t.size.clone(),
flags: flags
};
match build_window(params) {
Ok(h) => {
set_window_long(h, t.exit_on_close as usize);
Ok(h)
},
Err(e) => Err(Error::System(e))
}
}