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 */
29 static LRESULT CALLBACK
WindowFunc(HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
);
31 static char szWinName
[] = "MyWin";
44 static char *offscreen
;
45 static int screensize
;
47 static DWORD threadID
;
48 static DWORD mainthreadID
;
50 static HINSTANCE me
,prev
;
53 static RECT desktopSize
;
54 static int initialized
= 0;
56 static int cwidth
=640,cheight
=388;
60 static void openWindow(int _sizex
, int _sizey
)
67 ok
= (int)GetClientRect(GetDesktopWindow(), &r
);
75 hwnd
=CreateWindow(szWinName
,
78 posx
=100, //CW_USEDEFAULT,
79 posy
=200, //CW_USEDEFAULT,
83 SendMessage(hwnd
, 1024+5 /*SB_SETBORDERS*/, 0, 0);
85 ShowWindow(hwnd
,nWinMode
);
89 memDc
=CreateCompatibleDC(dc
);
90 // SetTimer(hwnd,1,1000,NULL);
91 // SetTimer(hwnd,1,33,NULL);
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;
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
);
117 SelectObject(memDc
,piccy
);
121 static void closeWindow()
128 static int initwin32()
134 me
= GetModuleHandle(NULL
);
138 if(GetClassInfo(0, szWinName
, &wcl
)) {
139 /* already registered */
143 wcl
.lpszClassName
=szWinName
;
144 wcl
.lpfnWndProc
=WindowFunc
;
146 wcl
.hIcon
=LoadIcon (NULL
, IDI_HAND
);
147 wcl
.hCursor
=LoadCursor(NULL
, IDC_HAND
);//IDC_CROSS);
148 wcl
.lpszMenuName
=NULL
;
151 wcl
.hbrBackground
=(HBRUSH
)GetStockObject(NULL_BRUSH
);//WHITE_BRUSH);
153 if(!RegisterClass(&wcl
))
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);
176 #define Q_REDRAW 0x7f01
177 #define Q_DUMMY 0x7f02
179 static LRESULT CALLBACK
WindowFunc(HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
187 BitBlt(dc
,0,0,sizex
,sizey
,memDc
,0,0,SRCCOPY
);
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
++) {
206 SetBitmapBits(piccy
, screensize
, screen
);
207 InvalidateRect(hwnd
,NULL
,1);
212 BitBlt(dc
,0,0,sizex
,sizey
,memDc
,0,0,SRCCOPY
);
214 BitBlt(dc
,0,0,sizex
,sizey
,memDc
,0,0,SRCCOPY
);
219 //InvalidateRect(hwnd,NULL,1);
221 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
226 static void ModeRGB_on(window_t
*win
)
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
)
241 static void ModeRGB_off(window_t
*win
)
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
)
255 win
->currentscr
= win
->lastscr
= 0;
257 static void ModeRGB_flippage(window_t
*win
)
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
)
268 win
->currentscr
= (byte
*)offscreen
;
270 static gfxevent_t
ModeRGB_getEvent(window_t
*win
)
277 //PostMessage(hwnd, Q_DUMMY, 0, 0);
279 event
.type
= GFXEVENT_NOTHING
;
286 if(!PeekMessage(&msg
,NULL
,0,0,0))
287 return event
;//nothing
289 while(GetMessage(&msg
,NULL
,0,0))
293 if(msg
.message
== Q_DUMMY
) {
294 event
.type
= GFXEVENT_NOTHING
;
296 } else if(msg
.message
== WM_LBUTTONDOWN
) {
297 event
.type
= GFXEVENT_MOUSEPRESS
;
299 event
.x
= (signed short int)LOWORD(lParam
);
300 event
.y
= (signed short int)HIWORD(lParam
);
303 } else if(msg
.message
== WM_LBUTTONUP
) {
304 event
.type
= GFXEVENT_MOUSERELEASE
;
306 event
.x
= (signed short int)LOWORD(lParam
);
307 event
.y
= (signed short int)HIWORD(lParam
);
310 } else if(msg
.message
== WM_MBUTTONDOWN
) {
311 event
.type
= GFXEVENT_MOUSEPRESS
;
313 event
.x
= (signed short int)LOWORD(lParam
);
314 event
.y
= (signed short int)HIWORD(lParam
);
317 } else if(msg
.message
== WM_MBUTTONUP
) {
318 event
.type
= GFXEVENT_MOUSERELEASE
;
320 event
.x
= (signed short int)LOWORD(lParam
);
321 event
.y
= (signed short int)HIWORD(lParam
);
324 } else if(msg
.message
== WM_RBUTTONDOWN
) {
325 event
.type
= GFXEVENT_MOUSEPRESS
;
327 event
.x
= (signed short int)LOWORD(lParam
);
328 event
.y
= (signed short int)HIWORD(lParam
);
331 } else if(msg
.message
== WM_RBUTTONUP
) {
332 event
.type
= GFXEVENT_MOUSERELEASE
;
334 event
.x
= (signed short int)LOWORD(lParam
);
335 event
.y
= (signed short int)HIWORD(lParam
);
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
);
343 } else if(msg
.message
== WM_CHAR
) {
344 event
.type
= GFXEVENT_KEYPRESS
;
345 event
.key
= (char)wParam
;
348 TranslateMessage(&msg
);
349 DispatchMessage(&msg
);
350 event
.type
= GFXEVENT_NOTHING
;
356 static void ModeRGB_move(window_t
*win
, int relx
,int rely
)
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
);
366 TranslateMessage(&msg
);
367 DispatchMessage(&msg
);
368 if(msg
.message
== Q_DUMMY
)
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
);
387 TranslateMessage(&msg
);
388 DispatchMessage(&msg
);
389 if(msg
.message
== Q_DUMMY
)
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");
400 if(height
<1) height
=1;
403 SetWindowPos(hwnd
, HWND_TOP
, 0, 0, width
, height
, SWP_NOMOVE
);
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
));
414 w
->currentscr
= w
->lastscr
= 0;
419 //printf("contruct(%d, %d)\n", x, y);
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
;