[CLT2012]
[reactos.git] / base / applications / msconfig / freeldrpage.c
1 /*
2 * PROJECT: ReactOS Applications
3 * LICENSE: LGPL - See COPYING in the top level directory
4 * FILE: base/applications/msconfig/freeldrpage.c
5 * PURPOSE: Freeloader configuration page message handler
6 * COPYRIGHT: Copyright 2005-2006 Christoph von Wittich <Christoph@ApiViewer.de>
7 * 2011 Gregor Schneider <Gregor.Schneider@reactos.org>
8 */
9
10 #include <precomp.h>
11
12 HWND hFreeLdrPage;
13 HWND hFreeLdrDialog;
14
15 typedef struct
16 {
17 ULONG TimeOut;
18 WCHAR szDefaultOS[512];
19 ULONG szDefaultPos;
20 ULONG OSConfigurationCount;
21 BOOL UseBootIni;
22 } FREELDR_SETTINGS;
23
24 static FREELDR_SETTINGS Settings = { 0, { 0, }, 0, 0, FALSE };
25
26 #define BUFFER_SIZE 512
27
28 static BOOL
29 LoadBootIni(WCHAR *szDrive, HWND hDlg)
30 {
31 WCHAR szBuffer[BUFFER_SIZE];
32 HWND hDlgCtrl;
33 FILE * file;
34 UINT length;
35 LRESULT pos;
36
37 wcscpy(szBuffer, szDrive);
38 wcscat(szBuffer, L"freeldr.ini");
39
40 file = _wfopen(szBuffer, L"rt");
41 if (!file)
42 {
43 wcscpy(szBuffer, szDrive);
44 wcscat(szBuffer, L"boot.ini");
45 file = _wfopen(szBuffer, L"rt");
46 if (!file)
47 return FALSE;
48 }
49
50 hDlgCtrl = GetDlgItem(hDlg, IDC_LIST_BOX);
51
52 while(!feof(file))
53 {
54 if (fgetws(szBuffer, BUFFER_SIZE, file))
55 {
56 length = wcslen(szBuffer);
57 if (length > 1)
58 {
59 szBuffer[length] = L'\0';
60 szBuffer[length - 1] = L'\0';
61
62 pos = SendMessageW(hDlgCtrl, LB_ADDSTRING, 0, (LPARAM)szBuffer);
63
64 if (szBuffer[0] == L'[')
65 continue;
66
67 if (!wcsncmp(szBuffer, L"timeout=", 8))
68 {
69 Settings.TimeOut = _wtoi(&szBuffer[8]);
70 continue;
71 }
72
73 if (!wcsncmp(szBuffer, L"default=", 8))
74 {
75 wcscpy(Settings.szDefaultOS, &szBuffer[8]);
76 continue;
77 }
78 if (pos != LB_ERR)
79 SendMessage(hDlgCtrl, LB_SETITEMDATA, pos, 1); // indicate that this item is an boot entry
80 Settings.OSConfigurationCount++;
81 }
82 }
83 }
84
85 fclose(file);
86 Settings.UseBootIni = TRUE;
87
88 pos = SendMessageW(hDlgCtrl, LB_FINDSTRING, 3, (LPARAM)Settings.szDefaultOS);
89 if (pos != LB_ERR)
90 {
91 Settings.szDefaultPos = pos;
92 SendMessage(hDlgCtrl, LB_SETCURSEL, pos, 0);
93 }
94
95 SetDlgItemInt(hDlg, IDC_TXT_BOOT_TIMEOUT, Settings.TimeOut, FALSE);
96 if (Settings.OSConfigurationCount < 2)
97 {
98 EnableWindow(GetDlgItem(hDlg, IDC_BTN_SET_DEFAULT_BOOT), FALSE);
99 EnableWindow(GetDlgItem(hDlg, IDC_BTN_MOVE_UP_BOOT_OPTION), FALSE);
100 EnableWindow(GetDlgItem(hDlg, IDC_BTN_MOVE_DOWN_BOOT_OPTION), FALSE);
101 }
102 return TRUE;
103 }
104
105 static BOOL
106 InitializeFreeLDRDialog(HWND hDlg)
107 {
108 WCHAR winDir[PATH_MAX];
109 WCHAR* ptr = NULL;
110
111 GetWindowsDirectoryW(winDir, PATH_MAX);
112 ptr = wcschr(winDir, L'\\');
113 if (ptr == NULL)
114 {
115 return FALSE;
116 }
117 ptr[1] = L'\0';
118 return LoadBootIni(winDir, hDlg);
119 }
120
121 INT_PTR CALLBACK
122 FreeLdrPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
123 {
124 LRESULT pos;
125
126 switch (message) {
127 case WM_INITDIALOG:
128 hFreeLdrDialog = hDlg;
129 SetWindowPos(hDlg, NULL, 10, 32, 0, 0, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSIZE | SWP_NOZORDER);
130 InitializeFreeLDRDialog(hDlg);
131 return TRUE;
132 case WM_COMMAND:
133 switch(HIWORD(wParam))
134 {
135 case LBN_SELCHANGE:
136 pos = SendMessage((HWND)lParam, LB_GETCURSEL, 0, 0);
137 if (pos != LB_ERR)
138 {
139 LPARAM res = SendMessage((HWND)lParam, LB_GETITEMDATA, pos, 0);
140 if (!res) //line is not a default one
141 SendMessage((HWND)lParam, LB_SETCURSEL, Settings.szDefaultPos, 0);
142 else
143 Settings.szDefaultPos = pos;
144
145
146 }
147 break;
148 }
149 }
150 return 0;
151 }