23 Dialogs
This section covers message boxes and file dialogs.
23.1 Message Boxes
procedure
(show-message-box title message [ #:type type #:window window]) → boolean? title : string? message : string? type : (or/c 'info 'warning 'error) = 'info window : (or/c window? #f) = #f
'info —
Information icon (default) 'warning —
Warning icon 'error —
Error icon
window is an optional parent window.
Returns #t on success.
(show-message-box "Welcome" "Hello, World!") (show-message-box "Oops" "Something went wrong" #:type 'error)
procedure
(show-confirm-dialog title message [ #:buttons buttons #:type type #:window window]) → (or/c 'yes 'no 'ok 'cancel #f) title : string? message : string?
buttons :
(or/c 'yes-no 'yes-no-cancel 'ok-cancel 'ok) = 'yes-no type : (or/c 'info 'warning 'error) = 'info window : (or/c window? #f) = #f
'yes-no —
Yes/No buttons, returns 'yes or 'no 'yes-no-cancel —
Yes/No/Cancel, returns 'yes, 'no, or 'cancel 'ok-cancel —
OK/Cancel, returns 'ok or 'cancel 'ok —
Single OK button, returns 'ok
Returns #f on failure.
(define result (show-confirm-dialog "Save?" "Save changes before closing?" #:buttons 'yes-no-cancel)) (case result [(yes) (save-and-close)] [(no) (close-without-saving)] [(cancel) (void)]) ; User canceled
23.2 File Dialogs
procedure
(open-file-dialog [ #:filters filters #:default-path default-path #:allow-multiple? allow-multiple? #:window window]) → (or/c string? (listof string?) #f) filters : (listof (cons/c string? string?)) = '() default-path : (or/c string? #f) = #f allow-multiple? : boolean? = #f window : (or/c window? #f) = #f
filters is a list of filter pairs: (cons name pattern). Patterns use semicolons to separate extensions (e.g., "png;jpg;gif").
default-path is the starting folder or file path.
If allow-multiple? is #t, returns a list of paths. Otherwise returns a single path string.
Returns #f if the user cancels.
;; Single file selection (define file (open-file-dialog #:filters '(("Images" . "png;jpg;gif") ("All Files" . "*")))) (when file (load-image file)) ;; Multiple file selection (define files (open-file-dialog #:filters '(("Text Files" . "txt;md")) #:allow-multiple? #t)) (when files (for ([f files]) (process-file f)))
procedure
(save-file-dialog [ #:filters filters #:default-path default-path #:window window]) → (or/c string? #f) filters : (listof (cons/c string? string?)) = '() default-path : (or/c string? #f) = #f window : (or/c window? #f) = #f
filters and default-path work the same as open-file-dialog.
Returns the selected path, or #f if the user cancels.
(define path (save-file-dialog #:filters '(("PNG Image" . "png")) #:default-path "screenshot.png")) (when path (save-screenshot path))
procedure
(open-folder-dialog [ #:default-path default-path #:allow-multiple? allow-multiple? #:window window]) → (or/c string? (listof string?) #f) default-path : (or/c string? #f) = #f allow-multiple? : boolean? = #f window : (or/c window? #f) = #f
If allow-multiple? is #t, returns a list of folder paths. Otherwise returns a single path string.
Returns #f if the user cancels.
(define folder (open-folder-dialog #:default-path "~")) (when folder (set-working-directory! folder))