4 Virtual Floppy Drive for Windows
6 open / close / format GUI utility functions
8 Copyright (c) 2003-2005 Ken Kato
12 #pragma message(__FILE__": Compiled as C++ for testing purpose.")
15 #define WIN32_LEAN_AND_MEAN
25 #include "vfdmsg_lib.h"
30 // message box constants added since Win2K
33 #ifndef MB_CANCELTRYCONTINUE
34 #define MB_CANCELTRYCONTINUE 0x00000006L
48 static PSTR
FormatSizeBytes(ULONG size
, PSTR buf
)
53 while ((comma
* 1000) < size
) {
57 len
= sprintf(buf
, "%lu", size
/ comma
);
62 len
+= sprintf(buf
+ len
, ",%03lu", size
/ comma
);
68 static PSTR
FormatSizeUnits(ULONG size
, PSTR buf
)
70 static const char *name
[3] = {
78 sprintf(buf
, "%u", size
);
80 sprintf(buf
, "%lu", size
);
89 while (unit
< 2 && dsize
>= 1000) {
95 sprintf(buf
, "%3.2f%s", dsize
, name
[unit
]);
97 else if (dsize
< 100) {
98 sprintf(buf
, "%3.1f%s", dsize
, name
[unit
]);
100 else if (dsize
< 1000) {
101 sprintf(buf
, "%3.0f%s", dsize
, name
[unit
]);
104 FormatSizeBytes((ULONG
)dsize
, buf
);
105 strcat(buf
, name
[unit
]);
112 // Close the current image
114 DWORD WINAPI
VfdGuiClose(
115 HWND hParent
, // parent window
116 ULONG nDevice
) // device number
125 VFDTRACE(0, ("VfdGuiClose()\n"));
127 hDevice
= VfdOpenDevice(nDevice
);
129 if (hDevice
== INVALID_HANDLE_VALUE
) {
130 return GetLastError();
133 // get current image information
135 ret
= VfdGetImageInfo(
144 if (ret
!= ERROR_SUCCESS
) {
145 CloseHandle(hDevice
);
149 param
.hDevice
= hDevice
;
150 param
.ImageName
= path
;
152 // check if RAM image is modified
154 if (param
.MediaFlags
& VFD_FLAG_DATA_MODIFIED
) {
155 PSTR msg
= ModuleMessage(MSG_MEDIA_MODIFIED
);
158 reply
= MessageBox(hParent
, msg
? msg
: "save?",
159 VFD_MSGBOX_TITLE
, MB_ICONQUESTION
| MB_YESNOCANCEL
);
161 if (reply
!= IDYES
) {
165 if (GuiSaveParam(hParent
, ¶m
) == ERROR_SUCCESS
) {
174 if (reply
== IDCANCEL
) {
175 CloseHandle(hDevice
);
176 return ERROR_CANCELLED
;
182 hourglass
= LoadCursor(NULL
, IDC_WAIT
);
186 // show the hourglass cursor
188 HCURSOR original
= SetCursor(hourglass
);
190 // close the current image
192 ret
= VfdCloseImage(hDevice
, FALSE
);
194 // restore the original cursor
198 if (ret
!= ERROR_ACCESS_DENIED
) {
199 // success or errors other than access denied
203 if (IS_WINDOWS_NT()) {
205 // Windows NT -- cannot force close
206 // show retry / cancel message box
208 PSTR msg
= ModuleMessage(MSG_UNMOUNT_FAILED
);
211 hParent
, msg
? msg
: "retry", VFD_MSGBOX_TITLE
,
212 MB_ICONEXCLAMATION
| MB_RETRYCANCEL
);
220 // Windows 2000 and later -- possible to force
221 // show cancel / retry / continue message box
223 PSTR msg
= ModuleMessage(MSG_UNMOUNT_CONFIRM
);
226 hParent
, msg
? msg
: "retry", VFD_MSGBOX_TITLE
,
227 MB_ICONEXCLAMATION
| MB_CANCELTRYCONTINUE
);
233 if (reply
== IDCONTINUE
) {
237 ret
= VfdCloseImage(hDevice
, TRUE
);
241 if (reply
== IDCANCEL
) {
242 ret
= ERROR_CANCELLED
;
245 else if (reply
== IDCONTINUE
) {
249 ret
= VfdCloseImage(hDevice
, TRUE
);
254 CloseHandle(hDevice
);
260 // Format the current media
262 DWORD WINAPI
VfdGuiFormat(
263 HWND hParent
, // parent window
264 ULONG nDevice
) // device number
270 msg
= ModuleMessage(MSG_FORMAT_WARNING
);
272 ret
= MessageBox(hParent
,
273 msg
? msg
: "Format?",
275 MB_ICONEXCLAMATION
| MB_OKCANCEL
);
281 if (ret
== IDCANCEL
) {
283 SystemMessage(ERROR_CANCELLED
),
284 VFD_MSGBOX_TITLE
, MB_ICONSTOP
);
286 return ERROR_CANCELLED
;
289 hDevice
= VfdOpenDevice(nDevice
);
291 if (hDevice
== INVALID_HANDLE_VALUE
) {
292 ret
= GetLastError();
298 original
= SetCursor(LoadCursor(NULL
, IDC_WAIT
));
300 ret
= VfdDismountVolume(hDevice
, FALSE
);
302 if (ret
== ERROR_ACCESS_DENIED
) {
308 msg
= ModuleMessage(MSG_UNMOUNT_CONFIRM
);
311 hParent
, msg
? msg
: "retry", VFD_MSGBOX_TITLE
,
312 MB_ICONEXCLAMATION
| MB_CANCELTRYCONTINUE
);
318 if (reply
== IDRETRY
) {
321 else if (reply
== IDCANCEL
) {
322 ret
= ERROR_CANCELLED
;
325 VfdDismountVolume(hDevice
, TRUE
);
330 if (ret
== ERROR_SUCCESS
) {
331 ret
= VfdFormatMedia(hDevice
);
336 CloseHandle(hDevice
);
342 ret
== ERROR_SUCCESS
? MB_ICONINFORMATION
: MB_ICONSTOP
);
348 // Set a text to a dialog control
358 p
= ModuleMessage(nMsg
);
362 SetDlgItemText(hWnd
, nCtrl
, p
);
365 SetWindowText(hWnd
, p
);
374 // Make file description text
376 void WINAPI
VfdMakeFileDesc(
379 VFD_FILETYPE nFileType
,
389 ZeroMemory(pBuffer
, nBufSize
);
392 case VFD_FILETYPE_RAW
:
393 type_str
= ModuleMessage(MSG_FILETYPE_RAW
);
396 case VFD_FILETYPE_ZIP
:
397 type_str
= ModuleMessage(MSG_FILETYPE_ZIP
);
404 if (nFileSize
== INVALID_FILE_SIZE
) {
405 size_str
= ModuleMessage(MSG_DESC_NEW_FILE
);
408 CHAR buf
[20], buf2
[20];
409 size_str
= ModuleMessage(MSG_DESC_FILESIZE
,
410 FormatSizeBytes(nFileSize
, buf
),
411 FormatSizeUnits(nFileSize
, buf2
));
418 if (nFileAttr
!= INVALID_FILE_ATTRIBUTES
) {
419 if (nFileAttr
& FILE_ATTRIBUTE_READONLY
) {
420 attr_ro
= ModuleMessage(MSG_ATTR_READONLY
);
423 if (nFileAttr
& FILE_ATTRIBUTE_COMPRESSED
) {
424 attr_cmp
= ModuleMessage(MSG_ATTR_COMPRESSED
);
427 if (nFileAttr
& FILE_ATTRIBUTE_ENCRYPTED
) {
428 attr_enc
= ModuleMessage(MSG_ATTR_ENCRYPTED
);
432 _snprintf(pBuffer
, nBufSize
- 1, "%s %s %s %s %s",
433 type_str
? type_str
: "",
434 size_str
? size_str
: "",
435 attr_ro
? attr_ro
: "",
436 attr_cmp
? attr_cmp
: "",
437 attr_enc
? attr_enc
: "");
456 void ShowContextMenu(
465 pt
.x
= ((int)(short)LOWORD(lParam
));
466 pt
.y
= ((int)(short)HIWORD(lParam
));
468 if (pt
.x
== -1 || pt
.y
== -1) {
471 GetWindowRect(hCtl
, &rc
);
472 pt
.x
= (rc
.left
+ rc
.right
) / 2;
473 pt
.y
= (rc
.top
+ rc
.bottom
) / 2;
475 id
= GetDlgCtrlID(hCtl
);
480 ScreenToClient(hDlg
, &pt2
);
483 ChildWindowFromPoint(hDlg
, pt2
));
486 if (id
< IDC_IMAGEFILE_LABEL
||
491 hMenu
= CreatePopupMenu();
493 AppendMenu(hMenu
, MF_STRING
, 1, "&What's This");
495 if (TrackPopupMenu(hMenu
, TPM_RETURNCMD
,
496 pt
.x
, pt
.y
, 0, hDlg
, NULL
))
498 ShowHelpWindow(hDlg
, id
);
505 // Show tool tip help
516 case IDC_IMAGEFILE_LABEL
:
518 msg
= MSG_HELP_IMAGEFILE
;
520 case IDC_IMAGEDESC_LABEL
:
521 case IDC_IMAGEFILE_DESC
:
522 msg
= MSG_HELP_IMAGEDESC
;
524 case IDC_TARGETFILE_LABEL
:
526 msg
= MSG_HELP_TARGETFILE
;
528 case IDC_DISKTYPE_LABEL
:
530 case IDC_DISKTYPE_FILE
:
531 case IDC_DISKTYPE_RAM
:
532 msg
= MSG_HELP_DISKTYPE
;
534 case IDC_MEDIATYPE_LABEL
:
536 msg
= MSG_HELP_MEDIATYPE
;
538 case IDC_WRITE_PROTECTED
:
539 msg
= MSG_HELP_PROTECT_NOW
;
541 case IDC_OPEN_PROTECTED
:
542 msg
= MSG_HELP_PROTECT_OPEN
;
545 msg
= MSG_HELP_BROWSE
;
554 msg
= MSG_HELP_CLOSE
;
557 msg
= MSG_HELP_FORMAT
;
560 msg
= MSG_HELP_CONTROL
;
563 msg
= MSG_HELP_OVERWRITE
;
566 msg
= MSG_HELP_TRUNCATE
;
572 GetWindowRect(GetDlgItem(hDlg
, nCtl
), &rc
);
574 help
= ModuleMessage(msg
);
577 VfdToolTip(hDlg
, help
, rc
.left
, (rc
.top
+ rc
.bottom
) / 2, TRUE
);