Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554/* vfdguisave.c
Virtual Floppy Drive for Windows Driver control library Save image GUI utility function
Copyright (c) 2003-2005 Ken Kato*/
#ifdef __cplusplus#pragma message(__FILE__": Compiled as C++ for testing purpose.")#endif // __cplusplus
#define WIN32_LEAN_AND_MEAN#include <windows.h>#if !defined(__REACTOS__) || defined(_MSC_VER)#pragma warning(push,3)#endif#include <commdlg.h>#if !defined(__REACTOS__) || defined(_MSC_VER)#pragma warning(pop)#endif
#include "vfdtypes.h"#include "vfdapi.h"#include "vfdlib.h"#ifndef __REACTOS__#include "vfdmsg.h"#else#include "vfdmsg_lib.h"#endif#include "vfdguirc.h"
//// local functions//#ifndef __REACTOS__static INT CALLBACK SaveDialogProc(#elsestatic INT_PTR CALLBACK SaveDialogProc(#endif HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
static void OnInit(HWND hDlg, PCSAVE_PARAM pParam);static void OnTarget(HWND hDlg, HWND hEdit);static void OnBrowse(HWND hDlg);static void OnOverwrite(HWND hDlg, HWND hCheck);static void OnTruncate(HWND hDlg, HWND hCheck);static DWORD OnOK(HWND hDlg);
//// Show Save Image dialog box//DWORD WINAPI VfdGuiSave( HWND hParent, ULONG nDevice){ SAVE_PARAM param; CHAR path[MAX_PATH]; DWORD ret;
// open the source device
param.hDevice = VfdOpenDevice(nDevice);
if (param.hDevice == INVALID_HANDLE_VALUE) { return GetLastError(); }
// get current image information
param.ImageName = path;
ret = VfdGetImageInfo( param.hDevice, param.ImageName, ¶m.DiskType, ¶m.MediaType, ¶m.MediaFlags, ¶m.FileType, ¶m.ImageSize);
if (ret == ERROR_SUCCESS) {
// show dialog box
ret = GuiSaveParam(hParent, ¶m); }
// close the source device
CloseHandle(param.hDevice);
return ret;}
DWORD GuiSaveParam( HWND hParent, PCSAVE_PARAM pParam){ switch (DialogBoxParam( g_hDllModule, MAKEINTRESOURCE(IDD_SAVEDIALOG), hParent, SaveDialogProc, (LPARAM)pParam)) { case IDOK: return ERROR_SUCCESS;
case IDCANCEL: return ERROR_CANCELLED;
default: return GetLastError(); }}
//// The dialog procedure//#ifndef __REACTOS__INT CALLBACK SaveDialogProc(#elseINT_PTR CALLBACK SaveDialogProc(#endif HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam){ switch (uMsg) { case WM_INITDIALOG: OnInit(hDlg, (PCSAVE_PARAM)lParam); return TRUE;
case WM_COMMAND: switch (wParam) { case MAKELONG(IDC_TARGETFILE, EN_CHANGE): OnTarget(hDlg, (HWND)lParam); return TRUE;
case IDC_BROWSE: OnBrowse(hDlg); return TRUE;
case IDC_OVERWRITE: OnOverwrite(hDlg, (HWND)lParam); return TRUE;
case IDC_TRUNCATE: OnTruncate(hDlg, (HWND)lParam); return TRUE;
case IDOK: if (OnOK(hDlg) == ERROR_SUCCESS) { EndDialog(hDlg, IDOK); } return TRUE;
case IDCANCEL: EndDialog(hDlg, IDCANCEL); return TRUE; } break;
case WM_CONTEXTMENU: ShowContextMenu(hDlg, (HWND)wParam, lParam); break;
case WM_HELP: { LPHELPINFO info = (LPHELPINFO)lParam;
if (info->iContextType == HELPINFO_WINDOW) { ShowHelpWindow(hDlg, info->iCtrlId); } } return TRUE; }
return FALSE;}
//// Initialize the dialog//void OnInit( HWND hDlg, PCSAVE_PARAM pParam){ // Store parameters
#ifndef __REACTOS__ SetWindowLong(hDlg, GWL_USERDATA, (ULONG)pParam);#else SetWindowLongPtr(hDlg, GWLP_USERDATA, (ULONG_PTR)pParam);#endif
// clear the target existence flag
SetWindowLong(hDlg, DWL_USER, 0);
// Set dialog window title
SetControlText(hDlg, 0, MSG_SAVE_TITLE);
// Set control captions
SetControlText(hDlg, IDC_IMAGEFILE_LABEL, MSG_IMAGEFILE_LABEL); SetControlText(hDlg, IDC_DISKTYPE_LABEL, MSG_DISKTYPE_LABEL); SetControlText(hDlg, IDC_MEDIATYPE_LABEL, MSG_MEDIATYPE_LABEL); SetControlText(hDlg, IDC_TARGETFILE_LABEL, MSG_TARGETFILE_LABEL); SetControlText(hDlg, IDC_IMAGEDESC_LABEL, MSG_DESCRIPTION_LABEL); SetControlText(hDlg, IDC_BROWSE, MSG_BROWSE_BUTTON); SetControlText(hDlg, IDC_OVERWRITE, MSG_OVERWRITE_CHECK); SetControlText(hDlg, IDC_TRUNCATE, MSG_TRUNCATE_CHECK); SetControlText(hDlg, IDOK, MSG_SAVE_BUTTON); SetControlText(hDlg, IDCANCEL, MSG_CANCEL_BUTTON);
// set disk type
if (pParam->DiskType == VFD_DISKTYPE_FILE) { SetDlgItemText(hDlg, IDC_DISKTYPE, "FILE"); } else { SetDlgItemText(hDlg, IDC_DISKTYPE, "RAM"); }
// display media type
SetDlgItemText(hDlg, IDC_MEDIATYPE, VfdMediaTypeName(pParam->MediaType));
// set current image and initial target
if (pParam->ImageName[0]) { SetDlgItemText(hDlg, IDC_IMAGEFILE, pParam->ImageName); SetDlgItemText(hDlg, IDC_TARGETFILE, pParam->ImageName); } else if (pParam->DiskType != VFD_DISKTYPE_FILE) { SetDlgItemText(hDlg, IDC_IMAGEFILE, "<RAM>"); OnTarget(hDlg, GetDlgItem(hDlg, IDC_TARGETFILE)); }}
//// Path is changed -- check specified target file//void OnTarget( HWND hDlg, HWND hEdit){ PCSAVE_PARAM param; CHAR buf[MAX_PATH]; ULONG file_size; VFD_FILETYPE file_type; DWORD file_attr; DWORD ret;
// clear the target existence flag
SetWindowLong(hDlg, DWL_USER, 0);
// clear the description and hint text
SetDlgItemText(hDlg, IDC_IMAGEFILE_DESC, NULL); SetDlgItemText(hDlg, IDC_IMAGEFILE_HINT, NULL);
// get the target file name
if (GetWindowText(hEdit, buf, sizeof(buf)) == 0) {
// target file is blank
EnableWindow(GetDlgItem(hDlg, IDC_OVERWRITE), FALSE); EnableWindow(GetDlgItem(hDlg, IDC_TRUNCATE), FALSE); EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);
return; } else { CHAR full[MAX_PATH]; PSTR file;
// convert into a full path
if (GetFullPathName(buf, sizeof(full), full, &file)) { strcpy(buf, full); } }
// // get the current image info //#ifndef __REACTOS__ param = (PCSAVE_PARAM)GetWindowLong(hDlg, GWL_USERDATA);#else param = (PCSAVE_PARAM)GetWindowLongPtr(hDlg, GWLP_USERDATA);#endif
if (_stricmp(param->ImageName, buf) == 0) {
// target is the current file
char desc[MAX_PATH];
VfdMakeFileDesc(desc, sizeof(desc), param->FileType, param->ImageSize, GetFileAttributes(param->ImageName));
SetDlgItemText(hDlg, IDC_IMAGEFILE_DESC, desc); SetControlText(hDlg, IDC_IMAGEFILE_HINT, MSG_CURRENT_FILE);
if (param->DiskType == VFD_DISKTYPE_FILE) {
// cannot overwrite the current FILE disk image
EnableWindow(GetDlgItem(hDlg, IDC_OVERWRITE), FALSE); EnableWindow(GetDlgItem(hDlg, IDC_TRUNCATE), FALSE); EnableWindow(GetDlgItem(hDlg, IDOK), FALSE); return; } }
// // check target image file // ret = VfdCheckImageFile( buf, &file_attr, &file_type, &file_size);
if (ret == ERROR_FILE_NOT_FOUND) { // file does not exist SetControlText(hDlg, IDC_IMAGEFILE_DESC, MSG_DESC_NEW_FILE); EnableWindow(GetDlgItem(hDlg, IDC_OVERWRITE), FALSE); EnableWindow(GetDlgItem(hDlg, IDC_TRUNCATE), FALSE); EnableWindow(GetDlgItem(hDlg, IDOK), TRUE); return; } else if (ret != ERROR_SUCCESS) { SetDlgItemText(hDlg, IDC_IMAGEFILE_DESC, SystemMessage(ret)); EnableWindow(GetDlgItem(hDlg, IDC_OVERWRITE), FALSE); EnableWindow(GetDlgItem(hDlg, IDC_TRUNCATE), FALSE); EnableWindow(GetDlgItem(hDlg, IDOK), FALSE); return; }
// set target file description
VfdMakeFileDesc(buf, sizeof(buf), file_type, file_size, file_attr);
SetDlgItemText(hDlg, IDC_IMAGEFILE_DESC, buf);
// check target file type
if (file_type == VFD_FILETYPE_ZIP) { SetControlText(hDlg, IDC_IMAGEFILE_HINT, MSG_TARGET_IS_ZIP); EnableWindow(GetDlgItem(hDlg, IDC_OVERWRITE), FALSE); EnableWindow(GetDlgItem(hDlg, IDC_TRUNCATE), FALSE); EnableWindow(GetDlgItem(hDlg, IDOK), FALSE); return; }
// the target is an existing raw file
EnableWindow(GetDlgItem(hDlg, IDC_OVERWRITE), TRUE);
// set truncate box
if (file_size > VfdGetMediaSize(param->MediaType)) { EnableWindow(GetDlgItem(hDlg, IDC_TRUNCATE), TRUE); SetControlText(hDlg, IDC_IMAGEFILE_HINT, MSG_SIZE_MISMATCH); } else { EnableWindow(GetDlgItem(hDlg, IDC_TRUNCATE), FALSE); }
// check overwrite setting
if (IsDlgButtonChecked(hDlg, IDC_OVERWRITE) != BST_CHECKED) { EnableWindow(GetDlgItem(hDlg, IDOK), FALSE); } else { EnableWindow(GetDlgItem(hDlg, IDOK), TRUE); }
// target file exists and overwritable
SetWindowLong(hDlg, DWL_USER, 1);}
//// Show save file dialog box//void OnBrowse( HWND hDlg){ OPENFILENAME ofn; PSTR title; CHAR file[MAX_PATH]; CHAR dir[MAX_PATH]; DWORD len;
title = ModuleMessage(MSG_SAVE_TITLE);
ZeroMemory(&ofn, sizeof(ofn)); ZeroMemory(file, sizeof(file)); ZeroMemory(dir, sizeof(dir));
len = GetDlgItemText(hDlg, IDC_TARGETFILE, file, sizeof(file));
if (len && file[len - 1] == '\\') { strcpy(dir, file); ZeroMemory(file, sizeof(file)); }
// Different structure sizes must be used for NT and 2K/XP ofn.lStructSize = IS_WINDOWS_NT() ? OPENFILENAME_SIZE_VERSION_400 : sizeof(ofn);
ofn.hwndOwner = hDlg; ofn.lpstrFile = file; ofn.nMaxFile = sizeof(file); ofn.lpstrInitialDir = dir; ofn.lpstrTitle = title ? title : "Save Image"; ofn.lpstrFilter = "*.*\0*.*\0";#ifndef __REACTOS__ ofn.Flags = OFN_ENABLESIZING | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;#else ofn.Flags = OFN_EXPLORER | OFN_ENABLESIZING | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;#endif
if (GetSaveFileName(&ofn)) { SetDlgItemText(hDlg, IDC_TARGETFILE, file); SetFocus(GetDlgItem(hDlg, IDC_TARGETFILE)); }
if (title) { LocalFree(title); }}
void OnOverwrite( HWND hDlg, HWND hCheck){ if (GetWindowLong(hDlg, DWL_USER)) { // the target file exists and overwritable if (SendMessage(hCheck, BM_GETCHECK, 0, 0) != BST_CHECKED) { EnableWindow(GetDlgItem(hDlg, IDOK), FALSE); } else { EnableWindow(GetDlgItem(hDlg, IDOK), TRUE); } }}
void OnTruncate( HWND hDlg, HWND hCheck){ UNREFERENCED_PARAMETER(hDlg); UNREFERENCED_PARAMETER(hCheck);}
//// Save image//DWORD OnOK( HWND hDlg){ PCSAVE_PARAM param; CHAR path[MAX_PATH]; BOOL overwrite; BOOL truncate; DWORD ret;
#ifndef __REACTOS__ param = (PCSAVE_PARAM)GetWindowLong(hDlg, GWL_USERDATA);#else param = (PCSAVE_PARAM)GetWindowLongPtr(hDlg, GWLP_USERDATA);#endif
if (!param) { return ERROR_INVALID_FUNCTION; }
// get the target image name
if (GetDlgItemText(hDlg, IDC_TARGETFILE, path, sizeof(path)) == 0) { return ERROR_INVALID_FUNCTION; }
if (GetWindowLong(hDlg, DWL_USER)) { // the target file exists and overwritable overwrite = (IsDlgButtonChecked(hDlg, IDC_OVERWRITE) == BST_CHECKED); truncate = (IsDlgButtonChecked(hDlg, IDC_TRUNCATE) == BST_CHECKED); } else { overwrite = FALSE; truncate = TRUE; }
retry: ret = VfdDismountVolume(param->hDevice, FALSE);
if (ret == ERROR_ACCESS_DENIED) { PSTR msg = ModuleMessage(MSG_UNMOUNT_CONFIRM);
int reply = MessageBox( hDlg, msg ? msg : "retry", VFD_MSGBOX_TITLE, MB_ICONEXCLAMATION | MB_CANCELTRYCONTINUE);
if (msg) { LocalFree(msg); }
if (reply == IDRETRY) { goto retry; } else if (reply == IDCANCEL) { return ERROR_CANCELLED; } else { VfdDismountVolume(param->hDevice, TRUE); } } else if (ret != ERROR_SUCCESS) {
MessageBox(hDlg, SystemMessage(ret), VFD_MSGBOX_TITLE, MB_ICONSTOP);
return ret; }
ret = VfdSaveImage(param->hDevice, path, overwrite, truncate);
if (ret != ERROR_SUCCESS) {
// show error message
MessageBox(hDlg, SystemMessage(ret), VFD_MSGBOX_TITLE, MB_ICONSTOP); }
return ret;}