2019-06-01 05:38:49 -05:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import "log"
|
|
|
|
import "time"
|
|
|
|
import "regexp"
|
|
|
|
|
|
|
|
import "github.com/andlabs/ui"
|
|
|
|
import _ "github.com/andlabs/ui/winmanifest"
|
|
|
|
|
2019-06-03 02:20:28 -05:00
|
|
|
const Xaxis = 0 // box that is horizontal
|
|
|
|
const Yaxis = 1 // box that is vertical
|
2019-06-02 13:40:44 -05:00
|
|
|
|
2019-06-01 05:38:49 -05:00
|
|
|
func GuiInit() {
|
2019-06-03 00:22:04 -05:00
|
|
|
Data.buttonMap = make(map[*ui.Button]*GuiButton)
|
2019-06-03 17:45:40 -05:00
|
|
|
Data.WindowMap = make(map[string]*GuiWindow)
|
|
|
|
|
2019-06-01 05:38:49 -05:00
|
|
|
ui.OnShouldQuit(func() bool {
|
|
|
|
ui.Quit()
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-03 16:17:29 -05:00
|
|
|
func InitGuiWindow(name string, gw *GuiWindow) *GuiWindow {
|
2019-06-01 16:10:12 -05:00
|
|
|
log.Println("InitGuiWindow() START")
|
2019-06-01 05:38:49 -05:00
|
|
|
var newGuiWindow GuiWindow
|
2019-06-02 17:49:52 -05:00
|
|
|
newGuiWindow.Width = Config.Width
|
|
|
|
newGuiWindow.Height = Config.Height
|
2019-06-03 16:17:29 -05:00
|
|
|
newGuiWindow.Name = name
|
2019-06-02 14:38:29 -05:00
|
|
|
newGuiWindow.MakeWindow = gw.MakeWindow
|
|
|
|
newGuiWindow.UiWindow = gw.UiWindow
|
|
|
|
newGuiWindow.UiTab = gw.UiTab
|
2019-06-01 16:10:12 -05:00
|
|
|
newGuiWindow.BoxMap = make(map[string]*GuiBox)
|
|
|
|
newGuiWindow.EntryMap = make(map[string]*GuiEntry)
|
2019-06-03 17:45:40 -05:00
|
|
|
Data.Windows = append(Data.Windows, &newGuiWindow)
|
|
|
|
|
|
|
|
if (Data.WindowMap == nil) {
|
|
|
|
log.Println("gui.InitGuiWindow() making the Data.WindowMap here")
|
|
|
|
Data.WindowMap = make(map[string]*GuiWindow)
|
|
|
|
}
|
|
|
|
Data.WindowMap[name] = &newGuiWindow
|
|
|
|
|
|
|
|
// make a blank entry for testing
|
|
|
|
// newGuiWindow.EntryMap["test"] = nil
|
2019-06-01 05:38:49 -05:00
|
|
|
|
2019-06-03 00:22:04 -05:00
|
|
|
if (Data.buttonMap == nil) {
|
|
|
|
GuiInit()
|
|
|
|
}
|
2019-06-01 16:10:12 -05:00
|
|
|
log.Println("InitGuiWindow() END *GuiWindow =", &newGuiWindow)
|
|
|
|
return &newGuiWindow
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-03 16:17:29 -05:00
|
|
|
func StartNewWindow(bg bool, name string, callback func(*GuiWindow) *GuiBox) {
|
2019-06-02 14:38:29 -05:00
|
|
|
log.Println("StartNewWindow() Create a new window")
|
|
|
|
var junk GuiWindow
|
|
|
|
junk.MakeWindow = callback
|
2019-06-03 16:17:29 -05:00
|
|
|
window := InitGuiWindow(name, &junk)
|
2019-06-01 05:38:49 -05:00
|
|
|
if (bg) {
|
2019-06-02 14:38:29 -05:00
|
|
|
log.Println("StartNewWindow() START NEW GOROUTINE for ui.Main()")
|
2019-06-01 05:38:49 -05:00
|
|
|
go ui.Main(func() {
|
2019-06-02 14:38:29 -05:00
|
|
|
log.Println("gui.StartNewWindow() inside ui.Main()")
|
2019-06-02 23:56:43 -05:00
|
|
|
go InitTabWindow(window)
|
2019-06-01 05:38:49 -05:00
|
|
|
})
|
2019-06-02 14:38:29 -05:00
|
|
|
time.Sleep(2000 * time.Millisecond) // this might make it more stable on windows?
|
2019-06-01 05:38:49 -05:00
|
|
|
} else {
|
2019-06-02 14:38:29 -05:00
|
|
|
log.Println("StartNewWindow() WAITING for ui.Main()")
|
2019-06-01 05:38:49 -05:00
|
|
|
ui.Main(func() {
|
2019-06-02 14:38:29 -05:00
|
|
|
log.Println("gui.StartNewWindow() inside ui.Main()")
|
2019-06-01 16:10:12 -05:00
|
|
|
InitTabWindow(window)
|
2019-06-01 05:38:49 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitTabWindow(gw *GuiWindow) {
|
2019-06-02 21:49:17 -05:00
|
|
|
log.Println("InitTabWindow() START. THIS WINDOW IS NOT YET SHOWN")
|
2019-06-01 05:38:49 -05:00
|
|
|
|
2019-06-03 16:17:29 -05:00
|
|
|
gw.UiWindow = ui.NewWindow(gw.Name, int(gw.Width), int(gw.Height), true)
|
2019-06-01 05:38:49 -05:00
|
|
|
gw.UiWindow.SetBorderless(false)
|
|
|
|
|
|
|
|
gw.UiWindow.OnClosing(func(*ui.Window) bool {
|
|
|
|
log.Println("InitTabWindow() OnClosing() THIS WINDOW IS CLOSING gw=", gw)
|
|
|
|
ui.Quit()
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
gw.UiTab = ui.NewTab()
|
|
|
|
gw.UiWindow.SetChild(gw.UiTab)
|
|
|
|
gw.UiWindow.SetMargined(true)
|
|
|
|
|
|
|
|
|
2019-06-02 21:49:17 -05:00
|
|
|
box := gw.MakeWindow(gw)
|
|
|
|
log.Println("InitTabWindow() END box =", box)
|
|
|
|
log.Println("InitTabWindow() END gw =", gw)
|
2019-06-01 05:38:49 -05:00
|
|
|
gw.UiWindow.Show()
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
// string handling examples that might be helpful for normalizeInt()
|
|
|
|
isAlpha := regexp.MustCompile(`^[A-Za-z]+$`).MatchString
|
|
|
|
|
|
|
|
for _, username := range []string{"userone", "user2", "user-three"} {
|
|
|
|
if !isAlpha(username) {
|
|
|
|
fmt.Printf("%q is not valid\n", username)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const alpha = "abcdefghijklmnopqrstuvwxyz"
|
|
|
|
|
|
|
|
func alphaOnly(s string) bool {
|
|
|
|
for _, char := range s {
|
|
|
|
if !strings.Contains(alpha, strings.ToLower(string(char))) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
func normalizeInt(s string) string {
|
|
|
|
// reg, err := regexp.Compile("[^a-zA-Z0-9]+")
|
|
|
|
reg, err := regexp.Compile("[^0-9]+")
|
|
|
|
if err != nil {
|
|
|
|
log.Println("normalizeInt() regexp.Compile() ERROR =", err)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
clean := reg.ReplaceAllString(s, "")
|
|
|
|
log.Println("normalizeInt() s =", clean)
|
|
|
|
return clean
|
|
|
|
}
|
|
|
|
|
2019-06-02 15:40:44 -05:00
|
|
|
func MessageWindow(gw *GuiWindow, msg1 string, msg2 string) {
|
2019-06-03 15:03:00 -05:00
|
|
|
log.Println("gui.MessageWindow() msg1 =", msg1)
|
|
|
|
log.Println("gui.MessageWindow() msg2 =", msg2)
|
2019-06-02 15:40:44 -05:00
|
|
|
ui.MsgBox(gw.UiWindow, msg1, msg2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ErrorWindow(gw *GuiWindow, msg1 string, msg2 string) {
|
2019-06-03 15:03:00 -05:00
|
|
|
log.Println("gui.ErrorWindow() msg1 =", msg1)
|
|
|
|
log.Println("gui.ErrorWindow() msg2 =", msg2)
|
2019-06-02 15:40:44 -05:00
|
|
|
ui.MsgBoxError(gw.UiWindow, msg1, msg2)
|
|
|
|
}
|