func ui_main() { w := ui.NewWidget() defer w.Close() w.SetWindowTitle("Number Place") place := ui.NewWidget() place.SetMinimumSize(ui.Sz(CELL_WIDTH*10, CELL_WIDTH*10)) img := ui.NewImageWithSize(CELL_WIDTH*9, CELL_WIDTH*9) defer img.Close() imgPainter := ui.NewPainterWithImage(img) imgPainter.InitFrom(place) defer imgPainter.Close() place.OnPaintEvent(func(ev *ui.PaintEvent) { drawPlace(ev, place) }) vbox := ui.NewVBoxLayout() hbox0 := ui.NewHBoxLayout() hbox := ui.NewHBoxLayout() hboxSelNum := ui.NewHBoxLayout() hbox7 := ui.NewHBoxLayout() hbox4 := ui.NewHBoxLayout() hbox1 := ui.NewHBoxLayout() vbox.AddWidget(place) vbox.AddLayout(hboxSelNum) vbox.AddLayout(hbox7) vbox.AddLayout(hbox4) vbox.AddLayout(hbox1) vbox.AddLayout(hbox0) vbox.AddLayout(hbox) lblSelectedNum := ui.NewLabelWithText(LBL_SELECTED) hboxSelNum.AddWidget(lblSelectedNum) Btn1 := ui.NewButtonWithText("1") Btn2 := ui.NewButtonWithText("2") Btn3 := ui.NewButtonWithText("3") Btn4 := ui.NewButtonWithText("4") Btn5 := ui.NewButtonWithText("5") Btn6 := ui.NewButtonWithText("6") Btn7 := ui.NewButtonWithText("7") Btn8 := ui.NewButtonWithText("8") Btn9 := ui.NewButtonWithText("9") BtnCLR := ui.NewButtonWithText("CLR") BtnReset := ui.NewButtonWithText("Reset") exitBtn := ui.NewButtonWithText("Exit") hbox7.AddWidget(Btn7) hbox7.AddWidget(Btn8) hbox7.AddWidget(Btn9) hbox7.AddStretch(0) hbox4.AddWidget(Btn4) hbox4.AddWidget(Btn5) hbox4.AddWidget(Btn6) hbox4.AddStretch(0) hbox1.AddWidget(Btn1) hbox1.AddWidget(Btn2) hbox1.AddWidget(Btn3) hbox1.AddStretch(0) hbox.AddSpacing(10) hbox0.AddWidget(BtnCLR) hbox0.AddStretch(0) hbox.AddStretch(0) hbox.AddWidget(BtnReset) hbox.AddWidget(exitBtn) selectdNumUpdate := func() { s := "" if selectedNum > 0 { s = strconv.Itoa(selectedNum) } lblSelectedNum.SetText(LBL_SELECTED + s) } place.OnMousePressEvent(func(ev *ui.MouseEvent) { pos := ev.Pos() fmt.Println("pos x", pos.X) fmt.Println("pos y", pos.Y) var ix int = pos.X / CELL_WIDTH var iy int = pos.Y / CELL_WIDTH if ix < 9 && iy < 9 { cursorx = ix cursory = iy if placeCells[iy][ix].kind != CELL_KIND_FIXED && placeCells[iy][ix].num == 0 { placeCells[iy][ix].num = selectedNum } else { selectedNum = placeCells[iy][ix].num } placeCheck() place.Update() selectdNumUpdate() } }) numBtnFunc := func(n int) { if cursory == -1 { return } if placeCells[cursory][cursorx].kind != CELL_KIND_FIXED { placeCells[cursory][cursorx].num = n selectedNum = n placeCheck() place.Update() selectdNumUpdate() } } Btn1.OnClicked(func() { numBtnFunc(1) }) Btn2.OnClicked(func() { numBtnFunc(2) }) Btn3.OnClicked(func() { numBtnFunc(3) }) Btn4.OnClicked(func() { numBtnFunc(4) }) Btn5.OnClicked(func() { numBtnFunc(5) }) Btn6.OnClicked(func() { numBtnFunc(6) }) Btn7.OnClicked(func() { numBtnFunc(7) }) Btn8.OnClicked(func() { numBtnFunc(8) }) Btn9.OnClicked(func() { numBtnFunc(9) }) BtnCLR.OnClicked(func() { numBtnFunc(0) }) BtnReset.OnClicked(func() { selectedNum = 0 selectdNumUpdate() for iy := 0; iy < 9; iy++ { for ix := 0; ix < 9; ix++ { if placeCells[iy][ix].kind != CELL_KIND_FIXED { placeCells[iy][ix].kind = CELL_KIND_NORMAL placeCells[iy][ix].num = 0 } } } place.Update() }) exitBtn.OnClicked(func() { w.Close() }) w.SetLayout(vbox) w.Show() w.OnCloseEvent(func(e *ui.CloseEvent) { }) ui.Run() }
func main_ui() { ui.OnInsertObject(func(v interface{}) { fmt.Println("add item", v) }) ui.OnRemoveObject(func(v interface{}) { fmt.Println("remove item", v) }) w := ui.NewMainWindow() defer w.Close() go func() { dock := ui.NewDockWidgetWithTitle("Dock") dock.SetDock(ui.NewButtonWithText("Hello")) w.AddDockWidget(ui.LeftDockWidgetArea, dock) btn := ui.NewButtonWithText("HideDock") w.SetCentralWidget(btn) w.SetSize(ui.Sz(200, 200)) tb := ui.NewToolBarWithTitle("Standard") tb.AddWidget(ui.NewButtonWithText("ok")) w.AddToolBar(tb) tb.OnCloseEvent(func(e *ui.CloseEvent) { fmt.Println("tb close", e) }) sb := ui.NewStatusBar() w.SetStatusBar(sb) sb.OnCloseEvent(func(e *ui.CloseEvent) { fmt.Println("sb close", e) }) btn.OnClicked(func() { dock.Hide() runtime.GC() btn.SetText(btn.Text()) }) dock.OnCloseEvent(func(e *ui.CloseEvent) { fmt.Println(e) }) go func() { for { timer := time.NewTimer(1) select { case <-timer.C: btn.SetText(btn.Text()) btn.SetText(btn.Text()) btn.SetText(btn.Text()) fmt.Println(">", btn.Text()) if btn.Text() != "HideDock" { panic("close") } } } }() dock.OnVisibilityChanged(func(b bool) { fmt.Println(b) if !b { time.AfterFunc(1e9, func() { if dock.IsValid() { dock.Show() } }) } }) w.Show() }() ui.Run() }