ReactOS 0.4.16-dev-109-gf4cb10f
MainDialog.cpp
Go to the documentation of this file.
1#include "Unfrag.h"
2#include "MainDialog.h"
3#include "resource.h"
4#include "Fraginator.h"
5#include "Defragment.h"
6#include "ReportDialog.h"
7
8
15
16
22
23
24static void InitDialog (HWND Dlg);
25void UpdateDefragInfo (HWND Dlg);
26void UpdatePriority (HWND Dlg);
27wstring GetDefaultTitle (void);
28wstring GetDefragTitle (void);
29void SetDisables (HWND Dlg);
31
32
33static void InitDialog (HWND Dlg)
34{
35 // Make internal list
36 DWORD DriveMask;
37 HWND DlgItem;
38 size_t d;
39
40 // Clear out wisecracks line for now
41 SetDlgItemText (Dlg, IDC_WISECRACKS, L"\"Defrag, baby!\"");
42
43 // Make list of logical drives
45 DriveMask = GetLogicalDrives ();
46
47 for (d = 0; d < 26; d++)
48 {
49 if (DriveMask & (1 << d))
50 {
51 wstring Name;
52
53 Name = (wchar_t)(L'A' + d);
54 Name += L':';
56 }
57 }
58
59 // Give list to dropdown list
60 DlgItem = GetDlgItem (Dlg, IDC_DRIVES_LIST);
61 SendMessage (DlgItem, CB_RESETCONTENT, 0, 0);
62 for (d = 0; d < DrivesList.size(); d++)
63 {
64 SendMessage (DlgItem, CB_ADDSTRING, 0, (LPARAM) DrivesList[d].c_str());
65 }
66
67 // Put in defrag methods
68 DlgItem = GetDlgItem (Dlg, IDC_METHODS_LIST);
69 SendMessage (DlgItem, CB_RESETCONTENT, 0, 0);
70 AnalyzeID = SendMessage (DlgItem, CB_ADDSTRING, 0, (LPARAM) L"Analyze Only");
71 FastID = SendMessage (DlgItem, CB_ADDSTRING, 0, (LPARAM) L"Fast Defrag");
72 ExtensiveID = SendMessage (DlgItem, CB_ADDSTRING, 0, (LPARAM) L"Extensive Defrag");
73
74 // Set up process priorities
75 DlgItem = GetDlgItem (Dlg, IDC_PRIORITY_LIST);
76 SendMessage (Dlg, CB_RESETCONTENT, 0, 0);
77 PriHighID = SendMessage (DlgItem, CB_ADDSTRING, 0, (LPARAM) L"High");
78 PriAboveNormID = SendMessage (DlgItem, CB_ADDSTRING, 0, (LPARAM) L"Above Normal");
79 PriNormalID = SendMessage (DlgItem, CB_ADDSTRING, 0, (LPARAM) L"Normal");
80 PriBelowNormID = SendMessage (DlgItem, CB_ADDSTRING, 0, (LPARAM) L"Below Normal");
81 PriIdleID = SendMessage (DlgItem, CB_ADDSTRING, 0, (LPARAM) L"Idle");
82 UpdatePriority (Dlg);
83
84 // Reset texts and progress meters
89
90 return;
91}
92
93
95{
96 wchar_t PercentText[100];
97 static double OldPercent = 200.0f;
98 static wstring OldStatus = L"Non";
99 wstring NewStatus;
100 double NewPercent;
101
102 if (Defrag == NULL)
103 return;
104
105 NewPercent = Defrag->GetStatusPercent ();
106 if (NewPercent > 100.0f)
107 NewPercent = 100.0f;
108 if (NewPercent < 0.0f)
109 NewPercent = 0.0f;
110 if (NewPercent != OldPercent)
111 {
112 swprintf (PercentText, L"%6.2f%%", NewPercent);
113 SendDlgItemMessage (Dlg, IDC_PERCENT, WM_SETTEXT, 0, (LPARAM) PercentText);
115 (WPARAM) (int)(NewPercent * 100.0f), 0);
116 OldPercent = NewPercent;
117 }
118
119 NewStatus = Defrag->GetStatusString ();
120 if (NewStatus != OldStatus)
121 { // Change & characters to && to avoid underlining
122 wstring Status;
123 wstring::iterator it;
124
125 Status = NewStatus;
126 it = Status.begin ();
127 while (it < Status.end())
128 {
129 if (*it == L'&')
130 {
131 Status.insert (it, 1, L'&');
132 it++;
133 }
134
135 it++;
136 }
137
139 (LPARAM) Status.c_str());
140
141 OldStatus = NewStatus;
142 }
143
144 return;
145}
146
147
148wstring GetDefaultTitle (void)
149{
150 wstring DefaultText;
151
152 DefaultText = wstring(wstring(APPNAME_GUI) + wstring(L" v") + wstring(APPVER_STR) +
153 wstring(L" (C) 2000 by Rick Brewster"));
154
155 return (DefaultText);
156}
157
158
159wstring GetDefragTitle (void)
160{
161 wstring DefragText;
162 wchar_t Percent[10];
163
164 swprintf (Percent, L"%.2f%%", Defrag->GetStatusPercent());
165
166 DefragText = GetDefaultTitle ();
167 if (Defrag != NULL)
168 {
169 DefragText = wstring(Percent) + wstring (L" - ") + Defrag->GetVolume().GetRootPath() +
170 wstring (L" - ") + DefragText;
171 }
172
173 return (DefragText);
174}
175
176
178{
179 // If a defrag is in process, set L'Start' button to say L'Stop' and disable
180 // the Select Drive and Select Action controls
181 if (Defrag != NULL && !Defrag->IsDoneYet() && !Defrag->HasError())
182 {
186 }
187 else
188 {
189 SendMessage (GetDlgItem (Dlg, IDC_STARTSTOP), WM_SETTEXT, 0, (LPARAM) L"Start");
194 }
195
196 return;
197}
198
199
201{
202 LRESULT Id;
204
206
207 if (Id == PriHighID)
209 else
210 if (Id == PriAboveNormID)
212 else
213 if (Id == PriNormalID)
215 else
216 if (Id == PriBelowNormID)
218 else
219 if (Id == PriIdleID)
221 else
222 return;
223
225 return;
226}
227
228
229// Save settings (ie, process priority and defrag type options)
230bool GetRegKeys (HKEY *RegKeyResult)
231{
232 HKEY RegKey;
233 LONG Error;
234
236 (
238 L"Software\\Fraginator",
239 0,
240 NULL,
243 NULL,
244 &RegKey,
245 NULL
246 );
247
248 if (Error != ERROR_SUCCESS)
249 return (false);
250
251 *RegKeyResult = RegKey;
252 return (true);
253}
254
255
256bool DoneRegKey (HKEY RegKey)
257{
258 RegCloseKey (RegKey);
259 return (true);
260}
261
262
264{
265 LRESULT DefragID;
266 DWORD DefragVal;
267 LRESULT PriID;
268 DWORD PriVal;
269 HKEY RegKey;
270
271 DefragID = SendDlgItemMessage (Dlg, IDC_METHODS_LIST, CB_GETCURSEL, 0, 0);
273
274 // Action
275 if (DefragID == AnalyzeID)
276 DefragVal = (DWORD) DefragAnalyze;
277 else
278 if (DefragID == FastID)
279 DefragVal = (DWORD) DefragFast;
280 else
281 if (DefragID == ExtensiveID)
282 DefragVal = (DWORD) DefragExtensive;
283
284 // Process Priority
285 if (PriID == PriHighID)
286 PriVal = HIGH_PRIORITY_CLASS;
287 else
288 if (PriID == PriAboveNormID)
290 else
291 if (PriID == PriNormalID)
292 PriVal = NORMAL_PRIORITY_CLASS;
293 else
294 if (PriID == PriBelowNormID)
296 else
297 if (PriID == PriIdleID)
298 PriVal = IDLE_PRIORITY_CLASS;
299
300 if (!GetRegKeys (&RegKey))
301 return;
302
304 (
305 RegKey,
306 L"Default Action",
307 0,
308 REG_DWORD,
309 (CONST BYTE *)&DefragVal,
310 sizeof (DefragVal)
311 );
312
314 (
315 RegKey,
316 L"Process Priority",
317 0,
318 REG_DWORD,
319 (CONST BYTE *)&PriVal,
320 sizeof (PriVal)
321 );
322
323 DoneRegKey (RegKey);
324 return;
325}
326
327
329{
330 DefragType DType;
331 DWORD DTypeVal;
332 LRESULT DefragID;
333 DWORD PriVal;
334 LRESULT PriID;
335 HKEY RegKey;
336 DWORD RegType;
337 DWORD RegSize;
338 LONG Error;
339
340 if (!GetRegKeys (&RegKey))
341 return;
342
343 RegSize = sizeof (DTypeVal);
344 RegType = REG_DWORD;
345
347 (
348 RegKey,
349 L"Default Action",
350 0,
351 &RegType,
352 (BYTE *)&DTypeVal,
353 &RegSize
354 );
355
356 if (Error != ERROR_SUCCESS)
357 DTypeVal = DefragAnalyze;
358
360 (
361 RegKey,
362 L"Process Priority",
363 0,
364 &RegType,
365 (BYTE *)&PriVal,
366 &RegSize
367 );
368
369 DoneRegKey (RegKey);
370
371 if (Error != ERROR_SUCCESS)
372 PriVal = NORMAL_PRIORITY_CLASS;
373
374 DType = (DefragType) DTypeVal;
375 switch (DType)
376 {
377 default:
378 case DefragAnalyze:
379 DefragID = AnalyzeID;
380 break;
381
382 case DefragFast:
383 DefragID = FastID;
384 break;
385
386 case DefragExtensive:
387 DefragID = ExtensiveID;
388 break;
389 }
390
391 switch (PriVal)
392 {
394 PriID = PriHighID;
395 break;
396
398 PriID = PriAboveNormID;
399 break;
400
401 default:
403 PriID = PriNormalID;
404 break;
405
407 PriID = PriBelowNormID;
408 break;
409
411 PriID = PriIdleID;
412 break;
413 }
414
417 return;
418}
419
420
421#define IDLETIME 25
422wstring OldWindowText = L"";
423
425{
426 static bool ReEntrance = false;
427
428 switch (Msg)
429 {
430 case WM_INITDIALOG:
431 Stopping = false;
432 SetWindowText (Dlg, GetDefaultTitle().c_str());
433 SetDisables (Dlg);
434 InitDialog (Dlg);
435 SetTimer (Dlg, 1, IDLETIME, NULL);
437 QuitWhenDone = false;
438 LoadSettings (Dlg);
439 UpdatePriority (Dlg);
440 return (1);
441
442
443 case WM_TIMER:
444 if (Defrag != NULL && !ReEntrance)
445 {
446 wstring NewTitle;
447
448 SendMessage (Dlg, WM_UPDATEINFO, 0, 0);
449
450 NewTitle = GetDefragTitle ();
451 if (NewTitle != OldWindowText)
452 {
453 OldWindowText = NewTitle;
454 SetWindowText (Dlg, NewTitle.c_str());
455 }
456
457 if (Defrag->IsDoneYet() || Defrag->HasError())
458 { // This is the code executed when defragging is finished (or stopped :)
460 !Defrag->HasError() &&
461 !Stopping)
462 { // Show report
463 ReEntrance = true;
464
467
468 ReEntrance = false;
469 }
470
471 delete Defrag;
472 Defrag = NULL;
473 SetDisables (Dlg);
474 SetWindowText (Dlg, GetDefaultTitle().c_str());
475
476 Stopping = false;
477
478 if (QuitWhenDone)
479 SendMessage (GetDlgItem (Dlg, IDC_QUIT), BM_CLICK, 0, 0);
480 }
481 }
482
483 SetTimer (Dlg, 1, IDLETIME, NULL);
484 return (0);
485
486
487 case WM_UPDATEINFO:
488 UpdateDefragInfo (Dlg);
489 return (1);
490
491
492 case WM_CLOSE:
493 SendMessage (GetDlgItem (Dlg, IDC_QUIT), BM_CLICK, 0, 0);
494 return (1);
495
496
497 case WM_COMMAND:
498 switch (LOWORD(WParam))
499 {
501 UpdatePriority (Dlg);
502 return (1);
503
504
505 case ID_MAIN_HELP:
506 ShellExecute (Dlg, L"open", L"Fraginator.chm", L"", L".", SW_SHOW);
507 return (1);
508
509
510 case IDC_QUIT:
511 if (Defrag == NULL)
512 { // This is the code executing when quitting
513 SaveSettings (Dlg);
514 EndDialog (Dlg, 0);
515 }
516 else
517 { // Tell defragging to finish and disable our button
518 QuitWhenDone = true;
521 }
522 return (1);
523
524
525 case IDC_STARTSTOP:
526 if (Defrag == NULL)
527 { // L"Start"
528 wchar_t Drive[10];
529 LRESULT ID;
530 DefragType Method;
531 HANDLE H;
532
533 if (Defrag != NULL)
534 return (1);
535
537 sizeof (Drive) - 1, (LPARAM) Drive);
538
539 if (wcslen(Drive) != 2 || Drive[1] != L':')
540 return (1);
541
543 Method = DefragInvalid;
544 if (ID == AnalyzeID)
545 Method = DefragAnalyze;
546 else
547 if (ID == FastID)
548 Method = DefragFast;
549 else
550 if (ID == ExtensiveID)
551 Method = DefragExtensive;
552
553 if (Method != DefragInvalid)
554 {
555 Defrag = StartDefragThread (Drive, Method, H);
556 Defrag->SetDoLimitLength (false);
557 SetWindowText (Dlg, GetDefragTitle().c_str());
558 SetDisables (Dlg);
559 }
560 }
561 else
562 { // L"Stop"
563 Stopping = true;
564 Defrag->Stop ();
567 }
568 return (1);
569 }
570 }
571
572 // Otherwise, return 0 to say we did not process the message.
573 return (0);
574}
DWORD Id
Defragment * Defrag
Definition: Fraginator.cpp:20
HINSTANCE GlobalHInstance
Definition: Fraginator.cpp:19
bool DoneRegKey(HKEY RegKey)
Definition: MainDialog.cpp:256
static void InitDialog(HWND Dlg)
Definition: MainDialog.cpp:33
void SetDisables(HWND Dlg)
Definition: MainDialog.cpp:177
bool GetRegKeys(HKEY *RegKeyResult)
Definition: MainDialog.cpp:230
wstring GetDefragTitle(void)
Definition: MainDialog.cpp:159
LRESULT PriNormalID
Definition: MainDialog.cpp:19
LRESULT PriAboveNormID
Definition: MainDialog.cpp:18
vector< wstring > DrivesList
Definition: MainDialog.cpp:9
bool QuitWhenDone
Definition: MainDialog.cpp:13
LRESULT PriBelowNormID
Definition: MainDialog.cpp:20
INT_PTR CALLBACK MainDialogProc(HWND Dlg, UINT Msg, WPARAM WParam, LPARAM LParam)
Definition: MainDialog.cpp:424
LRESULT FastID
Definition: MainDialog.cpp:11
#define IDLETIME
Definition: MainDialog.cpp:421
wstring GetDefaultTitle(void)
Definition: MainDialog.cpp:148
bool Stopping
Definition: MainDialog.cpp:14
LRESULT ExtensiveID
Definition: MainDialog.cpp:12
LRESULT AnalyzeID
Definition: MainDialog.cpp:10
LRESULT PriIdleID
Definition: MainDialog.cpp:21
void UpdatePriority(HWND Dlg)
Definition: MainDialog.cpp:200
void UpdateDefragInfo(HWND Dlg)
Definition: MainDialog.cpp:94
wstring OldWindowText
Definition: MainDialog.cpp:422
LRESULT PriHighID
Definition: MainDialog.cpp:17
#define WM_UPDATEINFO
Definition: MainDialog.h:8
INT_PTR CALLBACK ReportDialogProc(HWND Dlg, UINT Msg, WPARAM WParam, LPARAM LParam)
Defragment * StartDefragThread(wstring Drive, DefragType Method, HANDLE &Handle)
Definition: Unfrag.cpp:132
#define APPNAME_GUI
Definition: Unfrag.h:39
DefragType
Definition: Unfrag.h:67
@ DefragExtensive
Definition: Unfrag.h:70
@ DefragAnalyze
Definition: Unfrag.h:71
@ DefragInvalid
Definition: Unfrag.h:68
@ DefragFast
Definition: Unfrag.h:69
#define APPVER_STR
Definition: Unfrag.h:40
struct NameRec_ * Name
Definition: cdprocs.h:460
void SaveSettings(void)
Definition: settings.c:115
void LoadSettings(void)
Definition: settings.c:53
#define IDI_ICON
Definition: resource.h:5
BOOL Error
Definition: chkdsk.c:66
PWCHAR Drive
Definition: chkdsk.c:73
#define RegCloseKey(hKey)
Definition: registry.h:49
DriveVolume & GetVolume(void)
Definition: Defragment.h:59
bool IsDoneYet(void)
Definition: Defragment.cpp:151
wstring GetStatusString(void)
Definition: Defragment.cpp:133
void Stop(void)
Definition: Defragment.cpp:443
void SetDoLimitLength(bool L)
Definition: Defragment.h:67
DefragType GetDefragType(void)
Definition: Defragment.h:57
bool HasError(void)
Definition: Defragment.cpp:454
double GetStatusPercent(void)
Definition: Defragment.cpp:145
wstring GetRootPath(void)
Definition: DriveVolume.h:111
struct @1636 Msg[]
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define IDC_STATUS
Definition: resource.h:41
#define IDC_PROGRESS
Definition: resource.h:21
#define GetCurrentProcess()
Definition: compat.h:759
#define CALLBACK
Definition: compat.h:35
BOOL WINAPI SetPriorityClass(IN HANDLE hProcess, IN DWORD dwPriorityClass)
Definition: proc.c:1698
#define swprintf
Definition: precomp.h:40
unsigned long DWORD
Definition: ntddk_ex.h:95
Status
Definition: gdiplustypes.h:25
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define d
Definition: ke_i.h:81
#define IDC_DRIVES_LIST
Definition: resource.h:13
#define IDC_PERCENT
Definition: resource.h:15
#define IDD_REPORT
Definition: resource.h:7
#define IDC_PRIORITY_LIST
Definition: resource.h:40
#define IDC_QUIT
Definition: resource.h:10
#define ID_MAIN_HELP
Definition: resource.h:39
#define IDC_METHODS_LIST
Definition: resource.h:12
#define IDC_STARTSTOP
Definition: resource.h:11
#define IDC_WISECRACKS
Definition: resource.h:23
#define H
unsigned int UINT
Definition: ndis.h:50
#define KEY_ALL_ACCESS
Definition: nt_native.h:1041
#define REG_OPTION_NON_VOLATILE
Definition: nt_native.h:1057
#define DWORD
Definition: nt_native.h:44
#define L(x)
Definition: ntvdm.h:50
#define LOWORD(l)
Definition: pedump.c:82
#define CONST
Definition: pedump.c:81
long LONG
Definition: pedump.c:60
#define PBM_SETPOS
Definition: commctrl.h:2189
#define PBM_SETRANGE
Definition: commctrl.h:2188
#define ID
Definition: ruserpass.c:36
#define REG_DWORD
Definition: sdbapi.c:596
#define ShellExecute
Definition: shellapi.h:693
void push_back(const _Tp &__x=_STLP_DEFAULT_CONSTRUCTED(_Tp))
Definition: _vector.h:379
void resize(size_type __new_size, const _Tp &__x=_STLP_DEFAULT_CONSTRUCTED(_Tp))
Definition: _vector.h:639
size_type size() const
Definition: _vector.h:192
#define wchar_t
Definition: wchar.h:102
int32_t INT_PTR
Definition: typedefs.h:64
_In_ WDFINTERRUPT _In_ WDF_INTERRUPT_POLICY _In_ WDF_INTERRUPT_PRIORITY Priority
Definition: wdfinterrupt.h:655
#define NORMAL_PRIORITY_CLASS
Definition: winbase.h:181
#define BELOW_NORMAL_PRIORITY_CLASS
Definition: winbase.h:190
#define HIGH_PRIORITY_CLASS
Definition: winbase.h:183
DWORD WINAPI GetLogicalDrives(void)
Definition: disk.c:110
#define IDLE_PRIORITY_CLASS
Definition: winbase.h:182
#define ABOVE_NORMAL_PRIORITY_CLASS
Definition: winbase.h:191
LONG_PTR LPARAM
Definition: windef.h:208
LONG_PTR LRESULT
Definition: windef.h:209
UINT_PTR WPARAM
Definition: windef.h:207
#define HKEY_CURRENT_USER
Definition: winreg.h:11
#define RegSetValueEx
Definition: winreg.h:533
#define RegCreateKeyEx
Definition: winreg.h:501
#define RegQueryValueEx
Definition: winreg.h:524
#define WM_CLOSE
Definition: winuser.h:1624
#define MAKELPARAM(l, h)
Definition: winuser.h:4011
#define WM_COMMAND
Definition: winuser.h:1743
#define CB_SETCURSEL
Definition: winuser.h:1964
#define WM_GETTEXT
Definition: winuser.h:1621
#define DialogBoxParam
Definition: winuser.h:5776
#define CB_RESETCONTENT
Definition: winuser.h:1962
#define WM_INITDIALOG
Definition: winuser.h:1742
HWND WINAPI GetDlgItem(_In_opt_ HWND, _In_ int)
UINT_PTR WINAPI SetTimer(_In_opt_ HWND, _In_ UINT_PTR, _In_ UINT, _In_opt_ TIMERPROC)
#define WM_SETTEXT
Definition: winuser.h:1620
#define WM_TIMER
Definition: winuser.h:1745
#define BM_CLICK
Definition: winuser.h:1920
#define CB_ADDSTRING
Definition: winuser.h:1939
#define LoadIcon
Definition: winuser.h:5825
#define SendMessage
Definition: winuser.h:5855
BOOL WINAPI EnableWindow(_In_ HWND, _In_ BOOL)
#define SW_SHOW
Definition: winuser.h:778
#define SetWindowText
Definition: winuser.h:5869
#define CB_GETCURSEL
Definition: winuser.h:1946
#define GCL_HICON
Definition: winuser.h:666
#define SendDlgItemMessage
Definition: winuser.h:5854
#define MAKEINTRESOURCE
Definition: winuser.h:591
#define SetClassLong
Definition: winuser.h:5859
#define SetDlgItemText
Definition: winuser.h:5861
BOOL WINAPI EndDialog(_In_ HWND, _In_ INT_PTR)
unsigned char BYTE
Definition: xxhash.c:193