]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Ventoy2Disk/Ventoy2Disk/WinDialog.c
d531b4563ce414b065bb2d45482ed6fd89458299
[Ventoy.git] / Ventoy2Disk / Ventoy2Disk / WinDialog.c
1 /******************************************************************************
2 * WinDialog.c
3 *
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include <Windows.h>
22 #include <commctrl.h>
23 #include "resource.h"
24 #include "Language.h"
25 #include "Ventoy2Disk.h"
26
27 HINSTANCE g_hInst;
28
29 HWND g_DialogHwnd;
30 HWND g_ComboxHwnd;
31 HWND g_StaticLocalVerHwnd;
32 HWND g_StaticDiskVerHwnd;
33 HWND g_BtnInstallHwnd;
34 HWND g_BtnUpdateHwnd;
35 HWND g_ProgressBarHwnd;
36 HWND g_StaticStatusHwnd;
37 CHAR g_CurVersion[64];
38 HANDLE g_ThreadHandle = NULL;
39
40 void GetExeVersionInfo(const char *FilePath)
41 {
42 UINT length;
43 DWORD verBufferSize;
44 CHAR verBuffer[2048];
45 VS_FIXEDFILEINFO *verInfo = NULL;
46
47 verBufferSize = GetFileVersionInfoSizeA(FilePath, NULL);
48
49 if (verBufferSize > 0 && verBufferSize <= sizeof(verBuffer))
50 {
51 if (GetFileVersionInfoA(FilePath, 0, verBufferSize, (LPVOID)verBuffer))
52 {
53 VerQueryValueA(verBuffer, "\\", &verInfo, &length);
54
55 safe_sprintf(g_CurVersion, "%u.%u.%u.%u",
56 HIWORD(verInfo->dwProductVersionMS),
57 LOWORD(verInfo->dwProductVersionMS),
58 HIWORD(verInfo->dwProductVersionLS),
59 LOWORD(verInfo->dwProductVersionLS));
60 }
61 }
62 }
63
64 void SetProgressBarPos(int Pos)
65 {
66 CHAR Ratio[64];
67
68 if (Pos >= PT_FINISH)
69 {
70 Pos = PT_FINISH;
71 }
72
73 SendMessage(g_ProgressBarHwnd, PBM_SETPOS, Pos, 0);
74
75 safe_sprintf(Ratio, "Status - %.0lf%%", Pos * 100.0 / PT_FINISH);
76 SetWindowTextA(g_StaticStatusHwnd, Ratio);
77 }
78
79 static void OnComboxSelChange(HWND hCombox)
80 {
81 int nCurSelected;
82 PHY_DRIVE_INFO *CurDrive = NULL;
83
84 SetWindowTextA(g_StaticLocalVerHwnd, GetLocalVentoyVersion());
85 EnableWindow(g_BtnInstallHwnd, FALSE);
86 EnableWindow(g_BtnUpdateHwnd, FALSE);
87
88 nCurSelected = SendMessage(hCombox, CB_GETCURSEL, 0, 0);
89 if (CB_ERR == nCurSelected)
90 {
91 return;
92 }
93
94 CurDrive = GetPhyDriveInfoById(nCurSelected);
95 if (!CurDrive)
96 {
97 return;
98 }
99
100 SetWindowTextA(g_StaticDiskVerHwnd, CurDrive->VentoyVersion);
101
102 if (g_ForceOperation == 0)
103 {
104 if (CurDrive->VentoyVersion[0])
105 {
106 //only can update
107 EnableWindow(g_BtnInstallHwnd, FALSE);
108 EnableWindow(g_BtnUpdateHwnd, TRUE);
109 }
110 else
111 {
112 //only can install
113 EnableWindow(g_BtnInstallHwnd, TRUE);
114 EnableWindow(g_BtnUpdateHwnd, FALSE);
115 }
116 }
117 else
118 {
119 EnableWindow(g_BtnInstallHwnd, TRUE);
120 EnableWindow(g_BtnUpdateHwnd, TRUE);
121 }
122
123 InvalidateRect(g_DialogHwnd, NULL, TRUE);
124 UpdateWindow(g_DialogHwnd);
125 }
126
127 static void LanguageInit(void)
128 {
129 SetWindowText(GetDlgItem(g_DialogHwnd, IDC_STATIC_DEV), _G(STR_DEVICE));
130 SetWindowText(GetDlgItem(g_DialogHwnd, IDC_STATIC_LOCAL), _G(STR_LOCAL_VER));
131 SetWindowText(GetDlgItem(g_DialogHwnd, IDC_STATIC_DISK), _G(STR_DISK_VER));
132 SetWindowText(g_StaticStatusHwnd, _G(STR_STATUS));
133
134 SetWindowText(g_BtnInstallHwnd, _G(STR_INSTALL));
135 SetWindowText(g_BtnUpdateHwnd, _G(STR_UPDATE));
136 }
137
138 static BOOL InitDialog(HWND hWnd, WPARAM wParam, LPARAM lParam)
139 {
140 DWORD i;
141 HANDLE hCombox;
142 HFONT hStaticFont;
143 CHAR Letter[32];
144 CHAR DeviceName[256];
145 HICON hIcon;
146
147 g_DialogHwnd = hWnd;
148 g_ComboxHwnd = GetDlgItem(hWnd, IDC_COMBO1);
149 g_StaticLocalVerHwnd = GetDlgItem(hWnd, IDC_STATIC_LOCAL_VER);
150 g_StaticDiskVerHwnd = GetDlgItem(hWnd, IDC_STATIC_DISK_VER);
151 g_BtnInstallHwnd = GetDlgItem(hWnd, IDC_BUTTON4);
152 g_BtnUpdateHwnd = GetDlgItem(hWnd, IDC_BUTTON3);
153 g_ProgressBarHwnd = GetDlgItem(hWnd, IDC_PROGRESS1);
154 g_StaticStatusHwnd = GetDlgItem(hWnd, IDC_STATIC_STATUS);
155
156 hIcon = LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_ICON1));
157 SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
158 SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
159
160 SendMessage(g_ProgressBarHwnd, PBM_SETRANGE, (WPARAM)0, (LPARAM)(MAKELPARAM(0, PT_FINISH)));
161 PROGRESS_BAR_SET_POS(PT_START);
162
163 LanguageInit();
164
165 // Fill device combox
166 hCombox = GetDlgItem(hWnd, IDC_COMBO1);
167 for (i = 0; i < g_PhyDriveCount; i++)
168 {
169 if (g_PhyDriveList[i].Id < 0)
170 {
171 continue;
172 }
173
174 if (g_PhyDriveList[i].FirstDriveLetter >= 0)
175 {
176 safe_sprintf(Letter, "%C: ", g_PhyDriveList[i].FirstDriveLetter);
177 }
178 else
179 {
180 Letter[0] = 0;
181 }
182
183 safe_sprintf(DeviceName, "%s[%dGB] %s %s",
184 Letter,
185 GetHumanReadableGBSize(g_PhyDriveList[i].SizeInBytes),
186 g_PhyDriveList[i].VendorId,
187 g_PhyDriveList[i].ProductId
188 );
189 SendMessageA(hCombox, CB_ADDSTRING, 0, (LPARAM)DeviceName);
190 }
191
192 SendMessage(hCombox, CB_SETCURSEL, 0, 0);
193
194 // Set static text & font
195 hStaticFont = CreateFont(26, 0, 0, 0, FW_BOLD, FALSE, FALSE, 0,
196 ANSI_CHARSET, OUT_DEFAULT_PRECIS,
197 CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
198 DEFAULT_PITCH&FF_SWISS, TEXT("Courier New"));
199
200 SendMessage(g_StaticLocalVerHwnd, WM_SETFONT, (WPARAM)hStaticFont, TRUE);
201 SendMessage(g_StaticDiskVerHwnd, WM_SETFONT, (WPARAM)hStaticFont, TRUE);
202
203 OnComboxSelChange(g_ComboxHwnd);
204
205 SetFocus(g_ProgressBarHwnd);
206
207 return TRUE;
208 }
209
210 static DWORD WINAPI InstallVentoyThread(void* Param)
211 {
212 int rc;
213 PHY_DRIVE_INFO *pPhyDrive = (PHY_DRIVE_INFO *)Param;
214
215 rc = InstallVentoy2PhyDrive(pPhyDrive);
216 if (rc)
217 {
218 Log("This time install failed, now wait and retry...");
219 Sleep(10000);
220
221 Log("Now retry to install...");
222
223 rc = InstallVentoy2PhyDrive(pPhyDrive);
224 }
225
226 if (rc == 0)
227 {
228 PROGRESS_BAR_SET_POS(PT_FINISH);
229 MessageBox(g_DialogHwnd, _G(STR_INSTALL_SUCCESS), _G(STR_INFO), MB_OK | MB_ICONINFORMATION);
230 safe_strcpy(pPhyDrive->VentoyVersion, GetLocalVentoyVersion());
231 }
232 else
233 {
234 PROGRESS_BAR_SET_POS(PT_FINISH);
235 MessageBox(g_DialogHwnd, _G(STR_INSTALL_FAILED), _G(STR_ERROR), MB_OK | MB_ICONERROR);
236 }
237
238 PROGRESS_BAR_SET_POS(PT_START);
239 g_ThreadHandle = NULL;
240 SetWindowText(g_StaticStatusHwnd, _G(STR_STATUS));
241 OnComboxSelChange(g_ComboxHwnd);
242
243 return 0;
244 }
245
246 static DWORD WINAPI UpdateVentoyThread(void* Param)
247 {
248 int rc;
249 PHY_DRIVE_INFO *pPhyDrive = (PHY_DRIVE_INFO *)Param;
250
251 rc = UpdateVentoy2PhyDrive(pPhyDrive);
252 if (rc)
253 {
254 Log("This time update failed, now wait and retry...");
255 Sleep(10000);
256
257 Log("Now retry to update...");
258
259 rc = UpdateVentoy2PhyDrive(pPhyDrive);
260 }
261
262 if (rc == 0)
263 {
264 PROGRESS_BAR_SET_POS(PT_FINISH);
265 MessageBox(g_DialogHwnd, _G(STR_UPDATE_SUCCESS), _G(STR_INFO), MB_OK | MB_ICONINFORMATION);
266 safe_strcpy(pPhyDrive->VentoyVersion, GetLocalVentoyVersion());
267 }
268 else
269 {
270 PROGRESS_BAR_SET_POS(PT_FINISH);
271 MessageBox(g_DialogHwnd, _G(STR_UPDATE_FAILED), _G(STR_ERROR), MB_OK | MB_ICONERROR);
272 }
273
274 PROGRESS_BAR_SET_POS(PT_START);
275 g_ThreadHandle = NULL;
276 SetWindowText(g_StaticStatusHwnd, _G(STR_STATUS));
277 OnComboxSelChange(g_ComboxHwnd);
278
279 return 0;
280 }
281
282
283
284 static void OnInstallBtnClick(void)
285 {
286 int nCurSel;
287 PHY_DRIVE_INFO *pPhyDrive = NULL;
288
289 if (MessageBox(g_DialogHwnd, _G(STR_INSTALL_TIP), _G(STR_WARNING), MB_YESNO | MB_ICONWARNING) != IDYES)
290 {
291 return;
292 }
293
294 if (MessageBox(g_DialogHwnd, _G(STR_INSTALL_TIP2), _G(STR_WARNING), MB_YESNO | MB_ICONWARNING) != IDYES)
295 {
296 return;
297 }
298
299 if (g_ThreadHandle)
300 {
301 Log("Another thread is runing");
302 return;
303 }
304
305 nCurSel = SendMessage(g_ComboxHwnd, CB_GETCURSEL, 0, 0);
306 if (CB_ERR == nCurSel)
307 {
308 Log("Failed to get combox sel");
309 return;;
310 }
311
312 pPhyDrive = GetPhyDriveInfoById(nCurSel);
313 if (!pPhyDrive)
314 {
315 return;
316 }
317
318 EnableWindow(g_BtnInstallHwnd, FALSE);
319 EnableWindow(g_BtnUpdateHwnd, FALSE);
320
321 g_ThreadHandle = CreateThread(NULL, 0, InstallVentoyThread, (LPVOID)pPhyDrive, 0, NULL);
322 }
323
324
325
326 static void OnUpdateBtnClick(void)
327 {
328 int nCurSel;
329 PHY_DRIVE_INFO *pPhyDrive = NULL;
330
331 if (MessageBox(g_DialogHwnd, _G(STR_UPDATE_TIP), _G(STR_INFO), MB_YESNO | MB_ICONQUESTION) != IDYES)
332 {
333 return;
334 }
335
336 if (g_ThreadHandle)
337 {
338 Log("Another thread is runing");
339 return;
340 }
341
342 nCurSel = SendMessage(g_ComboxHwnd, CB_GETCURSEL, 0, 0);
343 if (CB_ERR == nCurSel)
344 {
345 Log("Failed to get combox sel");
346 return;;
347 }
348
349 pPhyDrive = GetPhyDriveInfoById(nCurSel);
350 if (!pPhyDrive)
351 {
352 return;
353 }
354
355 EnableWindow(g_BtnInstallHwnd, FALSE);
356 EnableWindow(g_BtnUpdateHwnd, FALSE);
357
358 g_ThreadHandle = CreateThread(NULL, 0, UpdateVentoyThread, (LPVOID)pPhyDrive, 0, NULL);
359 }
360
361
362 INT_PTR CALLBACK DialogProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
363 {
364 WORD NotifyCode;
365 WORD CtrlID;
366
367 switch (Message)
368 {
369 case WM_COMMAND:
370 {
371 NotifyCode = HIWORD(wParam);
372 CtrlID = LOWORD(wParam);
373
374 if (CtrlID == IDC_COMBO1 && NotifyCode == CBN_SELCHANGE)
375 {
376 OnComboxSelChange((HWND)lParam);
377 }
378
379 if (CtrlID == IDC_BUTTON4 && NotifyCode == BN_CLICKED)
380 {
381 OnInstallBtnClick();
382 }
383 else if (CtrlID == IDC_BUTTON3 && NotifyCode == BN_CLICKED)
384 {
385 OnUpdateBtnClick();
386 }
387
388
389 break;
390 }
391 case WM_INITDIALOG:
392 {
393 InitDialog(hWnd, wParam, lParam);
394 break;
395 }
396 case WM_CTLCOLORSTATIC:
397 {
398 if (GetDlgItem(hWnd, IDC_STATIC_LOCAL_VER) == (HANDLE)lParam ||
399 GetDlgItem(hWnd, IDC_STATIC_DISK_VER) == (HANDLE)lParam)
400 {
401 SetBkMode((HDC)wParam, TRANSPARENT);
402 SetTextColor((HDC)wParam, RGB(255, 0, 0));
403 return (LRESULT)(HBRUSH)(GetStockObject(HOLLOW_BRUSH));
404 }
405 else
406 {
407 break;
408 }
409 }
410 case WM_CLOSE:
411 {
412 if (g_ThreadHandle)
413 {
414 MessageBox(g_DialogHwnd, _G(STR_WAIT_PROCESS), _G(STR_INFO), MB_OK | MB_ICONINFORMATION);
415 }
416 else
417 {
418 EndDialog(hWnd, 0);
419 }
420 break;
421 }
422 }
423
424 return 0;
425 }
426
427 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
428 {
429 UNREFERENCED_PARAMETER(hPrevInstance);
430
431 if (!IsFileExist(VENTOY_FILE_VERSION))
432 {
433 MessageBox(NULL, _G(STR_INCORRECT_DIR), _G(STR_ERROR), MB_OK | MB_ICONERROR);
434 return ERROR_NOT_FOUND;
435 }
436
437 GetExeVersionInfo(__argv[0]);
438
439 Log("\n################################ Ventoy2Disk %s ################################", g_CurVersion);
440
441 ParseCmdLineOption(lpCmdLine);
442
443 DumpWindowsVersion();
444
445 Ventoy2DiskInit();
446
447 g_hInst = hInstance;
448 DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DialogProc);
449
450 Ventoy2DiskDestroy();
451
452 return 0;
453 }