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