📄 simpleui.cpp
字号:
/*____________________________________________________________________________
FreeAmp - The Free MP3 Player
Portions Copyright (C) 1998-1999 EMusic.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id: simpleui.cpp,v 1.27 2000/07/31 19:51:40 ijr Exp $
____________________________________________________________________________*/
/* system headers */
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
/* project headers */
#include "config.h"
#include "thread.h"
#include "simpleui.h"
#include "event.h"
#include "eventdata.h"
#include "Playlist.h"
#include "about.h"
#include "prefdialog.h"
#include "resource.h"
#define UWM_TRAY WM_USER + 666
HINSTANCE g_hInstance = NULL;
BOOL CALLBACK MainProc( HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam );
extern "C" SimpleUI *Initialize(FAContext *context)
{
return new SimpleUI(context);
}
INT WINAPI DllMain (HINSTANCE hInst,
ULONG ul_reason_being_called,
LPVOID lpReserved)
{
switch (ul_reason_being_called)
{
case DLL_PROCESS_ATTACH:
g_hInstance = hInst;
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return 1;
}
SimpleUI::
SimpleUI(FAContext *context):
UserInterface()
{
m_context = context;
m_prefs = context->prefs;
m_plm = m_context->plm;
m_target = m_context->target;
m_propManager = m_context->props;
m_scrolling = false;
m_uiSemaphore = new Semaphore();
m_uiThread = Thread::CreateThread();
m_uiThread->Create(UIThreadFunc,this);
m_uiSemaphore->Wait();
}
SimpleUI::
~SimpleUI()
{
delete m_uiSemaphore;
}
void
SimpleUI::
SetHwnd(HWND hwnd)
{
m_hwnd = hwnd;
m_hwndPlay = GetDlgItem(m_hwnd, IDC_PLAY);
m_hwndStop = GetDlgItem(m_hwnd, IDC_STOP);
m_hwndPause = GetDlgItem(m_hwnd, IDC_PAUSE);
m_hwndNext = GetDlgItem(m_hwnd, IDC_NEXTSONG);
m_hwndLast = GetDlgItem(m_hwnd, IDC_LASTSONG);
m_hwndSlider = GetDlgItem(m_hwnd, IDC_SLIDER);
m_hwndCurrent = GetDlgItem(m_hwnd, IDC_CURRENT_TIME);
m_hwndTotal = GetDlgItem(m_hwnd, IDC_TOTAL_TIME);
m_hwndStatus = GetDlgItem(m_hwnd, IDC_STATUS);
}
Error
SimpleUI::
AcceptEvent(Event* event)
{
Error result = kError_UnknownErr;
if (event)
{
switch (event->Type())
{
case INFO_Playing:
{
EnableWindow(m_hwndPlay, FALSE);
EnableWindow(m_hwndNext, TRUE);
EnableWindow(m_hwndLast, TRUE);
EnableWindow(m_hwndStop, TRUE);
EnableWindow(m_hwndPause, TRUE);
EnableWindow(m_hwndSlider, TRUE);
m_state = UIState_Playing;
break;
}
case INFO_Paused:
{
EnableWindow(m_hwndPlay, TRUE);
EnableWindow(m_hwndNext, FALSE);
EnableWindow(m_hwndLast, FALSE);
EnableWindow(m_hwndStop, TRUE);
EnableWindow(m_hwndPause, FALSE);
EnableWindow(m_hwndSlider, TRUE);
m_state = UIState_Paused;
break;
}
case INFO_Stopped:
{
EnableWindow(m_hwndPlay, TRUE);
EnableWindow(m_hwndNext, TRUE);
EnableWindow(m_hwndLast, TRUE);
EnableWindow(m_hwndStop, FALSE);
EnableWindow(m_hwndPause, FALSE);
SendMessage(m_hwndSlider,
TBM_SETPOS,
(WPARAM)TRUE,
(LPARAM)0);
EnableWindow(m_hwndSlider, FALSE);
char timeString[256] = "00:00:00";
SetWindowText(m_hwndCurrent, timeString);
m_state = UIState_Stopped;
break;
}
case INFO_MPEGInfo:
{
MpegInfoEvent *info = (MpegInfoEvent *)event;
char szTemp[256];
m_secondsPerFrame = info->GetSecondsPerFrame();
sprintf(szTemp, "%d kbps", info->GetBitRate()/1000);
SendMessage(m_hwndStatus,
SB_SETTEXT,
0,
(LPARAM)szTemp);
sprintf(szTemp, "%.1f kHz", ((float)info->GetSampleRate())/1000);
SendMessage(m_hwndStatus,
SB_SETTEXT,
1,
(LPARAM) szTemp);
SendMessage(m_hwndSlider,
TBM_SETRANGE,
(WPARAM)TRUE,
MAKELPARAM(0, info->GetTotalFrames()));
break;
}
case INFO_MediaInfo:
{
MediaInfoEvent *info = (MediaInfoEvent*)event;
char timeString[256] = "00:00:00";
char szTemp[256];
SetWindowText(m_hwndCurrent, timeString);
int32 seconds = (int32)ceil(info->m_totalSeconds);
m_totalSeconds = seconds;
int32 hours = seconds / 3600;
int32 minutes = seconds / 60 - hours * 60;
seconds = seconds - minutes * 60 - hours * 3600;
sprintf(timeString,"%02d:%02d:%02d",hours,
minutes,
seconds);
SetWindowText(m_hwndTotal, timeString);
sprintf(szTemp, "%d of %d", info->m_indexOfSong,info->m_totalSongs);
SendMessage(m_hwndStatus,
SB_SETTEXT,
2,
(LPARAM) szTemp);
SendMessage(m_hwndSlider,
TBM_SETPOS,
(WPARAM)TRUE,
(LPARAM)0);
SetWindowText(m_hwnd, info->m_filename);
SetTrayTooltip(info->m_filename);
/*totalFrames = info->totalFrames;
totalTime = info->totalTime;
char *path = info->filename;
char *pLastSlash = strrchr(path,'/');
char *dir = NULL;
char *fname = NULL;
if (pLastSlash) {
*pLastSlash = '\0';
fname = (char *)((int)pLastSlash + 1);
dir = path;
} else {
fname = path;
}
strncpy(fileName,fname,511);
fileName[511] = '\0';
if (info->tagInfo.contains_info)
{
fprintf(stderr,"Title : %30s Artist: %s\n",info->tagInfo.songname,info->tagInfo.artist);
fprintf(stderr,"Album : %30s Year: %4s, Genre: %d\n",info->tagInfo.album,info->tagInfo.year,(int)info->tagInfo.genre);
fprintf(stderr,"Comment: %30s \n",info->tagInfo.comment);
}
if (verboseMode == false)
{
cerr << "Playing MPEG stream from " << fileName << " ..." << endl;
cerr << "MPEG 1.0, Layer: III, Freq: " << info->freq << ", mode: Joint-Stereo, modext: 3, BPF : " << info->bytesPerFrame << endl;
cerr << "Channels: 2, copyright: No, original: Yes, CRC: No, emphasis: 0." << endl;
cerr << "Bitrate: " << info->bps/1000 << " KBits/s, Extension value: 0" << endl;
cerr << "Audio: 1:1 conversion, rate: " << info->freq << ", encoding: signed 16 bit, channels: 2" << endl;
}*/
break;
}
case INFO_MediaTimeInfo:
{
MediaTimeInfoEvent* info = (MediaTimeInfoEvent*)event;
char timeString[256] = "00:00:00";
static int32 lastSeconds = 0, lastMinutes = 0, lastHours = 0;
if(lastSeconds != info->m_seconds ||
lastMinutes != info->m_minutes ||
lastHours != info->m_hours)
{
lastSeconds = info->m_seconds;
lastMinutes = info->m_minutes;
lastHours = info->m_hours;
sprintf(timeString,"%02d:%02d:%02d",info->m_hours,
info->m_minutes,
info->m_seconds);
SetWindowText(m_hwndCurrent, timeString);
}
if(!m_scrolling)
{
int32 frame = (int32)((float)info->m_totalSeconds/m_secondsPerFrame);
SendMessage(m_hwndSlider,
TBM_SETPOS,
(WPARAM)TRUE,
(LPARAM)frame);
}
break;
}
case INFO_DoneOutputting:
{
break;
}
case INFO_PlaylistDonePlay:
{
char timeString[256] = "00:00:00";
char szTemp[256] = {0x00};
SetWindowText(m_hwndCurrent, timeString);
SetWindowText(m_hwndTotal, timeString);
SendMessage(m_hwndStatus,
SB_SETTEXT,
0,
(LPARAM)szTemp);
SendMessage(m_hwndStatus,
SB_SETTEXT,
1,
(LPARAM) szTemp);
SendMessage(m_hwndStatus,
SB_SETTEXT,
2,
(LPARAM) szTemp);
SetWindowText(m_hwnd, BRANDING);
break;
}
case CMD_Cleanup:
{
m_target->AcceptEvent(new Event(INFO_ReadyToDieUI));
break;
}
default:
break;
}
result = kError_NoErr;
}
return result;
}
void
SimpleUI::
ParseArgs(int32 argc, char** argv)
{
PlaylistManager* Playlist = m_plm;
char *arg = NULL;
bool shuffle = false;
bool autoplay = false;
int32 count = 0;
for(int32 i = 1;i < argc; i++)
{
arg = argv[i];
if (arg[0] == '-')
{
switch (arg[1])
{
case 's':
{
shuffle = true;
break;
}
case 'p':
{
autoplay = true;
break;
}
}
}
else
{
Playlist->AddItem(arg,0);
count++;
}
}
Playlist->SetCurrentIndex(0);
if(shuffle)
Playlist->SetShuffleMode(true);
if(count)
{
EnableWindow(m_hwndPlay, TRUE);
if(count > 1)
EnableWindow(m_hwndNext, TRUE);
}
}
void
SimpleUI::
CreateUI()
{
InitCommonControls();
DialogBoxParam( g_hInstance,
MAKEINTRESOURCE(IDD_PLAYER),
NULL,
MainProc,
(LPARAM)this);
m_target->AcceptEvent(new Event(CMD_QuitPlayer));
}
void
SimpleUI::
UIThreadFunc(void* arg)
{
SimpleUI* ui = (SimpleUI*)arg;
ui->CreateUI();
}
void
SimpleUI::
ReadPreferences()
{
m_prefs->GetPrefBoolean(kStayOnTopPref, &m_onTop);
m_prefs->GetPrefBoolean(kLiveInTrayPref, &m_liveInTray);
SetWindowPos( m_hwnd,
(m_onTop ? HWND_TOPMOST: HWND_NOTOPMOST),
0, 0, 0, 0,
SWP_NOMOVE|SWP_NOSIZE);
// what follows is a sad hack that actually makes windows
// refresh the toolbar correctly...
ShowWindow(m_hwnd, FALSE);
LONG styles, extendedStyles;
styles = GetWindowLong(m_hwnd, GWL_STYLE);
extendedStyles = GetWindowLong(m_hwnd, GWL_EXSTYLE);
if(extendedStyles & WS_EX_TOOLWINDOW)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -