source: trunk/src/comctl32/animate.c@ 1100

Last change on this file since 1100 was 1100, checked in by cbratschi, 26 years ago

trackbar: WM_ENABLE speed improved, hotkey: finished, animate: first work

File size: 10.5 KB
Line 
1/* $Id: animate.c,v 1.5 1999-10-01 15:53:31 cbratschi Exp $ */
2/*
3 * Animation control
4 *
5 * Copyright 1998, 1999 Eric Kohl
6 * Copyright 1999 Achim Hasenmueller
7 * Copyright 1999 Christoph Bratschi
8 *
9 * Status: can't play videos through MCI -> WINMM stubs
10 * Version: 5.00
11 */
12
13
14#include "winbase.h"
15#include "commctrl.h"
16#include "comctl32.h"
17#include "animate.h"
18
19
20#define ANIMATE_GetInfoPtr(hwnd) ((ANIMATE_INFO*)GetWindowLongA(hwnd,0))
21#define TIMER_ID 1
22
23static BOOL
24ANIMATE_LoadRes(ANIMATE_INFO *infoPtr,HINSTANCE hInst,LPSTR lpName,BOOL unicode)
25{
26 HRSRC hrsrc;
27 HGLOBAL handle;
28 LPVOID lpAvi;
29
30 if (unicode)
31 {
32 WCHAR buf[4];
33
34 lstrcpyAtoW(buf,"AVI");
35 hrsrc = FindResourceW(hInst,(LPWSTR)lpName,buf);
36 } else hrsrc = FindResourceA(hInst,lpName,"AVI");
37 if (!hrsrc) return FALSE;
38
39 handle = LoadResource (hInst, hrsrc);
40 if (!handle) return FALSE;
41
42 lpAvi = LockResource (handle);
43 if (!lpAvi) return FALSE;
44
45 infoPtr->lpAvi = lpAvi;
46 infoPtr->hRes = handle;
47 infoPtr->cSize = SizeofResource(hInst,hrsrc);
48
49 return TRUE;
50}
51
52static BOOL
53ANIMATE_LoadFile(ANIMATE_INFO *infoPtr,LPSTR lpName,BOOL unicode)
54{
55 HANDLE handle;
56 HFILE hFile;
57 LPVOID lpAvi;
58
59 if (unicode)
60 {
61 hFile = CreateFileW((LPWSTR)lpName,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
62 if (!hFile) return FALSE;
63
64 handle = CreateFileMappingW(hFile,NULL,PAGE_READONLY | SEC_COMMIT,0,0,NULL);
65 } else
66 {
67 hFile = CreateFileA (lpName,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
68 if (!hFile) return FALSE;
69
70 handle = CreateFileMappingA(hFile,NULL,PAGE_READONLY | SEC_COMMIT,0,0,NULL);
71 }
72 if (!handle)
73 {
74 CloseHandle (hFile);
75 return FALSE;
76 }
77
78 lpAvi = MapViewOfFile (handle, FILE_MAP_READ, 0, 0, 0);
79 if (!lpAvi)
80 {
81 CloseHandle (hFile);
82 return FALSE;
83 }
84
85 infoPtr->lpAvi = lpAvi;
86 infoPtr->hFile = hFile;
87 infoPtr->cSize = GetFileSize(hFile,NULL);
88
89 return TRUE;
90}
91
92static VOID
93ANIMATE_GetAviInfo (ANIMATE_INFO *infoPtr)
94{
95
96
97}
98
99static VOID
100ANIMATE_FreeResource(ANIMATE_INFO *infoPtr)
101{
102 if (infoPtr->hFile)
103 {
104 UnmapViewOfFile (infoPtr->lpAvi);
105 CloseHandle (infoPtr->hFile);
106 infoPtr->lpAvi = NULL;
107 infoPtr->hFile = NULL;
108 } else if (infoPtr->hRes)
109 {
110 GlobalFree (infoPtr->hRes);
111 infoPtr->lpAvi = NULL;
112 infoPtr->hRes = NULL;
113 }
114 infoPtr->cSize = 0;
115}
116
117static BOOL
118ANIMATE_IsLoaded(ANIMATE_INFO *infoPtr)
119{
120 return (infoPtr->lpAvi != NULL);
121}
122
123static BOOL
124ANIMATE_IsPlaying(ANIMATE_INFO *infoPtr)
125{
126 return infoPtr->bPlaying;
127}
128
129/* Message handlers */
130
131static LRESULT
132ANIMATE_Close(HWND hwnd,WPARAM wParam,LPARAM lParam)
133{
134 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hwnd);
135
136 ANIMATE_FreeResource(infoPtr);
137
138 return DefWindowProcA(hwnd,WM_CLOSE,wParam,lParam);
139}
140
141static LRESULT
142ANIMATE_Destroy (HWND hwnd, WPARAM wParam, LPARAM lParam)
143{
144 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hwnd);
145
146 /* free avi data */
147 ANIMATE_FreeResource(infoPtr);
148
149 /* free animate info data */
150 COMCTL32_Free(infoPtr);
151
152 return DefWindowProcA(hwnd,WM_DESTROY,wParam,lParam);
153}
154
155static LRESULT
156ANIMATE_NCCreate (HWND hwnd, WPARAM wParam, LPARAM lParam)
157{
158 ANIMATE_INFO *infoPtr;
159
160 /* allocate memory for info structure */
161 infoPtr = (ANIMATE_INFO *)COMCTL32_Alloc (sizeof(ANIMATE_INFO));
162
163 /* store pointer to info structure */
164 SetWindowLongA (hwnd,0,(DWORD)infoPtr);
165
166 /* set default settings */
167 infoPtr->lpAvi = NULL;
168 infoPtr->hFile = 0;
169 infoPtr->hRes = 0;
170 infoPtr->cSize = 0;
171 infoPtr->cRepeat = 0;
172 infoPtr->wFrom = 0;
173 infoPtr->wTo = 0;
174 infoPtr->wPos = 0;
175 infoPtr->bPlaying = FALSE;
176 infoPtr->cFrames = 0;
177 infoPtr->pos.x = 0;
178 infoPtr->pos.y = 0;
179 infoPtr->size.x = 0;
180 infoPtr->size.y = 0;
181 infoPtr->bThread = 0;
182
183 return DefWindowProcA(hwnd,WM_NCCREATE,wParam,lParam);
184}
185
186static LRESULT
187ANIMATE_EraseBackground (HWND hwnd, WPARAM wParam, LPARAM lParam)
188{
189 HBRUSH hBrush;
190 RECT rect;
191 HDC hdc = (HDC)wParam;
192
193 if (GetWindowLongA(hwnd,GWL_STYLE) & ACS_TRANSPARENT)
194 {
195 hBrush = SendMessageA(hwnd,WM_CTLCOLORSTATIC,hdc,hwnd);
196 } else hBrush = GetSysColorBrush(COLOR_WINDOW);
197
198 GetClientRect(hwnd,&rect);
199 FillRect((HDC)wParam,&rect,hBrush);
200
201 return TRUE;
202}
203
204static LRESULT
205ANIMATE_NCHitTest(HWND hwnd,WPARAM wParam,LPARAM lParam)
206{
207 return HTTRANSPARENT;
208}
209
210static VOID
211ANIMATE_DrawFrame(ANIMATE_INFO *infoPtr)
212{
213 //CB: todo
214 //MCI_PUT
215}
216
217static LRESULT
218ANIMATE_Paint(HWND hwnd,WPARAM wParam,LPARAM lParam)
219{
220 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hwnd);
221
222 if (ANIMATE_IsLoaded(infoPtr)) ANIMATE_DrawFrame(infoPtr);
223
224 return 0;
225}
226
227static LRESULT
228ANIMATE_Size(HWND hwnd,WPARAM wParam,LPARAM lParam)
229{
230 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hwnd);
231 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
232
233 if (ANIMATE_IsPlaying(infoPtr) && dwStyle & ACS_CENTER)
234 {
235 infoPtr->pos.x = (LOWORD(lParam)-infoPtr->size.x)/2;
236 infoPtr->pos.y = (HIWORD(lParam)-infoPtr->size.y)/2;
237 }
238
239 return DefWindowProcA(hwnd,WM_SIZE,wParam,lParam);
240}
241
242static LRESULT
243ANIMATE_StyleChanged(HWND hwnd,WPARAM wParam,LPARAM lParam)
244{
245 //CB: todo
246
247 return 0;
248}
249
250static LRESULT
251ANIMATE_Timer(HWND hwnd,WPARAM wParam,LPARAM lParam)
252{
253 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hwnd);
254 INT newPos = infoPtr->wPos+1;
255
256 if (wParam != TIMER_ID) return 0;
257
258 if (newPos == infoPtr->wTo || (infoPtr->wTo == -1 && newPos == infoPtr->cFrames-1))
259 {
260 if (infoPtr->cRepeat != -1)
261 {
262 infoPtr->cRepeat--;
263 if (infoPtr->cRepeat == 0) Animate_Stop(hwnd);
264 }
265 } else
266 {
267 infoPtr->wPos++;
268 ANIMATE_DrawFrame(infoPtr);
269 }
270
271 return 0;
272}
273
274/* Control messages */
275
276static LRESULT
277ANIMATE_Open(HWND hwnd,WPARAM wParam,LPARAM lParam,BOOL unicode)
278{
279 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hwnd);
280 HINSTANCE hInstance = (HINSTANCE)wParam;
281
282 ANIMATE_FreeResource(infoPtr);
283
284MessageBoxA(hwnd,"open",NULL,MB_OK);
285 if (!lParam) return TRUE;
286
287 if (HIWORD(lParam))
288 {
289 if (ANIMATE_LoadRes(infoPtr,hInstance,(LPSTR)lParam,unicode));
290 else if (ANIMATE_LoadFile(infoPtr,(LPSTR)lParam,unicode));
291 else return FALSE;
292 } else
293 {
294 if (!ANIMATE_LoadRes(infoPtr,hInstance,unicode ? (LPTSTR)MAKEINTRESOURCEW((INT)lParam):MAKEINTRESOURCEA((INT)lParam),unicode)) return FALSE;
295 }
296
297 ANIMATE_GetAviInfo(infoPtr);
298
299 if (GetWindowLongA(hwnd,GWL_STYLE) & ACS_AUTOPLAY) Animate_Play(hwnd,0,-1,1);
300
301 return TRUE;
302}
303
304static LRESULT
305ANIMATE_Play (HWND hwnd, WPARAM wParam, LPARAM lParam)
306{
307 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hwnd);
308 INT wFrom = (INT)LOWORD(lParam);
309 INT wTo = (INT)HIWORD(lParam);
310 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
311
312 if (!ANIMATE_IsLoaded(infoPtr)) return FALSE;
313
314 Animate_Stop(hwnd);
315
316 if (wFrom < 0) wFrom = 0;
317 if (wFrom > wTo && wTo != -1) return FALSE;
318 if (wFrom >= infoPtr->cFrames) wFrom = infoPtr->cFrames-1;
319 if (wTo >= infoPtr->cFrames) wTo = infoPtr->cFrames-1;
320
321 infoPtr->cRepeat = (INT)wParam;
322 infoPtr->wFrom = wFrom;
323 infoPtr->wTo = wTo;
324 infoPtr->wPos = infoPtr->wFrom;
325
326 if (dwStyle & ACS_CENTER)
327 {
328 RECT rect;
329
330 GetClientRect(hwnd,&rect);
331 infoPtr->pos.x = (rect.right-infoPtr->size.x)/2;
332 infoPtr->pos.y = (rect.bottom-infoPtr->size.y)/2;
333 } else
334 {
335 infoPtr->pos.x = 0;
336 infoPtr->pos.y = 0;
337 }
338
339 InvalidateRect(hwnd,NULL,FALSE);
340
341 if (wFrom == wTo)
342 {
343 SendMessageA(GetParent(hwnd),WM_COMMAND,MAKEWPARAM(GetWindowLongA(hwnd,GWL_ID),ACN_START),hwnd);
344 SendMessageA(GetParent(hwnd),WM_COMMAND,MAKEWPARAM(GetWindowLongA(hwnd,GWL_ID),ACN_STOP),hwnd);
345
346 return TRUE;
347 }
348
349 infoPtr->bPlaying = TRUE;
350
351 if (dwStyle & ACS_TIMER)
352 {
353 infoPtr->bThread = FALSE;
354 SetTimer(hwnd,TIMER_ID,infoPtr->msFrame,NULL);
355 } else
356 {
357 infoPtr->bThread = TRUE;
358 //CB:todo
359 }
360
361 SendMessageA(GetParent(hwnd),WM_COMMAND,MAKEWPARAM(GetWindowLongA(hwnd,GWL_ID),ACN_START),hwnd);
362
363 return TRUE;
364}
365
366static LRESULT
367ANIMATE_Stop (HWND hwnd, WPARAM wParam, LPARAM lParam)
368{
369 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hwnd);
370
371 if (!ANIMATE_IsLoaded(infoPtr)) return FALSE;
372 if (!ANIMATE_IsPlaying(infoPtr)) return TRUE;
373
374 if (infoPtr->bThread)
375 {
376 //CB:todo
377 } else
378 {
379 KillTimer(hwnd,TIMER_ID);
380 }
381
382 infoPtr->bPlaying = FALSE;
383 SendMessageA(GetParent(hwnd),WM_COMMAND,MAKEWPARAM(GetWindowLongA(hwnd,GWL_ID),ACN_STOP),hwnd);
384
385 return TRUE;
386}
387
388static LRESULT WINAPI
389ANIMATE_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
390{
391 switch (uMsg)
392 {
393 case ACM_OPENA:
394 return ANIMATE_Open(hwnd,wParam,lParam,FALSE);
395
396 case ACM_OPENW:
397 return ANIMATE_Open(hwnd,wParam,lParam,TRUE);
398
399 case ACM_PLAY:
400 return ANIMATE_Play (hwnd, wParam, lParam);
401
402 case ACM_STOP:
403 return ANIMATE_Stop (hwnd, wParam, lParam);
404
405 case WM_CLOSE:
406 return ANIMATE_Close(hwnd,wParam,lParam);
407
408 case WM_NCCREATE:
409 return ANIMATE_NCCreate(hwnd,wParam,lParam);
410
411 case WM_DESTROY:
412 return ANIMATE_Destroy (hwnd, wParam, lParam);
413
414 case WM_ERASEBKGND:
415 return ANIMATE_EraseBackground (hwnd, wParam, lParam);
416
417 case WM_NCHITTEST:
418 return ANIMATE_NCHitTest(hwnd,wParam,lParam);
419
420 case WM_PAINT:
421 return ANIMATE_Paint(hwnd,wParam,lParam);
422
423 case WM_SIZE:
424 return ANIMATE_Size(hwnd,wParam,lParam);
425
426 case WM_STYLECHANGED:
427 return ANIMATE_StyleChanged(hwnd,wParam,lParam);
428
429 case WM_TIMER:
430 return ANIMATE_Timer(hwnd,wParam,lParam);
431
432 default:
433// if (uMsg >= WM_USER)
434// ERR (animate, "unknown msg %04x wp=%08x lp=%08lx\n",
435// uMsg, wParam, lParam);
436 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
437 }
438
439 return 0;
440}
441
442
443VOID
444ANIMATE_Register (VOID)
445{
446 WNDCLASSA wndClass;
447
448 if (GlobalFindAtomA (ANIMATE_CLASSA)) return;
449
450 ZeroMemory (&wndClass, sizeof(WNDCLASSA));
451 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
452 wndClass.lpfnWndProc = (WNDPROC)ANIMATE_WindowProc;
453 wndClass.cbClsExtra = 0;
454 wndClass.cbWndExtra = sizeof(ANIMATE_INFO *);
455 wndClass.hCursor = LoadCursorA (0, IDC_ARROWA);
456 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
457 wndClass.lpszClassName = ANIMATE_CLASSA;
458
459 RegisterClassA (&wndClass);
460}
461
462
463VOID
464ANIMATE_Unregister (VOID)
465{
466 if (GlobalFindAtomA (ANIMATE_CLASSA))
467 UnregisterClassA (ANIMATE_CLASSA, (HINSTANCE)NULL);
468}
469
Note: See TracBrowser for help on using the repository browser.