func (b *Builder) InitWidget(d Widget, w walk.Window, customInit func() error) error { b.level++ defer func() { b.level-- }() var succeeded bool defer func() { if !succeeded { w.Dispose() } }() b.declWidgets = append(b.declWidgets, declWidget{d, w}) // Widget name, _, _, font, toolTipText, minSize, maxSize, stretchFactor, row, rowSpan, column, columnSpan, alwaysConsumeSpace, contextMenuItems, onKeyDown, onKeyPress, onKeyUp, onMouseDown, onMouseMove, onMouseUp, onSizeChanged := d.WidgetInfo() w.SetName(name) if name != "" { b.name2Window[name] = w } if toolTipText != "" { if widget, ok := w.(walk.Widget); ok { if err := widget.SetToolTipText(toolTipText); err != nil { return err } } } if err := w.SetMinMaxSize(minSize.toW(), maxSize.toW()); err != nil { return err } if len(contextMenuItems) > 0 { cm, err := walk.NewMenu() if err != nil { return err } b.deferBuildMenuActions(cm, contextMenuItems) w.SetContextMenu(cm) } if onKeyDown != nil { w.KeyDown().Attach(onKeyDown) } if onKeyPress != nil { w.KeyPress().Attach(onKeyPress) } if onKeyUp != nil { w.KeyUp().Attach(onKeyUp) } if onMouseDown != nil { w.MouseDown().Attach(onMouseDown) } if onMouseMove != nil { w.MouseMove().Attach(onMouseMove) } if onMouseUp != nil { w.MouseUp().Attach(onMouseUp) } if onSizeChanged != nil { w.SizeChanged().Attach(onSizeChanged) } if widget, ok := w.(walk.Widget); ok { if err := widget.SetAlwaysConsumeSpace(alwaysConsumeSpace); err != nil { return err } type SetStretchFactorer interface { SetStretchFactor(widget walk.Widget, factor int) error } if p := widget.Parent(); p != nil { if stretchFactor < 1 { stretchFactor = 1 } switch l := p.Layout().(type) { case SetStretchFactorer: if err := l.SetStretchFactor(widget, stretchFactor); err != nil { return err } case *walk.GridLayout: csf := l.ColumnStretchFactor(column) if csf < stretchFactor { csf = stretchFactor } l.SetColumnStretchFactor(column, csf) rsf := l.RowStretchFactor(row) if rsf < stretchFactor { rsf = stretchFactor } l.SetRowStretchFactor(row, rsf) if rowSpan < 1 { rowSpan = 1 } if columnSpan < 1 { columnSpan = 1 } if b.columns > 0 && row == 0 && column == 0 { if b.col+columnSpan > b.columns { b.row++ b.col = 0 } row = b.row column = b.col b.col += columnSpan } r := walk.Rectangle{column, row, columnSpan, rowSpan} if err := l.SetRange(widget, r); err != nil { return err } } } } oldParent := b.parent // Container var db *walk.DataBinder if dc, ok := d.(Container); ok { if wc, ok := w.(walk.Container); ok { dataBinder, layout, children := dc.ContainerInfo() if layout != nil { l, err := layout.Create() if err != nil { return err } if err := wc.SetLayout(l); err != nil { return err } } b.parent = wc defer func() { b.parent = oldParent }() if g, ok := layout.(Grid); ok { columns := b.columns defer func() { b.columns, b.row, b.col = columns, row, column+columnSpan }() b.columns = g.Columns b.row = 0 b.col = 0 } for _, child := range children { if err := child.Create(b); err != nil { return err } } if dataBinder.AssignTo != nil || dataBinder.DataSource != nil { if dataB, err := dataBinder.create(); err != nil { return err } else { db = dataB } } } } // Custom if customInit != nil { if err := customInit(); err != nil { return err } } b.parent = oldParent // Widget continued if font != nil { if f, err := font.Create(); err != nil { return err } else if f != nil { w.SetFont(f) } } if b.level == 1 { if err := b.initProperties(); err != nil { return err } } // Call Reset on DataBinder after customInit, so a Dialog gets a chance to first // wire up its DefaultButton to the CanSubmitChanged event of a DataBinder. if db != nil { if _, ok := d.(Container); ok { if wc, ok := w.(walk.Container); ok { b.Defer(func() error { // FIXME: Currently SetDataBinder must be called after initProperties. wc.SetDataBinder(db) if db.DataSource() == nil { return nil } return db.Reset() }) } } } if b.level == 1 { for _, f := range b.deferredFuncs { if err := f(); err != nil { return err } } } succeeded = true return nil }
func (b *Builder) InitWidget(d Widget, w walk.Widget, customInit func() error) error { b.level++ defer func() { b.level-- }() var succeeded bool defer func() { if !succeeded { w.Dispose() } }() b.declWidgets = append(b.declWidgets, declWidget{d, w}) // Widget name, _, _, font, toolTipText, minSize, maxSize, stretchFactor, row, rowSpan, column, columnSpan, contextMenuActions, onKeyDown, onMouseDown, onMouseMove, onMouseUp, onSizeChanged := d.WidgetInfo() w.SetName(name) if name != "" { b.name2Widget[name] = w } if toolTipText != "" { if err := w.SetToolTipText(toolTipText); err != nil { return err } } if err := w.SetMinMaxSize(minSize.toW(), maxSize.toW()); err != nil { return err } if len(contextMenuActions) > 0 { cm, err := walk.NewMenu() if err != nil { return err } if err := addToActionList(cm.Actions(), contextMenuActions); err != nil { return err } w.SetContextMenu(cm) } if onKeyDown != nil { w.KeyDown().Attach(onKeyDown) } if onMouseDown != nil { w.MouseDown().Attach(onMouseDown) } if onMouseMove != nil { w.MouseMove().Attach(onMouseMove) } if onMouseUp != nil { w.MouseUp().Attach(onMouseUp) } if onSizeChanged != nil { w.SizeChanged().Attach(onSizeChanged) } if p := w.Parent(); p != nil { switch l := p.Layout().(type) { case *walk.BoxLayout: if stretchFactor < 1 { stretchFactor = 1 } if err := l.SetStretchFactor(w, stretchFactor); err != nil { return err } case *walk.GridLayout: cs := columnSpan if cs < 1 { cs = 1 } rs := rowSpan if rs < 1 { rs = 1 } r := walk.Rectangle{column, row, cs, rs} if err := l.SetRange(w, r); err != nil { return err } } } oldParent := b.parent // Container var db *walk.DataBinder if dc, ok := d.(Container); ok { if wc, ok := w.(walk.Container); ok { dataBinder, layout, children := dc.ContainerInfo() if layout != nil { l, err := layout.Create() if err != nil { return err } if err := wc.SetLayout(l); err != nil { return err } } b.parent = wc defer func() { b.parent = oldParent }() for _, child := range children { if err := child.Create(b); err != nil { return err } } var err error if db, err = dataBinder.create(); err != nil { return err } } } // Custom if customInit != nil { if err := customInit(); err != nil { return err } } b.parent = oldParent // Widget continued if font != nil { if f, err := font.Create(); err != nil { return err } else if f != nil { w.SetFont(f) } } if b.level == 1 { if err := b.initProperties(); err != nil { return err } } // Call Reset on DataBinder after customInit, so a Dialog gets a chance to first // wire up its DefaultButton to the CanSubmitChanged event of a DataBinder. if db != nil { if _, ok := d.(Container); ok { if wc, ok := w.(walk.Container); ok { // FIXME: Currently SetDataBinder must be called after initProperties. wc.SetDataBinder(db) if err := db.Reset(); err != nil { return err } } } } if b.level == 1 { for _, f := range b.deferredFuncs { if err := f(); err != nil { return err } } } succeeded = true return nil }
/*初始化主窗口*/ func StartMF() { // var mainFrame *walk.MainWindow mw := new(TaxMainWindow) // 空集合 var taxs []*modelp.TaxRecordRef // 如果库里有数据会优先展示库里的数据 taxs = defaDisplayData() model = NewTaxRecModel(taxs) // 筛选参数绑定对象 var binder *walk.DataBinder param := new(StatFilter) // 主菜单 // var mainMenu *walk.Menu if _, err := (MainWindow{ AssignTo: &mw.MainWindow, Title: TITLE, MinSize: Size{800, 600}, // 最小大小 Layout: VBox{}, DataBinder: DataBinder{ AssignTo: &binder, DataSource: param, }, Children: []Widget{ // 表格部分 TableView{ Columns: []TableViewColumn{ {Title: "序号"}, {Title: "纳税人名称"}, {Title: "纳税人识别号"}, {Title: "行业"}, {Title: "经营范围"}, {Title: "法定代表人"}, {Title: "开业日期", Format: "2006-01-02"}, {Title: "生产经营地址"}, {Title: "注册资本", Format: "%.2f", Alignment: AlignFar}, {Title: "主管税务机关"}, {Title: "是否出口企业"}, {Title: "第一年应税收入", Format: "%.2f", Alignment: AlignFar}, {Title: "第一年出口收入", Format: "%.2f", Alignment: AlignFar}, {Title: "第一年增值税", Format: "%.2f", Alignment: AlignFar}, {Title: "第一年所得税", Format: "%.2f", Alignment: AlignFar}, {Title: "第一年税额合计", Format: "%.2f", Alignment: AlignFar}, {Title: "第二年应税收入", Format: "%.2f", Alignment: AlignFar}, {Title: "第二年出口收入", Format: "%.2f", Alignment: AlignFar}, {Title: "第二年增值税", Format: "%.2f", Alignment: AlignFar}, {Title: "第二年所得税", Format: "%.2f", Alignment: AlignFar}, {Title: "第二年税额合计", Format: "%.2f", Alignment: AlignFar}, {Title: "第三年应税收入", Format: "%.2f", Alignment: AlignFar}, {Title: "第三年出口收入", Format: "%.2f", Alignment: AlignFar}, {Title: "第三年增值税", Format: "%.2f", Alignment: AlignFar}, {Title: "第三年所得税", Format: "%.2f", Alignment: AlignFar}, {Title: "第三年税额合计", Format: "%.2f", Alignment: AlignFar}, {Title: "三年缴纳税款总计", Format: "%.2f", Alignment: AlignFar}, {Title: "稽查时间", Format: "2006-01-02"}, {Title: "稽查年度"}, {Title: "是否重点税源"}, }, Model: model, }, GroupBox{ Title: "筛选数据", Layout: VBox{}, Children: []Widget{ Composite{ Layout: HBox{}, Children: []Widget{ Label{ Text: "行业:", }, ComboBox{ AssignTo: &indusComb, Value: Bind("OrgIndus"), BindingMember: "Val", DisplayMember: "Name", Model: modelp.ShowTestIndus(), }, Label{ Text: "经营范围:", }, LineEdit{ Text: Bind("OrgBusScope"), }, }, }, Composite{ Layout: HBox{}, Children: []Widget{ Label{ Text: "税收收入大于:", }, NumberEdit{ Value: Bind("StatTaxSumStart", Range{0.00, 99999999999999999.99}), Prefix: "¥ ", Decimals: 2, }, Label{ Text: "税收收入小于:", }, NumberEdit{ Value: Bind("StatTaxSumEnd", Range{0.00, 99999999999999999.99}), Prefix: "¥ ", Decimals: 2, }, }, }, Composite{ Layout: HBox{}, Children: []Widget{ CheckBox{ Checked: Bind("IsImportant"), }, Label{ Text: "是否是重点税源企业", }, CheckBox{ Checked: Bind("OrgIsExport"), }, Label{ Text: "是否是出口企业", }, PushButton{ Text: "抽查数据", OnClicked: func() { // 提交数据到绑定器 if err := binder.Submit(); err != nil { log.Print(err) return } // 执行查询并更新列表 fmt.Println("Params:", param) model.RestRows(queryData(param)) }, }, PushButton{ Text: "导入数据", OnClicked: func() { mw.openFileAction() }, }, PushButton{ Text: "刷新数据", OnClicked: func() { // 刷新行业列表 getCombs() // 清空参数 err := binder.Reset() if err != nil { fmt.Println(err) } // 刷新数据 model.RestRows(defaDisplayData()) }, }, PushButton{ Text: "清空数据", OnClicked: func() { // 清空数据然后查询展示空列表 truncateData(vatTable) model.RestRows(defaDisplayData()) walk.MsgBox(mw, "成功", "清空成功", walk.MsgBoxIconInformation) }, }, }, }, }, }, }, // 这里直接Run,否则碰到了莫名奇妙的二次才能响应的问题 }.Run()); err != nil { fmt.Println(err) } // mw.MainWindow.Run() }