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 HMENU hMainMenu
= NULL
;
16 TCHAR szAppTitle
[256] = _T("");
17 TCHAR szPrevFile
[MAX_PATH
] = _T("\0");
19 BOOL bIsOpened
= FALSE
;
20 BOOL bIsPaused
= FALSE
;
25 static const TBBUTTON Buttons
[] =
26 { /* iBitmap, idCommand, fsState, fsStyle, bReserved[2], dwData, iString */
27 {TBICON_PLAY
, IDC_PLAY
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
28 {TBICON_STOP
, IDC_STOP
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
29 {TBICON_EJECT
, IDC_EJECT
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
30 {15, 0, TBSTATE_ENABLED
, BTNS_SEP
, {0}, 0, 0},
31 {TBICON_BACKWARD
, IDC_BACKWARD
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
32 {TBICON_SEEKBACK
, IDC_SEEKBACK
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
33 {TBICON_SEEKFORW
, IDC_SEEKFORW
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
34 {TBICON_FORWARD
, IDC_FORWARD
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0}
37 void EnableMenuItems(void)
39 MCI_GENERIC_PARMS mciGeneric
;
42 EnableMenuItem(hMainMenu
, IDM_CLOSE_FILE
, MF_BYCOMMAND
| MF_ENABLED
);
44 dwError
= mciSendCommand(wDeviceId
, MCI_CONFIGURE
, MCI_TEST
, (DWORD_PTR
)&mciGeneric
);
47 EnableMenuItem(hMainMenu
, IDM_DEVPROPS
, MF_BYCOMMAND
| MF_ENABLED
);
51 void DisableMenuItems(void)
53 EnableMenuItem(hMainMenu
, IDM_CLOSE_FILE
, MF_BYCOMMAND
| MF_GRAYED
);
54 EnableMenuItem(hMainMenu
, IDM_DEVPROPS
, MF_BYCOMMAND
| MF_GRAYED
);
58 SetImageList(HWND hwnd
)
60 HIMAGELIST hImageList
;
62 hImageList
= ImageList_Create(16, 16, ILC_MASK
| ILC_COLOR24
, 1, 1);
66 MessageBox(hwnd
, _T("ImageList it is not created!"), NULL
, MB_OK
);
70 ImageList_AddMasked(hImageList
,
71 LoadImage(hInstance
, MAKEINTRESOURCE(IDB_PLAYICON
), IMAGE_BITMAP
, 16, 16, LR_DEFAULTCOLOR
),
74 ImageList_AddMasked(hImageList
,
75 LoadImage(hInstance
, MAKEINTRESOURCE(IDB_STOPICON
), IMAGE_BITMAP
, 16, 16, LR_DEFAULTCOLOR
),
78 ImageList_AddMasked(hImageList
,
79 LoadImage(hInstance
, MAKEINTRESOURCE(IDB_EJECTICON
), IMAGE_BITMAP
, 16, 16, LR_DEFAULTCOLOR
),
82 ImageList_AddMasked(hImageList
,
83 LoadImage(hInstance
, MAKEINTRESOURCE(IDB_BACKWARDICON
), IMAGE_BITMAP
, 16, 16, LR_DEFAULTCOLOR
),
86 ImageList_AddMasked(hImageList
,
87 LoadImage(hInstance
, MAKEINTRESOURCE(IDB_SEEKBACKICON
), IMAGE_BITMAP
, 16, 16, LR_DEFAULTCOLOR
),
90 ImageList_AddMasked(hImageList
,
91 LoadImage(hInstance
, MAKEINTRESOURCE(IDB_SEEKFORWICON
), IMAGE_BITMAP
, 16, 16, LR_DEFAULTCOLOR
),
94 ImageList_AddMasked(hImageList
,
95 LoadImage(hInstance
, MAKEINTRESOURCE(IDB_FORWARDICON
), IMAGE_BITMAP
, 16, 16, LR_DEFAULTCOLOR
),
98 ImageList_Destroy((HIMAGELIST
)SendMessage(hToolBar
,
101 (LPARAM
)hImageList
));
105 ShowMCIError(HWND hwnd
, DWORD dwError
)
107 TCHAR szErrorMessage
[256];
108 TCHAR szTempMessage
[300];
110 if (mciGetErrorString(dwError
, szErrorMessage
, sizeof(szErrorMessage
) / sizeof(TCHAR
)) == FALSE
)
112 LoadString(hInstance
, IDS_DEFAULTMCIERRMSG
, szErrorMessage
, sizeof(szErrorMessage
) / sizeof(TCHAR
));
115 _stprintf(szTempMessage
, _T("MMSYS%u: %s"), dwError
, szErrorMessage
);
116 MessageBox(hwnd
, szTempMessage
, szAppTitle
, MB_OK
| MB_ICONEXCLAMATION
);
120 InitControls(HWND hwnd
)
122 INT NumButtons
= sizeof(Buttons
) / sizeof(Buttons
[0]);
124 InitCommonControls();
126 /* Create trackbar */
127 hTrackBar
= CreateWindowEx(0,
130 TBS_ENABLESELRANGE
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| WS_CLIPSIBLINGS
,
141 MessageBox(hwnd
, _T("TrackBar it is not created!"), NULL
, MB_OK
);
146 hToolBar
= CreateWindowEx(0,
149 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| WS_CLIPSIBLINGS
|
150 TBSTYLE_FLAT
| CCS_BOTTOM
| TBSTYLE_TOOLTIPS
,
161 MessageBox(hwnd
, _T("ToolBar it is not created!"), NULL
, MB_OK
);
166 SendMessage(hToolBar
, TB_ADDBUTTONS
, NumButtons
, (LPARAM
)Buttons
);
170 IsSupportedFileExtension(LPTSTR lpFileName
, LPTSTR lpDeviceName
, LPDWORD dwSize
)
176 pathend
= _tcsrchr(lpFileName
, '.');
185 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
, _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\MCI Extensions"), 0, KEY_READ
, &hKey
) == ERROR_SUCCESS
)
187 if (RegQueryValueEx(hKey
, pathend
, NULL
, &dwType
, (LPBYTE
)lpDeviceName
, dwSize
) == ERROR_SUCCESS
)
190 if (dwType
!= REG_SZ
)
207 MCI_GENERIC_PARMS mciGeneric
;
212 dwError
= mciSendCommand(wDeviceId
, MCI_CLOSE
, MCI_WAIT
, (DWORD_PTR
)&mciGeneric
);
213 if (dwError
) return dwError
;
223 OpenMciDevice(HWND hwnd
, LPTSTR lpType
, LPTSTR lpFileName
)
225 MCI_STATUS_PARMS mciStatus
;
226 MCI_OPEN_PARMS mciOpen
;
227 TCHAR szNewTitle
[MAX_PATH
];
235 mciOpen
.lpstrDeviceType
= lpType
;
236 mciOpen
.lpstrElementName
= lpFileName
;
237 mciOpen
.dwCallback
= 0;
238 mciOpen
.wDeviceID
= 0;
239 mciOpen
.lpstrAlias
= NULL
;
241 dwError
= mciSendCommand(0, MCI_OPEN
, MCI_OPEN_TYPE
| MCI_OPEN_ELEMENT
| MCI_WAIT
, (DWORD_PTR
)&mciOpen
);
244 ShowMCIError(hwnd
, dwError
);
248 mciStatus
.dwItem
= MCI_STATUS_LENGTH
;
250 dwError
= mciSendCommand(mciOpen
.wDeviceID
, MCI_STATUS
, MCI_STATUS_ITEM
| MCI_WAIT
, (DWORD_PTR
)&mciStatus
);
253 ShowMCIError(hwnd
, dwError
);
257 SendMessage(hTrackBar
, TBM_SETRANGEMIN
, (WPARAM
) TRUE
, (LPARAM
) 1);
258 SendMessage(hTrackBar
, TBM_SETRANGEMAX
, (WPARAM
) TRUE
, (LPARAM
) mciStatus
.dwReturn
);
259 SendMessage(hTrackBar
, TBM_SETPAGESIZE
, 0, 10);
260 SendMessage(hTrackBar
, TBM_SETLINESIZE
, 0, 1);
261 SendMessage(hTrackBar
, TBM_SETPOS
, (WPARAM
) TRUE
, (LPARAM
) 1);
263 if (mciStatus
.dwReturn
< 10000)
265 SendMessage(hTrackBar
, TBM_SETTICFREQ
, (WPARAM
) 100, (LPARAM
) 0);
267 else if (mciStatus
.dwReturn
< 100000)
269 SendMessage(hTrackBar
, TBM_SETTICFREQ
, (WPARAM
) 1000, (LPARAM
) 0);
271 else if (mciStatus
.dwReturn
< 1000000)
273 SendMessage(hTrackBar
, TBM_SETTICFREQ
, (WPARAM
) 10000, (LPARAM
) 0);
277 SendMessage(hTrackBar
, TBM_SETTICFREQ
, (WPARAM
) 100000, (LPARAM
) 0);
280 _stprintf(szNewTitle
, _T("%s - %s"), szAppTitle
, lpFileName
);
281 SetWindowText(hwnd
, szNewTitle
);
283 MaxFilePos
= mciStatus
.dwReturn
;
284 wDeviceId
= mciOpen
.wDeviceID
;
286 _tcscpy(szPrevFile
, lpFileName
);
294 StopPlayback(HWND hwnd
)
298 SendMessage(hTrackBar
, TBM_SETPOS
, (WPARAM
) TRUE
, (LPARAM
) 1);
299 KillTimer(hwnd
, IDT_PLAYTIMER
);
305 SeekPlayback(HWND hwnd
, DWORD dwNewPos
)
307 MCI_SEEK_PARMS mciSeek
;
308 MCI_PLAY_PARMS mciPlay
;
313 mciSeek
.dwTo
= (DWORD_PTR
)dwNewPos
;
314 dwError
= mciSendCommand(wDeviceId
, MCI_SEEK
, MCI_WAIT
| MCI_TO
, (DWORD_PTR
)&mciSeek
);
317 ShowMCIError(hwnd
, dwError
);
320 mciPlay
.dwCallback
= (DWORD_PTR
)hwnd
;
321 dwError
= mciSendCommand(wDeviceId
, MCI_PLAY
, MCI_NOTIFY
, (DWORD_PTR
)&mciPlay
);
324 ShowMCIError(hwnd
, dwError
);
330 SeekBackPlayback(HWND hwnd
)
332 MCI_STATUS_PARMS mciStatus
;
335 if (!bIsOpened
) return;
337 mciStatus
.dwItem
= MCI_STATUS_POSITION
;
338 mciSendCommand(wDeviceId
, MCI_STATUS
, MCI_STATUS_ITEM
, (DWORD_PTR
)&mciStatus
);
340 dwNewPos
= mciStatus
.dwReturn
- 1;
342 if((UINT
)dwNewPos
<= 1)
348 SeekPlayback(hwnd
, dwNewPos
);
353 SeekForwPlayback(HWND hwnd
)
355 MCI_STATUS_PARMS mciStatus
;
358 if (!bIsOpened
) return;
360 mciStatus
.dwItem
= MCI_STATUS_POSITION
;
361 mciSendCommand(wDeviceId
, MCI_STATUS
, MCI_STATUS_ITEM
, (DWORD_PTR
)&mciStatus
);
363 dwNewPos
= mciStatus
.dwReturn
+ 1;
365 if((UINT
)dwNewPos
>= MaxFilePos
)
371 SeekPlayback(hwnd
, dwNewPos
);
376 PausePlayback(HWND hwnd
)
378 MCI_GENERIC_PARMS mciGeneric
;
383 dwError
= mciSendCommand(wDeviceId
, MCI_PAUSE
, MCI_WAIT
, (DWORD_PTR
)&mciGeneric
);
386 ShowMCIError(hwnd
, dwError
);
393 ResumePlayback(HWND hwnd
)
395 MCI_GENERIC_PARMS mciGeneric
;
400 dwError
= mciSendCommand(wDeviceId
, MCI_RESUME
, MCI_WAIT
, (DWORD_PTR
)&mciGeneric
);
403 ShowMCIError(hwnd
, dwError
);
410 ShowDeviceProperties(HWND hwnd
)
412 MCI_GENERIC_PARMS mciGeneric
;
415 dwError
= mciSendCommand(wDeviceId
, MCI_CONFIGURE
, MCI_WAIT
, (DWORD_PTR
)&mciGeneric
);
418 MessageBox(0, _T("Can't display the device properties!"), NULL
, MB_OK
);
423 PlayTimerProc(HWND hwnd
, UINT uMsg
, UINT_PTR idEvent
, DWORD dwTime
)
425 MCI_STATUS_PARMS mciStatus
;
428 if (!bIsOpened
) KillTimer(hwnd
, IDT_PLAYTIMER
);
430 mciStatus
.dwItem
= MCI_STATUS_POSITION
;
431 mciSendCommand(wDeviceId
, MCI_STATUS
, MCI_STATUS_ITEM
, (DWORD_PTR
)&mciStatus
);
432 dwPos
= mciStatus
.dwReturn
;
434 if((UINT
)dwPos
>= MaxFilePos
)
440 SendMessage(hTrackBar
, TBM_SETPOS
, (WPARAM
) TRUE
, (LPARAM
) dwPos
);
445 PlayFile(HWND hwnd
, LPTSTR lpFileName
)
447 MCI_PLAY_PARMS mciPlay
;
448 TCHAR szLocalFileName
[MAX_PATH
];
449 TCHAR szDeviceName
[MAX_PATH
];
454 if (lpFileName
== NULL
)
456 if (szPrevFile
[0] == _T('\0'))
459 _tcscpy(szLocalFileName
, szPrevFile
);
463 _tcscpy(szLocalFileName
, lpFileName
);
466 if (GetFileAttributes(szLocalFileName
) == INVALID_FILE_ATTRIBUTES
)
471 dwSize
= sizeof(szDeviceName
) - 2;
472 _tcsnset(szDeviceName
, _T('\0'), dwSize
/ sizeof(TCHAR
));
473 IsSupported
= IsSupportedFileExtension(szLocalFileName
, szDeviceName
, &dwSize
);
475 if (IsSupported
== TRUE
)
477 OpenMciDevice(hwnd
, szDeviceName
, szLocalFileName
);
480 SetTimer(hwnd
, IDT_PLAYTIMER
, 100, (TIMERPROC
) PlayTimerProc
);
482 mciSendCommand(wDeviceId
, MCI_SEEK
, MCI_WAIT
| MCI_SEEK_TO_START
, 0);
484 mciPlay
.dwCallback
= (DWORD_PTR
)hwnd
;
486 mciPlay
.dwTo
= MaxFilePos
;
488 mciError
= mciSendCommand(wDeviceId
, MCI_PLAY
, MCI_NOTIFY
| MCI_FROM
/*| MCI_TO*/, (DWORD_PTR
)&mciPlay
);
491 ShowMCIError(hwnd
, mciError
);
496 OpenFileDialog(HWND hwnd
)
498 OPENFILENAME OpenFileName
;
499 TCHAR szFile
[MAX_PATH
+ 1] = _T("\0");
500 TCHAR szFilter
[MAX_PATH
], szCurrentDir
[MAX_PATH
];
502 ZeroMemory(&OpenFileName
, sizeof(OpenFileName
));
504 LoadString(hInstance
, IDS_ALL_TYPES_FILTER
, szFilter
, sizeof(szFilter
) / sizeof(TCHAR
));
506 if (!GetCurrentDirectory(sizeof(szCurrentDir
) / sizeof(TCHAR
), szCurrentDir
))
508 _tcscpy(szCurrentDir
, _T("c:\\"));
511 OpenFileName
.lStructSize
= sizeof(OpenFileName
);
512 OpenFileName
.hwndOwner
= hwnd
;
513 OpenFileName
.hInstance
= hInstance
;
514 OpenFileName
.lpstrFilter
= szFilter
;
515 OpenFileName
.lpstrFile
= szFile
;
516 OpenFileName
.nMaxFile
= sizeof(szFile
) / sizeof((szFile
)[0]);
517 OpenFileName
.lpstrInitialDir
= szCurrentDir
;
518 OpenFileName
.Flags
= OFN_FILEMUSTEXIST
| OFN_PATHMUSTEXIST
| OFN_HIDEREADONLY
| OFN_SHAREAWARE
;
519 OpenFileName
.lpstrDefExt
= _T("\0");
521 if (GetOpenFileName(&OpenFileName
))
523 PlayFile(hwnd
, OpenFileName
.lpstrFile
);
528 MainWndProc(HWND hwnd
, UINT Message
, WPARAM wParam
, LPARAM lParam
)
534 hMainMenu
= GetMenu(hwnd
);
540 TCHAR droppedfile
[MAX_PATH
];
542 drophandle
= (HDROP
)wParam
;
543 DragQueryFile(drophandle
, 0, droppedfile
, sizeof(droppedfile
));
544 DragFinish(drophandle
);
545 PlayFile(hwnd
, droppedfile
);
551 LPNMHDR pnmhdr
= (LPNMHDR
)lParam
;
553 switch (pnmhdr
->code
)
555 case TTN_GETDISPINFO
:
557 LPTOOLTIPTEXT lpttt
= (LPTOOLTIPTEXT
)lParam
;
558 UINT idButton
= (UINT
)lpttt
->hdr
.idFrom
;
563 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_PLAY
);
566 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_STOP
);
569 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_EJECT
);
572 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_BACKWARD
);
575 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_SEEKBACK
);
578 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_SEEKFORW
);
581 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_FORWARD
);
592 LPRECT pRect
= (LPRECT
)lParam
;
594 if (pRect
->right
- pRect
->left
< MAIN_WINDOW_MIN_WIDTH
)
595 pRect
->right
= pRect
->left
+ MAIN_WINDOW_MIN_WIDTH
;
597 if (pRect
->bottom
- pRect
->top
!= MAIN_WINDOW_HEIGHT
)
598 pRect
->bottom
= pRect
->top
+ MAIN_WINDOW_HEIGHT
;
608 if (hToolBar
&& hTrackBar
)
610 SendMessage(hToolBar
, TB_AUTOSIZE
, 0, 0);
611 SendMessage(hToolBar
, TB_GETITEMRECT
, 1, (LPARAM
)&Rect
);
613 Size
= GetSystemMetrics(SM_CYMENU
) + Rect
.bottom
;
614 MoveWindow(hTrackBar
, 0, 0, LOWORD(lParam
), HIWORD(lParam
) - Size
, TRUE
);
621 if (hTrackBar
== (HWND
) lParam
)
625 DWORD dwNewPos
= (DWORD
) SendMessage(hTrackBar
, TBM_GETPOS
, 0, 0);
626 SeekPlayback(hwnd
, dwNewPos
);
630 SendMessage(hTrackBar
, TBM_SETPOS
, TRUE
, 0);
637 switch (LOWORD(wParam
))
643 ResumePlayback(hwnd
);
649 if (szPrevFile
[0] == _T('\0'))
650 OpenFileDialog(hwnd
);
652 PlayFile(hwnd
, NULL
);
667 SeekBackPlayback(hwnd
);
671 SeekForwPlayback(hwnd
);
678 OpenFileDialog(hwnd
);
683 _tcscpy(szPrevFile
, _T("\0"));
687 ShowDeviceProperties(hwnd
);
691 ShellExecute(hwnd
, NULL
, _T("SNDVOL32.EXE"), NULL
, NULL
, SW_SHOWNORMAL
);
696 HICON mplayIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_MAIN
));
697 ShellAbout(hwnd
, szAppTitle
, 0, mplayIcon
);
698 DeleteObject(mplayIcon
);
702 PostMessage(hwnd
, WM_CLOSE
, 0, 0);
713 return DefWindowProc(hwnd
, Message
, wParam
, lParam
);
717 _tWinMain(HINSTANCE hInst
, HINSTANCE hPrevInst
, LPTSTR lpCmdLine
, INT nCmdShow
)
719 WNDCLASSEX WndClass
= {0};
720 TCHAR szClassName
[] = _T("ROSMPLAY32");
727 LoadString(hInstance
, IDS_APPTITLE
, szAppTitle
, sizeof(szAppTitle
) / sizeof(TCHAR
));
729 WndClass
.cbSize
= sizeof(WNDCLASSEX
);
730 WndClass
.lpszClassName
= szClassName
;
731 WndClass
.lpfnWndProc
= MainWndProc
;
732 WndClass
.hInstance
= hInstance
;
733 WndClass
.style
= CS_HREDRAW
| CS_VREDRAW
;
734 WndClass
.hIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_MAIN
));
735 WndClass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
736 WndClass
.hbrBackground
= (HBRUSH
)(COLOR_BTNFACE
+ 1);
737 WndClass
.lpszMenuName
= MAKEINTRESOURCE(IDR_MAINMENU
);
739 RegisterClassEx(&WndClass
);
741 hwnd
= CreateWindow(szClassName
,
743 WS_SYSMENU
| WS_MINIMIZEBOX
| WS_THICKFRAME
| WS_OVERLAPPED
| WS_CAPTION
| WS_CLIPCHILDREN
,
753 DragAcceptFiles(hwnd
, TRUE
);
757 dwError
= SearchPath(NULL
, _T("SNDVOL32.EXE"), NULL
, 0, NULL
, NULL
);
760 EnableMenuItem(hMainMenu
, IDM_VOLUMECTL
, MF_BYCOMMAND
| MF_GRAYED
);
764 ShowWindow(hwnd
, SW_SHOW
);
767 PlayFile(hwnd
, lpCmdLine
);
770 while (GetMessage(&msg
, NULL
, 0, 0))
772 TranslateMessage(&msg
);
773 DispatchMessage(&msg
);