Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 2 // See the file tools.license for terms. 3 4 #ifndef toolx_Windows_pixwin 5 #define toolx_Windows_pixwin 6 7 #include <windows.h> 8 #include <windowsx.h> 9 #include <wingdi.h> 10 11 #include <tools/sg/device_interactor> 12 #include <tools/sg/keys> 13 //#include <tools/log_file> 14 15 #include <string> 16 17 #if defined(_MSC_VER) && _MSC_VER < 1900 18 #elif defined(__MINGW32__) 19 #else 20 #define TOOLX_WINDOWS_TOUCH 21 #endif 22 23 namespace toolx { 24 namespace Windows { 25 26 #ifdef TOOLX_WINDOWS_TOUCH 27 inline bool is_message_touch_event() { 28 // from https://stackoverflow.com/questions/ 29 static const LONG_PTR c_SIGNATURE_MASK = 0xF 30 static const LONG_PTR c_MOUSEEVENTF_FROMTOUC 31 LONG_PTR extraInfo = ::GetMessageExtraInfo() 32 return ( ( extraInfo & c_SIGNATURE_MASK ) == 33 } 34 #endif 35 36 class pixwin { 37 static const std::string& s_class() { 38 static const std::string s_v("toolx::Windo 39 return s_v; 40 } 41 static void register_class(){ 42 static bool s_done = false; //not const, t 43 if(!s_done) { 44 WNDCLASS wc; 45 wc.style = CS_HREDRAW | CS_VREDR 46 wc.lpfnWndProc = (WNDPROC)proc; 47 wc.cbClsExtra = 0; 48 wc.cbWndExtra = 0; 49 wc.hInstance = ::GetModuleHandle(NUL 50 wc.hIcon = LoadIcon(NULL,IDI_APP 51 wc.hCursor = LoadCursor(NULL,IDC_A 52 wc.hbrBackground = GetSysColorBrush(COLO 53 wc.lpszMenuName = (PTSTR)s_class().c_st 54 wc.lpszClassName = (PTSTR)s_class().c_st 55 ::RegisterClass(&wc); 56 s_done = true; 57 } 58 } 59 public: 60 virtual void resize(unsigned int,unsigned in 61 //virtual void paint(HDC) {} 62 virtual void paint() {} 63 virtual void close(){} 64 65 virtual void left_button_up(unsigned int a_x 66 virtual void left_button_down(unsigned int a 67 virtual void mouse_move(unsigned int a_x,uns 68 public: 69 pixwin(HWND a_parent,unsigned int a_w,unsign 70 :m_parent(a_parent) 71 ,m_hwnd(0) 72 ,m_touch_available(false) 73 ,m_interactor(0) 74 { 75 register_class(); 76 m_hwnd = ::CreateWindow((PTSTR)s_class().c 77 //m_hwnd = ::CreateWindowEx(WS_EX_LAYERED,s_ 78 NULL, 79 WS_CHILD | WS_VIS 80 0,0, 81 a_w,a_h, 82 m_parent,NULL, 83 GetWindowInstance 84 NULL); 85 if(!m_hwnd) return; 86 ::SetWindowLongPtr(m_hwnd,GWLP_USERDATA,LO 87 88 #ifdef TOOLX_WINDOWS_TOUCH 89 {BYTE digitizer_status = (BYTE)::GetSystemM 90 if((digitizer_status & (0x80 + 0x40)) == 0 91 m_touch_available = false; 92 } else { 93 //BYTE nInputs = (BYTE)::GetSystemMetric 94 m_touch_available = true; 95 if(!::RegisterTouchWindow(m_hwnd,0)) m_t 96 }} 97 #endif 98 99 } 100 virtual ~pixwin(){ 101 if(m_hwnd) { 102 ::SetWindowLongPtr(m_hwnd,GWLP_USERDATA, 103 ::DestroyWindow(m_hwnd); 104 m_hwnd = 0; 105 } 106 } 107 protected: 108 pixwin(const pixwin& a_from) 109 :m_parent(a_from.m_parent) 110 ,m_hwnd(0) 111 ,m_touch_available(a_from.m_touch_available) 112 ,m_interactor(0) 113 { 114 if(!m_parent) return; 115 register_class(); 116 RECT rect; 117 ::GetClientRect(m_parent,&rect); 118 m_hwnd = ::CreateWindow((PTSTR)s_class().c 119 NULL, 120 WS_CHILD | WS_VIS 121 0,0, 122 rect.right-rect.l 123 rect.bottom-rect. 124 m_parent,NULL, 125 GetWindowInstance 126 NULL); 127 if(!m_hwnd) return; 128 ::SetWindowLongPtr(m_hwnd,GWLP_USERDATA,LO 129 130 #ifdef TOOLX_WINDOWS_TOUCH 131 if(a_from.m_touch_available) { 132 if(!::RegisterTouchWindow(m_hwnd,0)) m_t 133 } 134 #endif 135 } 136 protected: 137 pixwin& operator=(const pixwin&){return *thi 138 public: 139 void set_client_area_size(unsigned int a_w,u 140 RECT rect; 141 ::GetClientRect(m_hwnd,&rect); 142 ::MoveWindow(m_hwnd,rect.left,rect.top,a_w 143 } 144 HWND hwnd() const {return m_hwnd;} 145 void wm_paint() {paint();} 146 public: 147 void set_device_interactor(tools::sg::device 148 protected: 149 bool is_touch_event() { 150 #ifdef TOOLX_WINDOWS_TOUCH 151 if(!m_touch_available) return false; 152 return is_message_touch_event(); 153 #else 154 return false; 155 #endif 156 } 157 protected: 158 static LRESULT CALLBACK proc(HWND a_hwnd,UIN 159 switch (a_msg) { 160 case WM_SIZE:{ 161 int width = LOWORD(a_lparam); 162 int height = HIWORD(a_lparam); 163 pixwin* _this = (pixwin*)::GetWindowLong 164 if(_this) { 165 _this->resize(width,height); 166 } else { 167 // CreateWindow send a WM_SIZE but GWL 168 } 169 }return 0; 170 case WM_PAINT:{ 171 pixwin* _this = (pixwin*)::GetWindowLong 172 if(_this) _this->wm_paint(); 173 }return 0; 174 175 case WM_KEYDOWN:{ 176 pixwin* _this = (pixwin*)::GetWindowLong 177 if(_this) { 178 if(_this->m_interactor) { 179 tools::sg::key_down_event event(_thi 180 _this->m_interactor->key_press(event 181 } 182 } 183 } return 0; 184 case WM_KEYUP:{ 185 pixwin* _this = (pixwin*)::GetWindowLong 186 if(_this) { 187 if(_this->m_interactor) { 188 tools::sg::key_up_event event(_this- 189 _this->m_interactor->key_release(eve 190 } 191 } 192 } return 0; 193 194 case WM_LBUTTONDOWN:{ 195 pixwin* _this = (pixwin*)::GetWindowLong 196 if(_this) { 197 if(_this->m_interactor) { 198 tools::sg::mouse_down_event event(LO 199 _this->m_interactor->mouse_press(eve 200 } else { 201 RECT rect; 202 ::GetClientRect(a_hwnd,&rect); 203 unsigned int h = rect.bottom-rect.to 204 if(!_this->is_touch_event()) _this-> 205 } 206 } 207 }return 0; 208 case WM_LBUTTONUP:{ 209 pixwin* _this = (pixwin*)::GetWindowLong 210 if(_this) { 211 if(_this->m_interactor) { 212 tools::sg::mouse_up_event event(LOWO 213 _this->m_interactor->mouse_release(e 214 } else { 215 RECT rect; 216 ::GetClientRect(a_hwnd,&rect); 217 unsigned int h = rect.bottom-rect.to 218 if(!_this->is_touch_event()) _this->left_b 219 } 220 } 221 } return 0; 222 case WM_MOUSEMOVE:{ 223 pixwin* _this = (pixwin*)::GetWindowLong 224 if(_this) { 225 if(_this->m_interactor) { 226 tools::sg::mouse_move_event event(LO 227 _this->m_interactor->mouse_move(even 228 } else { 229 WPARAM state = a_wparam; 230 bool ldown = ((state & MK_LBUTTON)== 231 RECT rect; 232 ::GetClientRect(a_hwnd,&rect); 233 unsigned int h = rect.bottom-rect.to 234 if(!_this->is_touch_event()) _this-> 235 } 236 } 237 238 }return 0; 239 240 case WM_MOUSEWHEEL:{ 241 pixwin* _this = (pixwin*)::GetWindowLong 242 if(_this) { 243 if(_this->m_interactor) { 244 tools::sg::wheel_rotate_event event( 245 _this->m_interactor->wheel_rotate(event); 246 } 247 } 248 } return 0; 249 250 #ifdef TOOLX_WINDOWS_TOUCH 251 case WM_TOUCH:{ 252 pixwin* _this = (pixwin*)::GetWindowLong 253 if(_this && _this->m_touch_available) { 254 RECT rect; 255 //::GetWindowRect(hwnd,&rect); ??? 256 ::GetClientRect(a_hwnd,&rect); 257 unsigned int h = rect.bottom-rect.top; 258 259 unsigned int num_inputs = (int)a_wpara 260 //::printf("debug : WM_TOUCH : 001 : % 261 TOUCHINPUT* ti = new TOUCHINPUT[num_in 262 POINT p; 263 if(::GetTouchInputInfo((HTOUCHINPUT)a_ 264 for(unsigned int i=0;i<num_inputs; + 265 p.x = TOUCH_COORD_TO_PIXEL(ti[i].x 266 p.y = TOUCH_COORD_TO_PIXEL(ti[i].y 267 if(!::ScreenToClient(a_hwnd,&p)) { 268 if(ti[i].dwFlags & TOUCHEVENTF_DOW 269 //::printf("debug : TOUCHEVENTF_ 270 _this->left_button_down(p.x,h-p. 271 } else if (ti[i].dwFlags & TOUCHEV 272 //::printf("debug : TOUCHEVENTF_ 273 _this->left_button_up(p.x,h-p.y) 274 } else if (ti[i].dwFlags & TOUCHEV 275 bool ldown = true; //we assume t 276 //::printf("debug : TOUCHEVENTF_ 277 _this->mouse_move(p.x,h-p.y,ldow 278 } 279 } 280 } 281 ::CloseTouchInputHandle((HTOUCHINPUT)a 282 delete [] ti; 283 } 284 }return 0; 285 #endif //TOOLX_WINDOWS_TOUCH 286 case WM_DESTROY:wm__destroy(a_hwnd);return 287 } 288 return (DefWindowProc(a_hwnd,a_msg,a_wpara 289 } 290 static bool SetWindowPixelFormat(HDC a_HDC){ 291 PIXELFORMATDESCRIPTOR pfd; 292 pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); 293 pfd.nVersion = 1; 294 pfd.dwFlags = 295 PFD_DRAW_TO_WINDOW | 296 PFD_SUPPORT_OPENGL | 297 PFD_DOUBLEBUFFER | 298 PFD_STEREO_DONTCARE; 299 pfd.iPixelType = PFD_TYPE_RGBA; 300 pfd.cColorBits = 32; 301 pfd.cRedBits = 8; 302 pfd.cRedShift = 16; 303 pfd.cGreenBits = 8; 304 pfd.cGreenShift = 8; 305 pfd.cBlueBits = 8; 306 pfd.cBlueShift = 0; 307 pfd.cAlphaBits = 0; 308 pfd.cAlphaShift = 0; 309 pfd.cAccumBits = 64; 310 pfd.cAccumRedBits = 16; 311 pfd.cAccumGreenBits = 16; 312 pfd.cAccumBlueBits = 16; 313 pfd.cAccumAlphaBits = 0; 314 pfd.cDepthBits = 32; 315 pfd.cStencilBits = 8; 316 pfd.cAuxBuffers = 0; 317 pfd.iLayerType = PFD_MAIN_PLANE; 318 pfd.bReserved = 0; 319 pfd.dwLayerMask = 0; 320 pfd.dwVisibleMask = 0; 321 pfd.dwDamageMask = 0; 322 323 int pixelIndex = ::ChoosePixelFormat(a_HDC 324 if (pixelIndex==0) { 325 // Let's choose a default index. 326 pixelIndex = 1; 327 if (::DescribePixelFormat(a_HDC, 328 pixelIndex, 329 sizeof(PIXELFO 330 &pfd)==0) { 331 return false; 332 } 333 } 334 335 if (::SetPixelFormat(a_HDC,pixelIndex,&pfd 336 337 return true; 338 } 339 static void wm__destroy(HWND a_hwnd) { 340 pixwin* _this = (pixwin*)::GetWindowLongPt 341 if(_this) { //How to be sure that we have 342 _this->close(); 343 if(_this->m_hwnd!=a_hwnd) { 344 //::printf("WinTk::Component::wm_destr 345 } 346 _this->m_hwnd = 0; 347 } 348 ::SetWindowLongPtr(a_hwnd,GWLP_USERDATA,LO 349 } 350 protected: 351 tools::key_code convert_key(WPARAM a_key) { 352 if(a_key==VK_SHIFT) return tools::sg::key_ 353 return (tools::key_code)a_key; 354 } 355 protected: 356 HWND m_parent; 357 HWND m_hwnd; 358 bool m_touch_available; 359 tools::sg::device_interactor* m_interactor; 360 }; 361 362 }} 363 364 365 #endif