161 lines
4.1 KiB
Odin
161 lines
4.1 KiB
Odin
package tinyfiledialogs
|
|
|
|
import "core:c"
|
|
import "core:mem"
|
|
import "core:strings"
|
|
|
|
when ODIN_OS == .Windows {
|
|
foreign import lib {"tinyfiledialogs.lib", "system:comdlg32.lib", "system:Ole32.lib"}
|
|
} else when ODIN_OS == .Linux || ODIN_OS == .Darwin {
|
|
foreign import lib "libtinyfiledialogs.a"
|
|
}
|
|
|
|
@(default_calling_convention = "c", link_prefix = "tinyfd_")
|
|
foreign lib {
|
|
notifyPopup :: proc(title, message, icon_type: cstring) -> c.int ---
|
|
|
|
messageBox :: proc(title, message, dialog_type, icon_type: cstring, default_button: c.int) -> c.int ---
|
|
inputBox :: proc(title, message, default_input: cstring) -> cstring ---
|
|
|
|
saveFileDialog :: proc(title, default_path: cstring, pattern_count: c.int, patterns: [^]cstring, file_desc: cstring) -> cstring ---
|
|
openFileDialog :: proc(title, default_path: cstring, pattern_count: c.int, patterns: [^]cstring, file_desc: cstring, allow_multi: c.int) -> cstring ---
|
|
|
|
selectFolderDialog :: proc(title, default_path: cstring) -> cstring ---
|
|
|
|
colorChooser :: proc(title, default_hex_rgb: cstring, default_rgb, result_rgb: [3]byte) -> cstring ---
|
|
}
|
|
|
|
select_folder_dialog :: proc(
|
|
title, default_path: string,
|
|
alloc := context.allocator,
|
|
temp_alloc := context.temp_allocator,
|
|
) -> (
|
|
path: string,
|
|
success: bool,
|
|
) {
|
|
ctitle: cstring = nil
|
|
cdefault_path: cstring = nil
|
|
err: mem.Allocator_Error
|
|
|
|
if len(title) > 0 {
|
|
ctitle, err = strings.clone_to_cstring(title, temp_alloc)
|
|
if err != nil {
|
|
return {}, false
|
|
}
|
|
}
|
|
if len(default_path) > 0 {
|
|
cdefault_path, err = strings.clone_to_cstring(default_path, temp_alloc)
|
|
if err != nil {
|
|
return {}, false
|
|
}
|
|
}
|
|
res := selectFolderDialog(ctitle, cdefault_path)
|
|
path, err = strings.clone_from_cstring(res, alloc)
|
|
if err != nil {
|
|
return {}, false
|
|
}
|
|
return path, true
|
|
}
|
|
|
|
save_file_dialog :: proc(
|
|
title, default_path: string,
|
|
pattern_count: i32,
|
|
patterns: []string,
|
|
file_desc: string,
|
|
alloc := context.allocator,
|
|
temp_alloc := context.temp_allocator,
|
|
) -> (
|
|
path: string,
|
|
success: bool,
|
|
) {
|
|
ctitle: cstring = nil
|
|
cdefault_path: cstring = nil
|
|
cfile_desc: cstring = nil
|
|
cpatterns: [^]cstring = nil
|
|
err: mem.Allocator_Error
|
|
|
|
if len(title) > 0 {
|
|
ctitle, err = strings.clone_to_cstring(title, temp_alloc)
|
|
if err != nil {
|
|
return {}, false
|
|
}
|
|
}
|
|
if len(default_path) > 0 {
|
|
cdefault_path, err = strings.clone_to_cstring(default_path, temp_alloc)
|
|
if err != nil {
|
|
return {}, false
|
|
}
|
|
}
|
|
if len(cfile_desc) > 0 {
|
|
cfile_desc, err = strings.clone_to_cstring(file_desc, temp_alloc)
|
|
if err != nil {
|
|
return {}, false
|
|
}
|
|
}
|
|
|
|
if pattern_count > 0 {
|
|
cpatterns = make([^]cstring, pattern_count + 1, temp_alloc)
|
|
for p, i in patterns {
|
|
cpatterns[i] = strings.clone_to_cstring(p)
|
|
}
|
|
cpatterns[pattern_count] = nil // null terminate the array
|
|
}
|
|
res := saveFileDialog(ctitle, cdefault_path, pattern_count, cpatterns, cfile_desc)
|
|
path, err = strings.clone_from_cstring(res, alloc)
|
|
if err != nil {
|
|
return {}, false
|
|
}
|
|
return path, true
|
|
}
|
|
|
|
open_file_dialog :: proc(
|
|
title, default_path: string,
|
|
pattern_count: i32,
|
|
patterns: []string,
|
|
file_desc: string,
|
|
allow_multi: i32,
|
|
alloc := context.allocator,
|
|
temp_alloc := context.temp_allocator,
|
|
) -> (
|
|
path: string,
|
|
success: bool,
|
|
) {
|
|
ctitle: cstring = nil
|
|
cdefault_path: cstring = nil
|
|
cfile_desc: cstring = nil
|
|
cpatterns: [^]cstring = nil
|
|
err: mem.Allocator_Error
|
|
|
|
if len(title) > 0 {
|
|
ctitle, err = strings.clone_to_cstring(title, temp_alloc)
|
|
if err != nil {
|
|
return {}, false
|
|
}
|
|
}
|
|
if len(default_path) > 0 {
|
|
cdefault_path, err = strings.clone_to_cstring(default_path, temp_alloc)
|
|
if err != nil {
|
|
return {}, false
|
|
}
|
|
}
|
|
if len(cfile_desc) > 0 {
|
|
cfile_desc, err = strings.clone_to_cstring(file_desc, temp_alloc)
|
|
if err != nil {
|
|
return {}, false
|
|
}
|
|
}
|
|
|
|
if pattern_count > 0 {
|
|
cpatterns = make([^]cstring, pattern_count + 1, temp_alloc)
|
|
for p, i in patterns {
|
|
cpatterns[i] = strings.clone_to_cstring(p)
|
|
}
|
|
cpatterns[pattern_count] = nil // null terminate the array
|
|
}
|
|
res := openFileDialog(ctitle, cdefault_path, pattern_count, cpatterns, cfile_desc, allow_multi)
|
|
path, err = strings.clone_from_cstring(res, alloc)
|
|
if err != nil {
|
|
return {}, false
|
|
}
|
|
return path, true
|
|
}
|