2 #include "MainDialog.h"
4 #include "Fraginator.h"
5 #include "Defragment.h"
6 #include "ReportDialog.h"
9 vector
<wstring
> DrivesList
;
18 LRESULT PriAboveNormID
;
20 LRESULT PriBelowNormID
;
24 static void InitDialog (HWND Dlg
);
25 void UpdateDefragInfo (HWND Dlg
);
26 void UpdatePriority (HWND Dlg
);
27 wstring
GetDefaultTitle (void);
28 wstring
GetDefragTitle (void);
29 void SetDisables (HWND Dlg
);
30 INT_PTR CALLBACK
MainDialogProc (HWND Dlg
, UINT Msg
, WPARAM WParam
, LPARAM LParam
);
33 static void InitDialog (HWND Dlg
)
40 // Clear out wisecracks line for now
41 SetDlgItemText (Dlg
, IDC_WISECRACKS
, L
"\"Defrag, baby!\"");
43 // Make list of logical drives
44 DrivesList
.resize (0);
45 DriveMask
= GetLogicalDrives ();
47 for (d
= 0; d
< 26; d
++)
49 if (DriveMask
& (1 << d
))
53 Name
= (wchar_t)(L
'A' + d
);
55 DrivesList
.push_back (Name
);
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
++)
64 SendMessage (DlgItem
, CB_ADDSTRING
, 0, (LPARAM
) DrivesList
[d
].c_str());
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");
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");
84 // Reset texts and progress meters
85 SendDlgItemMessage (Dlg
, IDC_STATUS
, WM_SETTEXT
, 0, (LPARAM
) L
"");
86 SendDlgItemMessage (Dlg
, IDC_PERCENT
, WM_SETTEXT
, 0, (LPARAM
) L
"");
87 SendDlgItemMessage (Dlg
, IDC_PROGRESS
, PBM_SETRANGE
, 0, MAKELPARAM (0, 10000));
88 SendDlgItemMessage (Dlg
, IDC_PROGRESS
, PBM_SETPOS
, 0, 0);
94 void UpdateDefragInfo (HWND Dlg
)
96 wchar_t PercentText
[100];
97 static double OldPercent
= 200.0f
;
98 static wstring OldStatus
= L
"Non";
105 NewPercent
= Defrag
->GetStatusPercent ();
106 if (NewPercent
> 100.0f
)
108 if (NewPercent
< 0.0f
)
110 if (NewPercent
!= OldPercent
)
112 swprintf (PercentText
, L
"%6.2f%%", NewPercent
);
113 SendDlgItemMessage (Dlg
, IDC_PERCENT
, WM_SETTEXT
, 0, (LPARAM
) PercentText
);
114 SendDlgItemMessage (Dlg
, IDC_PROGRESS
, PBM_SETPOS
,
115 (WPARAM
) (int)(NewPercent
* 100.0f
), 0);
116 OldPercent
= NewPercent
;
119 NewStatus
= Defrag
->GetStatusString ();
120 if (NewStatus
!= OldStatus
)
121 { // Change & characters to && to avoid underlining
123 wstring::iterator it
;
126 it
= Status
.begin ();
127 while (it
< Status
.end())
131 Status
.insert (it
, 1, L
'&');
138 SendDlgItemMessage (Dlg
, IDC_STATUS
, WM_SETTEXT
, 0,
139 (LPARAM
) Status
.c_str());
141 OldStatus
= NewStatus
;
148 wstring
GetDefaultTitle (void)
152 DefaultText
= wstring(wstring(APPNAME_GUI
) + wstring(L
" v") + wstring(APPVER_STR
) +
153 wstring(L
" (C) 2000 by Rick Brewster"));
155 return (DefaultText
);
159 wstring
GetDefragTitle (void)
164 swprintf (Percent
, L
"%.2f%%", Defrag
->GetStatusPercent());
166 DefragText
= GetDefaultTitle ();
169 DefragText
= wstring(Percent
) + wstring (L
" - ") + Defrag
->GetVolume().GetRootPath() +
170 wstring (L
" - ") + DefragText
;
177 void SetDisables (HWND Dlg
)
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())
183 SendMessage (GetDlgItem (Dlg
, IDC_STARTSTOP
), WM_SETTEXT
, 0, (LPARAM
) L
"Stop");
184 EnableWindow (GetDlgItem (Dlg
, IDC_DRIVES_LIST
), FALSE
);
185 EnableWindow (GetDlgItem (Dlg
, IDC_METHODS_LIST
), FALSE
);
189 SendMessage (GetDlgItem (Dlg
, IDC_STARTSTOP
), WM_SETTEXT
, 0, (LPARAM
) L
"Start");
190 EnableWindow (GetDlgItem (Dlg
, IDC_STARTSTOP
), TRUE
);
191 EnableWindow (GetDlgItem (Dlg
, IDC_QUIT
), TRUE
);
192 EnableWindow (GetDlgItem (Dlg
, IDC_DRIVES_LIST
), TRUE
);
193 EnableWindow (GetDlgItem (Dlg
, IDC_METHODS_LIST
), TRUE
);
200 void UpdatePriority (HWND Dlg
)
205 Id
= SendDlgItemMessage (Dlg
, IDC_PRIORITY_LIST
, CB_GETCURSEL
, 0, 0);
208 Priority
= HIGH_PRIORITY_CLASS
;
210 if (Id
== PriAboveNormID
)
211 Priority
= ABOVE_NORMAL_PRIORITY_CLASS
;
213 if (Id
== PriNormalID
)
214 Priority
= NORMAL_PRIORITY_CLASS
;
216 if (Id
== PriBelowNormID
)
217 Priority
= BELOW_NORMAL_PRIORITY_CLASS
;
220 Priority
= IDLE_PRIORITY_CLASS
;
224 SetPriorityClass (GetCurrentProcess(), Priority
);
229 // Save settings (ie, process priority and defrag type options)
230 bool GetRegKeys (HKEY
*RegKeyResult
)
235 Error
= RegCreateKeyEx
238 L
"Software\\Fraginator",
241 REG_OPTION_NON_VOLATILE
,
248 if (Error
!= ERROR_SUCCESS
)
251 *RegKeyResult
= RegKey
;
256 bool DoneRegKey (HKEY RegKey
)
258 RegCloseKey (RegKey
);
263 void SaveSettings (HWND Dlg
)
271 DefragID
= SendDlgItemMessage (Dlg
, IDC_METHODS_LIST
, CB_GETCURSEL
, 0, 0);
272 PriID
= SendDlgItemMessage (Dlg
, IDC_PRIORITY_LIST
, CB_GETCURSEL
, 0, 0);
275 if (DefragID
== AnalyzeID
)
276 DefragVal
= (DWORD
) DefragAnalyze
;
278 if (DefragID
== FastID
)
279 DefragVal
= (DWORD
) DefragFast
;
281 if (DefragID
== ExtensiveID
)
282 DefragVal
= (DWORD
) DefragExtensive
;
285 if (PriID
== PriHighID
)
286 PriVal
= HIGH_PRIORITY_CLASS
;
288 if (PriID
== PriAboveNormID
)
289 PriVal
= ABOVE_NORMAL_PRIORITY_CLASS
;
291 if (PriID
== PriNormalID
)
292 PriVal
= NORMAL_PRIORITY_CLASS
;
294 if (PriID
== PriBelowNormID
)
295 PriVal
= BELOW_NORMAL_PRIORITY_CLASS
;
297 if (PriID
== PriIdleID
)
298 PriVal
= IDLE_PRIORITY_CLASS
;
300 if (!GetRegKeys (&RegKey
))
309 (CONST BYTE
*)&DefragVal
,
319 (CONST BYTE
*)&PriVal
,
328 void LoadSettings (HWND Dlg
)
340 if (!GetRegKeys (&RegKey
))
343 RegSize
= sizeof (DTypeVal
);
346 Error
= RegQueryValueEx
356 if (Error
!= ERROR_SUCCESS
)
357 DTypeVal
= DefragAnalyze
;
359 Error
= RegQueryValueEx
371 if (Error
!= ERROR_SUCCESS
)
372 PriVal
= NORMAL_PRIORITY_CLASS
;
374 DType
= (DefragType
) DTypeVal
;
379 DefragID
= AnalyzeID
;
386 case DefragExtensive
:
387 DefragID
= ExtensiveID
;
393 case HIGH_PRIORITY_CLASS
:
397 case ABOVE_NORMAL_PRIORITY_CLASS
:
398 PriID
= PriAboveNormID
;
402 case NORMAL_PRIORITY_CLASS
:
406 case BELOW_NORMAL_PRIORITY_CLASS
:
407 PriID
= PriBelowNormID
;
410 case IDLE_PRIORITY_CLASS
:
415 SendDlgItemMessage (Dlg
, IDC_PRIORITY_LIST
, CB_SETCURSEL
, PriID
, 0);
416 SendDlgItemMessage (Dlg
, IDC_METHODS_LIST
, CB_SETCURSEL
, DefragID
, 0);
422 wstring OldWindowText
= L
"";
424 INT_PTR CALLBACK
MainDialogProc (HWND Dlg
, UINT Msg
, WPARAM WParam
, LPARAM LParam
)
426 static bool ReEntrance
= false;
432 SetWindowText (Dlg
, GetDefaultTitle().c_str());
435 SetTimer (Dlg
, 1, IDLETIME
, NULL
);
436 SetClassLong (Dlg
, GCL_HICON
, (LONG
) LoadIcon (GlobalHInstance
, MAKEINTRESOURCE(IDI_ICON
)));
437 QuitWhenDone
= false;
439 UpdatePriority (Dlg
);
444 if (Defrag
!= NULL
&& !ReEntrance
)
448 SendMessage (Dlg
, WM_UPDATEINFO
, 0, 0);
450 NewTitle
= GetDefragTitle ();
451 if (NewTitle
!= OldWindowText
)
453 OldWindowText
= NewTitle
;
454 SetWindowText (Dlg
, NewTitle
.c_str());
457 if (Defrag
->IsDoneYet() || Defrag
->HasError())
458 { // This is the code executed when defragging is finished (or stopped :)
459 if (Defrag
->GetDefragType() == DefragAnalyze
&&
460 !Defrag
->HasError() &&
465 DialogBoxParam (GlobalHInstance
, MAKEINTRESOURCE (IDD_REPORT
),
466 Dlg
, ReportDialogProc
, (LPARAM
) Defrag
);
474 SetWindowText (Dlg
, GetDefaultTitle().c_str());
479 SendMessage (GetDlgItem (Dlg
, IDC_QUIT
), BM_CLICK
, 0, 0);
483 SetTimer (Dlg
, 1, IDLETIME
, NULL
);
488 UpdateDefragInfo (Dlg
);
493 SendMessage (GetDlgItem (Dlg
, IDC_QUIT
), BM_CLICK
, 0, 0);
498 switch (LOWORD(WParam
))
500 case IDC_PRIORITY_LIST
:
501 UpdatePriority (Dlg
);
506 ShellExecute (Dlg
, L
"open", L
"Fraginator.chm", L
"", L
".", SW_SHOW
);
512 { // This is the code executing when quitting
517 { // Tell defragging to finish and disable our button
519 SendMessage (GetDlgItem (Dlg
, IDC_STARTSTOP
), BM_CLICK
, 0, 0);
520 EnableWindow (GetDlgItem (Dlg
, IDC_QUIT
), FALSE
);
536 SendMessage (GetDlgItem (Dlg
, IDC_DRIVES_LIST
), WM_GETTEXT
,
537 sizeof (Drive
) - 1, (LPARAM
) Drive
);
539 if (wcslen(Drive
) != 2 || Drive
[1] != L
':')
542 ID
= SendMessage (GetDlgItem (Dlg
, IDC_METHODS_LIST
), CB_GETCURSEL
, 0, 0);
543 Method
= DefragInvalid
;
545 Method
= DefragAnalyze
;
550 if (ID
== ExtensiveID
)
551 Method
= DefragExtensive
;
553 if (Method
!= DefragInvalid
)
555 Defrag
= StartDefragThread (Drive
, Method
, H
);
556 Defrag
->SetDoLimitLength (false);
557 SetWindowText (Dlg
, GetDefragTitle().c_str());
565 EnableWindow (GetDlgItem (Dlg
, IDC_STARTSTOP
), FALSE
);
566 EnableWindow (GetDlgItem (Dlg
, IDC_QUIT
), FALSE
);
572 // Otherwise, return 0 to say we did not process the message.