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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*!
    Various helper functions to create and interact with system window.
*/
/*
    Copyright (C) 2016  Gabriel Dubé

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

use std::ptr;
use std::mem;
use std::hash::Hash;

use winapi::{HWND, HFONT, HBRUSH, WNDPROC, DWORD, LPARAM, BOOL, c_int};

use ui::{UiInner, Ui};
use controls::{AnyHandle};
use low::other_helper::to_utf16;
use error::{Error, SystemError};

/**
    Params used to build a system class

    Members:  
    • `class_name`: System class name  
    • `sysproc`: The system class procedure  
    • `background`: If specified, the background color of the created window  
    • `style`: System class style  
*/
pub struct SysclassParams<S: Into<String>> {
    pub class_name: S,
    pub sysproc: WNDPROC,
    pub background: Option<HBRUSH>,
    pub style: Option<u32>
}

/**
    Params used to build a system window

    Members:  
    • `title`: The window title  
    • `class_name`: The system class to use  
    • `position`: The window starting position  
    • `size`: The window starting size  
    • `flags`: Window creation flags  
    • `parent`: Window parent. Can be null.  
*/
pub struct WindowParams<S1: Into<String>, S2: Into<String>> {
    pub title: S1,
    pub class_name: S2,
    pub position: (i32, i32),
    pub size: (u32, u32),
    pub flags: DWORD,
    pub ex_flags: Option<DWORD>,
    pub parent: HWND
}

/**
    Try to create a system class using the parameters provided in `SysclassParams`. Will not fail if
    the system class already exists.
    
    Returns `Err(SystemError::SysclassCreationFailed)` if the system class creation failed.

    Note that if the system class window proc used is malformed, the program will most likely segfault.
*/
pub unsafe fn build_sysclass<S: Into<String>>(p: SysclassParams<S>) -> Result<(), SystemError> {
    use kernel32::{GetModuleHandleW, GetLastError};
    use user32::{LoadCursorW, RegisterClassExW};
    use winapi::{WNDCLASSEXW, CS_HREDRAW, CS_VREDRAW, IDC_ARROW, COLOR_WINDOW, UINT, ERROR_CLASS_ALREADY_EXISTS};

    let hmod = GetModuleHandleW(ptr::null_mut());
    if hmod.is_null() { return Err(SystemError::SystemClassCreation); }

    let class_name = to_utf16(p.class_name.into().as_ref());

    let background: HBRUSH = match p.background {
        Some(bg) => bg,
        None => mem::transmute(COLOR_WINDOW as usize)
    };

    let style: UINT = match p.style {
        Some(s) => s as UINT,
        None=> CS_HREDRAW | CS_VREDRAW
    };

    let class =
    WNDCLASSEXW {
        cbSize: mem::size_of::<WNDCLASSEXW>() as UINT,
        style: style,
        lpfnWndProc: p.sysproc, 
        cbClsExtra: 0,
        cbWndExtra: 0,
        hInstance: hmod,
        hIcon: ptr::null_mut(),
        hCursor: LoadCursorW(ptr::null_mut(), IDC_ARROW),
        hbrBackground: background,
        lpszMenuName: ptr::null(),
        lpszClassName: class_name.as_ptr(),
        hIconSm: ptr::null_mut()
    };

    let class_token = RegisterClassExW(&class);
    if class_token == 0 && GetLastError() != ERROR_CLASS_ALREADY_EXISTS { 
        Err(SystemError::SystemClassCreation)
    } else {
        Ok(())
    }
}

/**
    Try to create a system class using the parameters provided in `WindowParams`.
    
    Returns `Ok(HWND)` where HWND is the newly created window handle
    Returns `Err(SystemError::WindowCreationFail)` if the system window creation failed.

    Note that if the system class window proc used is malformed, the program will most likely segfault.
*/
pub unsafe fn build_window<S1: Into<String>, S2: Into<String>>(p: WindowParams<S1, S2>) -> Result<HWND, SystemError>{
    use kernel32::GetModuleHandleW;
    use user32::{CreateWindowExW, GetDesktopWindow, GetWindowRect};
    use winapi::{WS_EX_COMPOSITED, RECT};

    let hmod = GetModuleHandleW(ptr::null_mut());
    if hmod.is_null() { return Err(SystemError::WindowCreationFail); }

    let class_name = to_utf16(p.class_name.into().as_ref());
    let window_name = to_utf16(p.title.into().as_ref());

    let px = match p.position.0 { 
        ::defs::CENTER_POSITION => {
            let mut rect: RECT = mem::uninitialized();
            let parent = if p.parent.is_null() { GetDesktopWindow() } else {p.parent};
            GetWindowRect(parent, &mut rect);
            (rect.right/2) - ((p.size.0/2) as i32)
        },
        x => x
    };

    let py = match p.position.1 { 
        ::defs::CENTER_POSITION => {
            let mut rect: RECT = mem::uninitialized();
            let parent = if p.parent.is_null() { GetDesktopWindow() } else {p.parent};
            GetWindowRect(parent, &mut rect);
            (rect.bottom/2) - ((p.size.1/2) as i32)
        },
        y => y
    };

    let ex_flags = match p.ex_flags {
        Some(ex) => ex,
        None => WS_EX_COMPOSITED
    };

    let handle = CreateWindowExW (
        ex_flags,
        class_name.as_ptr(), window_name.as_ptr(),
        p.flags,
        px, py,
        p.size.0 as i32, p.size.1 as i32,
        p.parent,
        ptr::null_mut(),
        hmod,
        ptr::null_mut()
    );

    if handle.is_null() {
        Err(SystemError::WindowCreationFail)
    } else {
        fix_overlapped_window_size(handle, p.size);
        Ok(handle)
    }
}

/** 
    Fix: Window size include the non client area. This behaviour is not wanted
    Resize the client area to match the "true" size. 
*/
unsafe fn fix_overlapped_window_size(handle: HWND, size: (u32, u32)) {
    use winapi::{RECT, SWP_NOMOVE, SWP_NOZORDER, c_int};
    use user32::{GetClientRect, SetWindowPos};

    let mut rect: RECT = mem::uninitialized();
    GetClientRect(handle, &mut rect);

    let (w, h) = (size.0 as c_int, size.1 as c_int);
    let delta_width = w - rect.right;
    let delta_height = h - rect.bottom;
    
    SetWindowPos(handle, ptr::null_mut(), 0, 0,
      w+delta_width, h+delta_height,
      SWP_NOMOVE|SWP_NOZORDER);
}


unsafe extern "system" fn list_children_window<ID: Clone+Hash+'static>(handle: HWND, params: LPARAM) -> BOOL {
    let &mut (inner, ref mut ids): &mut (*mut UiInner<ID>, Vec<u64>) = mem::transmute(params);

    // Check if the window belongs to the ui
    if let Some(id) = ::low::events::window_id(handle, inner) {
        ids.push(id)
    }

    1
}

/**
    Return the children control found in the window. Includes the window menubar if one is present.
*/
#[allow(unused_variables)]
pub unsafe fn list_window_children<ID: Clone+Hash>(handle: HWND, ui: *mut UiInner<ID>) -> Vec<u64> {
    use user32::{GetMenu, EnumChildWindows};
    use low::menu_helper::list_menu_children;

    let mut children = Vec::new();

    let menu = GetMenu(handle);
    if !menu.is_null() {
        children.append(&mut list_menu_children((&*ui), menu) );
    }

    let mut params: (*mut UiInner<ID>, Vec<u64>) = (ui, children);
    EnumChildWindows(handle, Some(list_children_window::<ID>), mem::transmute(&mut params));

    params.1
}

/// Set the font of a window
pub unsafe fn set_window_font(handle: HWND, font_handle: Option<HFONT>, redraw: bool) {
    use user32::SendMessageW;
    use winapi::{WM_SETFONT, LPARAM};

    let font_handle = font_handle.unwrap_or(ptr::null_mut());

    SendMessageW(handle, WM_SETFONT, mem::transmute(font_handle), redraw as LPARAM);
}

/// Get the window text
#[inline(always)]
pub unsafe fn get_window_text(handle: HWND) -> String {
    use user32::{GetWindowTextW, GetWindowTextLengthW};
    use low::other_helper::from_utf16;

    let mut buffer_size = GetWindowTextLengthW(handle) as usize;
    if buffer_size == 0 { return String::new(); }

    buffer_size += 1;
    let mut buffer: Vec<u16> = Vec::with_capacity(buffer_size);
    buffer.set_len(buffer_size);

    if GetWindowTextW(handle, buffer.as_mut_ptr(), buffer_size as c_int) == 0 {
        String::new()
    } else {
        from_utf16(&buffer[..])
    }
}

/// Set the window text
#[inline(always)]
pub unsafe fn set_window_text<'a>(handle: HWND, text: &'a str) {
    use user32::SetWindowTextW;
    use low::other_helper::to_utf16;

    let text = to_utf16(text);
    SetWindowTextW(handle, text.as_ptr());
}


/// Set window position
#[inline(always)]
pub unsafe fn set_window_position(handle: HWND, x: i32, y: i32) {
    use user32::SetWindowPos;
    use winapi::{c_int, SWP_NOZORDER, SWP_NOSIZE, SWP_NOACTIVATE};

    SetWindowPos(handle, ptr::null_mut(), x as c_int, y as c_int, 0, 0, SWP_NOZORDER|SWP_NOSIZE|SWP_NOACTIVATE);
}

/// Get window position
#[inline(always)]
pub unsafe fn get_window_position(handle: HWND) -> (i32, i32) {
    use user32::{GetWindowRect, ScreenToClient, GetParent};
    use winapi::{RECT, POINT};
    
    let mut r: RECT = mem::uninitialized();
    GetWindowRect(handle, &mut r);

    let parent = GetParent(handle);
    if !parent.is_null() {
        let mut pt = POINT{x: r.left, y: r.top};
        ScreenToClient(parent, &mut pt);
        (pt.x as i32, pt.y as i32)
    } else {
        (r.left as i32, r.top as i32)
    }
}

/// Set window size
#[inline(always)]
pub unsafe fn set_window_size(handle: HWND, w: u32, h: u32, fix: bool) {
    use user32::SetWindowPos;
    use winapi::{c_int, SWP_NOZORDER, SWP_NOMOVE, SWP_NOACTIVATE};

    SetWindowPos(handle, ptr::null_mut(), 0, 0, w as c_int, h as c_int, SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE);

    if fix { fix_overlapped_window_size(handle, (w, h)); }
}

/// Get window size
#[inline(always)]
pub unsafe fn get_window_size(handle: HWND) -> (u32, u32) {
    use user32::GetClientRect;
    use winapi::RECT;
    
    let mut r: RECT = mem::uninitialized();
    GetClientRect(handle, &mut r);

    (r.right as u32, r.bottom as u32)
}

/// Get the window enabled state
#[inline(always)]
pub unsafe fn get_window_enabled(handle: HWND) -> bool {
    use winapi::{GWL_STYLE, WS_DISABLED, UINT};

    let style = get_window_long(handle, GWL_STYLE) as UINT;
    (style & WS_DISABLED) != WS_DISABLED
}

/// Set the window enabled state
#[inline(always)]
pub unsafe fn set_window_enabled(handle: HWND, enabled: bool) {
    use winapi::{GWL_STYLE, WS_DISABLED};
    use user32::{UpdateWindow, InvalidateRect};

    let old_style = get_window_long(handle, GWL_STYLE) as usize;
    if enabled {
        set_window_long(handle, GWL_STYLE, old_style&(!WS_DISABLED as usize) );
    } else {
        set_window_long(handle, GWL_STYLE, old_style|(WS_DISABLED as usize));
    }

    // Tell the control to redraw itself to show the new style.
    InvalidateRect(handle, ptr::null(), 1);
    UpdateWindow(handle);
}

/// Set window visibility
#[inline(always)]
pub unsafe fn set_window_visibility(handle: HWND, visible: bool) {
    use user32::ShowWindow;
    use winapi::{SW_HIDE, SW_SHOW};

    let visible = if visible { SW_SHOW } else { SW_HIDE };
    ShowWindow(handle, visible);
}

/**
    Get window visibility
*/
#[inline(always)]
pub unsafe fn get_window_visibility(handle: HWND) -> bool {
    use user32::IsWindowVisible;
    IsWindowVisible(handle) != 0
}


#[inline(always)]
pub fn handle_of_window<ID: Clone+Hash>(ui: &Ui<ID>, id: &ID, err: &'static str) -> Result<HWND, Error> {
    match ui.handle_of(id) {
        Ok(AnyHandle::HWND(h)) => Ok(h),
        Ok(_) => Err(Error::BadParent(err.to_string())),
        Err(e) => Err(e)
    }
}

#[inline(always)]
pub fn handle_of_font<ID: Clone+Hash>(ui: &Ui<ID>, id: &ID, err: &'static str) -> Result<HFONT, Error> {
    match ui.handle_of(id) {
        Ok(AnyHandle::HFONT(h)) => Ok(h),
        Ok(_) => Err(Error::BadResource(err.to_string())),
        Err(e) => Err(e)
    }
} 

#[cfg(target_arch = "x86")] use winapi::LONG;
#[cfg(target_arch = "x86_64")] use winapi::LONG_PTR;

#[inline(always)]
#[cfg(target_arch = "x86_64")]
pub fn get_window_long(handle: HWND, index: c_int) -> LONG_PTR {
    use user32::GetWindowLongPtrW;
    unsafe{ GetWindowLongPtrW(handle, index) }
}

#[inline(always)]
#[cfg(target_arch = "x86")]
pub fn get_window_long(handle: HWND, index: c_int) -> LONG {
    use user32::GetWindowLongW;
    unsafe { GetWindowLongW(handle, index) }
}

#[inline(always)]
#[cfg(target_arch = "x86_64")]
pub fn set_window_long(handle: HWND, index: c_int, v: usize) {
    use user32::SetWindowLongPtrW;
    unsafe{ SetWindowLongPtrW(handle, index, v as LONG_PTR); }
}

#[inline(always)]
#[cfg(target_arch = "x86")]
pub fn set_window_long(handle: HWND, index: c_int, v: usize) {
    use user32::SetWindowLongW;
    unsafe { SetWindowLongW(handle, index, v as LONG); }
}