func buildSwitchButtonDemo(event gwu.Event) gwu.Comp { p := gwu.NewPanel() p.SetCellPadding(1) row := gwu.NewHorizontalPanel() row.Add(gwu.NewLabel("Here's an ON/OFF switch which enables/disables the other one:")) sw := gwu.NewSwitchButton() sw.SetOnOff("ENB", "DISB") sw.SetState(true) row.Add(sw) p.Add(row) p.AddVSpace(10) row = gwu.NewHorizontalPanel() row.Add(gwu.NewLabel("And the other one:")) sw2 := gwu.NewSwitchButton() sw2.SetEnabled(true) sw2.Style().SetWidthPx(100) row.Add(sw2) sw.AddEHandlerFunc(func(e gwu.Event) { sw2.SetEnabled(sw.State()) e.MarkDirty(sw2) }, gwu.ETYPE_CLICK) p.Add(row) return p }
func buildPanelDemo(event gwu.Event) gwu.Comp { p := gwu.NewPanel() p.Add(gwu.NewLabel("Panel with horizontal layout:")) h := gwu.NewHorizontalPanel() for i := 1; i <= 5; i++ { h.Add(gwu.NewButton("Button " + strconv.Itoa(i))) } p.Add(h) p.AddVSpace(20) p.Add(gwu.NewLabel("Panel with vertical layout:")) v := gwu.NewVerticalPanel() for i := 1; i <= 5; i++ { v.Add(gwu.NewButton("Button " + strconv.Itoa(i))) } p.Add(v) p.AddVSpace(20) p.Add(gwu.NewLabel("Panel with natural layout:")) n := gwu.NewNaturalPanel() for i := 1; i <= 20; i++ { n.Add(gwu.NewButton("LONG BUTTON " + strconv.Itoa(i))) } p.Add(n) return p }
func buildButtonDemo(event gwu.Event) gwu.Comp { p := gwu.NewPanel() l := gwu.NewLabel("") btnp := gwu.NewHorizontalPanel() b := gwu.NewButton("Normal Button") b.AddEHandlerFunc(func(e gwu.Event) { switch e.Type() { case gwu.ETYPE_MOUSE_OVER: l.SetText("Mouse is over...") case gwu.ETYPE_MOUSE_OUT: l.SetText("Mouse is out.") case gwu.ETYPE_CLICK: x, y := e.Mouse() l.SetText(fmt.Sprintf("Clicked at x=%d, y=%d", x, y)) } e.MarkDirty(l) }, gwu.ETYPE_CLICK, gwu.ETYPE_MOUSE_OVER, gwu.ETYPE_MOUSE_OUT) btnp.Add(b) b = gwu.NewButton("Disabled Button") b.SetEnabled(false) btnp.Add(b) p.Add(btnp) p.Add(l) return p }
func buildLinkDemo(event gwu.Event) gwu.Comp { p := gwu.NewPanel() p.SetCellPadding(3) p.Add(gwu.NewLink("Visit Gowut Home page", "https://sites.google.com/site/gowebuitoolkit/")) p.Add(gwu.NewLink("Visit Gowut Project page", "http://code.google.com/p/gowut/")) row := gwu.NewHorizontalPanel() row.SetCellPadding(3) row.Add(gwu.NewLabel("Discussion forum:")) row.Add(gwu.NewLink("https://groups.google.com/d/forum/gowebuitoolkit", "https://groups.google.com/d/forum/gowebuitoolkit")) p.Add(row) row = gwu.NewHorizontalPanel() row.SetCellPadding(3) row.Add(gwu.NewLabel("Send e-mail to the Gowut author:")) email := "iczaaa" + "@" + "gmail.com" row.Add(gwu.NewLink("András Belicza <"+email+">", "mailto:"+email)) p.Add(row) return p }
func buildTextBoxDemo(event gwu.Event) gwu.Comp { p := gwu.NewPanel() p.Add(gwu.NewLabel("Enter your name (max 15 characters):")) row := gwu.NewHorizontalPanel() tb := gwu.NewTextBox("") tb.SetMaxLength(15) tb.AddSyncOnETypes(gwu.ETYPE_KEY_UP) length := gwu.NewLabel("") length.Style().SetFontSize("80%").SetFontStyle(gwu.FONT_STYLE_ITALIC) tb.AddEHandlerFunc(func(e gwu.Event) { rem := 15 - len(tb.Text()) length.SetText(fmt.Sprintf("(%d character%s left...)", rem, plural(rem))) e.MarkDirty(length) }, gwu.ETYPE_CHANGE, gwu.ETYPE_KEY_UP) row.Add(tb) row.Add(length) p.Add(row) p.AddVSpace(10) p.Add(gwu.NewLabel("Short biography:")) bio := gwu.NewTextBox("") bio.SetRows(5) bio.SetCols(40) p.Add(bio) p.AddVSpace(10) rtb := gwu.NewTextBox("This is just a read-only text box...") rtb.SetReadOnly(true) p.Add(rtb) p.AddVSpace(10) dtb := gwu.NewTextBox("...and a disabled one.") dtb.SetEnabled(false) p.Add(dtb) return p }
func buildListBoxDemo(event gwu.Event) gwu.Comp { p := gwu.NewPanel() row := gwu.NewHorizontalPanel() l := gwu.NewLabel("Select a background color:") row.Add(l) lb := gwu.NewListBox([]string{"", "Black", "Red", "Green", "Blue", "White"}) lb.AddEHandlerFunc(func(e gwu.Event) { l.Style().SetBackground(lb.SelectedValue()) e.MarkDirty(l) }, gwu.ETYPE_CHANGE) row.Add(lb) p.Add(row) p.AddVSpace(10) p.Add(gwu.NewLabel("Select numbers that add up to 89:")) sumLabel := gwu.NewLabel("") lb2 := gwu.NewListBox([]string{"1", "2", "4", "8", "16", "32", "64", "128"}) lb2.SetMulti(true) lb2.SetRows(10) lb2.AddEHandlerFunc(func(e gwu.Event) { sum := 0 for _, idx := range lb2.SelectedIndices() { sum += 1 << uint(idx) } if sum == 89 { sumLabel.SetText("Hooray! You did it!") } else { sumLabel.SetText(fmt.Sprintf("Now quite there... (sum = %d)", sum)) } e.MarkDirty(sumLabel) }, gwu.ETYPE_CHANGE) p.Add(lb2) p.Add(sumLabel) return p }
func startWebServer() { tables := gwu.NewHorizontalPanel() tables.SetVAlign(gwu.VA_TOP) updateTables(tables) win := gwu.NewWindow("scrolls", "ClockworkAgent") win.Style().SetFullWidth() win.SetHAlign(gwu.HA_CENTER) win.Style().SetBackground(` url(images/background.jpg) no-repeat center center fixed; background-size: cover; `) win.Add(tables) win.AddEHandlerFunc(func(e gwu.Event) { updateTables(tables) }, gwu.ETYPE_WIN_LOAD) server := gwu.NewServer("", "localhost:8080") server.AddWin(win) wd, err := os.Getwd() deny(err) server.AddStaticDir("images", wd+"/images") server.Start("scrolls") }
func buildShowcaseWin(sess gwu.Session) { win := gwu.NewWindow("show", "Showcase of Features - Gowut") win.Style().SetFullSize() win.AddEHandlerFunc(func(e gwu.Event) { switch e.Type() { case gwu.ETYPE_WIN_LOAD: fmt.Println("LOADING window:", e.Src().Id()) case gwu.ETYPE_WIN_UNLOAD: fmt.Println("UNLOADING window:", e.Src().Id()) } }, gwu.ETYPE_WIN_LOAD, gwu.ETYPE_WIN_UNLOAD) hiddenPan := gwu.NewNaturalPanel() sess.SetAttr("hiddenPan", hiddenPan) header := gwu.NewHorizontalPanel() header.Style().SetFullWidth().SetBorderBottom2(2, gwu.BRD_STYLE_SOLID, "#777777") l := gwu.NewLabel("Gowut - Showcase of Features") l.Style().SetFontWeight(gwu.FONT_WEIGHT_BOLD).SetFontSize("120%") header.Add(l) header.AddHConsumer() header.Add(gwu.NewLabel("Theme:")) themes := gwu.NewListBox([]string{"default", "debug"}) themes.AddEHandlerFunc(func(e gwu.Event) { win.SetTheme(themes.SelectedValue()) e.ReloadWin("show") }, gwu.ETYPE_CHANGE) header.Add(themes) header.AddHSpace(10) reset := gwu.NewLink("Reset", "#") reset.SetTarget("") reset.AddEHandlerFunc(func(e gwu.Event) { e.RemoveSess() e.ReloadWin("show") }, gwu.ETYPE_CLICK) header.Add(reset) setNoWrap(header) win.Add(header) content := gwu.NewHorizontalPanel() content.SetCellPadding(1) content.SetVAlign(gwu.VA_TOP) content.Style().SetFullSize() demoWrapper := gwu.NewPanel() demoWrapper.Style().SetPaddingLeftPx(5) demoWrapper.AddVSpace(10) demoTitle := gwu.NewLabel("") demoTitle.Style().SetFontWeight(gwu.FONT_WEIGHT_BOLD).SetFontSize("110%") demoWrapper.Add(demoTitle) demoWrapper.AddVSpace(10) links := gwu.NewPanel() links.SetCellPadding(1) links.Style().SetPaddingRightPx(5) demos := make(map[string]pdemo) var selDemo pdemo selectDemo := func(d pdemo, e gwu.Event) { if selDemo != nil { selDemo.link.Style().SetBackground("") if e != nil { e.MarkDirty(selDemo.link) } demoWrapper.Remove(selDemo.comp) } selDemo = d d.link.Style().SetBackground("#88ff88") demoTitle.SetText(d.link.Text()) if d.comp == nil { d.comp = d.buildFunc(e) } demoWrapper.Add(d.comp) if e != nil { e.MarkDirty(d.link, demoWrapper) } } createDemo := func(name string, buildFunc func(gwu.Event) gwu.Comp) pdemo { link := gwu.NewLabel(name) link.Style().SetFullWidth().SetCursor(gwu.CURSOR_POINTER).SetDisplay(gwu.DISPLAY_BLOCK).SetColor(gwu.CLR_BLUE) demo := &demo{link: link, buildFunc: buildFunc} link.AddEHandlerFunc(func(e gwu.Event) { selectDemo(demo, e) }, gwu.ETYPE_CLICK) links.Add(link) demos[name] = demo return demo } links.Style().SetFullHeight().SetBorderRight2(2, gwu.BRD_STYLE_SOLID, "#777777") links.AddVSpace(5) homeDemo := createDemo("Home", buildHomeDemo) selectDemo(homeDemo, nil) links.AddVSpace(5) l = gwu.NewLabel("Component Palette") l.Style().SetFontWeight(gwu.FONT_WEIGHT_BOLD).SetFontSize("110%") links.Add(l) links.AddVSpace(5) l = gwu.NewLabel("Containers") l.Style().SetFontWeight(gwu.FONT_WEIGHT_BOLD) links.Add(l) createDemo("Expander", buildExpanderDemo) createDemo("Link (as Container)", buildLinkContainerDemo) createDemo("Panel", buildPanelDemo) createDemo("Table", buildTableDemo) createDemo("TabPanel", buildTabPanelDemo) createDemo("Window", buildWindowDemo) links.AddVSpace(5) l = gwu.NewLabel("Input components") l.Style().SetFontWeight(gwu.FONT_WEIGHT_BOLD).SetDisplay(gwu.DISPLAY_BLOCK) links.Add(l) createDemo("CheckBox", buildCheckBoxDemo) createDemo("ListBox", buildListBoxDemo) createDemo("TextBox", buildTextBoxDemo) createDemo("PasswBox", buildPasswBoxDemo) createDemo("RadioButton", buildRadioButtonDemo) createDemo("SwitchButton", buildSwitchButtonDemo) links.AddVSpace(5) l = gwu.NewLabel("Other components") l.Style().SetFontWeight(gwu.FONT_WEIGHT_BOLD) links.Add(l) createDemo("Button", buildButtonDemo) createDemo("Html", buildHtmlDemo) createDemo("Image", buildImageDemo) createDemo("Label", buildLabelDemo) createDemo("Link", buildLinkDemo) createDemo("Timer", buildTimerDemo) links.AddVConsumer() setNoWrap(links) content.Add(links) content.Add(demoWrapper) content.CellFmt(demoWrapper).Style().SetFullWidth() win.Add(content) win.CellFmt(content).Style().SetFullSize() footer := gwu.NewHorizontalPanel() footer.Style().SetFullWidth().SetBorderTop2(2, gwu.BRD_STYLE_SOLID, "#777777") footer.Add(hiddenPan) footer.AddHConsumer() l = gwu.NewLabel("Copyright © 2013 András Belicza. All rights reserved.") l.Style().SetFontStyle(gwu.FONT_STYLE_ITALIC).SetFontSize("95%") footer.Add(l) footer.AddHSpace(10) link := gwu.NewLink("Visit Gowut Home page", "https://sites.google.com/site/gowebuitoolkit/") link.Style().SetFontStyle(gwu.FONT_STYLE_ITALIC).SetFontSize("95%") footer.Add(link) setNoWrap(footer) win.Add(footer) sess.AddWin(win) }
func buildTimerDemo(event gwu.Event) gwu.Comp { p := gwu.NewPanel() p.SetCellPadding(3) // Add timers to a panel which is always attached instead of our panel // because the user can switch to another component demo causing this panel to be removed // and that way timer events would address components that are not part of the window (returning error). hiddenPan := event.Session().Attr("hiddenPan").(gwu.Panel) p.Add(gwu.NewLabel("A Timer is used to detonate a bomb after 3 seconds.")) p.AddVSpace(10) defText := "You can defuse the bomb with the button below. Tick... Tack..." l := gwu.NewLabel(defText) p.Add(l) t := gwu.NewTimer(3 * time.Second) b := gwu.NewButton("Defuse!") t.AddEHandlerFunc(func(e gwu.Event) { l.SetText("BOOOOM! You were too slow!") l.Style().SetColor(gwu.CLR_RED) b.SetEnabled(false) e.MarkDirty(l, b) }, gwu.ETYPE_STATE_CHANGE) hiddenPan.Add(t) row := gwu.NewHorizontalPanel() b.AddEHandlerFunc(func(e gwu.Event) { t.SetActive(false) l.SetText("Bomb defused! Phew! Good Job!") l.Style().SetColor(gwu.CLR_GREEN) b.SetEnabled(false) e.MarkDirty(t, l, b) }, gwu.ETYPE_CLICK) row.Add(b) b2 := gwu.NewButton("Plant a new Bomb!") b2.AddEHandlerFunc(func(e gwu.Event) { t.SetActive(true) t.Reset() l.SetText(defText) l.Style().SetColor("") b.SetEnabled(true) e.MarkDirty(t, l, b) }, gwu.ETYPE_CLICK) row.Add(b2) p.Add(row) p.AddVSpace(20) p.Add(gwu.NewLabel("A Timer is used to refresh the time below repeatedly in every second for half a minute.")) tl := gwu.NewLabel("") p.Add(tl) t2 := gwu.NewTimer(time.Second) t2.SetRepeat(true) counter := 30 t2.AddEHandlerFunc(func(e gwu.Event) { counter-- tl.SetText(fmt.Sprintf("%s (%d remaining)", time.Now().Format("2006-01-02 15:04:05"), counter)) e.MarkDirty(tl) if counter <= 0 { t2.SetActive(false) e.MarkDirty(t2) } }, gwu.ETYPE_STATE_CHANGE) hiddenPan.Add(t2) b3 := gwu.NewButton("Restart") b3.AddEHandlerFunc(func(e gwu.Event) { counter = 30 t2.SetActive(true) e.MarkDirty(t2) }, gwu.ETYPE_CLICK) p.Add(b3) event.MarkDirty(hiddenPan) return p }
func main() { // Create and build a window win := gwu.NewWindow("main", "Test GUI Window") win.Style().SetFullWidth() win.SetHAlign(gwu.HA_CENTER) win.SetCellPadding(2) // Button which changes window content win.Add(gwu.NewLabel("I'm a label! Try clicking on the button=>")) btn := gwu.NewButton("Click me") btn.AddEHandler(&MyButtonHandler{text: ":-)"}, gwu.ETYPE_CLICK) win.Add(btn) btnsPanel := gwu.NewNaturalPanel() btn.AddEHandlerFunc(func(e gwu.Event) { // Create and add a new button... newbtn := gwu.NewButton("Extra #" + strconv.Itoa(btnsPanel.CompsCount())) newbtn.AddEHandlerFunc(func(e gwu.Event) { btnsPanel.Remove(newbtn) // ...which removes itself when clicked e.MarkDirty(btnsPanel) }, gwu.ETYPE_CLICK) btnsPanel.Insert(newbtn, 0) e.MarkDirty(btnsPanel) }, gwu.ETYPE_CLICK) win.Add(btnsPanel) // ListBox examples p := gwu.NewHorizontalPanel() p.Style().SetBorder2(1, gwu.BRD_STYLE_SOLID, gwu.CLR_BLACK) p.SetCellPadding(2) p.Add(gwu.NewLabel("A drop-down list being")) widelb := gwu.NewListBox([]string{"50", "100", "150", "200", "250"}) widelb.Style().SetWidth("50") widelb.AddEHandlerFunc(func(e gwu.Event) { widelb.Style().SetWidth(widelb.SelectedValue() + "px") e.MarkDirty(widelb) }, gwu.ETYPE_CHANGE) p.Add(widelb) p.Add(gwu.NewLabel("pixel wide. And a multi-select list:")) listBox := gwu.NewListBox([]string{"First", "Second", "Third", "Forth", "Fifth", "Sixth"}) listBox.SetMulti(true) listBox.SetRows(4) p.Add(listBox) countLabel := gwu.NewLabel("Selected count: 0") listBox.AddEHandlerFunc(func(e gwu.Event) { countLabel.SetText("Selected count: " + strconv.Itoa(len(listBox.SelectedIndices()))) e.MarkDirty(countLabel) }, gwu.ETYPE_CHANGE) p.Add(countLabel) win.Add(p) // Self-color changer check box greencb := gwu.NewCheckBox("I'm a check box. When checked, I'm green!") greencb.AddEHandlerFunc(func(e gwu.Event) { if greencb.State() { greencb.Style().SetBackground(gwu.CLR_GREEN) } else { greencb.Style().SetBackground("") } e.MarkDirty(greencb) }, gwu.ETYPE_CLICK) win.Add(greencb) // TextBox with echo p = gwu.NewHorizontalPanel() p.Add(gwu.NewLabel("Enter your name:")) tb := gwu.NewTextBox("") tb.AddSyncOnETypes(gwu.ETYPE_KEY_UP) p.Add(tb) p.Add(gwu.NewLabel("You entered:")) nameLabel := gwu.NewLabel("") nameLabel.Style().SetColor(gwu.CLR_RED) tb.AddEHandlerFunc(func(e gwu.Event) { nameLabel.SetText(tb.Text()) e.MarkDirty(nameLabel) }, gwu.ETYPE_CHANGE, gwu.ETYPE_KEY_UP) p.Add(nameLabel) win.Add(p) // Create and start a GUI server (omitting error check) server := gwu.NewServer("guitest", "localhost:8081") server.SetText("Test GUI App") server.AddWin(win) server.Start("") // Also opens windows list in browser }