コード例 #1
0
ファイル: widgets.go プロジェクト: sqp/godock
// IntegerSpin adds an integer scale widget.
//
func IntegerSpin(key *cftype.Key) {
	if key.NbElements > 1 { // TODO: remove temp test
		key.Log().Info("integer multi ffs", key.Type.String())
	}

	value := key.Value().Int()
	minValue, maxValue := minMaxValues(key)

	w := newgtk.SpinButtonWithRange(minValue, maxValue, 1)
	w.SetValue(float64(value))

	PackValuerAsInt(key, w, w, value)
}
コード例 #2
0
ファイル: widgets.go プロジェクト: sqp/godock
// Float adds a float selector widget. SpinButton or Horizontal Scale
//
func Float(key *cftype.Key) {
	if key.NbElements > 1 { // TODO: remove temp test
		key.Log().Info("Float multi", key.NbElements, key.Type.String())
	}

	values := key.Value().ListFloat()
	minValue, maxValue := minMaxValues(key)

	var valuers []WidgetValuer

	for k := 0; k < key.NbElements; k++ {
		var value float64
		if k < len(values) {
			value = values[k]
		}

		switch key.Type {
		case cftype.KeyFloatScale:
			adjustment := newgtk.Adjustment(
				value,
				minValue,
				maxValue,
				(maxValue-minValue)/20,
				(maxValue-minValue)/10, 0,
			)
			w := newgtk.Scale(gtk.ORIENTATION_HORIZONTAL, adjustment)
			w.Set("digits", 3)

			key.PackSubWidget(WrapKeyScale(key, w))
			valuers = append(valuers, w)

		case cftype.KeyFloatSpin:
			w := newgtk.SpinButtonWithRange(minValue, maxValue, 1)
			w.Set("digits", 3)
			w.SetValue(value)

			key.PackSubWidget(w)
			valuers = append(valuers, w)
		}
	}

	switch {
	case key.NbElements == 1:
		key.PackKeyWidget(key,
			func() interface{} { return valuers[0].GetValue() },
			func(uncast interface{}) { valuers[0].SetValue(uncast.(float64)) },
		)
		oldval, e := key.Storage().Default(key.Group, key.Name)
		if e == nil {
			PackReset(key, oldval.Float())
		}

	default:
		key.PackKeyWidget(key,
			func() interface{} { return listValuerGet(valuers) },
			func(uncast interface{}) { listValuerSet(valuers, uncast.([]float64)) },
		)
		oldval, e := key.Storage().Default(key.Group, key.Name)
		if e == nil {
			PackReset(key, oldval.ListFloat())
		}
	}
}
コード例 #3
0
ファイル: widgets.go プロジェクト: sqp/godock
// IntegerSize adds an integer selector widget.
//
func IntegerSize(key *cftype.Key) {
	if key.NbElements > 1 { // TODO: remove temp test
		key.Log().Info("IntegerSize multi", key.NbElements, key.Type.String())
	}

	toggle := newgtk.ToggleButton()
	img := newgtk.ImageFromIconName("media-playback-pause", gtk.ICON_SIZE_MENU) // get better image.
	toggle.SetImage(img)

	values := key.Value().ListInt()
	minValue, maxValue := minMaxValues(key)

	var valuers []WidgetValuer

	key.NbElements *= 2 // Two widgets to add.

	// Value pair data.
	var firstValue int
	var firstWidget *gtk.SpinButton
	var cbBlock func() func()

	for k := 0; k < key.NbElements; k++ {
		var value int
		if k < len(values) {
			value = values[k]
		}

		w := newgtk.SpinButtonWithRange(minValue, maxValue, 1)
		w.SetValue(float64(value))
		key.PackSubWidget(w)
		valuers = append(valuers, w)

		if k&1 == 0 { // first value, separator
			label := newgtk.Label("x")
			key.PackSubWidget(label)

			firstWidget = w
			firstValue = value

		} else { // second value. connect both spin values.
			if firstValue == value {
				toggle.SetActive(true)
			}

			cb0, e := firstWidget.Connect("value-changed", onValuePairChanged, &valuePair{
				linked: w,
				toggle: toggle})
			key.Log().Err(e, "IntegerSize connect value-changed 1")
			cb1, e := w.Connect("value-changed", onValuePairChanged, &valuePair{
				linked: firstWidget,
				toggle: toggle})
			key.Log().Err(e, "IntegerSize connect value-changed 2")

			cbBlock = func() func() {
				firstWidget.HandlerBlock(cb0)
				w.HandlerBlock(cb1)
				return func() {
					firstWidget.HandlerUnblock(cb0)
					w.HandlerUnblock(cb1)
				}
			}
		}
	}

	setValue := func(uncast interface{}) {
		if cbBlock == nil {
			key.Log().NewErr("no valuePair callbacks", "IntegerSize", key.Name)
		} else {
			defer cbBlock()() // This disables now and reenables during defer.
		}
		values := uncast.([]int)
		if len(values) < 2 {
			key.Log().NewErr("not enough values provided", "IntegerSize set value", key.Name, values)
			values = []int{0, 0}
		}

		listValuerSet(valuers, cast.IntsToFloats(values))
		toggle.SetActive(values[0] == values[1])
	}

	key.PackKeyWidget(key,
		func() interface{} { return cast.FloatsToInts(listValuerGet(valuers)) },
		setValue,
		toggle,
	)

	oldval, e := key.Storage().Default(key.Group, key.Name)
	key.Log().Err(e, "IntegerSize original value")
	if e == nil {
		PackReset(key, oldval.ListInt())
	}
}