示例#1
1
func (cb ComboBox) Create(builder *Builder) error {
	if _, ok := cb.Model.([]string); ok &&
		(cb.BindingMember != "" || cb.DisplayMember != "") {

		return errors.New("ComboBox.Create: BindingMember and DisplayMember must be empty for []string models.")
	}

	var w *walk.ComboBox
	var err error
	if cb.Editable {
		w, err = walk.NewComboBox(builder.Parent())
	} else {
		w, err = walk.NewDropDownBox(builder.Parent())
	}
	if err != nil {
		return err
	}

	return builder.InitWidget(cb, w, func() error {
		w.SetFormat(cb.Format)
		w.SetPrecision(cb.Precision)
		w.SetMaxLength(cb.MaxLength)

		if err := w.SetBindingMember(cb.BindingMember); err != nil {
			return err
		}
		if err := w.SetDisplayMember(cb.DisplayMember); err != nil {
			return err
		}

		if err := w.SetModel(cb.Model); err != nil {
			return err
		}

		if cb.OnCurrentIndexChanged != nil {
			w.CurrentIndexChanged().Attach(cb.OnCurrentIndexChanged)
		}

		if cb.AssignTo != nil {
			*cb.AssignTo = w
		}

		return nil
	})
}
示例#2
0
文件: gui.go 项目: pa001024/netdialer
func main() {
	fout, _ := os.Create("dialer.log")
	defer fout.Close()
	bo := bufio.NewWriter(fout)
	defer bo.Flush()
	util.INFO.SetOutput(bo)
	util.ERROR.SetOutput(bo)

	bin, err := ioutil.ReadFile("config.json")
	if err == nil {
		json.Unmarshal(bin, config)
	} else {
		util.ERROR.Log(err)
		err = nil
	}

	var usr, pwd *walk.LineEdit
	var raddr, rusr, rpwd *walk.LineEdit
	var lb, rb *walk.PushButton
	var mode *walk.ComboBox
	var mw *walk.MainWindow
	var db *walk.DataBinder
	var laddr, lusr, lpwd *walk.Splitter
	go func() {
		for mw == nil {
			runtime.Gosched()
		}
		ic, err := walk.NewIconFromResourceId(6)
		if err == nil {
			// func onLoad() {
			db.SetAutoSubmit(true)
			mw.SetIcon(ic)
			switch config.RouterType {
			case "hiwifi":
				laddr.SetVisible(true)
				lusr.SetVisible(false)
				lpwd.SetVisible(true)
			case "openwrt":
				laddr.SetVisible(true)
				lusr.SetVisible(true)
				lpwd.SetVisible(true)
			case "asus":
				laddr.SetVisible(true)
				lusr.SetVisible(true)
				lpwd.SetVisible(true)
			default:
				laddr.SetVisible(false)
				lusr.SetVisible(false)
				lpwd.SetVisible(false)
			}
			mw.SetSize(walk.Size{0, 0})
			// }
		}
	}()
	MainWindow{
		AssignTo: &mw,
		Title:    TITLE,
		MinSize:  Size{340, 0},
		Layout:   VBox{},
		DataBinder: DataBinder{
			AssignTo:   &db,
			DataSource: config,
		},
		Children: []Widget{
			HSplitter{
				Children: []Widget{
					Label{Text: "用户名", MaxSize: Size{60, 20}},
					LineEdit{Text: Bind("Username"), AssignTo: &usr, MaxSize: Size{0, 20}},
				}, MaxSize: Size{0, 20},
			},
			HSplitter{
				Children: []Widget{
					Label{Text: "密码", MaxSize: Size{60, 20}},
					LineEdit{Text: Bind("Password"), AssignTo: &pwd, MaxSize: Size{0, 20}, PasswordMode: true,
						OnMouseDown: func(x, y int, button walk.MouseButton) {
							pwd.SetPasswordMode(false)
						},
						OnMouseUp: func(x, y int, button walk.MouseButton) {
							pwd.SetPasswordMode(true)
						},
					},
				}, MaxSize: Size{0, 20},
			},
			HSplitter{
				Children: []Widget{
					Label{Text: "模式", MaxSize: Size{60, 20}},
					ComboBox{AssignTo: &mode,
						Editable: true, Value: Bind("RouterType"),
						Model:   []string{"local", "10.0.x.x(手动填写)", "openwrt", "hiwifi", "asus"},
						MaxSize: Size{0, 20},
						OnCurrentIndexChanged: func() {
							switch mode.CurrentIndex() {
							case 2: //"openwrt":
								laddr.SetVisible(true)
								lusr.SetVisible(true)
								lpwd.SetVisible(true)
							case 3: //"hiwifi":
								config.RouterAddr = "192.168.199.1"
								laddr.SetVisible(true)
								lusr.SetVisible(false)
								lpwd.SetVisible(true)
							case 4: //"asus":
								laddr.SetVisible(true)
								lusr.SetVisible(true)
								lpwd.SetVisible(true)
							default:
								laddr.SetVisible(false)
								lusr.SetVisible(false)
								lpwd.SetVisible(false)
							}
							mw.SetSize(walk.Size{0, 0})
						},
					},
				}, MaxSize: Size{0, 20},
			},
			HSplitter{
				AssignTo: &laddr,
				Children: []Widget{
					Label{Text: "路由地址", MaxSize: Size{60, 20}},
					LineEdit{Text: Bind("RouterAddr"), AssignTo: &raddr, MaxSize: Size{0, 20}},
				}, MaxSize: Size{0, 20},
			},
			HSplitter{
				AssignTo: &lusr,
				Children: []Widget{
					Label{Text: "路由用户名", MaxSize: Size{60, 20}},
					LineEdit{Text: Bind("RouterUser"), AssignTo: &rusr, MaxSize: Size{0, 20}},
				}, MaxSize: Size{0, 20},
			},
			HSplitter{
				AssignTo: &lpwd,
				Children: []Widget{
					Label{Text: "路由密码", MaxSize: Size{60, 20}},
					LineEdit{Text: Bind("RouterPwd"), AssignTo: &rpwd, MaxSize: Size{0, 20}, PasswordMode: true,
						OnMouseDown: func(x, y int, button walk.MouseButton) {
							rpwd.SetPasswordMode(false)
						},
						OnMouseUp: func(x, y int, button walk.MouseButton) {
							rpwd.SetPasswordMode(true)
						},
					},
				}, MaxSize: Size{0, 20},
			},
			HSplitter{
				Children: []Widget{
					PushButton{
						AssignTo: &lb,
						Text:     "开始连接",
						OnClicked: func() {
							if mode.Text() == "10.0.x.x(手动填写)" {
								walk.MsgBox(mw, "请填写IP", "手动填写需要自己获取IP 你可在路由器中自己查找 本地拨号请用local", walk.MsgBoxOK)
								return
							}
							lb.SetText("连接中...")
							lb.SetEnabled(false)
							rb.SetEnabled(false)
							go func() {
								d := netdialer.NewDialer(usr.Text(), pwd.Text())
								d.UserIP = selectMode(mode.Text())
								if d.UserIP == "" {
									walk.MsgBox(mw, "连接失败", "请检查设置", walk.MsgBoxOK)
									d = nil
									return
								}
								d.ConnectDirect()
								mw.SetTitle(TITLE + " [" + d.UserIP + "]")
								d = nil
								lb.SetEnabled(true)
								rb.SetEnabled(true)
								lb.SetText("开始连接")
								if err == nil {
									walk.MsgBox(mw, "连接成功", "感谢使用", walk.MsgBoxOK)
									util.INFO.Log("连接成功: 感谢使用")
								} else {
									walk.MsgBox(mw, "连接失败", err.Error(), walk.MsgBoxOK)
									util.INFO.Log("连接失败: ", err.Error())
								}
							}()
						},
					},
					PushButton{
						AssignTo: &rb,
						Text:     "断开连接",
						OnClicked: func() {
							if mode.Text() == "10.0.x.x(手动填写)" {
								walk.MsgBox(mw, "请填写IP", "手动填写需要自己获取IP 你可在路由器中自己查找", walk.MsgBoxOK)
								return
							}
							rb.SetText("断开中...")
							lb.SetEnabled(false)
							rb.SetEnabled(false)
							go func() {
								d := netdialer.NewDialer(usr.Text(), pwd.Text())
								d.UserIP = selectMode(mode.Text())
								if d.UserIP == "" {
									walk.MsgBox(mw, "连接失败", "请检查设置", walk.MsgBoxOK)
									util.INFO.Log("连接失败: 请检查设置")
									d = nil
									return
								}
								err := d.DisconnectDirect()
								mw.SetTitle(TITLE + " [" + d.UserIP + "]")
								d = nil
								lb.SetEnabled(true)
								rb.SetEnabled(true)
								rb.SetText("断开连接")
								if err == nil {
									walk.MsgBox(mw, "断开成功", "感谢使用", walk.MsgBoxOK)
									util.INFO.Log("断开成功: 感谢使用")
								} else {
									walk.MsgBox(mw, "断开失败", err.Error(), walk.MsgBoxOK)
									util.INFO.Log("断开失败: ", err.Error())
								}
							}()
						},
					},
				}, MaxSize: Size{0, 20},
			},
		},
	}.Run()
	saveConfig(config)
	bo.Flush()
}