54cd37f95ef5a536e43471eae58b61ebd434dda2
[reactos.git] / modules / rosapps / lib / vfdlib / vfdshprop.cpp
1 /*
2 vfdshprop.cpp
3
4 Virtual Floppy Drive for Windows
5 Driver control library
6 COM shell extension class property sheet functions
7
8 Copyright (c) 2003-2005 Ken Kato
9 */
10
11 #define WIN32_LEAN_AND_MEAN
12 #include <windows.h>
13 #include <shellapi.h>
14 #include <shlobj.h>
15 #include <stdio.h>
16
17 #include "vfdtypes.h"
18 #include "vfdapi.h"
19 #include "vfdlib.h"
20 #include "vfdver.h"
21 #ifndef __REACTOS__
22 #include "vfdmsg.h"
23 #else
24 #include "vfdmsg_lib.h"
25 #endif
26 #include "vfdguirc.h"
27
28 // class header
29 #include "vfdshext.h"
30
31 // property sheet property ID
32
33 #define VFD_PROPERTY_ID "VFD"
34
35 //
36 // local functions
37 //
38 static BOOL CALLBACK VfdPageDlgProc(
39 HWND hDlg,
40 UINT uMessage,
41 WPARAM wParam,
42 LPARAM lParam);
43
44 static UINT CALLBACK VfdPageCallback(
45 HWND hWnd,
46 UINT uMessage,
47 LPPROPSHEETPAGE ppsp);
48
49 static void OnPropInit(HWND hDlg);
50 static void OnControl(HWND hDlg);
51 static void UpdateImageInfo(HWND hDlg, ULONG nDevice);
52
53 //
54 // property sheet callback function
55 //
56 UINT CALLBACK VfdPageCallback(
57 HWND hWnd,
58 UINT uMessage,
59 LPPROPSHEETPAGE ppsp)
60 {
61 UNREFERENCED_PARAMETER(hWnd);
62
63 switch(uMessage) {
64 case PSPCB_CREATE:
65 return TRUE;
66
67 case PSPCB_RELEASE:
68 if (ppsp->lParam) {
69 ((LPCVFDSHEXT)(ppsp->lParam))->Release();
70 }
71 return TRUE;
72 }
73 return TRUE;
74 }
75
76 //
77 // property page dialog procedure
78 //
79 BOOL CALLBACK VfdPageDlgProc(
80 HWND hDlg,
81 UINT uMessage,
82 WPARAM wParam,
83 LPARAM lParam)
84 {
85 LPPROPSHEETPAGE psp;
86 LPCVFDSHEXT lpcs;
87
88 switch (uMessage) {
89 case WM_INITDIALOG:
90 SetWindowLong(hDlg, DWL_USER, lParam);
91
92 if (lParam) {
93 lpcs = (LPCVFDSHEXT)((LPPROPSHEETPAGE)lParam)->lParam;
94
95 OnPropInit(hDlg);
96 UpdateImageInfo(hDlg, lpcs->GetDevice());
97 }
98 return TRUE;
99
100 case WM_COMMAND:
101 psp = (LPPROPSHEETPAGE)GetWindowLong(hDlg, DWL_USER);
102
103 if (!psp) {
104 break;
105 }
106
107 lpcs = (LPCVFDSHEXT)psp->lParam;
108
109 if (!lpcs) {
110 break;
111 }
112
113 switch (wParam) {
114 case IDC_OPEN:
115 if (lpcs->DoVfdOpen(hDlg) == ERROR_SUCCESS) {
116 SendMessage((HWND)lParam,
117 BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
118 }
119 UpdateImageInfo(hDlg, lpcs->GetDevice());
120 break;
121
122 case IDC_SAVE:
123 if (lpcs->DoVfdSave(hDlg) == ERROR_SUCCESS) {
124 SendMessage((HWND)lParam,
125 BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
126 }
127 UpdateImageInfo(hDlg, lpcs->GetDevice());
128 break;
129
130 case IDC_CLOSE:
131 if (lpcs->DoVfdClose(hDlg) == ERROR_SUCCESS) {
132 SendMessage((HWND)lParam,
133 BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
134 }
135 UpdateImageInfo(hDlg, lpcs->GetDevice());
136 break;
137
138 case IDC_WRITE_PROTECTED:
139 lpcs->DoVfdProtect(hDlg);
140 break;
141
142 case IDC_FORMAT:
143 VfdGuiFormat(hDlg, lpcs->GetDevice());
144 break;
145
146 case IDC_CONTROL:
147 OnControl(hDlg);
148 break;
149 }
150 break;
151
152 case WM_CONTEXTMENU:
153 ShowContextMenu(hDlg, (HWND)wParam, lParam);
154 break;
155
156 case WM_HELP:
157 {
158 LPHELPINFO info = (LPHELPINFO)lParam;
159
160 if (info->iContextType == HELPINFO_WINDOW) {
161 ShowHelpWindow(hDlg, info->iCtrlId);
162 }
163 }
164 return TRUE;
165
166 default:
167 if (uMessage == g_nNotifyMsg) {
168 psp = (LPPROPSHEETPAGE)GetWindowLong(hDlg, DWL_USER);
169
170 if (!psp) {
171 break;
172 }
173
174 lpcs = (LPCVFDSHEXT)psp->lParam;
175
176 if (!lpcs) {
177 break;
178 }
179
180 UpdateImageInfo(hDlg, lpcs->GetDevice());
181 }
182 break;
183 }
184
185 return FALSE;
186 }
187
188 //
189 // initialize the property page
190 //
191 void OnPropInit(
192 HWND hDlg)
193 {
194 // set up control text
195
196 SetDlgItemText(hDlg, IDC_PROPERTY_TITLE, VFD_PRODUCT_DESC);
197 SetDlgItemText(hDlg, IDC_COPYRIGHT_STR, VFD_COPYRIGHT_STR);
198
199 SetControlText(hDlg, IDC_IMAGEFILE_LABEL, MSG_IMAGEFILE_LABEL);
200 SetControlText(hDlg, IDC_IMAGEDESC_LABEL, MSG_DESCRIPTION_LABEL);
201 SetControlText(hDlg, IDC_DISKTYPE_LABEL, MSG_DISKTYPE_LABEL);
202 SetControlText(hDlg, IDC_MEDIATYPE_LABEL, MSG_MEDIATYPE_LABEL);
203 SetControlText(hDlg, IDC_WRITE_PROTECTED, MSG_MENU_PROTECT);
204 SetControlText(hDlg, IDC_OPEN, MSG_OPEN_BUTTON);
205 SetControlText(hDlg, IDC_SAVE, MSG_SAVE_BUTTON);
206 SetControlText(hDlg, IDC_CLOSE, MSG_CLOSE_BUTTON);
207 SetControlText(hDlg, IDC_FORMAT, MSG_FORMAT_BUTTON);
208 SetControlText(hDlg, IDC_CONTROL, MSG_CONTROL_BUTTON);
209 }
210
211 //
212 // Control Panel button is clicked
213 //
214 void OnControl(
215 HWND hDlg)
216 {
217 CHAR module_path[MAX_PATH];
218 CHAR full_path[MAX_PATH];
219 PSTR file_name;
220 DWORD ret;
221
222 ret = GetModuleFileName(
223 g_hDllModule, module_path, sizeof(module_path));
224
225 if (ret == 0 || ret >= sizeof(module_path)) {
226 file_name = full_path;
227 }
228 else {
229 ret = GetFullPathName(
230 module_path, sizeof(full_path), full_path, &file_name);
231
232 if (ret == 0 || ret >= sizeof(full_path)) {
233 file_name = full_path;
234 }
235 }
236
237 strcpy(file_name, "vfdwin.exe");
238
239 VFDTRACE(0, ("Starting %s\n", full_path));
240
241 ret = (DWORD)ShellExecute(
242 hDlg, NULL, full_path, NULL, NULL, SW_SHOW);
243
244 if (ret > 32) {
245 PropSheet_PressButton(GetParent(hDlg), PSBTN_CANCEL);
246 }
247 else {
248 MessageBox(hDlg, SystemMessage(ret),
249 VFD_MSGBOX_TITLE, MB_ICONSTOP);
250 }
251 }
252
253 //
254 // Update image information on the property page
255 //
256 void UpdateImageInfo(
257 HWND hDlg,
258 ULONG nDevice)
259 {
260 HANDLE hDevice;
261 CHAR buf[MAX_PATH];
262 VFD_DISKTYPE disk_type;
263 VFD_MEDIA media_type;
264 VFD_FLAGS media_flags;
265 VFD_FILETYPE file_type;
266 ULONG image_size;
267 DWORD attrib;
268 ULONG ret;
269
270 hDevice = VfdOpenDevice(nDevice);
271
272 if (hDevice == INVALID_HANDLE_VALUE) {
273 MessageBox(hDlg,
274 SystemMessage(GetLastError()),
275 VFD_MSGBOX_TITLE, MB_ICONSTOP);
276 return;
277 }
278
279 // get current image information
280
281 ret = VfdGetImageInfo(
282 hDevice,
283 buf,
284 &disk_type,
285 &media_type,
286 &media_flags,
287 &file_type,
288 &image_size);
289
290 CloseHandle(hDevice);
291
292 if (ret != ERROR_SUCCESS) {
293 MessageBox(hDlg,
294 SystemMessage(ret),
295 VFD_MSGBOX_TITLE, MB_ICONSTOP);
296 return;
297 }
298
299 if (media_type == VFD_MEDIA_NONE) {
300
301 // drive is empty
302
303 SetDlgItemText(hDlg, IDC_IMAGEFILE, NULL);
304 SetDlgItemText(hDlg, IDC_IMAGEFILE_DESC, NULL);
305 SetDlgItemText(hDlg, IDC_DISKTYPE, NULL);
306 SetDlgItemText(hDlg, IDC_MEDIATYPE, NULL);
307
308 EnableWindow(GetDlgItem(hDlg, IDC_WRITE_PROTECTED), FALSE);
309 EnableWindow(GetDlgItem(hDlg, IDC_OPEN), TRUE);
310 EnableWindow(GetDlgItem(hDlg, IDC_SAVE), FALSE);
311 EnableWindow(GetDlgItem(hDlg, IDC_CLOSE), FALSE);
312 EnableWindow(GetDlgItem(hDlg, IDC_FORMAT), FALSE);
313
314 SendMessage(GetDlgItem(hDlg, IDC_OPEN),
315 BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
316
317 SetFocus(GetDlgItem(hDlg, IDC_OPEN));
318
319 return;
320 }
321
322 // display image file name
323
324 if (buf[0]) {
325 attrib = GetFileAttributes(buf);
326
327 if (attrib == INVALID_FILE_ATTRIBUTES) {
328 attrib = 0;
329 }
330 }
331 else {
332 if (disk_type != VFD_DISKTYPE_FILE) {
333 strcpy(buf, "<RAM>");
334 }
335 attrib = 0;
336 }
337
338 SetDlgItemText(hDlg, IDC_IMAGEFILE, buf);
339
340 // display image description
341
342 VfdMakeFileDesc(buf, sizeof(buf),
343 file_type, image_size, attrib);
344
345 SetDlgItemText(hDlg, IDC_IMAGEFILE_DESC, buf);
346
347 // display disk type
348
349 if (disk_type == VFD_DISKTYPE_FILE) {
350 SetDlgItemText(hDlg, IDC_DISKTYPE, "FILE");
351 }
352 else {
353 SetDlgItemText(hDlg, IDC_DISKTYPE, "RAM");
354 }
355
356 // display media type
357
358 SetDlgItemText(hDlg, IDC_MEDIATYPE,
359 VfdMediaTypeName(media_type));
360
361 // set write protect check box
362
363 if (media_flags & VFD_FLAG_WRITE_PROTECTED) {
364 CheckDlgButton(hDlg, IDC_WRITE_PROTECTED, BST_CHECKED);
365 }
366 else {
367 CheckDlgButton(hDlg, IDC_WRITE_PROTECTED, BST_UNCHECKED);
368 }
369
370 EnableWindow(GetDlgItem(hDlg, IDC_WRITE_PROTECTED), TRUE);
371 EnableWindow(GetDlgItem(hDlg, IDC_OPEN), FALSE);
372 EnableWindow(GetDlgItem(hDlg, IDC_SAVE), TRUE);
373 EnableWindow(GetDlgItem(hDlg, IDC_CLOSE), TRUE);
374 EnableWindow(GetDlgItem(hDlg, IDC_FORMAT), TRUE);
375
376 SendMessage(GetDlgItem(hDlg, IDC_CLOSE),
377 BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
378
379 SetFocus(GetDlgItem(hDlg, IDC_CLOSE));
380 }
381
382 //
383 // CVfdShExt class members inherited from IShellPropSheetExt
384 //
385
386 // Add property page
387 STDMETHODIMP CVfdShExt::AddPages(
388 LPFNADDPROPSHEETPAGE lpfnAddPage,
389 LPARAM lParam)
390 {
391 PROPSHEETPAGE psp;
392 HPROPSHEETPAGE hpage;
393
394 if (!m_pDataObj || m_nDevice == (ULONG)-1) {
395 // not a VFD drive
396 VFDTRACE(0, ("PropPage: Not a VFD drive\n"));
397
398 return NOERROR;
399 }
400
401 psp.dwSize = sizeof(psp); // no extra data.
402 psp.dwFlags = PSP_USEREFPARENT | PSP_USETITLE | PSP_USECALLBACK;
403 psp.hInstance = g_hDllModule;
404 psp.pszTemplate = MAKEINTRESOURCE(IDD_PROPDIALOG);
405 psp.hIcon = 0;
406 psp.pszTitle = "VFD";
407 psp.pfnDlgProc = VfdPageDlgProc;
408 psp.pcRefParent = &g_cDllRefCnt;
409 psp.pfnCallback = VfdPageCallback;
410 psp.lParam = (LPARAM)this;
411
412 AddRef();
413 hpage = CreatePropertySheetPage(&psp);
414
415 if (hpage) {
416 if (!lpfnAddPage(hpage, lParam)) {
417 DestroyPropertySheetPage(hpage);
418 Release();
419 }
420 }
421
422 return NOERROR;
423 }
424
425 STDMETHODIMP CVfdShExt::ReplacePage(
426 UINT uPageID,
427 LPFNADDPROPSHEETPAGE lpfnReplaceWith,
428 LPARAM lParam)
429 {
430 UNREFERENCED_PARAMETER(uPageID);
431 UNREFERENCED_PARAMETER(lpfnReplaceWith);
432 UNREFERENCED_PARAMETER(lParam);
433 return E_FAIL;
434 }