2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <commctrl.h>
|
|
|
|
#include <process.h>
|
|
|
|
#include <io.h>
|
2009-08-20 19:58:17 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "resource.h"
|
2007-12-31 07:15:43 -08:00
|
|
|
#include "progressui.h"
|
2009-01-08 22:25:49 -08:00
|
|
|
#include "readstrings.h"
|
|
|
|
#include "errors.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#define TIMER_ID 1
|
|
|
|
#define TIMER_INTERVAL 100
|
|
|
|
|
2007-07-11 12:04:48 -07:00
|
|
|
#define RESIZE_WINDOW(hwnd, extrax, extray) \
|
|
|
|
{ \
|
|
|
|
RECT windowSize; \
|
|
|
|
GetWindowRect(hwnd, &windowSize); \
|
|
|
|
SetWindowPos(hwnd, 0, 0, 0, windowSize.right - windowSize.left + extrax, \
|
|
|
|
windowSize.bottom - windowSize.top + extray, \
|
|
|
|
SWP_NOMOVE | SWP_NOZORDER); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MOVE_WINDOW(hwnd, dx, dy) \
|
|
|
|
{ \
|
2009-08-13 13:53:56 -07:00
|
|
|
RECT rc; \
|
|
|
|
POINT pt; \
|
|
|
|
GetWindowRect(hwnd, &rc); \
|
|
|
|
pt.x = rc.left; \
|
|
|
|
pt.y = rc.top; \
|
|
|
|
ScreenToClient(GetParent(hwnd), &pt); \
|
|
|
|
SetWindowPos(hwnd, 0, pt.x + dx, pt.y + dy, 0, 0, \
|
2007-07-11 12:04:48 -07:00
|
|
|
SWP_NOSIZE | SWP_NOZORDER); \
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static float sProgress; // between 0 and 100
|
|
|
|
static BOOL sQuit = FALSE;
|
2012-02-03 05:51:22 -08:00
|
|
|
static BOOL sIndeterminate = FALSE;
|
|
|
|
static StringTable sUIStrings;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static BOOL
|
2007-12-31 07:15:43 -08:00
|
|
|
GetStringsFile(WCHAR filename[MAX_PATH])
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-10 13:40:03 -07:00
|
|
|
if (!GetModuleFileNameW(nullptr, filename, MAX_PATH))
|
2007-03-22 10:30:00 -07:00
|
|
|
return FALSE;
|
2007-12-31 07:15:43 -08:00
|
|
|
|
|
|
|
WCHAR *dot = wcsrchr(filename, '.');
|
|
|
|
if (!dot || wcsicmp(dot + 1, L"exe"))
|
2007-03-22 10:30:00 -07:00
|
|
|
return FALSE;
|
|
|
|
|
2007-12-31 07:15:43 -08:00
|
|
|
wcscpy(dot + 1, L"ini");
|
2007-03-22 10:30:00 -07:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
UpdateDialog(HWND hDlg)
|
|
|
|
{
|
|
|
|
int pos = int(sProgress + 0.5f);
|
2009-08-13 13:53:56 -07:00
|
|
|
HWND hWndPro = GetDlgItem(hDlg, IDC_PROGRESS);
|
|
|
|
SendMessage(hWndPro, PBM_SETPOS, pos, 0L);
|
2007-07-11 12:04:48 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// The code in this function is from MSDN:
|
|
|
|
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/dialogboxes/usingdialogboxes.asp
|
|
|
|
static void
|
|
|
|
CenterDialog(HWND hDlg)
|
|
|
|
{
|
|
|
|
RECT rc, rcOwner, rcDlg;
|
|
|
|
|
|
|
|
// Get the owner window and dialog box rectangles.
|
|
|
|
HWND desktop = GetDesktopWindow();
|
|
|
|
|
|
|
|
GetWindowRect(desktop, &rcOwner);
|
|
|
|
GetWindowRect(hDlg, &rcDlg);
|
|
|
|
CopyRect(&rc, &rcOwner);
|
|
|
|
|
|
|
|
// Offset the owner and dialog box rectangles so that
|
|
|
|
// right and bottom values represent the width and
|
|
|
|
// height, and then offset the owner again to discard
|
|
|
|
// space taken up by the dialog box.
|
|
|
|
|
|
|
|
OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
|
|
|
|
OffsetRect(&rc, -rc.left, -rc.top);
|
|
|
|
OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
|
|
|
|
|
|
|
|
// The new position is the sum of half the remaining
|
|
|
|
// space and the owner's original position.
|
|
|
|
|
|
|
|
SetWindowPos(hDlg,
|
|
|
|
HWND_TOP,
|
|
|
|
rcOwner.left + (rc.right / 2),
|
|
|
|
rcOwner.top + (rc.bottom / 2),
|
|
|
|
0, 0, // ignores size arguments
|
|
|
|
SWP_NOSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InitDialog(HWND hDlg)
|
|
|
|
{
|
2009-01-08 22:25:49 -08:00
|
|
|
WCHAR szwTitle[MAX_TEXT_LEN];
|
|
|
|
WCHAR szwInfo[MAX_TEXT_LEN];
|
|
|
|
|
2012-02-03 05:51:22 -08:00
|
|
|
MultiByteToWideChar(CP_UTF8, 0, sUIStrings.title, -1, szwTitle,
|
2010-09-20 21:41:38 -07:00
|
|
|
sizeof(szwTitle)/sizeof(szwTitle[0]));
|
2012-02-03 05:51:22 -08:00
|
|
|
MultiByteToWideChar(CP_UTF8, 0, sUIStrings.info, -1, szwInfo,
|
2010-09-20 21:41:38 -07:00
|
|
|
sizeof(szwInfo)/sizeof(szwInfo[0]));
|
2009-01-08 22:25:49 -08:00
|
|
|
|
|
|
|
SetWindowTextW(hDlg, szwTitle);
|
|
|
|
SetWindowTextW(GetDlgItem(hDlg, IDC_INFO), szwInfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Set dialog icon
|
2013-10-10 13:40:03 -07:00
|
|
|
HICON hIcon = LoadIcon(GetModuleHandle(nullptr),
|
|
|
|
MAKEINTRESOURCE(IDI_DIALOG));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (hIcon)
|
|
|
|
SendMessage(hDlg, WM_SETICON, ICON_BIG, (LPARAM) hIcon);
|
|
|
|
|
2009-08-13 13:53:56 -07:00
|
|
|
HWND hWndPro = GetDlgItem(hDlg, IDC_PROGRESS);
|
|
|
|
SendMessage(hWndPro, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
|
2012-02-03 05:51:22 -08:00
|
|
|
if (sIndeterminate) {
|
|
|
|
LONG_PTR val = GetWindowLongPtr(hWndPro, GWL_STYLE);
|
|
|
|
SetWindowLongPtr(hWndPro, GWL_STYLE, val|PBS_MARQUEE);
|
|
|
|
SendMessage(hWndPro,(UINT) PBM_SETMARQUEE,(WPARAM) TRUE,(LPARAM)50 );
|
|
|
|
}
|
2009-08-13 13:53:56 -07:00
|
|
|
|
|
|
|
// Resize the dialog to fit all of the text if necessary.
|
|
|
|
RECT infoSize, textSize;
|
|
|
|
HWND hWndInfo = GetDlgItem(hDlg, IDC_INFO);
|
|
|
|
|
2009-08-20 19:58:17 -07:00
|
|
|
// Get the control's font for calculating the new size for the control
|
2009-08-13 13:53:56 -07:00
|
|
|
HDC hDCInfo = GetDC(hWndInfo);
|
|
|
|
HFONT hInfoFont, hOldFont;
|
|
|
|
hInfoFont = (HFONT)SendMessage(hWndInfo, WM_GETFONT, 0, 0);
|
|
|
|
|
|
|
|
if (hInfoFont)
|
|
|
|
hOldFont = (HFONT)SelectObject(hDCInfo, hInfoFont);
|
|
|
|
|
2009-08-20 19:58:17 -07:00
|
|
|
// Measure the space needed for the text on a single line. DT_CALCRECT means
|
|
|
|
// nothing is drawn.
|
|
|
|
if (DrawText(hDCInfo, szwInfo, -1, &textSize,
|
2009-08-13 13:53:56 -07:00
|
|
|
DT_CALCRECT | DT_NOCLIP | DT_SINGLELINE)) {
|
2009-08-20 19:58:17 -07:00
|
|
|
GetClientRect(hWndInfo, &infoSize);
|
2009-08-13 13:53:56 -07:00
|
|
|
SIZE extra;
|
2009-08-20 19:58:17 -07:00
|
|
|
// Calculate the additional space needed for the text by subtracting from
|
|
|
|
// the rectangle returned by DrawText the existing client rectangle's width
|
|
|
|
// and height.
|
|
|
|
extra.cx = (textSize.right - textSize.left) - \
|
|
|
|
(infoSize.right - infoSize.left);
|
|
|
|
extra.cy = (textSize.bottom - textSize.top) - \
|
|
|
|
(infoSize.bottom - infoSize.top);
|
2009-08-13 13:53:56 -07:00
|
|
|
if (extra.cx < 0)
|
|
|
|
extra.cx = 0;
|
|
|
|
if (extra.cy < 0)
|
|
|
|
extra.cy = 0;
|
|
|
|
if ((extra.cx > 0) || (extra.cy > 0)) {
|
|
|
|
RESIZE_WINDOW(hDlg, extra.cx, extra.cy);
|
|
|
|
RESIZE_WINDOW(hWndInfo, extra.cx, extra.cy);
|
|
|
|
RESIZE_WINDOW(hWndPro, extra.cx, 0);
|
|
|
|
MOVE_WINDOW(hWndPro, 0, extra.cy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hOldFont)
|
|
|
|
SelectObject(hDCInfo, hOldFont);
|
|
|
|
|
|
|
|
ReleaseDC(hWndInfo, hDCInfo);
|
2007-07-11 12:04:48 -07:00
|
|
|
|
2011-04-13 13:52:55 -07:00
|
|
|
CenterDialog(hDlg); // make dialog appear in the center of the screen
|
|
|
|
|
2013-10-10 13:40:03 -07:00
|
|
|
SetTimer(hDlg, TIMER_ID, TIMER_INTERVAL, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-11-18 17:20:44 -08:00
|
|
|
// Message handler for update dialog.
|
2007-03-22 10:30:00 -07:00
|
|
|
static LRESULT CALLBACK
|
|
|
|
DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
switch (message)
|
|
|
|
{
|
|
|
|
case WM_INITDIALOG:
|
|
|
|
InitDialog(hDlg);
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case WM_TIMER:
|
|
|
|
if (sQuit) {
|
|
|
|
EndDialog(hDlg, 0);
|
|
|
|
} else {
|
|
|
|
UpdateDialog(hDlg);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case WM_COMMAND:
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-12-31 07:15:43 -08:00
|
|
|
InitProgressUI(int *argc, NS_tchar ***argv)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-03 05:51:22 -08:00
|
|
|
/**
|
|
|
|
* Initializes the progress UI strings
|
|
|
|
*
|
|
|
|
* @return 0 on success, -1 on error
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
int
|
2012-02-03 05:51:22 -08:00
|
|
|
InitProgressUIStrings() {
|
2007-03-22 10:30:00 -07:00
|
|
|
// If we do not have updater.ini, then we should not bother showing UI.
|
2007-12-31 07:15:43 -08:00
|
|
|
WCHAR filename[MAX_PATH];
|
2012-02-03 05:51:22 -08:00
|
|
|
if (!GetStringsFile(filename)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return -1;
|
2012-02-03 05:51:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_waccess(filename, 04)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return -1;
|
2012-02-03 05:51:22 -08:00
|
|
|
}
|
|
|
|
|
2010-09-20 21:41:38 -07:00
|
|
|
// If the updater.ini doesn't have the required strings, then we should not
|
|
|
|
// bother showing UI.
|
2012-02-03 05:51:22 -08:00
|
|
|
if (ReadStrings(filename, &sUIStrings) != OK) {
|
2010-09-20 21:41:38 -07:00
|
|
|
return -1;
|
2012-02-03 05:51:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ShowProgressUI(bool indeterminate, bool initUIStrings)
|
|
|
|
{
|
|
|
|
sIndeterminate = indeterminate;
|
|
|
|
if (!indeterminate) {
|
|
|
|
// Only show the Progress UI if the process is taking a significant amount of
|
|
|
|
// time where a significant amount of time is defined as .5 seconds after
|
|
|
|
// ShowProgressUI is called sProgress is less than 70.
|
|
|
|
Sleep(500);
|
|
|
|
|
|
|
|
if (sQuit || sProgress > 70.0f)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (initUIStrings && InitProgressUIStrings() == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
INITCOMMONCONTROLSEX icc = {
|
|
|
|
sizeof(INITCOMMONCONTROLSEX),
|
|
|
|
ICC_PROGRESS_CLASS
|
|
|
|
};
|
|
|
|
InitCommonControlsEx(&icc);
|
|
|
|
|
2013-10-10 13:40:03 -07:00
|
|
|
DialogBox(GetModuleHandle(nullptr),
|
|
|
|
MAKEINTRESOURCE(IDD_DIALOG), nullptr,
|
2007-03-22 10:30:00 -07:00
|
|
|
(DLGPROC) DialogProc);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QuitProgressUI()
|
|
|
|
{
|
|
|
|
sQuit = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UpdateProgressUI(float progress)
|
|
|
|
{
|
|
|
|
sProgress = progress; // 32-bit writes are atomic
|
|
|
|
}
|