NB. built from project: ~Source\api\dirbrowse\dirbrowse
NB. dirbrowse globals

script_z_ '~system\main\dll.ijs'

coclass 'jdirbrowse'


NB. =========================================================
NB. Windows desktop, virtual folder at the root of the name space.
CSIDL_DESKTOP=: 0
NB. File system directory that contains the user's program groups
NB. (which are also file system directories).
CSIDL_PROGRAMS=: 2
NB. Control Panel, virtual folder containing icons for the control panel applications.
CSIDL_CONTROLS=: 3
NB. Printers folder, virtual folder containing installed printers.
CSIDL_PRINTERS=: 4
NB. File system directory that serves as a common respository for documents.
CSIDL_PERSONAL=: 5   NB. (Documents folder)
NB. File system directory that contains the user's favorite Internet Explorer URLs.
CSIDL_FAVORITES=: 6
NB. File system directory that corresponds to the user's Startup program group.
CSIDL_STARTUP=: 7
NB. File system directory that contains the user's most recently used documents.
CSIDL_RECENT=: 8   NB. (Recent folder)
NB. File system directory that contains Send To menu items.
CSIDL_SENDTO=: 9
NB. Recycle bin, file system directory containing file objects in the user's recycle bin.
NB. The location of this directory is not in the registry; it is marked with the hidden and
NB. system attributes to prevent the user from moving or deleting it.
CSIDL_BITBUCKET=: 10
NB. File system directory containing Start menu items.
CSIDL_STARTMENU=: 11
NB. File system directory used to physically store file objects on the desktop
NB. (not to be confused with the desktop folder itself).
CSIDL_DESKTOPDIRECTORY=: 16
NB. My Computer, virtual folder containing everything on the local computer: storage
NB. devices, printers, and Control Panel. The folder may also contain mapped network drives.
CSIDL_DRIVES=: 17
NB. Network Neighborhood, virtual folder representing the top level of the network hierarchy.
CSIDL_NETWORK=: 18
NB. File system directory containing objects that appear in the network neighborhood.
CSIDL_NETHOOD=: 19
NB. Virtual folder containing fonts.
CSIDL_FONTS=: 20
NB. File system directory that serves as a common repository for document templates.
CSIDL_TEMPLATES=: 21   NB. (ShellNew folder)

NB. dirbrowse for folder
NB.
NB. main definitions:
NB.
NB. dirbrowse    defined in z
NB. cdcallback   defined in base

NB.
NB. BROWSEINFO Structure
NB. --------------------------------------------------------------------------------
NB.
NB. Contains parameters for the SHBrowseForFolder function and receives
NB. information about the folder selected by the user.
NB. ...
NB. ulFlags
NB. Flags specifying the options for the dialog box. This member can include
NB. zero or a combination of the following values
NB. ...
NB. BIF_NEWDIALOGSTYLE
NB. Version 5.0. Use the new user interface. Setting this flag provides the
NB. user with a larger dialog box that can be resized. The dialog box has
NB. several new capabilities including: drag and drop capability within the
NB. dialog box, reordering, shortcut menus, new folders, delete, and other
NB. shortcut menu commands. To use this flag, you must call OleInitialize or
NB. CoInitialize before calling SHBrowseForFolder
NB. ...
NB. #define BIF_NEWDIALOGSTYLE     0x0040   // Use the new dialog layout
NB. with the ability to resize 

NB. =========================================================
NB. some API definitions needed for BrowseForFolder
SHBrowseForFolder=: 'shell32 SHBrowseForFolderA  i *c'&cd
SHGetPathFromIDList=: 'shell32  SHGetPathFromIDListA *c i *c'&cd
SHGetSpecialFolderLocation=: 'shell32 SHGetSpecialFolderLocation i i i i'&cd
CoTaskMemFree=: 'ole32 CoTaskMemFree i i'&cd
SendMessage=: 'user32 SendMessageA i i i i *c'&cd

NB. delete trailing blanks
dtb=: #~ [: +./\. ' '&~:

NB. =========================================================
dirbrowse=: 3 : 0
NB. returns selected directory
NB. y. - Dialog title;initial dir;top level dir(see CSIDL_ variables)
NB. x. - 0 - select directories only
NB.      1 - select directories and files
0 dirbrowse y.
:

if. 0 = #y. do.
  y=. '';(jpath '~home\');0
else.
  y=. y.
end.

NB. y is character string, result is memory address of a copy of the string, null-terminated
allostring=. (] [ [ memw (, 0&,@>:@#)~) mema@>:@#

how=. x.{64+4+0 16384

'y dir top'=. 3{.y,<0
Idir=. allostring dir                               NB. initial dir
Title=. allostring y								NB. Dialog title
DisplayName=. allostring 128$' '                    NB. Buffer
hwnd=. ".wd'qhwndx'

ptr=. mema 4                                        NB. memory for pointer to LPITEMIDLIST
if. 0~:>0{SHGetSpecialFolderLocation 0;top;ptr do.  NB. check if CSIDL_ is valid
  memf each Title,ptr,DisplayName
  'Shell error!!'
  return.
end.

dir=. memr ptr,0,1,4                                 NB. get pointer to LPITEMIDLIST struct
cdcb4=: cdcb 4                                       NB. define callback with 4 parameters
NB. fill out BROWSEINFO structure
BROWSEINFO=. 2 ic hwnd,dir,DisplayName,Title,how,cdcb4,Idir,0

LPITEMIDLIST=. >{.SHBrowseForFolder <BROWSEINFO      NB. get selected folder
path=. >2{SHGetPathFromIDList LPITEMIDLIST;128$' '   NB. get path for selected folder
j=. CoTaskMemFree LPITEMIDLIST					     NB. free up memory
memf each Title,ptr,DisplayName
dtb (path i. {. a.) {. path
)

NB. =========================================================
NB. cdcallback
cdcallback=: verb define
NB. callback for BrowseForFolder
select. 1{y.
case. 1 do. 			 			 			 NB. message is BFFM_INITIALIZED
  dir=. memr (>3{y.),0,_1 			 			 NB. get initial directory
  SendMessage (>0{y.);(1024+102);1;dir 			 NB. set initial directory
case. 2 do. 						 			 NB. message is BFFM_SELCHANGED
  path=. >2{SHGetPathFromIDList (>2{y.);128$' '  NB. get path for selected item
  SendMessage (>0{y.);(1024+100);0;path			 NB. set status text
end.
0
)



dirbrowse_z_=: dirbrowse_jdirbrowse_
cdcallback_z_=: cdcallback_jdirbrowse_
