Skip to content

Commit

Permalink
update slider control
Browse files Browse the repository at this point in the history
  • Loading branch information
ktye committed Jan 8, 2016
1 parent 356dfe7 commit c2463ed
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 136 deletions.
26 changes: 13 additions & 13 deletions declarative/trackbar.go → declarative/slider.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012 The Walk Authors. All rights reserved.
// Copyright 2016 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

Expand All @@ -10,8 +10,8 @@ import (
"github.com/lxn/walk"
)

type TrackBar struct {
AssignTo **walk.TrackBar
type Slider struct {
AssignTo **walk.Slider
Name string
Enabled Property
Visible Property
Expand Down Expand Up @@ -40,29 +40,29 @@ type TrackBar struct {
Orientation Orientation
}

func (tb TrackBar) Create(builder *Builder) error {
w, err := walk.NewTrackBarWithOrientation(builder.Parent(), walk.Orientation(tb.Orientation))
func (sl Slider) Create(builder *Builder) error {
w, err := walk.NewSliderWithOrientation(builder.Parent(), walk.Orientation(sl.Orientation))
if err != nil {
return err
}

return builder.InitWidget(tb, w, func() error {
if tb.MaxValue > tb.MinValue {
w.SetRange(tb.MinValue, tb.MaxValue)
return builder.InitWidget(sl, w, func() error {
if sl.MaxValue > sl.MinValue {
w.SetRange(sl.MinValue, sl.MaxValue)
}

if tb.AssignTo != nil {
*tb.AssignTo = w
if sl.AssignTo != nil {
*sl.AssignTo = w
}

if tb.OnValueChanged != nil {
w.ValueChanged().Attach(tb.OnValueChanged)
if sl.OnValueChanged != nil {
w.ValueChanged().Attach(sl.OnValueChanged)
}

return nil
})
}

func (w TrackBar) WidgetInfo() (name string, disabled, hidden bool, font *Font, toolTipText string, minSize, maxSize Size, stretchFactor, row, rowSpan, column, columnSpan int, alwaysConsumeSpace bool, contextMenuItems []MenuItem, OnKeyDown walk.KeyEventHandler, OnKeyPress walk.KeyEventHandler, OnKeyUp walk.KeyEventHandler, OnMouseDown walk.MouseEventHandler, OnMouseMove walk.MouseEventHandler, OnMouseUp walk.MouseEventHandler, OnSizeChanged walk.EventHandler) {
func (w Slider) WidgetInfo() (name string, disabled, hidden bool, font *Font, toolTipText string, minSize, maxSize Size, stretchFactor, row, rowSpan, column, columnSpan int, alwaysConsumeSpace bool, contextMenuItems []MenuItem, OnKeyDown walk.KeyEventHandler, OnKeyPress walk.KeyEventHandler, OnKeyUp walk.KeyEventHandler, OnMouseDown walk.MouseEventHandler, OnMouseMove walk.MouseEventHandler, OnMouseUp walk.MouseEventHandler, OnSizeChanged walk.EventHandler) {
return w.Name, false, false, &w.Font, "", w.MinSize, w.MaxSize, w.StretchFactor, w.Row, w.RowSpan, w.Column, w.ColumnSpan, w.AlwaysConsumeSpace, w.ContextMenuItems, w.OnKeyDown, w.OnKeyPress, w.OnKeyUp, w.OnMouseDown, w.OnMouseMove, w.OnMouseUp, w.OnSizeChanged
}
File renamed without changes.
35 changes: 18 additions & 17 deletions examples/trackbar/trackbar.go → examples/slider/slider.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013 The Walk Authors. All rights reserved.
// Copyright 2016 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

Expand All @@ -12,24 +12,24 @@ import (
)

func main() {
var tbv, tbh *walk.TrackBar
var slv, slh *walk.Slider
var maxEdit, minEdit, valueEdit *walk.NumberEdit

data := struct{ Min, Max, Value int }{0, 100, 30}

MainWindow{
Title: "Walk TrackBar Example",
Title: "Walk Slider Example",
MinSize: Size{320, 240},
Layout: HBox{},
Children: []Widget{
TrackBar{
AssignTo: &tbv,
Slider{
AssignTo: &slv,
MinValue: data.Min,
MaxValue: data.Max,
Value: data.Value,
Orientation: Vertical,
OnValueChanged: func() {
data.Value = tbv.Value()
data.Value = slv.Value()
valueEdit.SetValue(float64(data.Value))

},
Expand All @@ -46,45 +46,46 @@ func main() {
Value: float64(data.Min),
OnValueChanged: func() {
data.Min = int(minEdit.Value())
tbh.SetRange(data.Min, data.Max)
tbv.SetRange(data.Min, data.Max)
slh.SetRange(data.Min, data.Max)
slv.SetRange(data.Min, data.Max)
},
},
NumberEdit{
AssignTo: &valueEdit,
Value: float64(data.Value),
OnValueChanged: func() {
data.Value = int(valueEdit.Value())
tbh.SetValue(data.Value)
tbv.SetValue(data.Value)
slh.SetValue(data.Value)
slv.SetValue(data.Value)
},
},
NumberEdit{
AssignTo: &maxEdit,
Value: float64(data.Max),
OnValueChanged: func() {
data.Max = int(maxEdit.Value())
tbh.SetRange(data.Min, data.Max)
tbv.SetRange(data.Min, data.Max)
slh.SetRange(data.Min, data.Max)
slv.SetRange(data.Min, data.Max)
},
},
TrackBar{
Slider{
ColumnSpan: 3,
AssignTo: &tbh,
AssignTo: &slh,
MinValue: data.Min,
MaxValue: data.Max,
Value: data.Value,
OnValueChanged: func() {
data.Value = tbh.Value()
data.Value = slh.Value()
valueEdit.SetValue(float64(data.Value))
},
},
VSpacer{},
PushButton{
ColumnSpan: 3,
Text: "Print state",
OnClicked: func() {
log.Printf("H: < %d | %d | %d >\n", tbh.MinValue(), tbh.Value(), tbh.MaxValue())
log.Printf("V: < %d | %d | %d >\n", tbv.MinValue(), tbv.Value(), tbv.MaxValue())
log.Printf("H: < %d | %d | %d >\n", slh.MinValue(), slh.Value(), slh.MaxValue())
log.Printf("V: < %d | %d | %d >\n", slv.MinValue(), slv.Value(), slv.MaxValue())
},
},
},
Expand Down
File renamed without changes.
136 changes: 136 additions & 0 deletions slider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright 2016 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build windows

package walk

import (
"log" // TODO remove
"unsafe" // TODO remove
)

import (
"github.com/lxn/win"
)

type Slider struct {
WidgetBase
valueChangedPublisher EventPublisher
layoutFlags LayoutFlags
}

func NewSlider(parent Container) (*Slider, error) {
return NewSliderWithOrientation(parent, Horizontal)
}

func NewSliderWithOrientation(parent Container, orientation Orientation) (*Slider, error) {
sl := new(Slider)

var style uint32 = win.WS_VISIBLE | win.TBS_TOOLTIPS
if orientation == Vertical {
style |= win.TBS_VERT
sl.layoutFlags = ShrinkableVert | GrowableVert | GreedyVert
} else {
sl.layoutFlags = ShrinkableHorz | GrowableHorz | GreedyHorz
}

if err := InitWidget(
sl,
parent,
"msctls_trackbar32",
style,
0); err != nil {
return nil, err
}

sl.MustRegisterProperty("Value", NewProperty(
func() interface{} {
return sl.Value()
},
func(v interface{}) error {
sl.SetValue(v.(int))
return nil
},
sl.valueChangedPublisher.Event()))

return sl, nil
}

func (sl *Slider) LayoutFlags() LayoutFlags {
return sl.layoutFlags
}

func (sl *Slider) SizeHint() Size {
return sl.MinSizeHint()
}

func (sl *Slider) MinSizeHint() Size {
return sl.dialogBaseUnitsToPixels(Size{20, 20})
}

func (sl *Slider) MinValue() int {
return int(sl.SendMessage(win.TBM_GETRANGEMIN, 0, 0))
}

func (sl *Slider) MaxValue() int {
return int(sl.SendMessage(win.TBM_GETRANGEMAX, 0, 0))
}

func (sl *Slider) SetRange(min, max int) {
sl.SendMessage(win.TBM_SETRANGEMIN, 0, uintptr(min))
sl.SendMessage(win.TBM_SETRANGEMAX, 1, uintptr(max))
}

func (sl *Slider) Value() int {
return int(sl.SendMessage(win.TBM_GETPOS, 0, 0))
}

func (sl *Slider) SetValue(value int) {
sl.SendMessage(win.TBM_SETPOS, 1, uintptr(value))
sl.valueChangedPublisher.Publish()
}

// ValueChanged returns an Event that can be used to track changes to Value.
func (sl *Slider) ValueChanged() *Event {
return sl.valueChangedPublisher.Event()
}

// I'm not sure anymore, if this is the right way!
// It seems the proper way is to hook on WM_HSCROLL and WM_VSCROLL.
// But they are not received here!
// I read, a trackbar does not receive them, it sends them to it's parents.
// Any Idea on what to do then?
func (sl *Slider) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_NOTIFY: // 0x4E
code := ((*win.NMHDR)(unsafe.Pointer(lParam))).Code
switch code {
case win.TRBN_THUMBPOSCHANGING:
// TRBN_THUMBPOSCHANGING is documented to exist only on Vista and above.
// I run windows 10, still I never recieve it.
log.Fatal("RECEIVED TRBN_THUMBPOSCHANGING") // This never happens.
sl.valueChangedPublisher.Publish()
case win.NM_CUSTOMDRAW:
// This is received however, show we do anything with it?
nmcd := (*win.NMCUSTOMDRAW)(unsafe.Pointer(lParam))
log.Printf("WM_NOTIFY NM_CUSTOMDRAW: %+v code=%x\n", nmcd, nmcd.Hdr.Code)
default:
// I don't receive anything else, when I change the slider with an arrow key.
// There are some others however, when I drag with the mouse.
log.Printf("Other WM_NOFITY NMHDR CODE: 0x%x\n", code)
}
case win.WM_GETDLGCODE:
// I recieve this too. lParm is 0x8fca8
log.Printf("WM_GETDLGCODE: 0x%x 0x%x", wParam, lParam)
default:
log.Printf("Other msg: %v 0x%x\n", msg, msg)
// I receive these other messages, when I press an arrow key on a slider:
// 0x101 WM_KEYUP
// 0x100 WM_KEYFIRST
// 0xf WM_PAINT
// There is no WM_VSROLL (0x114) or WM_HSCROLL (0x115).
}
return sl.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}
106 changes: 0 additions & 106 deletions trackbar.go

This file was deleted.

0 comments on commit c2463ed

Please sign in to comment.