github.com/secoba/wails/v2@v2.6.4/internal/frontend/desktop/windows/winc/slider.go (about)

     1  //go:build windows
     2  
     3  /*
     4   * Copyright (C) 2019 The Winc Authors. All Rights Reserved.
     5   */
     6  
     7  package winc
     8  
     9  import "github.com/secoba/wails/v2/internal/frontend/desktop/windows/winc/w32"
    10  
    11  type Slider struct {
    12  	ControlBase
    13  	prevPos int
    14  
    15  	onScroll EventManager
    16  }
    17  
    18  func NewSlider(parent Controller) *Slider {
    19  	tb := new(Slider)
    20  
    21  	tb.InitControl("msctls_trackbar32", parent, 0, w32.WS_TABSTOP|w32.WS_VISIBLE|w32.WS_CHILD /*|w32.TBS_AUTOTICKS*/)
    22  	RegMsgHandler(tb)
    23  
    24  	tb.SetFont(DefaultFont)
    25  	tb.SetText("Slider")
    26  	tb.SetSize(200, 32)
    27  
    28  	tb.SetRange(0, 100)
    29  	tb.SetPage(10)
    30  	return tb
    31  }
    32  
    33  func (tb *Slider) OnScroll() *EventManager {
    34  	return &tb.onScroll
    35  }
    36  
    37  func (tb *Slider) Value() int {
    38  	ret := w32.SendMessage(tb.hwnd, w32.TBM_GETPOS, 0, 0)
    39  	return int(ret)
    40  }
    41  
    42  func (tb *Slider) SetValue(v int) {
    43  	tb.prevPos = v
    44  	w32.SendMessage(tb.hwnd, w32.TBM_SETPOS, uintptr(w32.BoolToBOOL(true)), uintptr(v))
    45  }
    46  
    47  func (tb *Slider) Range() (min, max int) {
    48  	min = int(w32.SendMessage(tb.hwnd, w32.TBM_GETRANGEMIN, 0, 0))
    49  	max = int(w32.SendMessage(tb.hwnd, w32.TBM_GETRANGEMAX, 0, 0))
    50  	return min, max
    51  }
    52  
    53  func (tb *Slider) SetRange(min, max int) {
    54  	w32.SendMessage(tb.hwnd, w32.TBM_SETRANGE, uintptr(w32.BoolToBOOL(true)), uintptr(w32.MAKELONG(uint16(min), uint16(max))))
    55  }
    56  
    57  func (tb *Slider) SetPage(pagesize int) {
    58  	w32.SendMessage(tb.hwnd, w32.TBM_SETPAGESIZE, 0, uintptr(pagesize))
    59  }
    60  
    61  func (tb *Slider) WndProc(msg uint32, wparam, lparam uintptr) uintptr {
    62  	/*
    63  		// REMOVE:
    64  		// following code did not work, used workaround below
    65  		code := w32.LOWORD(uint32(wparam))
    66  
    67  		switch code {
    68  		case w32.TB_ENDTRACK:
    69  			tb.onScroll.Fire(NewEvent(tb, nil))
    70  		}*/
    71  
    72  	newPos := tb.Value()
    73  	if newPos != tb.prevPos {
    74  		tb.onScroll.Fire(NewEvent(tb, nil))
    75  		tb.prevPos = newPos
    76  	}
    77  
    78  	return w32.DefWindowProc(tb.hwnd, msg, wparam, lparam)
    79  }