slope/dialogs.go

253 lines
8.0 KiB
Go
Raw Normal View History

// +build dialog all
package main
import (
"fmt"
"runtime"
"time"
"github.com/gen2brain/dlgs"
)
const dialogIsOn = true
var dialogLib = vars{
"gui-choose-file": func(a ...expression) expression {
if len(a) < 1 {
return exception("'gui-choose-file' expected a title string, too few arguments were given")
}
title := String(a[0], false)
filter := ""
if len(a) > 1 {
filter = String(a[1], false)
}
dir := false
if len(a) > 2 {
dir = AnythingToBool(a[2]).(bool)
}
str, b, err := dlgs.File(title, filter, dir)
if err != nil {
return makeDialogException("gui-choose-file", err)
}
if b {
return str
}
return false
},
"gui-choose-files": func(a ...expression) expression {
if len(a) < 1 {
return exception("'gui-choose-files' expected a title string, too few arguments were given")
}
title := String(a[0], false)
filter := ""
if len(a) > 1 {
filter = String(a[1], false)
}
strs, b, err := dlgs.FileMulti(title, filter)
if err != nil {
return makeDialogException("gui-choose-files", err)
}
if b {
return StringSliceToExpressionSlice(strs)
}
return false
},
"gui-choose-color": func(a ...expression) expression {
if len(a) < 1 {
return exception("'gui-choose-color' expected a title string, too few arguments were given")
}
title := String(a[0], false)
defaultHex := "#FFFFFF"
if len(a) > 1 {
defaultHex = String(a[1], false)
}
clr, b, err := dlgs.Color(title, defaultHex)
if err != nil {
return makeDialogException("gui-choose-color", err)
}
if b {
r, g, b, _ := clr.RGBA()
return fmt.Sprintf("#%02x%02x%02x", r, g, b)
}
return false
},
"gui-choose-date": func(a ...expression) expression {
if len(a) < 2 {
return exception("'gui-choose-date' expected a title string and a message string, too few arguments were given")
}
title := String(a[0], false)
msg := String(a[1], false)
d, b, err := dlgs.Date(title, msg, time.Now())
if err != nil {
return makeDialogException("gui-choose-date", err)
}
if b {
return d.Format("%w %f %d %g:%I:%S %O %Z %Y")
}
return false
},
"gui-show-info": func(a ...expression) expression {
if len(a) < 2 {
return exception("'gui-show-info' expected a title string and a message string, too few arguments were given")
}
title := String(a[0], false)
msg := String(a[1], false)
_, err := dlgs.Info(title, msg)
if err != nil {
return makeDialogException("gui-show-info", err)
}
return true
},
"gui-show-warning": func(a ...expression) expression {
if len(a) < 2 {
return exception("'gui-show-warning' expected a title string and a message string, too few arguments were given")
}
title := String(a[0], false)
msg := String(a[1], false)
_, err := dlgs.Warning(title, msg)
if err != nil {
return makeDialogException("gui-show-warning", err)
}
return true
},
"gui-show-message": func(a ...expression) expression {
if len(a) < 2 {
return exception("'gui-show-message' expected a title string and a message string, too few arguments were given")
}
title := String(a[0], false)
msg := String(a[1], false)
_, err := dlgs.MessageBox(title, msg)
if err != nil {
return makeDialogException("gui-show-message", err)
}
return true
},
"gui-show-error": func(a ...expression) expression {
if len(a) < 2 {
return exception("'gui-show-error' expected a title string and a message string, too few arguments were given")
}
title := String(a[0], false)
msg := String(a[1], false)
_, err := dlgs.Error(title, msg)
if err != nil {
return makeDialogException("gui-show-error", err)
}
return true
},
"gui-get-text": func(a ...expression) expression {
if len(a) < 2 {
return exception("'gui-get-text' expected a title string and a message string, too few arguments were given")
}
title := String(a[0], false)
msg := String(a[1], false)
defaultText := ""
if len(a) > 2 {
defaultText = String(a[2], false)
}
s, b, err := dlgs.Entry(title, msg, defaultText)
if err != nil {
return makeDialogException("gui-get-text", err)
}
if b {
return s
}
return false
},
"gui-get-password": func(a ...expression) expression {
if len(a) < 2 {
return exception("'gui-get-password' expected a title string and a message string, too few arguments were given")
}
title := String(a[0], false)
msg := String(a[1], false)
s, b, err := dlgs.Password(title, msg)
if err != nil {
return makeDialogException("gui-get-password", err)
}
if b {
return s
}
return false
},
"gui-get-confirm": func(a ...expression) expression {
if len(a) < 2 {
return exception("'gui-get-confirm' expected a title string and a message string, too few arguments were given")
}
title := String(a[0], false)
msg := String(a[1], false)
defaultCancel := false
if len(a) > 2 {
defaultCancel = AnythingToBool(a[2]).(bool)
}
b, err := dlgs.Question(title, msg, defaultCancel)
if err != nil {
return makeDialogException("gui-get-password", err)
}
return b
},
"gui-select": func(a ...expression) expression {
if len(a) < 3 {
return exception("'gui-select' expected a title string, a message string, and a list of options; too few arguments were given")
}
title := String(a[0], false)
msg := String(a[1], false)
exSlice, ok := a[2].([]expression)
if !ok {
return exception("'gui-select' expected a list as its third argument; a non-list value was given")
}
list := ExpressionSliceToStringSlice(exSlice)
s, b, err := dlgs.List(title, msg, list)
if err != nil {
return makeDialogException("gui-select", err)
}
if b {
return s
}
return false
},
"gui-select-multiple": func(a ...expression) expression {
if len(a) < 3 {
return exception("'gui-select-multiple' expected a title string, a message string, and a list of options; too few arguments were given")
}
title := String(a[0], false)
msg := String(a[1], false)
exSlice, ok := a[2].([]expression)
if !ok {
return exception("'gui-select-multiple' expected a list as its third argument; a non-list value was given")
}
list := ExpressionSliceToStringSlice(exSlice)
sSlice, b, err := dlgs.ListMulti(title, msg, list)
if err != nil {
return makeDialogException("gui-select-multiple", err)
}
if b {
return StringSliceToExpressionSlice(sSlice)
}
return false
},
}
var dialogUsageStrings = map[string]string{
"gui-choose-file": "(gui-choose-file [title: string] [filter: string] [[directory-only?: bool]]) => string|#f",
"gui-choose-files": "(gui-choose-files [title: string] [filter: string]) => list|#f",
"gui-choose-color": "(gui-choose-color [title: string] [[default: string]]) => string|#f\n\nWill default to #FFFFFF unless a default is given. The default should be a string formatted like: \"#FF0022\". The returned string will be in the same format",
"gui-choose-date": "(gui-choose-date [title: string] [message: string]) => string|#f\n\nThe starting date defaults to the current date. Returns a date string in the slope default format, or #f if the window is closed or cancelled",
"gui-show-info": "(gui-show-info [title: string] [message: string]) => #t",
"gui-show-warning": "(gui-show-info [title: string] [message: string]) => #t",
"gui-show-error": "(gui-show-info [title: string] [message: string]) => #t",
"gui-show-message": "(gui-show-info [title: string] [message: string]) => #t",
"gui-get-text": "(gui-get-text [title: string] [message: string] [[default: string]]) => string|#f",
"gui-get-password": "(gui-get-text [title: string] [message: string]) => string|#f",
"gui-get-confirm": "(gui-show-info [title: string] [message: string]) => bool",
"gui-select": "(gui-select [title: string] [message: string] [options: list]) => string|#f",
"gui-select-multiple": "(gui-select-multiple [title: string] [message: string] [options: list]) => list|#f",
}
func makeDialogException(procName string, e error) exception {
if e == dlgs.ErrUnsupported {
return exception("The gui-* procedures have not been implemented for " + runtime.GOOS)
}
return exception(fmt.Sprintf("'%s' not available for %s", procName, runtime.GOOS))
}