fixed pointer casting errors
[swftools.git] / lib / gfxwindow_win32.c
blobae661b0d3e7e807b88ac290ff499c765eec4e322
1 /* gfxwindow.h
3 Simple GUI abstraction- Windows implementation
5 Part of the swftools package.
7 Copyright (c) 2005 Matthias Kramm <kramm@quiss.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
23 #include <windows.h>
24 #include <commctrl.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include "gfx.h"
29 static LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
31 static char szWinName[] = "MyWin";
32 static HDC hdc;
33 static HWND hwnd;
34 static char*text="";
35 static int textx;
36 static int texty;
37 static int a = 0;
38 static HDC dc, memDc;
39 static HBITMAP piccy;
41 static int sizex=640;
42 static int sizey=388;
43 static char *screen;
44 static char *offscreen;
45 static int screensize;
47 static DWORD threadID;
48 static DWORD mainthreadID;
49 static HANDLE thread;
50 static HINSTANCE me,prev;
51 static int nWinMode;
52 static int posx,posy;
53 static RECT desktopSize;
54 static int initialized = 0;
56 static int cwidth=640,cheight=388;
58 #define DIB
60 static void openWindow(int _sizex, int _sizey)
62 RECT r;int ok;
64 sizex = _sizex;
65 sizey = _sizey;
67 ok = (int)GetClientRect(GetDesktopWindow(), &r);
68 if(!ok) {
69 r.left = r.top = 0;
70 r.right = 1280;
71 r.bottom = 1024;
73 desktopSize = r;
75 hwnd=CreateWindow(szWinName,
76 0, //window title
77 WS_POPUP|WS_VISIBLE,
78 posx=100, //CW_USEDEFAULT,
79 posy=200, //CW_USEDEFAULT,
80 640,388,
81 HWND_DESKTOP,
82 NULL,me,NULL);
83 SendMessage(hwnd, 1024+5 /*SB_SETBORDERS*/, 0, 0);
85 ShowWindow(hwnd,nWinMode);
86 UpdateWindow(hwnd);
88 dc=GetDC(hwnd);
89 memDc=CreateCompatibleDC(dc);
90 // SetTimer(hwnd,1,1000,NULL);
91 // SetTimer(hwnd,1,33,NULL);
93 #ifdef DIB
94 void * ppvBits = 0;
95 BITMAPINFO info;
96 memset(&info, sizeof(info), 0);
97 info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
98 info.bmiHeader.biWidth = sizex;
99 info.bmiHeader.biHeight = sizey;
100 info.bmiHeader.biPlanes= 1;
101 info.bmiHeader.biBitCount = 24;
102 info.bmiHeader.biCompression = BI_RGB;
103 info.bmiHeader.biSizeImage = sizex*sizey*3;
105 piccy = CreateDIBSection(0, &info, DIB_RGB_COLORS, &ppvBits, 0,0);
106 screen = (char*)ppvBits;
107 offscreen = (char*)malloc(sizex*sizey*4);
108 screensize = sizex*sizey*3;
109 GdiFlush();
110 #else
111 screen = (char*)malloc(sizex*sizey*3);
112 offscreen = (char*)malloc(sizex*sizey*4);
113 screensize = sizex*sizey*3;
114 piccy = CreateBitmap(sizex, sizey, 1, 24, screen);
115 #endif
117 SelectObject(memDc,piccy);
121 static void closeWindow()
123 DeleteDC(memDc);
124 ReleaseDC(hwnd,dc);
125 KillTimer(hwnd,1);
128 static int initwin32()
130 MSG msg;
131 int ret;
132 WNDCLASS wcl;
134 me = GetModuleHandle(NULL);
135 prev = NULL;
136 nWinMode = SW_SHOW;
138 if(GetClassInfo(0, szWinName, &wcl)) {
139 /* already registered */
140 exit(1);
142 wcl.hInstance =me;
143 wcl.lpszClassName=szWinName;
144 wcl.lpfnWndProc =WindowFunc;
145 wcl.style =2;
146 wcl.hIcon =LoadIcon (NULL, IDI_HAND);
147 wcl.hCursor =LoadCursor(NULL, IDC_HAND);//IDC_CROSS);
148 wcl.lpszMenuName =NULL;
149 wcl.cbClsExtra =0;
150 wcl.cbWndExtra =0;
151 wcl.hbrBackground=(HBRUSH)GetStockObject(NULL_BRUSH);//WHITE_BRUSH);
153 if(!RegisterClass(&wcl))
154 exit(1);
156 //mainthreadID = GetCurrentThreadId();
158 //openWindow(640,388);
160 //thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)(maincaller), (/*data*/0), 0, (LPDWORD)&(threadID));
162 /*while(GetMessage(&msg,NULL,0,0))
164 TranslateMessage(&msg);
165 DispatchMessage(&msg);
168 //closeWindow();
169 //ret = msg.wParam;
171 initialized = 1;
173 return ret;
176 #define Q_REDRAW 0x7f01
177 #define Q_DUMMY 0x7f02
179 static LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
181 switch(message)
183 case WM_DESTROY:
184 PostQuitMessage(0);
185 break;
186 case WM_PAINT:
187 BitBlt(dc,0,0,sizex,sizey,memDc,0,0,SRCCOPY);
188 break;
189 case Q_DUMMY:
190 break;
191 case Q_REDRAW: {
192 int x,y;
193 for(y=0;y<sizey;y++) {
194 unsigned char*o = (unsigned char*)&offscreen[y*sizex*4];
195 unsigned char*s = (unsigned char*)&screen[(sizey-y-1)*sizex*3];
196 for(x=0;x<sizex;x++) {
197 s[0] = o[0];
198 s[1] = o[1];
199 s[2] = o[2];
200 s+=3;
201 o+=4;
205 #ifndef DIB
206 SetBitmapBits(piccy, screensize, screen);
207 InvalidateRect(hwnd,NULL,1);
208 #endif
210 #ifdef DIB
211 //SetDIBitsToDevice
212 BitBlt(dc,0,0,sizex,sizey,memDc,0,0,SRCCOPY);
213 #else
214 BitBlt(dc,0,0,sizex,sizey,memDc,0,0,SRCCOPY);
215 #endif
216 break;
218 case WM_TIMER:
219 //InvalidateRect(hwnd,NULL,1);
220 default:
221 return DefWindowProc(hwnd, message, wParam, lParam);
223 return 0;
226 static void ModeRGB_on(window_t*win)
228 MSG msg;
229 //printf("on()\n");
230 openWindow(sizex, sizey);
231 win->currentscr = (unsigned char*)offscreen;
233 while(GetMessage(&msg,NULL,0,0))
235 TranslateMessage(&msg);
236 DispatchMessage(&msg);
237 if(msg.message == WM_PAINT)
238 break;
241 static void ModeRGB_off(window_t*win)
243 MSG msg;
244 //printf("off()\n");
245 //TODO: shouldn't this be DestroyWindow(hwnd)?
246 PostMessage(hwnd, WM_DESTROY, 0, 0);
247 while(GetMessage(&msg,NULL,0,0))
249 TranslateMessage(&msg);
250 DispatchMessage(&msg);
251 if(msg.message == WM_DESTROY)
252 break;
254 closeWindow();
255 win->currentscr = win->lastscr = 0;
257 static void ModeRGB_flippage(window_t*win)
259 MSG msg;
260 PostMessage(hwnd, Q_REDRAW, 0, 0);
261 while(GetMessage(&msg,NULL,0,0))
263 TranslateMessage(&msg);
264 DispatchMessage(&msg);
265 if(msg.message == Q_REDRAW)
266 break;
268 win->currentscr = (byte*)offscreen;
270 static gfxevent_t ModeRGB_getEvent(window_t*win)
272 MSG msg;
273 WPARAM wParam;
274 LPARAM lParam;
275 gfxevent_t event;
277 //PostMessage(hwnd, Q_DUMMY, 0, 0);
279 event.type = GFXEVENT_NOTHING;
280 event.key = 0;
281 event.x = 0;
282 event.y = 0;
283 event.type = 0;
284 event.button = 0;
286 if(!PeekMessage(&msg,NULL,0,0,0))
287 return event;//nothing
289 while(GetMessage(&msg,NULL,0,0))
291 lParam = msg.lParam;
292 wParam = msg.wParam;
293 if(msg.message == Q_DUMMY) {
294 event.type = GFXEVENT_NOTHING;
295 break;
296 } else if(msg.message == WM_LBUTTONDOWN) {
297 event.type = GFXEVENT_MOUSEPRESS;
298 event.button = 1;
299 event.x = (signed short int)LOWORD(lParam);
300 event.y = (signed short int)HIWORD(lParam);
301 SetCapture(hwnd);
302 break;
303 } else if(msg.message == WM_LBUTTONUP) {
304 event.type = GFXEVENT_MOUSERELEASE;
305 event.button = 1;
306 event.x = (signed short int)LOWORD(lParam);
307 event.y = (signed short int)HIWORD(lParam);
308 ReleaseCapture();
309 break;
310 } else if(msg.message == WM_MBUTTONDOWN) {
311 event.type = GFXEVENT_MOUSEPRESS;
312 event.button = 2;
313 event.x = (signed short int)LOWORD(lParam);
314 event.y = (signed short int)HIWORD(lParam);
315 SetCapture(hwnd);
316 break;
317 } else if(msg.message == WM_MBUTTONUP) {
318 event.type = GFXEVENT_MOUSERELEASE;
319 event.button = 1;
320 event.x = (signed short int)LOWORD(lParam);
321 event.y = (signed short int)HIWORD(lParam);
322 ReleaseCapture();
323 break;
324 } else if(msg.message == WM_RBUTTONDOWN) {
325 event.type = GFXEVENT_MOUSEPRESS;
326 event.button = 3;
327 event.x = (signed short int)LOWORD(lParam);
328 event.y = (signed short int)HIWORD(lParam);
329 SetCapture(hwnd);
330 break;
331 } else if(msg.message == WM_RBUTTONUP) {
332 event.type = GFXEVENT_MOUSERELEASE;
333 event.button = 3;
334 event.x = (signed short int)LOWORD(lParam);
335 event.y = (signed short int)HIWORD(lParam);
336 ReleaseCapture();
337 break;
338 } else if(msg.message == WM_MOUSEMOVE) {
339 event.type = GFXEVENT_MOUSEMOVE;
340 event.x = (signed short int)LOWORD(lParam);
341 event.y = (signed short int)HIWORD(lParam);
342 break;
343 } else if(msg.message == WM_CHAR) {
344 event.type = GFXEVENT_KEYPRESS;
345 event.key = (char)wParam;
346 break;
347 } else {
348 TranslateMessage(&msg);
349 DispatchMessage(&msg);
350 event.type = GFXEVENT_NOTHING;
351 break;
354 return event;
356 static void ModeRGB_move(window_t*win, int relx,int rely)
358 MSG msg;
359 PostMessage(hwnd, Q_DUMMY, 0, 0);
360 while(GetMessage(&msg,NULL,0,0))
362 if(msg.message == WM_LBUTTONUP) {
363 SendMessage(hwnd, msg.message, msg.wParam, msg.lParam);
364 return;
366 TranslateMessage(&msg);
367 DispatchMessage(&msg);
368 if(msg.message == Q_DUMMY)
369 break;
371 posx+=relx;
372 posy+=rely;
374 if(posx > desktopSize.right-cwidth) posx = desktopSize.right-cwidth;
375 if(posy > desktopSize.bottom-cheight) posy = desktopSize.bottom-cheight;
376 if(posx < desktopSize.left) posx = desktopSize.left;
377 if(posy < desktopSize.top) posy = desktopSize.top;
379 SetWindowPos(hwnd, HWND_TOP, posx, posy, 0, 0, SWP_NOSIZE);
380 PostMessage(hwnd, Q_DUMMY, 0, 0);
381 while(GetMessage(&msg,NULL,0,0))
383 if(msg.message == WM_LBUTTONUP) {
384 SendMessage(hwnd, msg.message, msg.wParam, msg.lParam);
385 break;;
387 TranslateMessage(&msg);
388 DispatchMessage(&msg);
389 if(msg.message == Q_DUMMY)
390 break;
393 static void ModeRGB_resize(window_t*win, int width,int height)
395 if(width>sizex || height>sizey) {
396 printf("mode24::resize: can only make windows smaller\n");
397 return;
399 if(width<1) width=1;
400 if(height<1) height=1;
401 cwidth = width;
402 cheight = height;
403 SetWindowPos(hwnd, HWND_TOP, 0, 0, width, height, SWP_NOMOVE);
404 win->move(win,0,0);
408 window_t* window_new(window_t*win, int width, int height)
410 window_t*w = (window_t*)malloc(sizeof(window_t));
411 memset(w, 0, sizeof(window_t));
412 if(!initialized)
413 initwin32();
414 w->currentscr = w->lastscr = 0;
415 sizex = width;
416 sizey = height;
417 cwidth = sizex;
418 cheight = sizey;
419 //printf("contruct(%d, %d)\n", x, y);
420 w->on = ModeRGB_on;
421 w->off = ModeRGB_off;
422 w->move = ModeRGB_move;
423 w->resize = ModeRGB_resize;
424 w->flippage = ModeRGB_flippage;
425 w->getEvent = ModeRGB_getEvent;
426 w->width = width;
427 w->height = height;
428 return w;