1 | #include "copyProgress.h"
|
---|
2 |
|
---|
3 | #include <windows.h>
|
---|
4 | #include <commctrl.h>
|
---|
5 |
|
---|
6 | #include <stdio.h>
|
---|
7 |
|
---|
8 | extern HINSTANCE app_instance;
|
---|
9 | extern void gsInstall_getDesktopExt(int *x, int *y);
|
---|
10 |
|
---|
11 | LRESULT CALLBACK copyProgressBar_windowProc(HWND window, UINT message,
|
---|
12 | WPARAM wParam, LPARAM lParam)
|
---|
13 | {
|
---|
14 | switch (message)
|
---|
15 | {
|
---|
16 | case WM_CREATE:
|
---|
17 | {
|
---|
18 | CREATESTRUCT *createStruct = (CREATESTRUCT *) lParam;
|
---|
19 | long *createParams;
|
---|
20 | copyProgressBar *progressBar;
|
---|
21 |
|
---|
22 | createParams = (long *) createStruct->lpCreateParams;
|
---|
23 |
|
---|
24 | SetWindowLong(window, GWL_USERDATA, createParams[0]);
|
---|
25 | progressBar = (copyProgressBar *) GetWindowLong(window, GWL_USERDATA);
|
---|
26 | if (progressBar == NULL)
|
---|
27 | {
|
---|
28 | // TODO: error handling
|
---|
29 | }
|
---|
30 |
|
---|
31 | CreateWindowEx( 0, "STATIC", (char *) createParams[1], WS_CHILD | WS_VISIBLE,
|
---|
32 | 10, 5, 180, 20, window, (HMENU) 0,
|
---|
33 | app_instance, NULL);
|
---|
34 | }
|
---|
35 | break;
|
---|
36 |
|
---|
37 | default:
|
---|
38 | return(DefWindowProc(window, message, wParam, lParam));
|
---|
39 | }
|
---|
40 |
|
---|
41 | return 0;
|
---|
42 | }
|
---|
43 |
|
---|
44 | void copyProgressBar::registerClass()
|
---|
45 | { static BOOL registered = FALSE;
|
---|
46 |
|
---|
47 | if (registered == FALSE)
|
---|
48 | { WNDCLASS classdef;
|
---|
49 |
|
---|
50 | classdef.style = 0;
|
---|
51 | classdef.lpfnWndProc = copyProgressBar_windowProc;
|
---|
52 | classdef.cbClsExtra = 0;
|
---|
53 | classdef.cbWndExtra = sizeof(copyProgressBar *) + sizeof(long);
|
---|
54 | classdef.hInstance = app_instance;
|
---|
55 | classdef.hIcon = 0;
|
---|
56 | classdef.hCursor = NULL;
|
---|
57 | classdef.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
|
---|
58 | classdef.lpszMenuName = NULL;
|
---|
59 | classdef.lpszClassName = "CopyProgressBar";
|
---|
60 | RegisterClass(&classdef);
|
---|
61 | registered = TRUE;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | copyProgressBar::copyProgressBar(unsigned long totalSize, unsigned long totalFiles)
|
---|
66 | { this->total = totalSize;
|
---|
67 | this->totalFiles = totalFiles;
|
---|
68 | this->copied = 0;
|
---|
69 | this->copiedFiles = 0;
|
---|
70 | this->showing = false;
|
---|
71 | }
|
---|
72 |
|
---|
73 | copyProgressBar::copyProgressBar()
|
---|
74 | { this->total = 0;
|
---|
75 | this->totalFiles = 0;
|
---|
76 | this->copied = 0;
|
---|
77 | this->copiedFiles = 0;
|
---|
78 | this->showing = false;
|
---|
79 | }
|
---|
80 |
|
---|
81 | // just ensure everything is tidily terminated
|
---|
82 | copyProgressBar::~copyProgressBar()
|
---|
83 | { this->close();
|
---|
84 | }
|
---|
85 |
|
---|
86 | void copyProgressBar::init(unsigned long totalSize, unsigned long totalFiles)
|
---|
87 | { this->total = totalSize;
|
---|
88 | this->totalFiles = totalFiles;
|
---|
89 | this->copied = 0;
|
---|
90 | this->copiedFiles = 0;
|
---|
91 | }
|
---|
92 |
|
---|
93 | void copyProgressBar::done(unsigned long bytesDone)
|
---|
94 | {
|
---|
95 | static char progressText[100] = "Remaining: ";
|
---|
96 | static int progressTimePos = strlen(progressText);
|
---|
97 | double seconds;
|
---|
98 |
|
---|
99 | // note the progress made by the extra bytes
|
---|
100 | this->copied += bytesDone;
|
---|
101 | this->copiedFiles += 1;
|
---|
102 |
|
---|
103 | // present the progress in the progress bar
|
---|
104 | double percent = (double) this->copied / (double) this->total;
|
---|
105 | percent = percent * 100;
|
---|
106 | SendMessage(this->progressBar, PBM_SETPOS, (int) percent, 0L);
|
---|
107 |
|
---|
108 | // present time remaining
|
---|
109 | seconds = difftime(time(NULL), this->startTime);
|
---|
110 | if (this->copied == 0)
|
---|
111 | { return;
|
---|
112 | }
|
---|
113 | seconds = seconds * (double) (this->total - this->copied) / (double) this->copied;
|
---|
114 |
|
---|
115 | sprintf(&progressText[progressTimePos], "%d:%02d %3.1fMb (%d files)", (int) (seconds / 60), ((int) seconds) % 60, double (this->total - this->copied) / (double) (1024 * 1024), this->totalFiles - this->copiedFiles);
|
---|
116 | SetWindowText(this->progressTime, progressText);
|
---|
117 | }
|
---|
118 |
|
---|
119 | void copyProgressBar::done(unsigned long bytesDone, LPSTR message)
|
---|
120 | {
|
---|
121 | this->done(bytesDone);
|
---|
122 | this->message(message);
|
---|
123 | }
|
---|
124 |
|
---|
125 | void copyProgressBar::message(LPSTR message)
|
---|
126 | { SetWindowText(this->progressText, message);
|
---|
127 | }
|
---|
128 |
|
---|
129 | void copyProgressBar::show()
|
---|
130 | { long params[2];
|
---|
131 | int sx, sy, dx, dy;
|
---|
132 |
|
---|
133 | params[0] = (long) this;
|
---|
134 | params[1] = (long) "Copying files....";
|
---|
135 | this->registerClass();
|
---|
136 |
|
---|
137 | gsInstall_getDesktopExt(&sx, &sy);
|
---|
138 |
|
---|
139 | dx = (sx - 400) >> 1;
|
---|
140 | dy = (sy - 110) >> 1;
|
---|
141 |
|
---|
142 |
|
---|
143 | // Create the parent to hold the progress elements
|
---|
144 | this->progressParent = CreateWindowEx( WS_EX_APPWINDOW, "CopyProgressBar",
|
---|
145 | "Greenstone Installer", WS_VISIBLE,
|
---|
146 | dx, dy, 400, 110,
|
---|
147 | 0, (HMENU) 0,
|
---|
148 | app_instance, (void *) params);
|
---|
149 | // Create the progress bar itself
|
---|
150 | this->progressBar = CreateWindowEx(0, PROGRESS_CLASS, NULL, WS_CHILD | WS_VISIBLE,
|
---|
151 | 10, 60, 380, GetSystemMetrics(SM_CYVSCROLL),
|
---|
152 | this->progressParent, (HMENU) 0,
|
---|
153 | app_instance, NULL);
|
---|
154 |
|
---|
155 | // Create the text reporter
|
---|
156 | this->progressText = CreateWindowEx(0, "STATIC", "", WS_CHILD | SS_LEFT | WS_VISIBLE,
|
---|
157 | 10, 5, 380, 20,
|
---|
158 | this->progressParent, (HMENU) 0,
|
---|
159 | app_instance, NULL);
|
---|
160 |
|
---|
161 | // Create the time reporter
|
---|
162 | this->progressTime = CreateWindowEx(0, "STATIC", "", WS_CHILD | SS_LEFT | WS_VISIBLE,
|
---|
163 | 10, 30, 380, 20,
|
---|
164 | this->progressParent, (HMENU) 0,
|
---|
165 | app_instance, NULL);
|
---|
166 |
|
---|
167 | // Note that we are now showing the progress bar
|
---|
168 | this->showing = true;
|
---|
169 | }
|
---|
170 |
|
---|
171 | // ensure that if the windows are showing, they are closed before the object
|
---|
172 | // is destroyed
|
---|
173 | void copyProgressBar::close()
|
---|
174 | { if (this->showing == true)
|
---|
175 | {
|
---|
176 | ShowWindow(this->progressBar, SW_HIDE);
|
---|
177 | DestroyWindow(this->progressBar);
|
---|
178 |
|
---|
179 | ShowWindow(this->progressParent, SW_HIDE);
|
---|
180 | DestroyWindow(this->progressParent);
|
---|
181 | this->showing = false;
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|