From 45df88b6b44dfb2326b3e550b3bcccd8c888a599 Mon Sep 17 00:00:00 2001 From: oabrivard Date: Sun, 20 Aug 2023 00:13:29 +0200 Subject: [PATCH] Initial commit --- .gitignore | 2 + tipcalc/go.mod | 5 ++ tipcalc/go.sum | 2 + tipcalc/tipcalc.go | 122 +++++++++++++++++++++++++ tipcalc/tipcalcwin.glade | 188 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 319 insertions(+) create mode 100644 tipcalc/go.mod create mode 100644 tipcalc/go.sum create mode 100644 tipcalc/tipcalc.go create mode 100644 tipcalc/tipcalcwin.glade diff --git a/.gitignore b/.gitignore index adf8f72..e620e5a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.DS_Store + # ---> Go # If you prefer the allow list template instead of the deny list, see community template: # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore diff --git a/tipcalc/go.mod b/tipcalc/go.mod new file mode 100644 index 0000000..916646a --- /dev/null +++ b/tipcalc/go.mod @@ -0,0 +1,5 @@ +module gitea.paas.celticinfo.fr/oabrivard/efp-go/tipcalc + +go 1.20 + +require github.com/gotk3/gotk3 v0.6.2 diff --git a/tipcalc/go.sum b/tipcalc/go.sum new file mode 100644 index 0000000..25e468d --- /dev/null +++ b/tipcalc/go.sum @@ -0,0 +1,2 @@ +github.com/gotk3/gotk3 v0.6.2 h1:sx/PjaKfKULJPTPq8p2kn2ZbcNFxpOJqi4VLzMbEOO8= +github.com/gotk3/gotk3 v0.6.2/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q= diff --git a/tipcalc/tipcalc.go b/tipcalc/tipcalc.go new file mode 100644 index 0000000..0109594 --- /dev/null +++ b/tipcalc/tipcalc.go @@ -0,0 +1,122 @@ +package main + +import ( + "flag" + "fmt" + "log" + "math" + "strconv" + + "github.com/gotk3/gotk3/glib" + "github.com/gotk3/gotk3/gtk" +) + +const UIMain = "tip_calc_window.glade" + +func graphicalUI() { + // Create Gtk Application, change appID to your application domain name reversed. + const appID = "fr.abrivard.tip_calc" + application, err := gtk.ApplicationNew(appID, glib.APPLICATION_FLAGS_NONE) + // Check to make sure no errors when creating Gtk Application + if err != nil { + log.Fatal("Could not create application.", err) + } + + application.Connect("activate", func() { + builder, err := gtk.BuilderNew() + if err != nil { + log.Fatalln("Couldn't make builder:", err) + } + + err = builder.AddFromFile(UIMain) + if err != nil { + log.Fatalln("Couldn't add UI XML to builder:", err) + } + + var obj glib.IObject + + obj, _ = builder.GetObject("TipScale") + scale := obj.(*gtk.Scale) + scaleAdj := scale.GetAdjustment() + + obj, _ = builder.GetObject("TipEntry") + tipEntry := obj.(*gtk.Entry) + + _ = glib.BindProperty(scaleAdj.Object, "value", tipEntry.Object, "text", glib.BINDING_DEFAULT|glib.BINDING_BIDIRECTIONAL|glib.BINDING_SYNC_CREATE) + + obj, _ = builder.GetObject("BillEntry") + billEntry := obj.(*gtk.Entry) + + obj, _ = builder.GetObject("TipAmountLbl") + tipAmount := obj.(*gtk.Label) + + obj, _ = builder.GetObject("TotalBillLbl") + totalBill := obj.(*gtk.Label) + + obj, _ = builder.GetObject("ComputeCmd") + button := obj.(*gtk.Button) + + button.Connect("clicked", func() { + billText, _ := billEntry.GetText() + tipText, _ := tipEntry.GetText() + + if bill, err := strconv.ParseFloat(billText, 64); err == nil && bill >= 0 { + if tipPercent, err := strconv.ParseFloat(tipText, 64); err == nil && tipPercent >= 0 { + tip := math.Round(tipPercent*bill) / 100 + tipAmount.SetLabel(fmt.Sprintf("The tip is %.2f", tip)) + totalBill.SetLabel(fmt.Sprintf("The total is %.2f", bill+tip)) + return + } + } + + tipAmount.SetLabel("Please enter numbers >= 0") + totalBill.SetLabel("") + }) + + obj, _ = builder.GetObject("MainWindow") + wnd := obj.(*gtk.ApplicationWindow) + wnd.ShowAll() + application.AddWindow(wnd) + }) + // Run Gtk application + application.Run([]string{}) +} + +func readPositiveFloat() float64 { + for { + var line string + _, err := fmt.Scanln(&line) + if err != nil { + panic(err) + } + + if value, err := strconv.ParseFloat(line, 64); err == nil && value >= 0 { + return value + } + + fmt.Print("Please enter a value >= 0 : ") + } +} + +func consoleUI() { + fmt.Print("What is the bill? ") + bill := readPositiveFloat() + + fmt.Print("What is the tip percentage? ") + tipPercent := readPositiveFloat() + + tip := math.Round(tipPercent*bill) / 100 + + fmt.Printf("The tip is %.2f\nThe total is %.2f\n", tip, bill+tip) +} + +func main() { + uiPtr := flag.String("ui", "c", "c for console, g for GUI") + flag.Parse() + + if *uiPtr == "c" { + consoleUI() + } else { + graphicalUI() + } +} diff --git a/tipcalc/tipcalcwin.glade b/tipcalc/tipcalcwin.glade new file mode 100644 index 0000000..0bebdf2 --- /dev/null +++ b/tipcalc/tipcalcwin.glade @@ -0,0 +1,188 @@ + + + + + + 100 + 15 + 1 + 10 + + + False + False + + + True + False + vertical + + + True + False + + + True + False + What is the bill? + + + False + True + 5 + 0 + + + + + True + True + start + 48 + 13 + 1 + $0.00 + + + False + False + 1 + + + + + False + True + 0 + + + + + True + False + + + True + False + What is the tip? + + + False + True + 5 + 0 + + + + + True + True + 6 + 15.00 + 1 + + + False + False + 1 + + + + + True + False + % + + + False + True + 1 + 2 + + + + + True + True + tip_adjustment + False + 0 + 2 + 2 + False + False + left + + + True + True + 3 + + + + + False + True + 1 + + + + + True + False + right + + + False + True + 2 + + + + + True + False + right + + + False + True + 3 + + + + + True + False + True + + + + + + Compute Tip + True + True + True + center + + + False + True + 1 + + + + + + + + False + False + 4 + + + + + +