yvbolt/main.go
2024-06-08 13:44:18 +12:00

203 lines
5.2 KiB
Go

package main
import (
"fmt"
"os"
"strings"
"unicode/utf8"
"unsafe"
_ "github.com/ying32/govcl/pkgs/winappres" // Extra _syso files for Windows
"github.com/ying32/govcl/vcl"
"github.com/ying32/govcl/vcl/types"
)
type TMainForm struct {
*vcl.TForm
Menu *vcl.TMainMenu
Buckets *vcl.TTreeView
Tabs *vcl.TPageControl
propertiesBox *vcl.TMemo
contentBox *vcl.TListView
dbs []loadedDatabase
}
var (
mainForm *TMainForm
)
func main() {
vcl.RunApp(&mainForm)
}
func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
const MY_SPACING = 6
const MY_WIDTH = 180
f.SetCaption("yvbolt")
mnuFile := vcl.NewMenuItem(f)
mnuFile.SetCaption("File")
mnuFileOpen := vcl.NewMenuItem(mnuFile)
mnuFileOpen.SetCaption("Open...")
mnuFileOpen.SetShortCutFromString("Ctrl+O")
mnuFileOpen.SetOnClick(f.OnMnuFileOpenClick)
mnuFile.Add(mnuFileOpen)
mnuFileSqliteMemory := vcl.NewMenuItem(mnuFile)
mnuFileSqliteMemory.SetCaption("New SQLite in-memory database")
mnuFileSqliteMemory.SetOnClick(f.OnmnuFileSqliteMemoryClick)
mnuFile.Add(mnuFileSqliteMemory)
mnuSep := vcl.NewMenuItem(mnuFile)
mnuSep.SetCaption("-") // Creates separator
mnuFile.Add(mnuSep)
mnuFileExit := vcl.NewMenuItem(mnuFile)
mnuFileExit.SetCaption("Exit")
mnuFileExit.SetOnClick(f.OnMnuFileExitClick)
mnuFile.Add(mnuFileExit)
f.Menu = vcl.NewMainMenu(f)
f.Menu.Items().Add(mnuFile)
f.Buckets = vcl.NewTreeView(f)
f.Buckets.SetParent(f)
f.Buckets.SetAlign(types.AlLeft)
f.Buckets.SetWidth(MY_WIDTH)
f.Buckets.SetReadOnly(true) // prevent click to rename on nodes
f.Buckets.SetOnExpanding(f.OnNavExpanding)
f.Buckets.SetOnChange(f.OnNavChange)
hsplit := vcl.NewSplitter(f)
hsplit.SetParent(f)
hsplit.SetAlign(types.AlLeft)
hsplit.SetLeft(1) // Just needs to be further "over" than f.Buckets for auto-alignment
f.Tabs = vcl.NewPageControl(f)
f.Tabs.SetParent(f)
f.Tabs.SetAlign(types.AlClient)
propertiesTab := vcl.NewTabSheet(f.Tabs)
propertiesTab.SetParent(f.Tabs)
propertiesTab.SetCaption("Properties")
f.propertiesBox = vcl.NewMemo(propertiesTab)
f.propertiesBox.SetParent(propertiesTab)
f.propertiesBox.BorderSpacing().SetAround(MY_SPACING)
f.propertiesBox.SetAlign(types.AlClient) // fill remaining space
f.propertiesBox.SetReadOnly(true)
f.propertiesBox.SetEnabled(false)
f.propertiesBox.SetBorderStyle(types.BsNone)
f.propertiesBox.SetText("Open a database to get started...")
dataTab := vcl.NewTabSheet(f.Tabs)
dataTab.SetParent(f.Tabs)
dataTab.SetCaption("Data")
f.contentBox = vcl.NewListView(dataTab)
f.contentBox.SetParent(dataTab)
f.contentBox.BorderSpacing().SetAround(MY_SPACING)
f.contentBox.SetAlign(types.AlClient) // fill remaining space
f.contentBox.SetViewStyle(types.VsReport) // "Report style" i.e. has columns
f.contentBox.SetAutoWidthLastColumn(true)
f.contentBox.SetReadOnly(true)
colKey := f.contentBox.Columns().Add()
colKey.SetCaption("Key")
colKey.SetWidth(MY_WIDTH)
colKey.SetAlignment(types.TaLeftJustify)
colVal := f.contentBox.Columns().Add()
colVal.SetCaption("Value")
}
func (f *TMainForm) OnMnuFileOpenClick(sender vcl.IObject) {
dlg := vcl.NewOpenDialog(f)
dlg.SetTitle("Select a database file...")
dlg.SetFilter("Bolt database|*.db|SQLite database|*.db3|All files|*.*")
ret := dlg.Execute() // Fake blocking
if ret {
f.addDatabaseFromFile(dlg.FileName())
}
}
func (f *TMainForm) OnMnuFileExitClick(sender vcl.IObject) {
os.Exit(0)
}
func (f *TMainForm) OnmnuFileSqliteMemoryClick(sender vcl.IObject) {
f.SQLite_AddFromFile(`:memory:`)
}
func (f *TMainForm) OnNavChange(sender vcl.IObject, node *vcl.TTreeNode) {
if node.Data() == nil {
vcl.ShowMessage("unexpected nil data")
return
}
ndata := (*navData)(node.Data())
ndata.ld.RenderForNav(f, ndata) // Handover to the database type's own renderer function
}
func formatUtf8(in []byte) string {
if !utf8.Valid(in) {
return fmt.Sprintf("<<Invalid UTF-8 %q>>", in)
}
return string(in)
}
func (f *TMainForm) OnNavExpanding(sender vcl.IObject, node *vcl.TTreeNode, allowExpansion *bool) {
if node.Data() == nil {
vcl.ShowMessage("unexpected nil data")
*allowExpansion = false
return
}
ndata := (*navData)(node.Data())
if ndata.childrenLoaded {
return
}
// Find the child buckets from this point under the element
nextBucketNames, err := ndata.ld.NavChildren(ndata)
if err != nil {
vcl.ShowMessage(fmt.Sprintf("Failed to find child buckets under %q: %s", strings.Join(ndata.bucketPath, `/`), err.Error()))
*allowExpansion = false
return
}
ndata.childrenLoaded = true // don't repeat this work
if len(nextBucketNames) == 0 {
node.SetHasChildren(false)
*allowExpansion = false
} else {
// Populate LCL child nodes
for _, bucketName := range nextBucketNames {
node := f.Buckets.Items().AddChild(node, formatUtf8([]byte(bucketName)))
node.SetHasChildren(true) // dynamically populate in OnNavExpanding
navData := &navData{
ld: ndata.ld,
childrenLoaded: false, // will be loaded dynamically
bucketPath: []string{}, // empty = root
}
navData.bucketPath = append(navData.bucketPath, ndata.bucketPath...)
navData.bucketPath = append(navData.bucketPath, bucketName)
node.SetData(unsafe.Pointer(navData))
ndata.ld.Keepalive(navData)
}
*allowExpansion = true
}
}