comparison deps/Platinum/ThirdParty/Neptune/Source/System/Win32/NptWin32MessageQueue.cpp @ 0:3425707ddbf6

Initial import (hopefully this mercurial stuff works...)
author fraserofthenight
date Mon, 06 Jul 2009 08:06:28 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3425707ddbf6
1 /*****************************************************************
2 |
3 | Neptune - Win32 Message Queue
4 |
5 | (c) 2001-2008 Gilles Boccon-Gibod
6 | Author: Gilles Boccon-Gibod (bok@bok.net)
7 |
8 ****************************************************************/
9
10 /*----------------------------------------------------------------------
11 | includes
12 +---------------------------------------------------------------------*/
13 #include "NptWin32MessageQueue.h"
14
15 /*----------------------------------------------------------------------
16 | platform adaptation
17 +---------------------------------------------------------------------*/
18 #if defined(_WIN32_WCE)
19 #define GetWindowLongPtr GetWindowLong
20 #define SetWindowLongPtr SetWindowLong
21 #endif
22
23 /*----------------------------------------------------------------------
24 | constants
25 +---------------------------------------------------------------------*/
26 const int NPT_WIN32_MESSAGE_ID_BASE = WM_USER + 9200;
27
28 /*----------------------------------------------------------------------
29 | NPT_Win32WindowMessageQueue
30 +---------------------------------------------------------------------*/
31 NPT_Win32WindowMessageQueue::NPT_Win32WindowMessageQueue()
32 {
33 // create a hidden window to process our incoming messages
34 WNDCLASS wclass;
35
36 // compute a unique class name
37 m_ClassName[0] = 'N';
38 m_ClassName[1] = 'P';
39 m_ClassName[2] = 'T';
40 m_ClassName[3] = 'H';
41 m_ClassName[4] = 'W';
42 NPT_String tid = NPT_String::FromInteger(GetCurrentThreadId());
43 for (unsigned int i=0; i<=tid.GetLength(); i++) {
44 m_ClassName[5+i] = tid.GetChars()[i];
45 }
46
47 // register a window class
48 wclass.style = 0;
49 wclass.lpfnWndProc = NPT_Win32WindowMessageQueue::WindowProcedure;
50 wclass.cbClsExtra = 0;
51 wclass.cbWndExtra = 0;
52 wclass.hInstance = GetModuleHandle(NULL);
53 wclass.hIcon = NULL;
54 wclass.hCursor = NULL;
55 wclass.hbrBackground = NULL;
56 wclass.lpszMenuName = NULL;
57 wclass.lpszClassName = m_ClassName;
58
59 // register the class and ignore any error because we might
60 // be registering the class more than once
61 RegisterClass(&wclass);
62
63 // create the hidden window
64 m_WindowHandle = CreateWindow(
65 wclass.lpszClassName, // pointer to registered class name
66 TEXT(""), // pointer to window name
67 0, // window style
68 0, // horizontal position of window
69 0, // vertical position of window
70 0, // window width
71 0, // window height
72 NULL, // handle to parent or owner window
73 NULL, // handle to menu or child-window identifier
74 wclass.hInstance, // handle to application instance
75 NULL);
76
77 // set a pointer to ourself as user data */
78 #if defined(_MSC_VER)
79 #pragma warning( push )
80 #pragma warning( disable: 4244) // we have to test for this because SetWindowLongPtr
81 // is incorrectly defined, so we'll get a C4244 warning
82 #endif // _MSC_VER
83 if (m_WindowHandle) {
84 SetWindowLongPtr(m_WindowHandle, GWL_USERDATA, NPT_POINTER_TO_LONG(this));
85 }
86 #if defined(_MSC_VER)
87 #pragma warning( pop )
88 #endif // _MSC_VER
89 m_hInstance = wclass.hInstance;
90 }
91
92 /*----------------------------------------------------------------------
93 | NPT_Win32WindowMessageQueue
94 +---------------------------------------------------------------------*/
95 NPT_Win32WindowMessageQueue::~NPT_Win32WindowMessageQueue()
96 {
97 // remove ourself as user data to ensure we're not called anymore
98 SetWindowLongPtr(m_WindowHandle, GWL_USERDATA, 0);
99
100 // destroy the hidden window
101 DestroyWindow(m_WindowHandle);
102
103 // unregister the window class
104 UnregisterClass(m_ClassName, m_hInstance);
105 }
106
107 /*----------------------------------------------------------------------
108 | NPT_Win32WindowMessageQueue::WindowProcedure
109 +---------------------------------------------------------------------*/
110 LRESULT CALLBACK
111 NPT_Win32WindowMessageQueue::WindowProcedure(HWND window,
112 UINT message,
113 WPARAM wparam,
114 LPARAM lparam)
115 {
116 // if it is a windows message, just pass it along
117 if (message != (UINT) NPT_WIN32_MESSAGE_ID_BASE) {
118 return DefWindowProc(window, message, wparam, lparam);
119 }
120
121 // dispatch the message to the handler
122 #if defined(_MSC_VER)
123 #pragma warning( push )
124 #pragma warning( disable: 4312) // we have to test for this because GetWindowLongPtr
125 // is incorrectly defined, so we'll get a C4244 warning
126 #endif // _MSC_VER
127 NPT_Win32WindowMessageQueue* queue = reinterpret_cast<NPT_Win32WindowMessageQueue *>(GetWindowLongPtr(window, GWL_USERDATA));
128 #if defined(_MSC_VER)
129 #pragma warning( pop )
130 #endif // _MSC_VER
131 if (queue == NULL) {
132 return 0;
133 }
134 queue->HandleMessage(reinterpret_cast<NPT_Message*>(lparam),
135 reinterpret_cast<NPT_MessageHandler*>(wparam));
136 return 0;
137 }
138
139 /*----------------------------------------------------------------------
140 | NPT_Win32WindowMessageQueue::PumpMessage
141 +---------------------------------------------------------------------*/
142 NPT_Result
143 NPT_Win32WindowMessageQueue::PumpMessage(NPT_Timeout)
144 {
145 // you cannot pump messages on this type of queue, since they will
146 // be pumped by the main windows message loop
147 return NPT_ERROR_NOT_SUPPORTED;
148 }
149
150 /*----------------------------------------------------------------------
151 | NPT_Win32WindowMessageQueue::QueueMessage
152 +---------------------------------------------------------------------*/
153 NPT_Result
154 NPT_Win32WindowMessageQueue::QueueMessage(NPT_Message* message,
155 NPT_MessageHandler* handler)
156 {
157 int result;
158
159 result = ::PostMessage(m_WindowHandle,
160 NPT_WIN32_MESSAGE_ID_BASE,
161 (WPARAM)handler,
162 (LPARAM)message);
163
164 if (result == 0) return NPT_FAILURE;
165 return NPT_SUCCESS;
166 }
167
168 /*----------------------------------------------------------------------
169 | NPT_Win32WindowMessageQueue::HandleMessage
170 +---------------------------------------------------------------------*/
171 NPT_Result
172 NPT_Win32WindowMessageQueue::HandleMessage(NPT_Message* message,
173 NPT_MessageHandler* handler)
174 {
175 NPT_Result result = NPT_FAILURE;
176
177 if (message && handler) {
178 result = handler->HandleMessage(message);
179 }
180 delete message;
181 return result;
182 }
183