dymokomi/luce-dialogs
Source of release 0.6.1
0.6.1 / src / dialogs / windows / native.lucb
#==============================================================================================
#
# native - GetOpenFileNameW, GetSaveFileNameW and SHBrowseForFolderW
#
# DESCRIPTION:
# The common dialogs of comdlg32 and shell32's folder browser, run modally. Text
# crosses as UTF-16; the chosen path comes back in the caller's buffer and is copied
# out as UTF-8.
#
#==============================================================================================
from windows_abi import MultiByteToWideChar, WideCharToMultiByte
## OPENFILENAMEW, as comdlg32 lays it out for x64.
extern struct WinOpenFileName:
size: u32
owner: void*?
instance: void*?
filter: u16*?
custom_filter: u16*?
custom_filter_capacity: u32
filter_index: u32
file: u16*
file_capacity: u32
file_title: u16*?
file_title_capacity: u32
initial_directory: u16*?
title: u16*?
flags: u32
file_offset: u16
file_extension: u16
default_extension: u16*?
custom_data: isize
hook: void*?
template_name: u16*?
reserved_pointer: void*?
reserved: u32
flags_extra: u32
extern func GetOpenFileNameW(options: WinOpenFileName*) -> i32
extern func GetSaveFileNameW(options: WinOpenFileName*) -> i32
## `text` as a NUL-terminated UTF-16 buffer, allocated in the heap.
func win_wide(text: str) -> u16[]!:
let wide = try new u16[text.length + 1] --- in memory.heap
var count: i32 = 0
if text.length > 0:
count = MultiByteToWideChar(65001, 8, (c.str)text, (i32)text.length, wide.data, (i32)wide.length - 1)
if count == 0:
free(wide) in memory.heap
error(failed, "the dialog text could not be converted to UTF-16")
wide[(usize)count] = 0
return wide
extern func GetActiveWindow() -> void*?
func win_choose(message: str, name: str, save: bool, multiple: bool) -> interop.Owned[str]?!:
let title = try win_wide(message)
defer free(title) in memory.heap
let path = try new u16[32768] --- in memory.heap
defer free(path) in memory.heap
for index in 0..<path.length:
path[index] = 0
if save and name.length > 0:
let suggested = try win_wide(name)
defer free(suggested) in memory.heap
var index: usize = 0
while index < suggested.length and index + 1 < path.length:
path[index] = suggested[index]
index += 1
# OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR, plus FILEMUSTEXIST to
# open and OVERWRITEPROMPT to save.
# Owned by the app's window, so the panel stays in front of it and is modal to it.
var options = WinOpenFileName(size = 152, owner = GetActiveWindow(), instance = none, filter = none, custom_filter = none, custom_filter_capacity = 0, filter_index = 0,
file = path.data, file_capacity = (u32)path.length, file_title = none, file_title_capacity = 0, initial_directory = none,
title = title.data, flags = 0x80800 | 0x8 | (0x2 if save else 0x1000) | (0x200 if multiple else 0), file_offset = 0, file_extension = 0, default_extension = none,
custom_data = 0, hook = none, template_name = none, reserved_pointer = none, reserved = 0, flags_extra = 0)
let chosen = GetSaveFileNameW(&options) if save else GetOpenFileNameW(&options)
if chosen == 0:
return none
var length: usize = 0
while length < path.length and path[length] != 0:
length += 1
if length == 0:
return none
# Several files come back as the folder, then each name, NUL-separated,
# a double NUL at the end; one file as its whole path.
if not multiple or length + 1 >= path.length or path[length + 1] == 0:
let whole = try win_utf8(path[..<length])
defer free(whole) in memory.heap
return try interop.copy_text((str)whole)
var joined: collections.Buffer
defer joined.destroy()
let folder = try win_utf8(path[..<length])
defer free(folder) in memory.heap
var start = length + 1
var first = true
while start < path.length and path[start] != 0:
var end = start
while end < path.length and path[end] != 0:
end += 1
let file = try win_utf8(path[start..<end])
defer free(file) in memory.heap
if not first:
try joined.put_text("\n")
try joined.put_text((str)folder)
try joined.put_text("\\")
try joined.put_text((str)file)
first = false
start = end + 1
return try interop.copy_text(joined.text())
## BROWSEINFOW, as shell32 lays it out for x64.
extern struct WinBrowseInfo:
owner: void*?
root: void*?
display_name: u16*?
title: u16*?
flags: u32
callback: void*?
parameter: isize
image: i32
extern func SHBrowseForFolderW(info: WinBrowseInfo*) -> void*?
extern func SHGetPathFromIDListW(list: void*, path: u16*) -> i32
extern func CoTaskMemFree(memory: void*?)
extern func CoInitializeEx(reserved: void*?, mode: u32) -> i32
## Ask for a folder in shell32's browser: the new-style dialog, folders of the file
## system only, owned by the app's window.
func win_choose_folder(message: str) -> interop.Owned[str]?!:
# The new-style browser needs COM; a second initialization on this thread is harmless.
_ = CoInitializeEx(none, 0x2)
let title = try win_wide(message)
defer free(title) in memory.heap
let path = try new u16[32768] --- in memory.heap
defer free(path) in memory.heap
for index in 0..<path.length:
path[index] = 0
# BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE | BIF_EDITBOX.
var info = WinBrowseInfo(owner = GetActiveWindow(), root = none, display_name = none, title = title.data, flags = 0x1 | 0x40 | 0x10,
callback = none, parameter = 0, image = 0)
let chosen = SHBrowseForFolderW(&info) else return none
defer CoTaskMemFree(chosen)
if SHGetPathFromIDListW(chosen, path.data) == 0:
return none
var length: usize = 0
while length < path.length and path[length] != 0:
length += 1
if length == 0:
return none
let whole = try win_utf8(path[..<length])
defer free(whole) in memory.heap
return try interop.copy_text((str)whole)
## UTF-16 `wide` as UTF-8 bytes in the heap; free them there.
func win_utf8(wide: const u16[]) -> u8[]!:
let needed = WideCharToMultiByte(65001, 0, wide.data, (i32)wide.length, none, 0, none, none)
if needed <= 0:
error(failed, "the chosen path could not be converted to UTF-8")
let bytes = try new u8[(usize)needed] --- in memory.heap
if WideCharToMultiByte(65001, 0, wide.data, (i32)wide.length, bytes.data, needed, none, none) != needed:
free(bytes) in memory.heap
error(failed, "the chosen path could not be converted to UTF-8")
return bytes