ReactOS  0.4.14-dev-358-gbef841c
vfdguiut.c
Go to the documentation of this file.
1 /*
2  vfdguiut.c
3 
4  Virtual Floppy Drive for Windows
5  Driver control library
6  open / close / format GUI utility functions
7 
8  Copyright (c) 2003-2005 Ken Kato
9 */
10 
11 #ifdef __cplusplus
12 #pragma message(__FILE__": Compiled as C++ for testing purpose.")
13 #endif // __cplusplus
14 
15 #define WIN32_LEAN_AND_MEAN
16 #include <windows.h>
17 #include <stdio.h>
18 
19 #include "vfdtypes.h"
20 #include "vfdapi.h"
21 #include "vfdlib.h"
22 #ifndef __REACTOS__
23 #include "vfdmsg.h"
24 #else
25 #include "vfdmsg_lib.h"
26 #endif
27 #include "vfdguirc.h"
28 
29 //
30 // message box constants added since Win2K
31 //
32 
33 #ifndef MB_CANCELTRYCONTINUE
34 #define MB_CANCELTRYCONTINUE 0x00000006L
35 #endif
36 
37 #ifndef IDTRYAGAIN
38 #define IDTRYAGAIN 10
39 #endif
40 
41 #ifndef IDCONTINUE
42 #define IDCONTINUE 11
43 #endif
44 
45 //
46 // local funcitons
47 //
49 {
50  ULONG comma = 1;
51  int len;
52 
53  while ((comma * 1000) < size) {
54  comma *= 1000;
55  }
56 
57  len = sprintf(buf, "%lu", size / comma);
58 
59  while (comma > 1) {
60  size %= comma;
61  comma /= 1000;
62  len += sprintf(buf + len, ",%03lu", size / comma);
63  }
64 
65  return buf;
66 }
67 
69 {
70  static const char *name[3] = {
71  " KB", " MB", " GB"
72  };
73  int unit;
74  double dsize;
75 
76  if (size < 1000) {
77 #ifndef __REACTOS__
78  sprintf(buf, "%u", size);
79 #else
80  sprintf(buf, "%lu", size);
81 #endif
82  return buf;
83  }
84 
85  dsize = size;
86  dsize /= 1024;
87  unit = 0;
88 
89  while (unit < 2 && dsize >= 1000) {
90  dsize /= 1000;
91  unit++;
92  }
93 
94  if (dsize < 10) {
95  sprintf(buf, "%3.2f%s", dsize, name[unit]);
96  }
97  else if (dsize < 100) {
98  sprintf(buf, "%3.1f%s", dsize, name[unit]);
99  }
100  else if (dsize < 1000) {
101  sprintf(buf, "%3.0f%s", dsize, name[unit]);
102  }
103  else {
104  FormatSizeBytes((ULONG)dsize, buf);
105  strcat(buf, name[unit]);
106  }
107 
108  return buf;
109 }
110 
111 //
112 // Close the current image
113 //
115  HWND hParent, // parent window
116  ULONG nDevice) // device number
117 {
118  HANDLE hDevice;
120  CHAR path[MAX_PATH];
121  HCURSOR hourglass;
122  DWORD ret;
123  int reply;
124 
125  VFDTRACE(0, ("VfdGuiClose()\n"));
126 
127  hDevice = VfdOpenDevice(nDevice);
128 
129  if (hDevice == INVALID_HANDLE_VALUE) {
130  return GetLastError();
131  }
132 
133  // get current image information
134 
136  hDevice,
137  path,
138  &param.DiskType,
139  &param.MediaType,
140  &param.MediaFlags,
141  &param.FileType,
142  &param.ImageSize);
143 
144  if (ret != ERROR_SUCCESS) {
145  CloseHandle(hDevice);
146  return ret;
147  }
148 
149  param.hDevice = hDevice;
150  param.ImageName = path;
151 
152  // check if RAM image is modified
153 
154  if (param.MediaFlags & VFD_FLAG_DATA_MODIFIED) {
155  PSTR msg = ModuleMessage(MSG_MEDIA_MODIFIED);
156 
157  for (;;) {
158  reply = MessageBox(hParent, msg ? msg : "save?",
160 
161  if (reply != IDYES) {
162  break;
163  }
164 
166  break;
167  }
168  }
169 
170  if (msg) {
171  LocalFree(msg);
172  }
173 
174  if (reply == IDCANCEL) {
175  CloseHandle(hDevice);
176  return ERROR_CANCELLED;
177  }
178  }
179 
180  // close the image
181 
182  hourglass = LoadCursor(NULL, IDC_WAIT);
183 
184  for (;;) {
185 
186  // show the hourglass cursor
187 
188  HCURSOR original = SetCursor(hourglass);
189 
190  // close the current image
191 
192  ret = VfdCloseImage(hDevice, FALSE);
193 
194  // restore the original cursor
195 
196  SetCursor(original);
197 
198  if (ret != ERROR_ACCESS_DENIED) {
199  // success or errors other than access denied
200  break;
201  }
202 
203  if (IS_WINDOWS_NT()) {
204 
205  // Windows NT -- cannot force close
206  // show retry / cancel message box
207 
208  PSTR msg = ModuleMessage(MSG_UNMOUNT_FAILED);
209 
210  reply = MessageBox(
211  hParent, msg ? msg : "retry", VFD_MSGBOX_TITLE,
213 
214  if (msg) {
215  LocalFree(msg);
216  }
217  }
218  else {
219 
220  // Windows 2000 and later -- possible to force
221  // show cancel / retry / continue message box
222 
223  PSTR msg = ModuleMessage(MSG_UNMOUNT_CONFIRM);
224 
225  reply = MessageBox(
226  hParent, msg ? msg : "retry", VFD_MSGBOX_TITLE,
228 
229  if (msg) {
230  LocalFree(msg);
231  }
232 
233  if (reply == IDCONTINUE) {
234 
235  // try forced close
236 
237  ret = VfdCloseImage(hDevice, TRUE);
238  }
239  }
240 
241  if (reply == IDCANCEL) {
243  break;
244  }
245  else if (reply == IDCONTINUE) {
246 
247  // try forced close
248 
249  ret = VfdCloseImage(hDevice, TRUE);
250  break;
251  }
252  }
253 
254  CloseHandle(hDevice);
255 
256  return ret;
257 }
258 
259 //
260 // Format the current media
261 //
263  HWND hParent, // parent window
264  ULONG nDevice) // device number
265 {
266  HANDLE hDevice;
267  ULONG ret;
268  PSTR msg;
269 
270  msg = ModuleMessage(MSG_FORMAT_WARNING);
271 
273  msg ? msg : "Format?",
276 
277  if (msg) {
278  LocalFree(msg);
279  }
280 
281  if (ret == IDCANCEL) {
285 
286  return ERROR_CANCELLED;
287  }
288 
289  hDevice = VfdOpenDevice(nDevice);
290 
291  if (hDevice == INVALID_HANDLE_VALUE) {
292  ret = GetLastError();
293  }
294  else {
295  HCURSOR original;
296 
297 retry:
298  original = SetCursor(LoadCursor(NULL, IDC_WAIT));
299 
300  ret = VfdDismountVolume(hDevice, FALSE);
301 
302  if (ret == ERROR_ACCESS_DENIED) {
303  PSTR msg;
304  int reply;
305 
306  SetCursor(original);
307 
308  msg = ModuleMessage(MSG_UNMOUNT_CONFIRM);
309 
310  reply = MessageBox(
311  hParent, msg ? msg : "retry", VFD_MSGBOX_TITLE,
313 
314  if (msg) {
315  LocalFree(msg);
316  }
317 
318  if (reply == IDRETRY) {
319  goto retry;
320  }
321  else if (reply == IDCANCEL) {
323  }
324  else {
325  VfdDismountVolume(hDevice, TRUE);
326  ret = ERROR_SUCCESS;
327  }
328  }
329 
330  if (ret == ERROR_SUCCESS) {
331  ret = VfdFormatMedia(hDevice);
332  }
333 
334  SetCursor(original);
335 
336  CloseHandle(hDevice);
337  }
338 
343 
344  return ret;
345 }
346 
347 //
348 // Set a text to a dialog control
349 //
351  HWND hWnd,
352  UINT nCtrl,
353  DWORD nMsg)
354 {
355  PSTR p = NULL;
356 
357  if (nMsg) {
358  p = ModuleMessage(nMsg);
359  }
360 
361  if (nCtrl) {
362  SetDlgItemText(hWnd, nCtrl, p);
363  }
364  else {
365  SetWindowText(hWnd, p);
366  }
367 
368  if (p) {
369  LocalFree(p);
370  }
371 }
372 
373 //
374 // Make file description text
375 //
377  PSTR pBuffer,
378  ULONG nBufSize,
379  VFD_FILETYPE nFileType,
380  ULONG nFileSize,
381  DWORD nFileAttr)
382 {
383  PSTR type_str;
384  PSTR size_str;
385  PSTR attr_ro;
386  PSTR attr_enc;
387  PSTR attr_cmp;
388 
389  ZeroMemory(pBuffer, nBufSize);
390 
391  switch (nFileType) {
392  case VFD_FILETYPE_RAW:
393  type_str = ModuleMessage(MSG_FILETYPE_RAW);
394  break;
395 
396  case VFD_FILETYPE_ZIP:
397  type_str = ModuleMessage(MSG_FILETYPE_ZIP);
398  break;
399 
400  default:
401  type_str = NULL;
402  }
403 
404  if (nFileSize == INVALID_FILE_SIZE) {
405  size_str = ModuleMessage(MSG_DESC_NEW_FILE);
406  }
407  else {
408  CHAR buf[20], buf2[20];
409  size_str = ModuleMessage(MSG_DESC_FILESIZE,
410  FormatSizeBytes(nFileSize, buf),
411  FormatSizeUnits(nFileSize, buf2));
412  }
413 
414  attr_ro = NULL;
415  attr_cmp = NULL;
416  attr_enc = NULL;
417 
418  if (nFileAttr != INVALID_FILE_ATTRIBUTES) {
419  if (nFileAttr & FILE_ATTRIBUTE_READONLY) {
420  attr_ro = ModuleMessage(MSG_ATTR_READONLY);
421  }
422 
423  if (nFileAttr & FILE_ATTRIBUTE_COMPRESSED) {
424  attr_cmp = ModuleMessage(MSG_ATTR_COMPRESSED);
425  }
426 
427  if (nFileAttr & FILE_ATTRIBUTE_ENCRYPTED) {
428  attr_enc = ModuleMessage(MSG_ATTR_ENCRYPTED);
429  }
430  }
431 
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 : "");
438 
439  if (type_str) {
440  LocalFree(type_str);
441  }
442  if (size_str) {
443  LocalFree(size_str);
444  }
445  if (attr_ro) {
446  LocalFree(attr_ro);
447  }
448  if (attr_cmp) {
450  }
451  if (attr_enc) {
452  LocalFree(attr_enc);
453  }
454 }
455 
457  HWND hDlg,
458  HWND hCtl,
459  LPARAM lParam)
460 {
461  POINT pt;
462  UINT id;
463  HMENU hMenu;
464 
465  pt.x = ((int)(short)LOWORD(lParam));
466  pt.y = ((int)(short)HIWORD(lParam));
467 
468  if (pt.x == -1 || pt.y == -1) {
469  RECT rc;
470 
471  GetWindowRect(hCtl, &rc);
472  pt.x = (rc.left + rc.right) / 2;
473  pt.y = (rc.top + rc.bottom) / 2;
474 
475  id = GetDlgCtrlID(hCtl);
476  }
477  else {
478  POINT pt2 = pt;
479 
480  ScreenToClient(hDlg, &pt2);
481 
482  id = GetDlgCtrlID(
483  ChildWindowFromPoint(hDlg, pt2));
484  }
485 
486  if (id < IDC_IMAGEFILE_LABEL ||
487  id > IDC_TRUNCATE) {
488  return;
489  }
490 
491  hMenu = CreatePopupMenu();
492 
493  AppendMenu(hMenu, MF_STRING, 1, "&What's This");
494 
495  if (TrackPopupMenu(hMenu, TPM_RETURNCMD,
496  pt.x, pt.y, 0, hDlg, NULL))
497  {
498  ShowHelpWindow(hDlg, id);
499  }
500 
501  DestroyMenu(hMenu);
502 }
503 
504 //
505 // Show tool tip help
506 //
508  HWND hDlg,
509  UINT nCtl)
510 {
511  UINT msg;
512  RECT rc;
513  PSTR help;
514 
515  switch (nCtl) {
516  case IDC_IMAGEFILE_LABEL:
517  case IDC_IMAGEFILE:
518  msg = MSG_HELP_IMAGEFILE;
519  break;
520  case IDC_IMAGEDESC_LABEL:
521  case IDC_IMAGEFILE_DESC:
522  msg = MSG_HELP_IMAGEDESC;
523  break;
525  case IDC_TARGETFILE:
526  msg = MSG_HELP_TARGETFILE;
527  break;
528  case IDC_DISKTYPE_LABEL:
529  case IDC_DISKTYPE:
530  case IDC_DISKTYPE_FILE:
531  case IDC_DISKTYPE_RAM:
532  msg = MSG_HELP_DISKTYPE;
533  break;
534  case IDC_MEDIATYPE_LABEL:
535  case IDC_MEDIATYPE:
536  msg = MSG_HELP_MEDIATYPE;
537  break;
538  case IDC_WRITE_PROTECTED:
539  msg = MSG_HELP_PROTECT_NOW;
540  break;
541  case IDC_OPEN_PROTECTED:
542  msg = MSG_HELP_PROTECT_OPEN;
543  break;
544  case IDC_BROWSE:
545  msg = MSG_HELP_BROWSE;
546  break;
547  case IDC_OPEN:
548  msg = MSG_HELP_OPEN;
549  break;
550  case IDC_SAVE:
551  msg = MSG_HELP_SAVE;
552  break;
553  case IDC_CLOSE:
554  msg = MSG_HELP_CLOSE;
555  break;
556  case IDC_FORMAT:
557  msg = MSG_HELP_FORMAT;
558  break;
559  case IDC_CONTROL:
560  msg = MSG_HELP_CONTROL;
561  break;
562  case IDC_OVERWRITE:
563  msg = MSG_HELP_OVERWRITE;
564  break;
565  case IDC_TRUNCATE:
566  msg = MSG_HELP_TRUNCATE;
567  break;
568  default:
569  return;
570  }
571 
572  GetWindowRect(GetDlgItem(hDlg, nCtl), &rc);
573 
575 
576  if (help) {
577  VfdToolTip(hDlg, help, rc.left, (rc.top + rc.bottom) / 2, TRUE);
578  LocalFree(help);
579  }
580 }
581 
PSTR ModuleMessage(DWORD nFormat,...)
Definition: vfdlib.c:124
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:406
#define IS_WINDOWS_NT()
Definition: vfdcmd.c:159
#define ERROR_SUCCESS
Definition: deptool.c:10
#define VFD_MSGBOX_TITLE
Definition: vfdlib.h:53
#define IDYES
Definition: winuser.h:829
char * strcat(char *DstString, const char *SrcString)
Definition: utclib.c:568
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
#define IDC_DISKTYPE_LABEL
Definition: vfdguirc.h:20
#define pt(x, y)
Definition: drawing.c:79
GLsizei const GLchar ** path
Definition: glext.h:7234
#define IDC_BROWSE
Definition: resource.h:21
#define TPM_RETURNCMD
Definition: winuser.h:2362
#define IDC_MEDIATYPE_LABEL
Definition: vfdguirc.h:24
char CHAR
Definition: xmlstorage.h:175
#define MF_STRING
Definition: winuser.h:138
HWND hWnd
Definition: settings.c:17
LONG top
Definition: windef.h:292
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define ZeroMemory
Definition: winbase.h:1642
#define INVALID_FILE_SIZE
Definition: winbase.h:529
#define MB_ICONSTOP
Definition: winuser.h:797
LONG left
Definition: windef.h:291
HICON HCURSOR
Definition: windef.h:284
LONG right
Definition: windef.h:293
DWORD WINAPI VfdDismountVolume(HANDLE hDevice, BOOL bForce)
Definition: vfdctl.c:2629
#define IDC_SAVE
Definition: resource.h:16
void ShowContextMenu(HWND hDlg, HWND hCtl, LPARAM lParam)
Definition: vfdguiut.c:456
void ShowHelpWindow(HWND hDlg, UINT nCtl)
Definition: vfdguiut.c:507
DWORD GuiSaveParam(HWND hParent, PCSAVE_PARAM pParam)
Definition: vfdguisave.c:101
void WINAPI VfdMakeFileDesc(PSTR pBuffer, ULONG nBufSize, VFD_FILETYPE nFileType, ULONG nFileSize, DWORD nFileAttr)
Definition: vfdguiut.c:376
#define MB_ICONEXCLAMATION
Definition: winuser.h:779
#define sprintf(buf, format,...)
Definition: sprintf.c:55
DWORD WINAPI VfdGuiFormat(HWND hParent, ULONG nDevice)
Definition: vfdguiut.c:262
int WINAPI GetDlgCtrlID(_In_ HWND)
#define _snprintf
Definition: xmlstorage.h:200
#define VFD_FLAG_DATA_MODIFIED
Definition: vfdtypes.h:69
#define ERROR_ACCESS_DENIED
Definition: compat.h:87
static PSTR FormatSizeBytes(ULONG size, PSTR buf)
Definition: vfdguiut.c:48
#define IDC_IMAGEFILE
Definition: vfdguirc.h:14
#define IDC_CLOSE
Definition: resource.h:52
png_const_structrp png_const_inforp int * unit
Definition: png.h:2161
smooth NULL
Definition: ftsmooth.c:416
PVOID pBuffer
#define MB_ICONQUESTION
Definition: winuser.h:783
#define MB_YESNOCANCEL
Definition: winuser.h:812
#define IDC_WAIT
Definition: winuser.h:684
LONG_PTR LPARAM
Definition: windef.h:208
#define MB_OKCANCEL
Definition: winuser.h:798
static PSTR FormatSizeUnits(ULONG size, PSTR buf)
Definition: vfdguiut.c:68
GLsizeiptr size
Definition: glext.h:5919
#define AppendMenu
Definition: winuser.h:5632
HWND WINAPI GetDlgItem(_In_opt_ HWND, _In_ int)
#define IDC_MEDIATYPE
Definition: vfdguirc.h:25
PCSTR SystemMessage(DWORD nError)
Definition: vfdlib.c:147
GLfloat param
Definition: glext.h:5796
#define MAX_PATH
Definition: compat.h:26
#define WINAPI
Definition: msvc.h:8
DWORD WINAPI VfdFormatMedia(HANDLE hDevice)
Definition: vfdctl.c:2523
#define FILE_ATTRIBUTE_READONLY
Definition: nt_native.h:702
void WINAPI VfdToolTip(HWND hParent, PCSTR sText, int pos_x, int pos_y, BOOL stick)
Definition: vfdguitip.c:134
unsigned long DWORD
Definition: ntddk_ex.h:95
#define IDC_CONTROL
Definition: vfdguirc.h:33
#define IDC_IMAGEFILE_LABEL
Definition: vfdguirc.h:13
#define VFDTRACE(LEVEL, STRING)
Definition: vfddbg.h:72
#define INVALID_FILE_ATTRIBUTES
Definition: vfdcmd.c:23
#define SetDlgItemText
Definition: winuser.h:5750
#define IDRETRY
Definition: winuser.h:827
#define FILE_ATTRIBUTE_COMPRESSED
Definition: nt_native.h:711
#define IDC_TRUNCATE
Definition: vfdguirc.h:35
#define MB_ICONINFORMATION
Definition: winuser.h:796
int ret
UCHAR VFD_FILETYPE
Definition: vfdtypes.h:62
#define IDC_TARGETFILE_LABEL
Definition: vfdguirc.h:18
#define IDCONTINUE
Definition: vfdguiut.c:42
const DOCKBAR PVOID HWND hParent
Definition: tooldock.h:22
#define IDC_OPEN_PROTECTED
Definition: vfdguirc.h:27
DWORD WINAPI VfdGuiClose(HWND hParent, ULONG nDevice)
Definition: vfdguiut.c:114
VOID VfdCloseImage(IN PDEVICE_EXTENSION DeviceExtension)
Definition: vfdimg.c:435
GLenum GLsizei len
Definition: glext.h:6722
#define MessageBox
Definition: winuser.h:5723
HANDLE WINAPI VfdOpenDevice(ULONG nTarget)
Definition: vfdctl.c:1215
#define IDC_DISKTYPE_FILE
Definition: vfdguirc.h:22
#define IDC_IMAGEFILE_DESC
Definition: vfdguirc.h:16
#define IDC_DISKTYPE
Definition: vfdguirc.h:21
#define LoadCursor
Definition: winuser.h:5713
DWORD WINAPI VfdGetImageInfo(HANDLE hDevice, PSTR sFileName, PVFD_DISKTYPE pDiskType, PVFD_MEDIA pMediaType, PVFD_FLAGS pMediaFlags, PVFD_FILETYPE pFileType, PULONG pImageSize)
Definition: vfdctl.c:1839
BOOL WINAPI DestroyMenu(_In_ HMENU)
#define IDC_OPEN
Definition: resource.h:18
#define FILE_ATTRIBUTE_ENCRYPTED
Definition: ntifs_ex.h:385
#define ERROR_CANCELLED
Definition: winerror.h:726
#define IDC_WRITE_PROTECTED
Definition: vfdguirc.h:26
HCURSOR WINAPI SetCursor(_In_opt_ HCURSOR)
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
Definition: services.c:325
signed char * PSTR
Definition: retypes.h:7
#define SetWindowText
Definition: winuser.h:5758
unsigned int UINT
Definition: ndis.h:50
#define IDC_TARGETFILE
Definition: vfdguirc.h:19
#define MB_CANCELTRYCONTINUE
Definition: vfdguiut.c:34
HMENU WINAPI CreatePopupMenu(void)
Definition: menu.c:846
#define msg(x)
Definition: auth_time.c:54
#define IDC_OVERWRITE
Definition: vfdguirc.h:34
Definition: name.c:36
HWND WINAPI ChildWindowFromPoint(_In_ HWND, _In_ POINT)
#define HIWORD(l)
Definition: typedefs.h:246
unsigned int ULONG
Definition: retypes.h:1
GLenum GLuint id
Definition: glext.h:5579
#define MB_RETRYCANCEL
Definition: winuser.h:799
LONG bottom
Definition: windef.h:294
BOOL WINAPI GetWindowRect(_In_ HWND, _Out_ LPRECT)
#define IDC_DISKTYPE_RAM
Definition: vfdguirc.h:23
GLfloat GLfloat p
Definition: glext.h:8902
#define IDCANCEL
Definition: winuser.h:825
BOOL WINAPI ScreenToClient(_In_ HWND, _Inout_ LPPOINT)
#define IDC_IMAGEDESC_LABEL
Definition: vfdguirc.h:15
int attr_cmp(struct attr_cache_entry *lhs, struct attr_cache_entry *rhs)
Definition: name_cache.c:113
static const CLSID *static CLSID *static const GUID VARIANT VARIANT *static IServiceProvider DWORD *static HMENU
Definition: ordinal.c:60
int help
Definition: sort.c:20
LPARAM lParam
Definition: combotst.c:139
BOOL WINAPI TrackPopupMenu(_In_ HMENU, _In_ UINT, _In_ int, _In_ int, _Reserved_ int, _In_ HWND, _Reserved_ LPCRECT)
#define LOWORD(l)
Definition: pedump.c:82
void SetControlText(HWND hWnd, UINT nCtrl, DWORD nMsg)
Definition: vfdguiut.c:350
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
#define IDC_FORMAT
Definition: vfdguirc.h:32