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
/*!
    Low level events functions
*/
/*
    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::mem;
use std::ptr;
use std::hash::Hash;
//use std::any::Any;

use winapi::{HWND, UINT, WPARAM, LPARAM, UINT_PTR, DWORD_PTR, LRESULT};

use ui::UiInner;

/// A magic number to identify the NWG subclass that dispatches events
const EVENTS_DISPATCH_ID: UINT_PTR = 2465;

/// A structure saved in a subclass to retrieve the ui in the callback
struct UiInnerWithId<ID: Hash+Clone+'static> {
  pub inner: *mut UiInner<ID>,
  pub id: u64,
}

/**
  Proc that dispatches the NWG events
*/
#[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)
  }
}

/**
    Add a subclass that dispatches the system event to the application callbacks to a window control.
*/
pub fn hook_window_events<ID: Hash+Clone+'static>(uiinner: &mut UiInner<ID>, id: u64, handle: HWND) { unsafe {
  use comctl32::SetWindowSubclass;

  // While definitely questionable in term of safeness, the reference to the UiInner is actually (always)
  // a raw pointer belonging to a Ui. Also, when the Ui goes out of scope, every window control
  // gets destroyed BEFORE the UiInner, this guarantees that uinner lives long enough.
  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));
}}

/**
  Remove a subclass and free the associated 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);
  }
}}

/**
    Dispatch the messages waiting the the system message queue to the associated Uis. This includes NWG custom messages.

    Return once a quit event was received.
*/
#[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); 
  }
}

/**
    Send a WM_QUIT to the system queue. Breaks the dispatch_events loop.
*/
#[inline(always)]
pub unsafe fn exit() {
  use user32::PostMessageW;
  use winapi::WM_QUIT;

  PostMessageW(ptr::null_mut(), WM_QUIT, 0, 0);
}