2 * PROJECT: ReactOS Multimedia Player
3 * FILE: base\applications\mplay32\mplay32.c
4 * PROGRAMMERS: Dmitry Chapyshev (dmitry@reactos.org)
9 #define MAIN_WINDOW_HEIGHT 125
10 #define MAIN_WINDOW_MIN_WIDTH 250
12 HINSTANCE hInstance
= NULL
;
13 HWND hTrackBar
= NULL
;
15 TCHAR szAppTitle
[256] = _T("");
16 TCHAR szPrevFile
[MAX_PATH
] = _T("\0");
18 BOOL bIsOpened
= FALSE
;
19 BOOL bIsPaused
= FALSE
;
22 /* Known types table */
23 static const TYPEBYEXT ExtTypes
[] =
25 { _T(".wav"), WAVE_FILE
},
26 { _T(".wave"), WAVE_FILE
},
27 { _T(".mid"), MIDI_FILE
},
28 { _T(".midi"), MIDI_FILE
},
29 { _T(".cda"), AUDIOCD_FILE
},
30 { _T(".avi"), AVI_FILE
},
35 static const TBBUTTON Buttons
[] =
36 { /* iBitmap, idCommand, fsState, fsStyle, bReserved[2], dwData, iString */
37 {TBICON_PLAY
, IDC_PLAY
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
38 {TBICON_STOP
, IDC_STOP
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
39 {TBICON_EJECT
, IDC_EJECT
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
40 {15, 0, TBSTATE_ENABLED
, BTNS_SEP
, {0}, 0, 0},
41 {TBICON_BACKWARD
, IDC_BACKWARD
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
42 {TBICON_SEEKBACK
, IDC_SEEKBACK
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
43 {TBICON_SEEKFORW
, IDC_SEEKFORW
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
44 {TBICON_FORWARD
, IDC_FORWARD
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0}
48 SetImageList(HWND hwnd
)
50 HIMAGELIST hImageList
;
52 hImageList
= ImageList_Create(16, 16, ILC_MASK
| ILC_COLOR24
, 1, 1);
56 MessageBox(hwnd
, _T("ImageList it is not created!"), NULL
, MB_OK
);
60 ImageList_AddMasked(hImageList
,
61 LoadImage(hInstance
, MAKEINTRESOURCE(IDB_PLAYICON
), IMAGE_BITMAP
, 16, 16, LR_DEFAULTCOLOR
),
64 ImageList_AddMasked(hImageList
,
65 LoadImage(hInstance
, MAKEINTRESOURCE(IDB_STOPICON
), IMAGE_BITMAP
, 16, 16, LR_DEFAULTCOLOR
),
68 ImageList_AddMasked(hImageList
,
69 LoadImage(hInstance
, MAKEINTRESOURCE(IDB_EJECTICON
), IMAGE_BITMAP
, 16, 16, LR_DEFAULTCOLOR
),
72 ImageList_AddMasked(hImageList
,
73 LoadImage(hInstance
, MAKEINTRESOURCE(IDB_BACKWARDICON
), IMAGE_BITMAP
, 16, 16, LR_DEFAULTCOLOR
),
76 ImageList_AddMasked(hImageList
,
77 LoadImage(hInstance
, MAKEINTRESOURCE(IDB_SEEKBACKICON
), IMAGE_BITMAP
, 16, 16, LR_DEFAULTCOLOR
),
80 ImageList_AddMasked(hImageList
,
81 LoadImage(hInstance
, MAKEINTRESOURCE(IDB_SEEKFORWICON
), IMAGE_BITMAP
, 16, 16, LR_DEFAULTCOLOR
),
84 ImageList_AddMasked(hImageList
,
85 LoadImage(hInstance
, MAKEINTRESOURCE(IDB_FORWARDICON
), IMAGE_BITMAP
, 16, 16, LR_DEFAULTCOLOR
),
88 ImageList_Destroy((HIMAGELIST
)SendMessage(hToolBar
,
95 InitControls(HWND hwnd
)
97 INT NumButtons
= sizeof(Buttons
) / sizeof(Buttons
[0]);
101 /* Create trackbar */
102 hTrackBar
= CreateWindowEx(0,
105 TBS_ENABLESELRANGE
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| WS_CLIPSIBLINGS
,
116 MessageBox(hwnd
, _T("TrackBar it is not created!"), NULL
, MB_OK
);
121 hToolBar
= CreateWindowEx(0,
124 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| WS_CLIPSIBLINGS
|
125 TBSTYLE_FLAT
| CCS_BOTTOM
| TBSTYLE_TOOLTIPS
,
136 MessageBox(hwnd
, _T("ToolBar it is not created!"), NULL
, MB_OK
);
141 SendMessage(hToolBar
, TB_ADDBUTTONS
, NumButtons
, (LPARAM
)Buttons
);
145 IsSupportedFileExtension(LPWSTR lpFileName
)
147 TCHAR szExt
[MAX_PATH
];
148 INT DotPos
= 0, i
, j
;
150 for (i
= _tcslen(lpFileName
); i
>= 0; --i
)
152 if (lpFileName
[i
] == '.')
154 DotPos
= _tcslen(lpFileName
) - i
;
159 if (!DotPos
) return UNSUPPORTED_FILE
;
161 szExt
[DotPos
+ 1] = _T('\0');
162 for (i
= _tcslen(lpFileName
), j
= DotPos
; j
>= 0; --i
, --j
)
164 szExt
[j
] = lpFileName
[i
];
169 if (ExtTypes
[i
].uType
== UNSUPPORTED_FILE
)
171 return UNSUPPORTED_FILE
;
174 if (_tcscmp(ExtTypes
[i
].szExt
, szExt
) == 0)
176 return ExtTypes
[i
].uType
;
180 return UNSUPPORTED_FILE
;
186 MCI_GENERIC_PARMS mciGeneric
;
191 dwError
= mciSendCommand(wDeviceId
, MCI_CLOSE
, MCI_WAIT
, (DWORD
)(LPMCI_GENERIC_PARMS
)&mciGeneric
);
192 if (dwError
) return dwError
;
200 OpenMciDevice(LPTSTR lpType
, LPTSTR lpFileName
)
202 MCI_STATUS_PARMS mciStatus
;
203 MCI_OPEN_PARMS mciOpen
;
211 mciOpen
.lpstrDeviceType
= lpType
;
212 mciOpen
.lpstrElementName
= lpFileName
;
213 mciOpen
.dwCallback
= 0;
214 mciOpen
.wDeviceID
= 0;
215 mciOpen
.lpstrAlias
= NULL
;
217 dwError
= mciSendCommand(0, MCI_OPEN
, MCI_OPEN_TYPE
| MCI_OPEN_ELEMENT
| MCI_WAIT
, (DWORD
)(LPVOID
)&mciOpen
);
220 MessageBox(0, _T("Can't open device! (1)"), NULL
, MB_OK
);
224 mciStatus
.dwItem
= MCI_STATUS_LENGTH
;
226 dwError
= mciSendCommand(mciOpen
.wDeviceID
, MCI_STATUS
, MCI_STATUS_ITEM
| MCI_WAIT
, (DWORD
)(LPVOID
)&mciStatus
);
229 MessageBox(0, _T("Can't open device! (2)"), NULL
, MB_OK
);
233 SendMessage(hTrackBar
, TBM_SETRANGE
, (WPARAM
) TRUE
, (LPARAM
) MAKELONG(1, mciStatus
.dwReturn
));
234 SendMessage(hTrackBar
, TBM_SETPAGESIZE
, 0, 10);
235 SendMessage(hTrackBar
, TBM_SETLINESIZE
, 0, 1);
236 SendMessage(hTrackBar
, TBM_SETPOS
, (WPARAM
) TRUE
, (LPARAM
) 1);
238 if (mciStatus
.dwReturn
< 10000)
240 SendMessage(hTrackBar
, TBM_SETTICFREQ
, (WPARAM
) 100, (LPARAM
) 0);
242 else if (mciStatus
.dwReturn
< 100000)
244 SendMessage(hTrackBar
, TBM_SETTICFREQ
, (WPARAM
) 1000, (LPARAM
) 0);
246 else if (mciStatus
.dwReturn
< 1000000)
248 SendMessage(hTrackBar
, TBM_SETTICFREQ
, (WPARAM
) 10000, (LPARAM
) 0);
252 SendMessage(hTrackBar
, TBM_SETTICFREQ
, (WPARAM
) 100000, (LPARAM
) 0);
255 MaxFilePos
= mciStatus
.dwReturn
;
256 wDeviceId
= mciOpen
.wDeviceID
;
258 lstrcpy(szPrevFile
, lpFileName
);
263 StopPlayback(HWND hwnd
)
267 SendMessage(hTrackBar
, TBM_SETPOS
, (WPARAM
) TRUE
, (LPARAM
) 1);
268 KillTimer(hwnd
, IDT_PLAYTIMER
);
274 SeekPlayback(HWND hwnd
, DWORD dwNewPos
)
276 MCI_SEEK_PARMS mciSeek
;
277 MCI_PLAY_PARMS mciPlay
;
282 mciSeek
.dwTo
= dwNewPos
;
283 dwError
= mciSendCommand(wDeviceId
, MCI_SEEK
, MCI_WAIT
| MCI_TO
, (DWORD
)(LPVOID
)&mciSeek
);
286 MessageBox(hwnd
, _T("SeekPlayback: Can't seek!"), NULL
, MB_OK
);
289 mciPlay
.dwCallback
= (DWORD
)hwnd
;
290 dwError
= mciSendCommand(wDeviceId
, MCI_PLAY
, MCI_NOTIFY
, (DWORD
)(LPVOID
)&mciPlay
);
293 MessageBox(hwnd
, _T("SeekPlayback: Can't play!"), NULL
, MB_OK
);
299 SeekBackPlayback(HWND hwnd
)
301 MCI_STATUS_PARMS mciStatus
;
304 if (!bIsOpened
) return;
306 mciStatus
.dwItem
= MCI_STATUS_POSITION
;
307 mciSendCommand(wDeviceId
, MCI_STATUS
, MCI_STATUS_ITEM
, (DWORD
)(LPVOID
)&mciStatus
);
309 dwNewPos
= mciStatus
.dwReturn
- 1;
311 if((UINT
)dwNewPos
<= 1)
317 SeekPlayback(hwnd
, dwNewPos
);
322 SeekForwPlayback(HWND hwnd
)
324 MCI_STATUS_PARMS mciStatus
;
327 if (!bIsOpened
) return;
329 mciStatus
.dwItem
= MCI_STATUS_POSITION
;
330 mciSendCommand(wDeviceId
, MCI_STATUS
, MCI_STATUS_ITEM
, (DWORD
)(LPVOID
)&mciStatus
);
332 dwNewPos
= mciStatus
.dwReturn
+ 1;
334 if((UINT
)dwNewPos
>= MaxFilePos
)
340 SeekPlayback(hwnd
, dwNewPos
);
345 PausePlayback(HWND hwnd
)
347 MCI_GENERIC_PARMS mciGeneric
;
352 dwError
= mciSendCommand(wDeviceId
, MCI_PAUSE
, MCI_WAIT
, (DWORD
)(LPMCI_GENERIC_PARMS
)&mciGeneric
);
355 MessageBox(hwnd
, _T("Can't pause!"), NULL
, MB_OK
);
362 ResumePlayback(HWND hwnd
)
364 MCI_GENERIC_PARMS mciGeneric
;
369 dwError
= mciSendCommand(wDeviceId
, MCI_RESUME
, MCI_WAIT
, (DWORD
)(LPMCI_GENERIC_PARMS
)&mciGeneric
);
372 MessageBox(hwnd
, _T("Can't resume!"), NULL
, MB_OK
);
379 PlayTimerProc(HWND hwnd
, UINT uMsg
, UINT_PTR idEvent
, DWORD dwTime
)
381 MCI_STATUS_PARMS mciStatus
;
384 if (!bIsOpened
) KillTimer(hwnd
, IDT_PLAYTIMER
);
386 mciStatus
.dwItem
= MCI_STATUS_POSITION
;
387 mciSendCommand(wDeviceId
, MCI_STATUS
, MCI_STATUS_ITEM
, (DWORD
)(LPVOID
)&mciStatus
);
388 dwPos
= mciStatus
.dwReturn
;
390 if((UINT
)dwPos
>= MaxFilePos
)
396 SendMessage(hTrackBar
, TBM_SETPOS
, (WPARAM
) TRUE
, (LPARAM
) dwPos
);
401 PlayFile(HWND hwnd
, LPWSTR lpFileName
)
403 MCI_PLAY_PARMS mciPlay
;
404 TCHAR szLocalFileName
[MAX_PATH
];
408 if (lpFileName
== NULL
)
410 if (szPrevFile
[0] == _T('\0'))
413 lstrcpy(szLocalFileName
, szPrevFile
);
417 lstrcpy(szLocalFileName
, lpFileName
);
420 if (GetFileAttributes(szLocalFileName
) == INVALID_FILE_ATTRIBUTES
)
422 MessageBox(hwnd
, _T("Wrong path to a file!"), NULL
, MB_OK
);
426 FileType
= IsSupportedFileExtension(szLocalFileName
);
430 case UNSUPPORTED_FILE
:
431 MessageBox(hwnd
, _T("Unsupported format!"), NULL
, MB_OK
);
434 OpenMciDevice(_T("waveaudio"), szLocalFileName
);
437 OpenMciDevice(_T("sequencer"), szLocalFileName
);
440 OpenMciDevice(_T("cdaudio"), szLocalFileName
);
443 OpenMciDevice(_T("avivideo"), szLocalFileName
);
447 SetTimer(hwnd
, IDT_PLAYTIMER
, 100, (TIMERPROC
) PlayTimerProc
);
449 dwError
= mciSendCommand(wDeviceId
, MCI_SEEK
, MCI_WAIT
| MCI_SEEK_TO_START
, 0);
451 mciPlay
.dwCallback
= (DWORD
)hwnd
;
453 mciPlay
.dwTo
= MaxFilePos
;
455 dwError
= mciSendCommand(wDeviceId
, MCI_PLAY
, MCI_NOTIFY
| MCI_FROM
| MCI_TO
, (DWORD
)(LPVOID
)&mciPlay
);
458 MessageBox(hwnd
, _T("Can't play!"), NULL
, MB_OK
);
463 OpenFileDialog(HWND hwnd
)
465 OPENFILENAME OpenFileName
;
466 TCHAR szFile
[MAX_PATH
+ 1] = _T("\0");
467 TCHAR szFilter
[MAX_PATH
], szCurrentDir
[MAX_PATH
];
469 ZeroMemory(&OpenFileName
, sizeof(OpenFileName
));
471 LoadString(hInstance
, IDS_ALL_TYPES_FILTER
, szFilter
, sizeof(szFilter
) / sizeof(TCHAR
));
473 if (!GetCurrentDirectory(sizeof(szCurrentDir
) / sizeof(TCHAR
), szCurrentDir
))
475 lstrcpy(szCurrentDir
, _T("c:\\"));
478 OpenFileName
.lStructSize
= sizeof(OpenFileName
);
479 OpenFileName
.hwndOwner
= hwnd
;
480 OpenFileName
.hInstance
= hInstance
;
481 OpenFileName
.lpstrFilter
= szFilter
;
482 OpenFileName
.lpstrFile
= szFile
;
483 OpenFileName
.nMaxFile
= sizeof(szFile
) / sizeof((szFile
)[0]);
484 OpenFileName
.lpstrInitialDir
= szCurrentDir
;
485 OpenFileName
.Flags
= OFN_FILEMUSTEXIST
| OFN_PATHMUSTEXIST
| OFN_HIDEREADONLY
| OFN_SHAREAWARE
;
486 OpenFileName
.lpstrDefExt
= _T("\0");
488 if (GetOpenFileName(&OpenFileName
))
490 PlayFile(hwnd
, OpenFileName
.lpstrFile
);
495 MainWndProc(HWND hwnd
, UINT Message
, WPARAM wParam
, LPARAM lParam
)
505 LPNMHDR pnmhdr
= (LPNMHDR
)lParam
;
507 switch (pnmhdr
->code
)
509 case TTN_GETDISPINFO
:
511 LPTOOLTIPTEXT lpttt
= (LPTOOLTIPTEXT
)lParam
;
512 UINT idButton
= (UINT
)lpttt
->hdr
.idFrom
;
517 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_PLAY
);
520 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_STOP
);
523 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_EJECT
);
526 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_BACKWARD
);
529 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_SEEKBACK
);
532 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_SEEKFORW
);
535 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_FORWARD
);
546 LPRECT pRect
= (LPRECT
)lParam
;
548 if (pRect
->right
- pRect
->left
< MAIN_WINDOW_MIN_WIDTH
)
549 pRect
->right
= pRect
->left
+ MAIN_WINDOW_MIN_WIDTH
;
551 if (pRect
->bottom
- pRect
->top
!= MAIN_WINDOW_HEIGHT
)
552 pRect
->bottom
= pRect
->top
+ MAIN_WINDOW_HEIGHT
;
562 if (hToolBar
&& hTrackBar
)
564 SendMessage(hToolBar
, TB_AUTOSIZE
, 0, 0);
565 SendMessage(hToolBar
, TB_GETITEMRECT
, 1, (LPARAM
)&Rect
);
567 Size
= GetSystemMetrics(SM_CYMENU
) + Rect
.bottom
;
568 MoveWindow(hTrackBar
, 0, 0, LOWORD(lParam
), HIWORD(lParam
) - Size
, TRUE
);
575 if (hTrackBar
== (HWND
) lParam
)
577 DWORD dwNewPos
= (DWORD
) SendMessage(hTrackBar
, TBM_GETPOS
, 0, 0);
578 SeekPlayback(hwnd
, dwNewPos
);
584 switch (LOWORD(wParam
))
590 ResumePlayback(hwnd
);
596 if (szPrevFile
[0] == _T('\0'))
597 OpenFileDialog(hwnd
);
599 PlayFile(hwnd
, NULL
);
614 SeekBackPlayback(hwnd
);
618 SeekForwPlayback(hwnd
);
625 OpenFileDialog(hwnd
);
630 lstrcpy(szPrevFile
, _T("\0"));
634 ShellAbout(hwnd
, szAppTitle
, 0, 0);
638 PostMessage(hwnd
, WM_CLOSE
, 0, 0);
649 return DefWindowProc(hwnd
, Message
, wParam
, lParam
);
653 _tWinMain(HINSTANCE hInst
, HINSTANCE hPrevInst
, LPTSTR lpCmdLine
, INT nCmdShow
)
655 WNDCLASSEX WndClass
= {0};
656 TCHAR szClassName
[] = _T("ROSMPLAY32");
662 LoadString(hInstance
, IDS_APPTITLE
, szAppTitle
, sizeof(szAppTitle
) / sizeof(TCHAR
));
664 WndClass
.cbSize
= sizeof(WNDCLASSEX
);
665 WndClass
.lpszClassName
= szClassName
;
666 WndClass
.lpfnWndProc
= MainWndProc
;
667 WndClass
.hInstance
= hInstance
;
668 WndClass
.style
= CS_HREDRAW
| CS_VREDRAW
;
669 WndClass
.hIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_MAIN
));
670 WndClass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
671 WndClass
.hbrBackground
= (HBRUSH
)(COLOR_BTNFACE
+ 1);
672 WndClass
.lpszMenuName
= MAKEINTRESOURCE(IDR_MAINMENU
);
674 RegisterClassEx(&WndClass
);
676 hwnd
= CreateWindow(szClassName
,
678 WS_SYSMENU
| WS_MINIMIZEBOX
| WS_THICKFRAME
| WS_OVERLAPPED
| WS_CAPTION
| WS_CLIPCHILDREN
,
689 ShowWindow(hwnd
, SW_SHOW
);
693 while (GetMessage(&msg
, NULL
, 0, 0))
695 TranslateMessage(&msg
);
696 DispatchMessage(&msg
);