added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / fxwt / fxwt_win32.cpp
1 /*
2 This file is part of fxwt, the window system toolkit of 3dengfx.
3
4 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 /* main fxwt event handling and system abstraction.
22  *
23  * Author: John Tsiombikas 2004
24  * modified: Mihalis Georgoulopoulos 2006
25  */
26
27 #include "3dengfx_config.h"
28
29 #if GFX_LIBRARY == NATIVE && NATIVE_LIB == NATIVE_WIN32
30
31 #include "gfx_library.h"
32 #include "fxwt.hpp"
33 #include "3dengfx/3denginefx.hpp"
34 #include "common/err_msg.h"
35
36 using std::list;
37 using namespace fxwt;
38
39 long CALLBACK fxwt_win32_handle_event(HWND__ *win, unsigned int msg, unsigned int wparam, long lparam);
40 static void button_event(int bn, bool state, int x, int y);
41 static int vkey_to_keysym(unsigned int vkey);
42
43 extern HWND__ *fxwt_win32_win;
44 extern HDC__ *fxwt_win32_dc;
45
46 Vector2 fxwt::get_mouse_pos_normalized() {
47         return Vector2(0, 0);
48 }
49
50 void fxwt::set_window_title(const char *title) {
51         SetWindowText(fxwt_win32_win, title);
52 }
53
54 void fxwt::swap_buffers() {
55         SwapBuffers(fxwt_win32_dc);
56         Sleep(0);
57 }
58
59 int fxwt::main_loop() {
60         set_verbosity(3);
61
62         while(1) {
63                 if(!idle_handlers.empty()) {
64                         MSG event;
65                         while(PeekMessage(&event, 0, 0, 0, PM_REMOVE)) {
66                                 TranslateMessage(&event);
67                                 DispatchMessage(&event);
68                         }
69
70                         list<void (*)()>::iterator iter = idle_handlers.begin();
71                         while(iter != idle_handlers.end()) {
72                                 (*iter++)();
73                         }
74                 } else {
75                         MSG event;
76                         if(!GetMessage(&event, 0, 0, 0)) break;
77                         TranslateMessage(&event);
78                         DispatchMessage(&event);
79                 }
80         }
81
82         return 0;
83 }
84
85 long CALLBACK fxwt_win32_handle_event(HWND__ *win, unsigned int msg, unsigned int wparam, long lparam) {
86         static int window_mapped;
87
88         switch(msg) {
89         case WM_SHOWWINDOW:
90                 if(wparam) {
91                         window_mapped = 1;
92                 } else {
93             window_mapped = 0;
94                 }
95                 break;
96
97         case WM_PAINT:
98                 if(window_mapped) {
99                         list<void (*)()>::iterator iter = disp_handlers.begin();
100                         while(iter != disp_handlers.end()) {
101                                 (*iter++)();
102                         }
103                 }
104                 break;
105
106         case WM_CLOSE:
107                 exit(0);
108
109         case WM_KEYDOWN:
110                 {
111                         list<void (*)(int)>::iterator iter = keyb_handlers.begin();
112                         while(iter != keyb_handlers.end()) {
113                                 (*iter++)(vkey_to_keysym(wparam));
114                         }
115                 }
116                 break;
117
118         case WM_CHAR:
119                 {
120                         list<void (*)(int)>::iterator iter = keyb_handlers.begin();
121                         while(iter != keyb_handlers.end()) {
122                                 (*iter++)(wparam);
123                         }
124                 }
125                 break;
126
127         case WM_MOUSEMOVE:
128                 {
129                         list<void (*)(int, int)>::iterator iter = motion_handlers.begin();
130                         while(iter != motion_handlers.end()) {
131                                 (*iter++)(LOWORD(lparam), HIWORD(lparam));
132                         }
133                 }
134                 break;
135
136         case WM_MOUSEWHEEL:
137                 {
138                         int bn = HIWORD(wparam) > 0 ? BN_WHEELUP : BN_WHEELDOWN;
139                         button_event(bn, true, LOWORD(lparam), HIWORD(lparam));
140                 }
141                 break;
142
143         case WM_LBUTTONDOWN:
144                 button_event(BN_LEFT, true, LOWORD(lparam), HIWORD(lparam));
145                 break;
146                 
147         case WM_MBUTTONDOWN:
148                 button_event(BN_MIDDLE, true, LOWORD(lparam), HIWORD(lparam));
149                 break;
150                 
151         case WM_RBUTTONDOWN:
152                 button_event(BN_RIGHT, true, LOWORD(lparam), HIWORD(lparam));
153                 break;
154
155         case WM_LBUTTONUP:
156                 button_event(BN_LEFT, false, LOWORD(lparam), HIWORD(lparam));
157                 break;
158                 
159         case WM_MBUTTONUP:
160                 button_event(BN_MIDDLE, false, LOWORD(lparam), HIWORD(lparam));
161                 break;
162                 
163         case WM_RBUTTONUP:
164                 button_event(BN_RIGHT, false, LOWORD(lparam), HIWORD(lparam));
165                 break;
166
167
168         default:
169                 break;
170         //      DefWindowProc(win, msg, wparam, lparam);
171         }
172
173         return DefWindowProc(win, msg, wparam, lparam);
174 }
175
176 static void button_event(int bn, bool state, int x, int y) {
177         button_state[bn] = state;
178         list<void (*)(int, int, int, int)>::iterator iter = button_handlers.begin();
179         while(iter != button_handlers.end()) {
180                 (*iter++)(bn, state, x, y);
181         }
182 }
183
184
185 static int win32_keysyms[] = 
186 {
187 //      0               1               2               3               4               5               6               7               8               9
188         0,              0,              0,              0,              0,              0,              0,              0,              8,              9,              // 000
189         0,              0,              12,             13,             0,              0,              304,    306,    0,              19,             // 010
190         301,    0,              0,              0,              0,              0,              0,              27,             0,              0,              // 020
191         0,              0,              0,              280,    281,    279,    278,    276,    273,    275,    // 030
192         274,    0,              0,              0,              0,              0,              0,              0,              0,              0,              // 040
193         0,              0,              0,              0,              0,              0,              0,              0,              0,              0,              // 050
194         0,              0,              0,              0,              0,              0,              0,              0,              0,              0,              // 060
195         0,              0,              0,              0,              0,              0,              0,              0,              0,              0,              // 070
196         0,              0,              0,              0,              0,              0,              0,              0,              0,              0,              // 080
197         0,              311,    312,    0,              0,              0,              256,    257,    258,    259,    // 090
198         260,    261,    262,    263,    264,    265,    268,    270,    0,              269,    // 100
199         0,              267,    282,    283,    284,    285,    286,    287,    288,    289,    // 110
200         290,    291,    292,    293,    294,    295,    296,    0,              127,    0,              // 120
201         0,              0,              0,              0,              0,              0,              0,              0,              0,              0,              // 130
202         0,              0,              0,              0,              300,    302,                                                                    // 140
203 };
204
205 static int vkey_to_keysym(unsigned int vkey) {
206         if (vkey < 146) return win32_keysyms[vkey];
207         return 0;
208 }
209
210 #endif  // GFX_LIBRARY == NATIVE && NATIVE_LIB == NATIVE_WIN32