PageRenderTime 23ms CodeModel.GetById 1ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/toolkit/mozapps/update/updater/progressui_win.cpp

http://github.com/zpao/v8monkey
C++ | 308 lines | 194 code | 46 blank | 68 comment | 25 complexity | 2106c899bf03533df9e26096f4dec709 MD5 | raw file
  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/* ***** BEGIN LICENSE BLOCK *****
  4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5 *
  6 * The contents of this file are subject to the Mozilla Public License Version
  7 * 1.1 (the "License"); you may not use this file except in compliance with
  8 * the License. You may obtain a copy of the License at
  9 * http://www.mozilla.org/MPL/
 10 *
 11 * Software distributed under the License is distributed on an "AS IS" basis,
 12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 13 * for the specific language governing rights and limitations under the
 14 * License.
 15 *
 16 * The Original Code is mozilla.org code.
 17 *
 18 * The Initial Developer of the Original Code is Google Inc.
 19 * Portions created by the Initial Developer are Copyright (C) 2005
 20 * the Initial Developer. All Rights Reserved.
 21 *
 22 * Contributor(s):
 23 *  Darin Fisher <darin@meer.net>
 24 *  Masayuki Nakano <masayuki@d-toybox.com>
 25 *  Robert Strong <robert.bugzilla@gmail.com>
 26 *
 27 * Alternatively, the contents of this file may be used under the terms of
 28 * either the GNU General Public License Version 2 or later (the "GPL"), or
 29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 30 * in which case the provisions of the GPL or the LGPL are applicable instead
 31 * of those above. If you wish to allow use of your version of this file only
 32 * under the terms of either the GPL or the LGPL, and not to allow others to
 33 * use your version of this file under the terms of the MPL, indicate your
 34 * decision by deleting the provisions above and replace them with the notice
 35 * and other provisions required by the GPL or the LGPL. If you do not delete
 36 * the provisions above, a recipient may use your version of this file under
 37 * the terms of any one of the MPL, the GPL or the LGPL.
 38 *
 39 * ***** END LICENSE BLOCK ***** */
 40
 41#include <stdio.h>
 42#include <windows.h>
 43#include <commctrl.h>
 44#include <process.h>
 45#include <io.h>
 46
 47#include "resource.h"
 48#include "progressui.h"
 49#include "readstrings.h"
 50#include "errors.h"
 51
 52#define TIMER_ID 1
 53#define TIMER_INTERVAL 100
 54
 55#define RESIZE_WINDOW(hwnd, extrax, extray) \
 56  { \
 57    RECT windowSize; \
 58    GetWindowRect(hwnd, &windowSize); \
 59    SetWindowPos(hwnd, 0, 0, 0, windowSize.right - windowSize.left + extrax, \
 60                 windowSize.bottom - windowSize.top + extray, \
 61                 SWP_NOMOVE | SWP_NOZORDER); \
 62  }
 63
 64#define MOVE_WINDOW(hwnd, dx, dy) \
 65  { \
 66    RECT rc; \
 67    POINT pt; \
 68    GetWindowRect(hwnd, &rc); \
 69    pt.x = rc.left; \
 70    pt.y = rc.top; \
 71    ScreenToClient(GetParent(hwnd), &pt); \
 72    SetWindowPos(hwnd, 0, pt.x + dx, pt.y + dy, 0, 0, \
 73                 SWP_NOSIZE | SWP_NOZORDER); \
 74  }
 75
 76static float sProgress;  // between 0 and 100
 77static BOOL  sQuit = FALSE;
 78static BOOL sIndeterminate = FALSE;
 79static StringTable sUIStrings;
 80
 81static BOOL
 82GetStringsFile(WCHAR filename[MAX_PATH])
 83{
 84  if (!GetModuleFileNameW(NULL, filename, MAX_PATH))
 85    return FALSE;
 86 
 87  WCHAR *dot = wcsrchr(filename, '.');
 88  if (!dot || wcsicmp(dot + 1, L"exe"))
 89    return FALSE;
 90
 91  wcscpy(dot + 1, L"ini");
 92  return TRUE;
 93}
 94
 95static void
 96UpdateDialog(HWND hDlg)
 97{
 98  int pos = int(sProgress + 0.5f);
 99  HWND hWndPro = GetDlgItem(hDlg, IDC_PROGRESS);
100  SendMessage(hWndPro, PBM_SETPOS, pos, 0L);
101}
102
103// The code in this function is from MSDN:
104// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/dialogboxes/usingdialogboxes.asp
105static void
106CenterDialog(HWND hDlg)
107{
108  RECT rc, rcOwner, rcDlg;
109
110  // Get the owner window and dialog box rectangles. 
111  HWND desktop = GetDesktopWindow();
112
113  GetWindowRect(desktop, &rcOwner); 
114  GetWindowRect(hDlg, &rcDlg); 
115  CopyRect(&rc, &rcOwner); 
116
117  // Offset the owner and dialog box rectangles so that 
118  // right and bottom values represent the width and 
119  // height, and then offset the owner again to discard 
120  // space taken up by the dialog box. 
121
122  OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top); 
123  OffsetRect(&rc, -rc.left, -rc.top); 
124  OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom); 
125
126  // The new position is the sum of half the remaining 
127  // space and the owner's original position. 
128
129  SetWindowPos(hDlg, 
130               HWND_TOP, 
131               rcOwner.left + (rc.right / 2), 
132               rcOwner.top + (rc.bottom / 2), 
133               0, 0,          // ignores size arguments 
134               SWP_NOSIZE); 
135}
136
137static void
138InitDialog(HWND hDlg)
139{
140  WCHAR szwTitle[MAX_TEXT_LEN];
141  WCHAR szwInfo[MAX_TEXT_LEN];
142
143  MultiByteToWideChar(CP_UTF8, 0, sUIStrings.title, -1, szwTitle,
144                      sizeof(szwTitle)/sizeof(szwTitle[0]));
145  MultiByteToWideChar(CP_UTF8, 0, sUIStrings.info, -1, szwInfo,
146                      sizeof(szwInfo)/sizeof(szwInfo[0]));
147
148  SetWindowTextW(hDlg, szwTitle);
149  SetWindowTextW(GetDlgItem(hDlg, IDC_INFO), szwInfo);
150
151  // Set dialog icon
152  HICON hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_DIALOG));
153  if (hIcon)
154    SendMessage(hDlg, WM_SETICON, ICON_BIG, (LPARAM) hIcon);
155
156  HWND hWndPro = GetDlgItem(hDlg, IDC_PROGRESS);
157  SendMessage(hWndPro, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
158  if (sIndeterminate) {
159    LONG_PTR val = GetWindowLongPtr(hWndPro, GWL_STYLE);
160    SetWindowLongPtr(hWndPro, GWL_STYLE, val|PBS_MARQUEE); 
161    SendMessage(hWndPro,(UINT) PBM_SETMARQUEE,(WPARAM) TRUE,(LPARAM)50 );
162  }
163
164  // Resize the dialog to fit all of the text if necessary.
165  RECT infoSize, textSize;
166  HWND hWndInfo = GetDlgItem(hDlg, IDC_INFO);
167
168  // Get the control's font for calculating the new size for the control
169  HDC hDCInfo = GetDC(hWndInfo);
170  HFONT hInfoFont, hOldFont;
171  hInfoFont = (HFONT)SendMessage(hWndInfo, WM_GETFONT, 0, 0);
172
173  if (hInfoFont)
174    hOldFont = (HFONT)SelectObject(hDCInfo, hInfoFont);
175
176  // Measure the space needed for the text on a single line. DT_CALCRECT means
177  // nothing is drawn.
178  if (DrawText(hDCInfo, szwInfo, -1, &textSize,
179               DT_CALCRECT | DT_NOCLIP | DT_SINGLELINE)) {
180    GetClientRect(hWndInfo, &infoSize);
181    SIZE extra;
182    // Calculate the additional space needed for the text by subtracting from
183    // the rectangle returned by DrawText the existing client rectangle's width
184    // and height.
185    extra.cx = (textSize.right - textSize.left) - \
186               (infoSize.right - infoSize.left);
187    extra.cy = (textSize.bottom - textSize.top) - \
188               (infoSize.bottom - infoSize.top);
189    if (extra.cx < 0)
190      extra.cx = 0;
191    if (extra.cy < 0)
192      extra.cy = 0;
193    if ((extra.cx > 0) || (extra.cy > 0)) {
194      RESIZE_WINDOW(hDlg, extra.cx, extra.cy);
195      RESIZE_WINDOW(hWndInfo, extra.cx, extra.cy);
196      RESIZE_WINDOW(hWndPro, extra.cx, 0);
197      MOVE_WINDOW(hWndPro, 0, extra.cy);
198    }
199  }
200
201  if (hOldFont)
202    SelectObject(hDCInfo, hOldFont);
203
204  ReleaseDC(hWndInfo, hDCInfo);
205
206  CenterDialog(hDlg);  // make dialog appear in the center of the screen
207
208  SetTimer(hDlg, TIMER_ID, TIMER_INTERVAL, NULL);
209}
210
211// Message handler for update dialog.
212static LRESULT CALLBACK
213DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
214{
215  switch (message)
216  {
217  case WM_INITDIALOG:
218    InitDialog(hDlg);
219    return TRUE;
220
221  case WM_TIMER:
222    if (sQuit) {
223      EndDialog(hDlg, 0);
224    } else {
225      UpdateDialog(hDlg);
226    }
227    return TRUE;
228
229  case WM_COMMAND:
230    return TRUE;
231  }
232  return FALSE;
233}
234
235int
236InitProgressUI(int *argc, NS_tchar ***argv)
237{
238  return 0;
239}
240
241/**
242 * Initializes the progress UI strings
243 * 
244 * @return 0 on success, -1 on error
245*/
246int
247InitProgressUIStrings() {
248  // If we do not have updater.ini, then we should not bother showing UI.
249  WCHAR filename[MAX_PATH];
250  if (!GetStringsFile(filename)) {
251    return -1;
252  }
253
254  if (_waccess(filename, 04)) {
255    return -1;
256  }
257  
258  // If the updater.ini doesn't have the required strings, then we should not
259  // bother showing UI.
260  if (ReadStrings(filename, &sUIStrings) != OK) {
261    return -1;
262  }
263
264  return 0;
265}
266
267int
268ShowProgressUI(bool indeterminate, bool initUIStrings)
269{
270  sIndeterminate = indeterminate;
271  if (!indeterminate) {
272    // Only show the Progress UI if the process is taking a significant amount of
273    // time where a significant amount of time is defined as .5 seconds after
274    // ShowProgressUI is called sProgress is less than 70.
275    Sleep(500);
276
277    if (sQuit || sProgress > 70.0f)
278      return 0;
279  }
280
281  if (initUIStrings && InitProgressUIStrings() == -1) {
282    return -1;
283  }
284
285  INITCOMMONCONTROLSEX icc = {
286    sizeof(INITCOMMONCONTROLSEX),
287    ICC_PROGRESS_CLASS
288  };
289  InitCommonControlsEx(&icc);
290
291  DialogBox(GetModuleHandle(NULL),
292            MAKEINTRESOURCE(IDD_DIALOG), NULL,
293            (DLGPROC) DialogProc);
294
295  return 0;
296}
297
298void
299QuitProgressUI()
300{
301  sQuit = TRUE;
302}
303
304void
305UpdateProgressUI(float progress)
306{
307  sProgress = progress;  // 32-bit writes are atomic
308}