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