[KERNEL32]
[reactos.git] / reactos / base / applications / dxdiag / dxdiag.c
1 /*
2 * PROJECT: ReactX Diagnosis Application
3 * LICENSE: LGPL - See COPYING in the top level directory
4 * FILE: base/applications/dxdiag/dxdiag.c
5 * PURPOSE: ReactX diagnosis application entry
6 * COPYRIGHT: Copyright 2008 Johannes Anderwald
7 *
8 */
9
10 #include "precomp.h"
11
12 /* globals */
13 HINSTANCE hInst = 0;
14 HWND hTabCtrlWnd;
15
16 //---------------------------------------------------------------
17 VOID
18 DestroyTabCtrlDialogs(PDXDIAG_CONTEXT pContext)
19 {
20 UINT Index;
21
22 /* destroy default dialogs */
23 for(Index = 0; Index < sizeof(pContext->hDialogs) / sizeof(HWND); Index++)
24 {
25 if (pContext->hDialogs[Index])
26 DestroyWindow(pContext->hDialogs[Index]);
27 }
28
29 /* destroy display dialogs */
30 for(Index = 0; Index < pContext->NumDisplayAdapter; Index++)
31 {
32 if (pContext->hDisplayWnd[Index])
33 DestroyWindow(pContext->hDisplayWnd[Index]);
34 }
35
36 /* destroy audio dialogs */
37 for(Index = 0; Index < pContext->NumSoundAdapter; Index++)
38 {
39 if (pContext->hSoundWnd[Index])
40 DestroyWindow(pContext->hSoundWnd[Index]);
41 }
42
43 }
44
45 //---------------------------------------------------------------
46 VOID
47 InsertTabCtrlItem(HWND hDlgCtrl, INT Position, LPWSTR uId)
48 {
49 WCHAR szName[100];
50 TCITEMW item;
51
52 /* setup item info */
53 memset(&item, 0, sizeof(TCITEM));
54 item.mask = TCIF_TEXT;
55
56 /* load item name */
57 if (!HIWORD(uId))
58 {
59 szName[0] = L'\0';
60 if (!LoadStringW(hInst, LOWORD(uId), szName, 100))
61 return;
62 szName[99] = L'\0';
63 item.pszText = szName;
64 }
65 else
66 {
67 item.pszText = uId;
68 }
69
70
71 SendMessageW(hDlgCtrl, TCM_INSERTITEM, Position, (LPARAM)&item);
72 }
73
74 VOID
75 TabCtrl_OnSelChange(PDXDIAG_CONTEXT pContext)
76 {
77 INT Index;
78 INT CurSel;
79
80 /* retrieve new page */
81 CurSel = TabCtrl_GetCurSel(hTabCtrlWnd);
82 if (CurSel < 0 || CurSel > pContext->NumDisplayAdapter + pContext->NumSoundAdapter + 5)
83 return;
84
85 /* hide all windows */
86 for(Index = 0; Index < 5; Index++)
87 ShowWindow(pContext->hDialogs[Index], SW_HIDE);
88
89 for(Index = 0; Index < pContext->NumDisplayAdapter; Index++)
90 ShowWindow(pContext->hDisplayWnd[Index], SW_HIDE);
91
92 for(Index = 0; Index < pContext->NumSoundAdapter; Index++)
93 ShowWindow(pContext->hSoundWnd[Index], SW_HIDE);
94
95
96 if (CurSel == 0 || CurSel > pContext->NumDisplayAdapter + pContext->NumSoundAdapter)
97 {
98 if (CurSel)
99 CurSel -= pContext->NumDisplayAdapter + pContext->NumSoundAdapter;
100 ShowWindow(pContext->hDialogs[CurSel], SW_SHOW);
101 return;
102 }
103
104 if (CurSel -1 < pContext->NumDisplayAdapter)
105 {
106 ShowWindow(pContext->hDisplayWnd[CurSel-1], SW_SHOW);
107 return;
108 }
109
110 CurSel -= pContext->NumDisplayAdapter + 1;
111 ShowWindow(pContext->hSoundWnd[CurSel], SW_SHOW);
112 }
113
114 VOID
115 InitializeTabCtrl(HWND hwndDlg, PDXDIAG_CONTEXT pContext)
116 {
117 /* get tabctrl */
118 hTabCtrlWnd = GetDlgItem(hwndDlg, IDC_TAB_CONTROL);
119 pContext->hTabCtrl = hTabCtrlWnd;
120
121 /* create the dialogs */
122 pContext->hDialogs[0] = CreateDialogParamW(hInst, MAKEINTRESOURCEW(IDD_SYSTEM_DIALOG), hTabCtrlWnd, SystemPageWndProc, (LPARAM)pContext);
123 pContext->hDialogs[1] = CreateDialogParamW(hInst, MAKEINTRESOURCEW(IDD_MUSIC_DIALOG), hTabCtrlWnd, MusicPageWndProc, (LPARAM)pContext);
124 pContext->hDialogs[2] = CreateDialogParamW(hInst, MAKEINTRESOURCEW(IDD_INPUT_DIALOG), hTabCtrlWnd, InputPageWndProc, (LPARAM)pContext);
125 pContext->hDialogs[3] = CreateDialogParamW(hInst, MAKEINTRESOURCEW(IDD_NETWORK_DIALOG), hTabCtrlWnd, NetworkPageWndProc, (LPARAM)pContext);
126 pContext->hDialogs[4] = CreateDialogParamW(hInst, MAKEINTRESOURCEW(IDD_HELP_DIALOG), hTabCtrlWnd, HelpPageWndProc, (LPARAM)pContext);
127
128 /* insert tab ctrl items */
129 InsertTabCtrlItem(hTabCtrlWnd, 0, MAKEINTRESOURCEW(IDS_SYSTEM_DIALOG));
130 InitializeDisplayAdapters(pContext);
131 InitializeDirectSoundPage(pContext);
132 InsertTabCtrlItem(hTabCtrlWnd, pContext->NumDisplayAdapter + pContext->NumSoundAdapter + 1, MAKEINTRESOURCEW(IDS_MUSIC_DIALOG));
133 InsertTabCtrlItem(hTabCtrlWnd, pContext->NumDisplayAdapter + pContext->NumSoundAdapter + 2, MAKEINTRESOURCEW(IDS_INPUT_DIALOG));
134 InsertTabCtrlItem(hTabCtrlWnd, pContext->NumDisplayAdapter + pContext->NumSoundAdapter + 3, MAKEINTRESOURCEW(IDS_NETWORK_DIALOG));
135 InsertTabCtrlItem(hTabCtrlWnd, pContext->NumDisplayAdapter + pContext->NumSoundAdapter + 4, MAKEINTRESOURCEW(IDS_HELP_DIALOG));
136 TabCtrl_OnSelChange(pContext);
137 }
138
139 VOID
140 InitializeDxDiagDialog(HWND hwndDlg)
141 {
142 PDXDIAG_CONTEXT pContext;
143 HICON hIcon;
144
145 pContext = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DXDIAG_CONTEXT));
146 if (!pContext)
147 return;
148
149 /* store window handle */
150 pContext->hMainDialog = hwndDlg;
151
152 /* store the context */
153 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pContext);
154
155 /* initialize the tab ctrl */
156 InitializeTabCtrl(hwndDlg, pContext);
157
158 /* load application icon */
159 hIcon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON, 16, 16, 0);
160 if (!hIcon)
161 return;
162 /* display icon */
163 SendMessage(hwndDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
164 }
165
166
167 INT_PTR CALLBACK
168 DxDiagWndProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
169 {
170 LPNMHDR pnmh;
171 PDXDIAG_CONTEXT pContext;
172
173 pContext = (PDXDIAG_CONTEXT)GetWindowLongPtr(hwndDlg, DWLP_USER);
174
175 switch (message)
176 {
177 case WM_INITDIALOG:
178 InitializeDxDiagDialog(hwndDlg);
179 return TRUE;
180 case WM_COMMAND:
181 if (LOWORD(wParam) == IDC_BUTTON_SAVE_INFO)
182 {
183 //TODO
184 /* handle save information */
185 return TRUE;
186 }
187
188 if (LOWORD(wParam) == IDC_BUTTON_NEXT)
189 {
190 INT CurSel;
191
192 /* retrieve current page */
193 CurSel = TabCtrl_GetCurSel(hTabCtrlWnd);
194 CurSel++;
195
196 /* enable/disable next button */
197 EnableWindow(GetDlgItem(hwndDlg, IDC_BUTTON_NEXT),
198 (CurSel != TabCtrl_GetItemCount(hTabCtrlWnd) - 1));
199
200 /* switch to next page */
201 SendMessageW(hTabCtrlWnd, TCM_SETCURSEL, CurSel, 0L);
202
203 return TRUE;
204 }
205
206 if (LOWORD(wParam) == IDC_BUTTON_HELP)
207 {
208 //TODO
209 /* handle help button */
210 return TRUE;
211 }
212
213 if (LOWORD(wParam) == IDCANCEL || LOWORD(wParam) == IDC_BUTTON_EXIT) {
214 EndDialog(hwndDlg, LOWORD(wParam));
215 return TRUE;
216 }
217 break;
218
219 case WM_NOTIFY:
220 pnmh = (LPNMHDR)lParam;
221 if ((pnmh->hwndFrom == hTabCtrlWnd) && (pnmh->idFrom == IDC_TAB_CONTROL) && (pnmh->code == TCN_SELCHANGE))
222 {
223 INT CurSel = TabCtrl_GetCurSel(hTabCtrlWnd);
224
225 /* enable/disable next button */
226 EnableWindow(GetDlgItem(hwndDlg, IDC_BUTTON_NEXT),
227 (CurSel != TabCtrl_GetItemCount(hTabCtrlWnd) - 1));
228
229 TabCtrl_OnSelChange(pContext);
230 }
231 break;
232 case WM_DESTROY:
233 DestroyTabCtrlDialogs(pContext);
234 return DefWindowProc(hwndDlg, message, wParam, lParam);
235 }
236 return 0;
237 }
238
239 int APIENTRY wWinMain(HINSTANCE hInstance,
240 HINSTANCE hPrevInstance,
241 LPWSTR lpCmdLine,
242 int nCmdShow)
243 {
244
245 INITCOMMONCONTROLSEX InitControls;
246
247 UNREFERENCED_PARAMETER(hPrevInstance);
248 UNREFERENCED_PARAMETER(lpCmdLine);
249 UNREFERENCED_PARAMETER(nCmdShow);
250
251 InitControls.dwSize = sizeof(INITCOMMONCONTROLSEX);
252 InitControls.dwICC = ICC_TAB_CLASSES | ICC_LISTVIEW_CLASSES | ICC_STANDARD_CLASSES | ICC_TREEVIEW_CLASSES;
253 InitCommonControlsEx(&InitControls);
254
255 hInst = hInstance;
256
257 DialogBox(hInst, MAKEINTRESOURCE(IDD_MAIN_DIALOG), NULL, DxDiagWndProc);
258
259 return 0;
260 }