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
use std::mem;
use std::ptr;
use std::hash::Hash;
use winapi::{HWND, UINT, WPARAM, LPARAM, UINT_PTR, DWORD_PTR, LRESULT};
use ui::UiInner;
const EVENTS_DISPATCH_ID: UINT_PTR = 2465;
struct UiInnerWithId<ID: Hash+Clone+'static> {
pub inner: *mut UiInner<ID>,
pub id: u64,
}
#[allow(unused_variables)]
unsafe extern "system" fn process_events<ID: Hash+Clone+'static>(hwnd: HWND, msg: UINT, w: WPARAM, l: LPARAM, id: UINT_PTR, data: DWORD_PTR) -> LRESULT {
use comctl32::{DefSubclassProc};
use winapi::{WM_KEYDOWN, WM_KEYUP, WM_UNICHAR, WM_CHAR, UNICODE_NOCHAR, WM_MENUCOMMAND, WM_CLOSE, c_int};
use events::{Event, EventArgs};
let handled = match msg {
WM_KEYDOWN | WM_KEYUP => {
let ui: &mut UiInnerWithId<ID> = mem::transmute(data);
let (inner, id) = (ui.inner, ui.id);
let evt = if msg == WM_KEYDOWN { Event::KeyDown } else { Event::KeyUp };
(&mut *inner).trigger(id, evt, EventArgs::Key(w as u32));
true
},
WM_MENUCOMMAND => {
let ui: &mut UiInnerWithId<ID> = mem::transmute(data);
let inner = ui.inner;
let id = ::low::menu_helper::get_menu_id( mem::transmute(l), w as c_int );
(&mut *inner).trigger(id, Event::Clicked, EventArgs::None);
true
},
WM_UNICHAR | WM_CHAR => {
let ui: &mut UiInnerWithId<ID> = mem::transmute(data);
let (inner, id) = (ui.inner, ui.id);
if w == UNICODE_NOCHAR {
return 1;
}
if let Some(c) = ::std::char::from_u32(w as u32) {
(&mut *inner).trigger(id, Event::Char, EventArgs::Char( c ));
}
true
},
WM_CLOSE => {
let ui: &mut UiInnerWithId<ID> = mem::transmute(data);
let (inner, id) = (ui.inner, ui.id);
(&mut *inner).trigger(id, Event::Closed, EventArgs::None);
false
},
_ => { false }
};
if handled {
0
} else {
DefSubclassProc(hwnd, msg, w, l)
}
}
pub fn hook_window_events<ID: Hash+Clone+'static>(uiinner: &mut UiInner<ID>, id: u64, handle: HWND) { unsafe {
use comctl32::SetWindowSubclass;
let ui_inner_raw: *mut UiInner<ID> = uiinner as *mut UiInner<ID>;
let data: *mut UiInnerWithId<ID> = Box::into_raw(Box::new(UiInnerWithId{ inner: ui_inner_raw, id: id }));
SetWindowSubclass(handle, Some(process_events::<ID>), EVENTS_DISPATCH_ID, mem::transmute(data));
}}
pub fn unhook_window_events<ID: Hash+Clone+'static>(handle: HWND) { unsafe {
use comctl32::{RemoveWindowSubclass, GetWindowSubclass};
use winapi::{TRUE, DWORD_PTR};
let mut data: DWORD_PTR = 0;
if GetWindowSubclass(handle, Some(process_events::<ID>), EVENTS_DISPATCH_ID, &mut data) == TRUE {
Box::from_raw(mem::transmute::<_, *mut UiInnerWithId<ID>>(data));
RemoveWindowSubclass(handle, Some(process_events::<ID>), EVENTS_DISPATCH_ID);
}
}}
#[inline(always)]
pub unsafe fn dispatch_events() {
use winapi::MSG;
use user32::{GetMessageW, TranslateMessage, DispatchMessageW};
let mut msg: MSG = mem::uninitialized();
while GetMessageW(&mut msg, ptr::null_mut(), 0, 0) != 0 {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
#[inline(always)]
pub unsafe fn exit() {
use user32::PostMessageW;
use winapi::WM_QUIT;
PostMessageW(ptr::null_mut(), WM_QUIT, 0, 0);
}