Initial commit

main
oabrivard 2 years ago
parent 5aff492692
commit 45df88b6b4

2
.gitignore vendored

@ -1,3 +1,5 @@
.DS_Store
# ---> Go # ---> Go
# If you prefer the allow list template instead of the deny list, see community template: # 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 # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore

@ -0,0 +1,5 @@
module gitea.paas.celticinfo.fr/oabrivard/efp-go/tipcalc
go 1.20
require github.com/gotk3/gotk3 v0.6.2

@ -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=

@ -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()
}
}

@ -0,0 +1,188 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkAdjustment" id="tip_adjustment">
<property name="upper">100</property>
<property name="value">15</property>
<property name="step-increment">1</property>
<property name="page-increment">10</property>
</object>
<object class="GtkApplicationWindow" id="MainWindow">
<property name="can-focus">False</property>
<property name="resizable">False</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">What is the bill?</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">5</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="BillEntry">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="halign">start</property>
<property name="margin-end">48</property>
<property name="width-chars">13</property>
<property name="xalign">1</property>
<property name="placeholder-text" translatable="yes">$0.00</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">What is the tip?</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">5</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="TipEntry">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="width-chars">6</property>
<property name="text" translatable="yes">15.00</property>
<property name="xalign">1</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">%</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">1</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkScale" id="TipScale">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="adjustment">tip_adjustment</property>
<property name="restrict-to-fill-level">False</property>
<property name="fill-level">0</property>
<property name="round-digits">2</property>
<property name="digits">2</property>
<property name="draw-value">False</property>
<property name="has-origin">False</property>
<property name="value-pos">left</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="TipAmountLbl">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="justify">right</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="TotalBillLbl">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="justify">right</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="homogeneous">True</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkButton" id="ComputeCmd">
<property name="label" translatable="yes">Compute Tip</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="halign">center</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">4</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
Loading…
Cancel
Save