📄 downloadui.cpp
字号:
break;
}
case IDC_RESUME:
{
m_dlm->QueueDownload(dli, true);
m_dlm->ResumeDownloads();
EnableWindow(m_hwndPause, TRUE);
EnableWindow(m_hwndCancel, TRUE);
EnableWindow(m_hwndResume, FALSE);
break;
}
}
}
}
}
break;
}
case IDC_ADDURL:
{
HWND hwndURL = GetDlgItem(m_hwnd, IDC_URL);
uint32 length = GetWindowTextLength(hwndURL) + 1; // + 0x00
if(length)
{
char* sp = NULL;
char* url = new char[length + 7]; // http://
GetWindowText(hwndURL, url + 7, length);
if(sp = strstr(url + 7, "://"))
{
if(strncasecmp(url + 7, "http://", 7))
{
MessageBox(m_hwnd,
"Only HTTP downloads are supported at this time.",
"Invalid protocol",
MB_OK|MB_ICONINFORMATION);
break;
}
sp = url + 7;
}
else
{
memcpy(url, "http://", 7);
sp = url;
}
m_dlm->AddItem(sp);
delete [] url;
SetWindowText(hwndURL, "");
}
break;
}
case IDC_CLOSE:
{
if (GetAsyncKeyState(VK_SHIFT) < 0)
{
ShowWindow(GetDlgItem(m_hwnd, IDC_DLMTEXT), SW_HIDE);
ShowWindow(GetDlgItem(m_hwnd, IDC_FREETRACKS), SW_HIDE);
ShowWindow(GetDlgItem(m_hwnd, IDC_URL), SW_SHOW);
ShowWindow(GetDlgItem(m_hwnd, IDC_ADDURL), SW_SHOW);
break;
}
SendMessage(m_hwnd, WM_CLOSE, 0, 0);
break;
}
}
return TRUE;
}
BOOL DownloadUI::Notify(int32 controlId, NMHDR* nmh)
{
BOOL result = TRUE;
switch(controlId)
{
case IDC_LIST:
{
NM_LISTVIEW* nmlv = (NM_LISTVIEW*)nmh;
if(nmh->code == LVN_ITEMCHANGED)
{
ListView_RedrawItems(m_hwndInfo, 0, ListView_GetItemCount(m_hwndInfo) - 1);
DownloadItem* dli = (DownloadItem*)nmlv->lParam;
SetWindowText(m_hwndResume, "Resume");
switch(dli->GetState())
{
case kDownloadItemState_Queued:
EnableWindow(m_hwndPause, FALSE);
EnableWindow(m_hwndCancel, TRUE);
EnableWindow(m_hwndResume, m_dlm->IsPaused());
SetWindowText(m_hwndResume, "Start");
break;
case kDownloadItemState_Downloading:
EnableWindow(m_hwndPause, TRUE);
EnableWindow(m_hwndCancel, TRUE);
EnableWindow(m_hwndResume, FALSE);
break;
case kDownloadItemState_Cancelled:
case kDownloadItemState_Error:
EnableWindow(m_hwndPause, FALSE);
EnableWindow(m_hwndCancel, FALSE);
EnableWindow(m_hwndResume, TRUE);
break;
case kDownloadItemState_Paused:
EnableWindow(m_hwndPause, FALSE);
EnableWindow(m_hwndCancel, TRUE);
EnableWindow(m_hwndResume, TRUE);
break;
case kDownloadItemState_Done:
EnableWindow(m_hwndPause, FALSE);
EnableWindow(m_hwndCancel, FALSE);
EnableWindow(m_hwndResume, FALSE);
break;
default:
break;
}
//OutputDebugString("LVN_ITEMCHANGING\r\n");
}
break;
}
}
return result;
}
BOOL CALLBACK DownloadUI::MainProc( HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam )
{
BOOL result = FALSE;
static DownloadUI* m_ui = NULL;
switch(msg)
{
case WM_INITDIALOG:
{
m_ui = (DownloadUI*)lParam;
m_ui->SetWindowHandle(hwnd);
result = m_ui->InitDialog();
result = FALSE;
break;
}
case WM_INITMENUPOPUP:
{
HMENU menuPopup = (HMENU)wParam;
BOOL fSystemMenu = (BOOL) HIWORD(lParam);
if(fSystemMenu)
{
int32 count = GetMenuItemCount(menuPopup);
for(int32 i = 0; i < count; i++)
{
int32 id = GetMenuItemID(menuPopup, i);
if(61488 == id || id == 61440) // MAXIMIZE & SIZE COMMAND
{
EnableMenuItem(menuPopup, id, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
}
}
}
result = TRUE;
break;
}
case WM_MEASUREITEM:
{
MEASUREITEMSTRUCT* mis = (MEASUREITEMSTRUCT*) lParam;
TEXTMETRIC tm;
HDC hdc;
HFONT hFont;
HWND hwndLV;
// Make sure the control is the listview control
if (mis->CtlType != ODT_LISTVIEW || mis->CtlID == IDC_INFO)
return FALSE;
// Get the handle of the ListView control we're using
hwndLV = GetDlgItem(hwnd, mis->CtlID);
// Get the font the control is currently using
hFont = (HFONT)(DWORD) SendMessage(hwndLV, WM_GETFONT, 0, 0L);
// Set the font of the DC to the same font the control is using
hdc = GetDC(hwndLV);
SelectObject(hdc, hFont);
// Get the height of the font used by the control
if (!GetTextMetrics(hdc, &tm))
return FALSE;
// Add a little extra space between items
mis->itemHeight = tm.tmHeight + 1;
// Make sure there is enough room for the images which are CY_SMICON high
if (mis->itemHeight < 17)
mis->itemHeight = 17;
// Clean up
ReleaseDC(hwndLV, hdc);
result = TRUE;
break;
}
case WM_DRAWITEM:
{
result = m_ui->DrawItem(wParam, (DRAWITEMSTRUCT*) lParam);
break;
}
case WM_COMMAND:
{
result = m_ui->Command(wParam, (HWND)lParam);
break;
}
case WM_HELP:
{
m_ui->ShowHelp(Download_Manager);
return 1;
}
case WM_NOTIFY:
{
result = m_ui->Notify(wParam, (NMHDR*)lParam);
break;
}
case WM_CLOSE:
{
ShowWindow(hwnd, SW_HIDE);
result = TRUE;
break;
}
case WM_DESTROY:
{
result = m_ui->Destroy();
break;
}
}
return result;
}
uint32 DownloadUI::CalcStringEllipsis(HDC hdc, string& displayString, int32 columnWidth)
{
const TCHAR szEllipsis[] = TEXT("...");
SIZE sizeString;
SIZE sizeEllipsis;
string temp;
// Adjust the column width to take into account the edges
//columnWidth -= 4;
temp = displayString;
GetTextExtentPoint32(hdc, temp.c_str(), temp.size(), &sizeString);
// If the width of the string is greater than the column width shave
// the string and add the ellipsis
if(sizeString.cx >= columnWidth)
{
GetTextExtentPoint32(hdc, szEllipsis, strlen(szEllipsis), &sizeEllipsis);
while(temp.size() > 1)
{
temp.erase(temp.size() - 1, 1);
GetTextExtentPoint32(hdc, temp.c_str(), temp.size(), &sizeString);
if((sizeString.cx + sizeEllipsis.cx) < columnWidth)
{
// The string with the ellipsis finally fits
// Concatenate the two strings and break out of the loop
temp += szEllipsis;
displayString = temp;
break;
}
else if(temp.size() == 1)
{
temp += szEllipsis;
displayString = temp;
break;
}
}
}
GetTextExtentPoint32(hdc, displayString.c_str(), displayString.size(), &sizeString);
return sizeString.cx;
}
void DownloadUI::UpdateOverallProgress()
{
uint32 itemCount = ListView_GetItemCount(m_hwndList);
uint32 totalBytes = 0, doneBytes = 0;
uint32 totalItems = 0, doneItems = 0;
DownloadItem* dli = NULL;
if(itemCount)
{
LV_ITEM item;
for(uint32 i = 0; i < itemCount; i++)
{
item.mask = LVIF_PARAM;
item.iItem = i;
item.lParam = 0;
if(ListView_GetItem(m_hwndList, &item))
{
dli = (DownloadItem*)item.lParam;
DownloadItemState state = dli->GetState();
if(state == kDownloadItemState_Queued ||
state == kDownloadItemState_Downloading)
{
totalItems++;
totalBytes += dli->GetTotalBytes();
doneBytes += dli->GetBytesReceived();
}
else if(state == kDownloadItemState_Done)
{
doneItems++;
totalItems++;
totalBytes += dli->GetTotalBytes();
doneBytes += dli->GetTotalBytes();
}
}
}
}
m_totalBytes = totalBytes;
m_doneBytes = doneBytes;
m_totalItems = totalItems;
m_doneItems = doneItems;
InvalidateRect(m_hwndProgress, NULL, TRUE);
}
LRESULT WINAPI
FreeTracksWndProc(HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
DownloadUI* ui = (DownloadUI*)GetProp( hwnd, "ui" );
// Pass all non-custom messages to old window proc
return ui->FreeTracksWndProc(hwnd, msg, wParam, lParam);
}
LRESULT DownloadUI::FreeTracksWndProc(HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
WNDPROC lpOldProc = (WNDPROC)GetProp( hwnd, "oldproc" );
switch(msg)
{
case WM_DESTROY:
{
// Put back old window proc and
SetWindowLong( hwnd, GWL_WNDPROC, (DWORD)lpOldProc );
// remove window property
RemoveProp( hwnd, "oldproc" );
RemoveProp( hwnd, "ui" );
break;
}
case WM_SETCURSOR:
{
if(m_overURL)
{
SetCursor(m_handCursor);
return TRUE;
}
break;
}
case WM_MOUSEMOVE:
{
POINT pt;
pt.x = LOWORD(lParam); // horizontal position of cursor
pt.y = HIWORD(lParam); // vertical position of cursor
if(PtInRect(&m_urlRect, pt))
m_overURL = true;
else
m_overURL = false;
break;
}
case WM_LBUTTONDOWN:
{
POINT pt;
pt.x = LOWORD(lParam); // horizontal position of cursor
pt.y = HIWORD(lParam); // vertical position of cursor
if(PtInRect(&m_urlRect, pt))
{
char url[256];
GetWindowText(hwnd, url, sizeof(url));
ShellExecute(hwnd,
"open",
url,
NULL,
NULL,
SW_SHOWNORMAL);
}
break;
}
}
return CallWindowProc(lpOldProc, hwnd, msg, wParam, lParam);
}
void DownloadUI::ShowHelp(uint32 topic)
{
string oHelpFile;
char dir[MAX_PATH];
uint32 len = sizeof(dir);
m_context->prefs->GetInstallDirectory(dir, &len);
oHelpFile = string(dir);
oHelpFile += string("\\"HELP_FILE);
WinHelp(m_hwnd, oHelpFile.c_str(), HELP_CONTEXT, topic);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -