1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include <stdio.h>
8 #include <windows.h>
9 #include <commctrl.h>
10 #include <process.h>
11 #include <io.h>
12
13 #include "resource.h"
14 #include "progressui.h"
15 #include "readstrings.h"
16 #include "updatererrors.h"
17
18 #define TIMER_ID 1
19 #define TIMER_INTERVAL 100
20
21 #define RESIZE_WINDOW(hwnd, extrax, extray) \
22 { \
23 RECT windowSize; \
24 GetWindowRect(hwnd, &windowSize); \
25 SetWindowPos(hwnd, 0, 0, 0, windowSize.right - windowSize.left + extrax, \
26 windowSize.bottom - windowSize.top + extray, \
27 SWP_NOMOVE | SWP_NOZORDER); \
28 }
29
30 #define MOVE_WINDOW(hwnd, dx, dy) \
31 { \
32 RECT rc; \
33 POINT pt; \
34 GetWindowRect(hwnd, &rc); \
35 pt.x = rc.left; \
36 pt.y = rc.top; \
37 ScreenToClient(GetParent(hwnd), &pt); \
38 SetWindowPos(hwnd, 0, pt.x + dx, pt.y + dy, 0, 0, \
39 SWP_NOSIZE | SWP_NOZORDER); \
40 }
41
42 static float sProgress; // between 0 and 100
43 static BOOL sQuit = FALSE;
44 static BOOL sIndeterminate = FALSE;
45 static StringTable sUIStrings;
46
GetStringsFile(WCHAR filename[MAX_PATH])47 static BOOL GetStringsFile(WCHAR filename[MAX_PATH]) {
48 if (!GetModuleFileNameW(nullptr, filename, MAX_PATH)) {
49 return FALSE;
50 }
51
52 WCHAR* dot = wcsrchr(filename, '.');
53 if (!dot || wcsicmp(dot + 1, L"exe")) {
54 return FALSE;
55 }
56
57 wcscpy(dot + 1, L"ini");
58 return TRUE;
59 }
60
UpdateDialog(HWND hDlg)61 static void UpdateDialog(HWND hDlg) {
62 int pos = int(sProgress + 0.5f);
63 HWND hWndPro = GetDlgItem(hDlg, IDC_PROGRESS);
64 SendMessage(hWndPro, PBM_SETPOS, pos, 0L);
65 }
66
67 // The code in this function is from MSDN:
68 // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/dialogboxes/usingdialogboxes.asp
CenterDialog(HWND hDlg)69 static void CenterDialog(HWND hDlg) {
70 RECT rc, rcOwner, rcDlg;
71
72 // Get the owner window and dialog box rectangles.
73 HWND desktop = GetDesktopWindow();
74
75 GetWindowRect(desktop, &rcOwner);
76 GetWindowRect(hDlg, &rcDlg);
77 CopyRect(&rc, &rcOwner);
78
79 // Offset the owner and dialog box rectangles so that
80 // right and bottom values represent the width and
81 // height, and then offset the owner again to discard
82 // space taken up by the dialog box.
83
84 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
85 OffsetRect(&rc, -rc.left, -rc.top);
86 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
87
88 // The new position is the sum of half the remaining
89 // space and the owner's original position.
90
91 SetWindowPos(hDlg, HWND_TOP, rcOwner.left + (rc.right / 2),
92 rcOwner.top + (rc.bottom / 2), 0, 0, // ignores size arguments
93 SWP_NOSIZE);
94 }
95
InitDialog(HWND hDlg)96 static void InitDialog(HWND hDlg) {
97 mozilla::UniquePtr<WCHAR[]> szwTitle;
98 mozilla::UniquePtr<WCHAR[]> szwInfo;
99
100 int bufferSize =
101 MultiByteToWideChar(CP_UTF8, 0, sUIStrings.title.get(), -1, nullptr, 0);
102 szwTitle = mozilla::MakeUnique<WCHAR[]>(bufferSize);
103 MultiByteToWideChar(CP_UTF8, 0, sUIStrings.title.get(), -1, szwTitle.get(),
104 bufferSize);
105 bufferSize =
106 MultiByteToWideChar(CP_UTF8, 0, sUIStrings.info.get(), -1, nullptr, 0);
107 szwInfo = mozilla::MakeUnique<WCHAR[]>(bufferSize);
108 MultiByteToWideChar(CP_UTF8, 0, sUIStrings.info.get(), -1, szwInfo.get(),
109 bufferSize);
110
111 SetWindowTextW(hDlg, szwTitle.get());
112 SetWindowTextW(GetDlgItem(hDlg, IDC_INFO), szwInfo.get());
113
114 // Set dialog icon
115 HICON hIcon = LoadIcon(GetModuleHandle(nullptr), MAKEINTRESOURCE(IDI_DIALOG));
116 if (hIcon) {
117 SendMessage(hDlg, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
118 }
119
120 HWND hWndPro = GetDlgItem(hDlg, IDC_PROGRESS);
121 SendMessage(hWndPro, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
122 if (sIndeterminate) {
123 LONG_PTR val = GetWindowLongPtr(hWndPro, GWL_STYLE);
124 SetWindowLongPtr(hWndPro, GWL_STYLE, val | PBS_MARQUEE);
125 SendMessage(hWndPro, (UINT)PBM_SETMARQUEE, (WPARAM)TRUE, (LPARAM)50);
126 }
127
128 // Resize the dialog to fit all of the text if necessary.
129 RECT infoSize, textSize;
130 HWND hWndInfo = GetDlgItem(hDlg, IDC_INFO);
131
132 // Get the control's font for calculating the new size for the control
133 HDC hDCInfo = GetDC(hWndInfo);
134 HFONT hInfoFont, hOldFont = NULL;
135 hInfoFont = (HFONT)SendMessage(hWndInfo, WM_GETFONT, 0, 0);
136
137 if (hInfoFont) {
138 hOldFont = (HFONT)SelectObject(hDCInfo, hInfoFont);
139 }
140
141 // Measure the space needed for the text on a single line. DT_CALCRECT means
142 // nothing is drawn.
143 if (DrawText(hDCInfo, szwInfo.get(), -1, &textSize,
144 DT_CALCRECT | DT_NOCLIP | DT_SINGLELINE)) {
145 GetClientRect(hWndInfo, &infoSize);
146 SIZE extra;
147 // Calculate the additional space needed for the text by subtracting from
148 // the rectangle returned by DrawText the existing client rectangle's width
149 // and height.
150 extra.cx =
151 (textSize.right - textSize.left) - (infoSize.right - infoSize.left);
152 extra.cy =
153 (textSize.bottom - textSize.top) - (infoSize.bottom - infoSize.top);
154 if (extra.cx < 0) {
155 extra.cx = 0;
156 }
157 if (extra.cy < 0) {
158 extra.cy = 0;
159 }
160 if ((extra.cx > 0) || (extra.cy > 0)) {
161 RESIZE_WINDOW(hDlg, extra.cx, extra.cy);
162 RESIZE_WINDOW(hWndInfo, extra.cx, extra.cy);
163 RESIZE_WINDOW(hWndPro, extra.cx, 0);
164 MOVE_WINDOW(hWndPro, 0, extra.cy);
165 }
166 }
167
168 if (hOldFont) {
169 SelectObject(hDCInfo, hOldFont);
170 }
171
172 ReleaseDC(hWndInfo, hDCInfo);
173
174 CenterDialog(hDlg); // make dialog appear in the center of the screen
175
176 SetTimer(hDlg, TIMER_ID, TIMER_INTERVAL, nullptr);
177 }
178
179 // Message handler for update dialog.
DialogProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)180 static LRESULT CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam,
181 LPARAM lParam) {
182 switch (message) {
183 case WM_INITDIALOG:
184 InitDialog(hDlg);
185 return TRUE;
186
187 case WM_TIMER:
188 if (sQuit) {
189 EndDialog(hDlg, 0);
190 } else {
191 UpdateDialog(hDlg);
192 }
193 return TRUE;
194
195 case WM_COMMAND:
196 return TRUE;
197 }
198 return FALSE;
199 }
200
InitProgressUI(int * argc,WCHAR *** argv)201 int InitProgressUI(int* argc, WCHAR*** argv) { return 0; }
202
203 /**
204 * Initializes the progress UI strings
205 *
206 * @return 0 on success, -1 on error
207 */
InitProgressUIStrings()208 int InitProgressUIStrings() {
209 // If we do not have updater.ini, then we should not bother showing UI.
210 WCHAR filename[MAX_PATH];
211 if (!GetStringsFile(filename)) {
212 return -1;
213 }
214
215 if (_waccess(filename, 04)) {
216 return -1;
217 }
218
219 // If the updater.ini doesn't have the required strings, then we should not
220 // bother showing UI.
221 if (ReadStrings(filename, &sUIStrings) != OK) {
222 return -1;
223 }
224
225 return 0;
226 }
227
ShowProgressUI(bool indeterminate,bool initUIStrings)228 int ShowProgressUI(bool indeterminate, bool initUIStrings) {
229 sIndeterminate = indeterminate;
230 if (!indeterminate) {
231 // Only show the Progress UI if the process is taking a significant amount
232 // of time where a significant amount of time is defined as .5 seconds after
233 // ShowProgressUI is called sProgress is less than 70.
234 Sleep(500);
235
236 if (sQuit || sProgress > 70.0f) {
237 return 0;
238 }
239 }
240
241 // Don't load the UI if there's an <exe_name>.Local directory for redirection.
242 WCHAR appPath[MAX_PATH + 1] = {L'\0'};
243 if (!GetModuleFileNameW(nullptr, appPath, MAX_PATH)) {
244 return -1;
245 }
246
247 if (wcslen(appPath) + wcslen(L".Local") >= MAX_PATH) {
248 return -1;
249 }
250
251 wcscat(appPath, L".Local");
252
253 if (!_waccess(appPath, 04)) {
254 return -1;
255 }
256
257 // Don't load the UI if the strings for the UI are not provided.
258 if (initUIStrings && InitProgressUIStrings() == -1) {
259 return -1;
260 }
261
262 if (!GetModuleFileNameW(nullptr, appPath, MAX_PATH)) {
263 return -1;
264 }
265
266 // Use an activation context that supports visual styles for the controls.
267 ACTCTXW actx = {0};
268 actx.cbSize = sizeof(ACTCTXW);
269 actx.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID | ACTCTX_FLAG_HMODULE_VALID;
270 actx.hModule = GetModuleHandle(NULL); // Use the embedded manifest
271 // This is needed only for Win XP but doesn't cause a problem with other
272 // versions of Windows.
273 actx.lpSource = appPath;
274 actx.lpResourceName = MAKEINTRESOURCE(IDR_COMCTL32_MANIFEST);
275
276 HANDLE hactx = INVALID_HANDLE_VALUE;
277 hactx = CreateActCtxW(&actx);
278 ULONG_PTR actxCookie = NULL;
279 if (hactx != INVALID_HANDLE_VALUE) {
280 // Push the specified activation context to the top of the activation stack.
281 ActivateActCtx(hactx, &actxCookie);
282 }
283
284 INITCOMMONCONTROLSEX icc = {sizeof(INITCOMMONCONTROLSEX), ICC_PROGRESS_CLASS};
285 InitCommonControlsEx(&icc);
286
287 DialogBox(GetModuleHandle(nullptr), MAKEINTRESOURCE(IDD_DIALOG), nullptr,
288 (DLGPROC)DialogProc);
289
290 if (hactx != INVALID_HANDLE_VALUE) {
291 // Deactivate the context now that the comctl32.dll is loaded.
292 DeactivateActCtx(0, actxCookie);
293 }
294
295 return 0;
296 }
297
QuitProgressUI()298 void QuitProgressUI() { sQuit = TRUE; }
299
UpdateProgressUI(float progress)300 void UpdateProgressUI(float progress) {
301 sProgress = progress; // 32-bit writes are atomic
302 }
303