Skip to content

Instantly share code, notes, and snippets.

@alex-ilin
Created May 24, 2017 21:43
Show Gist options
  • Save alex-ilin/99b7d0eb134c779c34f9a9504b71abfa to your computer and use it in GitHub Desktop.
Save alex-ilin/99b7d0eb134c779c34f9a9504b71abfa to your computer and use it in GitHub Desktop.
Use GetSaveFileName to ask user for a file name (the Save As dialog)
! Copyright (C) 2017 Alexander Ilin.
USING: accessors alien alien.c-types alien.libraries
alien.syntax classes.struct destructors kernel libc math
sequences strings windows windows.types ;
IN: ask
CONSTANT: OFN_OVERWRITEPROMPT 2
STRUCT: OPENFILENAME
{ lStructSize DWORD }
{ hwndOwner HWND }
{ hInstance HINSTANCE }
{ lpstrFilter LPCTSTR }
{ lpstrCustomFilter LPTSTR }
{ nMaxCustFilter DWORD }
{ nFilterIndex DWORD }
{ lpstrFile LPTSTR }
{ nMaxFile DWORD }
{ lpstrFileTitle LPTSTR }
{ nMaxFileTitle DWORD }
{ lpstrInitialDir LPCTSTR }
{ lpstrTitle LPCTSTR }
{ Flags DWORD }
{ nFileOffset WORD }
{ nFileExtension WORD }
{ lpstrDefExt LPCTSTR }
{ lCustData LPARAM }
{ lpfnHook PVOID }
{ lpTemplateName LPCTSTR } ;
! { pvReserved PVOID }
! { dwReserved DWORD }
! { FlagsEx DWORD } ;
TYPEDEF: OPENFILENAME* LPOPENFILENAME
<< "comdlg32" "comdlg32.dll" cdecl add-library >>
LIBRARY: comdlg32
FUNCTION: BOOL GetSaveFileNameW ( LPOPENFILENAME lpofn )
ALIAS: GetSaveFileName GetSaveFileNameW
: ask-file-name ( -- string/f )
[
OPENFILENAME [ malloc-struct &free ] [ heap-size ] bi >>lStructSize
MAX_UNICODE_PATH [
malloc &free [
B{ 0 0 0 0 } dup length memcpy ! Zero the first few bytes.
] [ >>lpstrFile ] bi
] [
! Convert byte size to available string characters.
2 /i 1 - >>nMaxFile
] bi
OFN_OVERWRITEPROMPT >>Flags
dup GetSaveFileName
[ lpstrFile>> >string ] [ drop f ] if
] with-destructors ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment