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

     1  //go:build windows
     2  
     3  /*
     4   * Copyright (C) 2019 The Winc Authors. All Rights Reserved.
     5   * Copyright (C) 2010-2013 Allen Dang. All Rights Reserved.
     6   */
     7  
     8  package winc
     9  
    10  import (
    11  	"fmt"
    12  
    13  	"github.com/secoba/wails/v2/internal/frontend/desktop/windows/winc/w32"
    14  )
    15  
    16  type Button struct {
    17  	ControlBase
    18  	onClick EventManager
    19  }
    20  
    21  func (bt *Button) OnClick() *EventManager {
    22  	return &bt.onClick
    23  }
    24  
    25  func (bt *Button) WndProc(msg uint32, wparam, lparam uintptr) uintptr {
    26  	switch msg {
    27  	case w32.WM_COMMAND:
    28  		bt.onClick.Fire(NewEvent(bt, nil))
    29  		/*case w32.WM_LBUTTONDOWN:
    30  			w32.SetCapture(bt.Handle())
    31  		case w32.WM_LBUTTONUP:
    32  			w32.ReleaseCapture()*/
    33  		/*case win.WM_GETDLGCODE:
    34  		println("GETDLGCODE")*/
    35  	}
    36  	return w32.DefWindowProc(bt.hwnd, msg, wparam, lparam)
    37  	//return bt.W32Control.WndProc(msg, wparam, lparam)
    38  }
    39  
    40  func (bt *Button) Checked() bool {
    41  	result := w32.SendMessage(bt.hwnd, w32.BM_GETCHECK, 0, 0)
    42  	return result == w32.BST_CHECKED
    43  }
    44  
    45  func (bt *Button) SetChecked(checked bool) {
    46  	wparam := w32.BST_CHECKED
    47  	if !checked {
    48  		wparam = w32.BST_UNCHECKED
    49  	}
    50  	w32.SendMessage(bt.hwnd, w32.BM_SETCHECK, uintptr(wparam), 0)
    51  }
    52  
    53  // SetIcon sets icon on the button. Recommended icons are 32x32 with 32bit color depth.
    54  func (bt *Button) SetIcon(ico *Icon) {
    55  	w32.SendMessage(bt.hwnd, w32.BM_SETIMAGE, w32.IMAGE_ICON, uintptr(ico.handle))
    56  }
    57  
    58  func (bt *Button) SetResIcon(iconID uint16) {
    59  	if ico, err := NewIconFromResource(GetAppInstance(), iconID); err == nil {
    60  		bt.SetIcon(ico)
    61  		return
    62  	}
    63  	panic(fmt.Sprintf("missing icon with icon ID: %d", iconID))
    64  }
    65  
    66  type PushButton struct {
    67  	Button
    68  }
    69  
    70  func NewPushButton(parent Controller) *PushButton {
    71  	pb := new(PushButton)
    72  
    73  	pb.InitControl("BUTTON", parent, 0, w32.BS_PUSHBUTTON|w32.WS_TABSTOP|w32.WS_VISIBLE|w32.WS_CHILD)
    74  	RegMsgHandler(pb)
    75  
    76  	pb.SetFont(DefaultFont)
    77  	pb.SetText("Button")
    78  	pb.SetSize(100, 22)
    79  
    80  	return pb
    81  }
    82  
    83  // SetDefault is used for dialogs to set default button.
    84  func (pb *PushButton) SetDefault() {
    85  	pb.SetAndClearStyleBits(w32.BS_DEFPUSHBUTTON, w32.BS_PUSHBUTTON)
    86  }
    87  
    88  // IconButton does not display text, requires SetResIcon call.
    89  type IconButton struct {
    90  	Button
    91  }
    92  
    93  func NewIconButton(parent Controller) *IconButton {
    94  	pb := new(IconButton)
    95  
    96  	pb.InitControl("BUTTON", parent, 0, w32.BS_ICON|w32.WS_TABSTOP|w32.WS_VISIBLE|w32.WS_CHILD)
    97  	RegMsgHandler(pb)
    98  
    99  	pb.SetFont(DefaultFont)
   100  	// even if text would be set it would not be displayed
   101  	pb.SetText("")
   102  	pb.SetSize(100, 22)
   103  
   104  	return pb
   105  }
   106  
   107  type CheckBox struct {
   108  	Button
   109  }
   110  
   111  func NewCheckBox(parent Controller) *CheckBox {
   112  	cb := new(CheckBox)
   113  
   114  	cb.InitControl("BUTTON", parent, 0, w32.WS_TABSTOP|w32.WS_VISIBLE|w32.WS_CHILD|w32.BS_AUTOCHECKBOX)
   115  	RegMsgHandler(cb)
   116  
   117  	cb.SetFont(DefaultFont)
   118  	cb.SetText("CheckBox")
   119  	cb.SetSize(100, 22)
   120  
   121  	return cb
   122  }
   123  
   124  type RadioButton struct {
   125  	Button
   126  }
   127  
   128  func NewRadioButton(parent Controller) *RadioButton {
   129  	rb := new(RadioButton)
   130  
   131  	rb.InitControl("BUTTON", parent, 0, w32.WS_TABSTOP|w32.WS_VISIBLE|w32.WS_CHILD|w32.BS_AUTORADIOBUTTON)
   132  	RegMsgHandler(rb)
   133  
   134  	rb.SetFont(DefaultFont)
   135  	rb.SetText("RadioButton")
   136  	rb.SetSize(100, 22)
   137  
   138  	return rb
   139  }
   140  
   141  type GroupBox struct {
   142  	Button
   143  }
   144  
   145  func NewGroupBox(parent Controller) *GroupBox {
   146  	gb := new(GroupBox)
   147  
   148  	gb.InitControl("BUTTON", parent, 0, w32.WS_CHILD|w32.WS_VISIBLE|w32.WS_GROUP|w32.BS_GROUPBOX)
   149  	RegMsgHandler(gb)
   150  
   151  	gb.SetFont(DefaultFont)
   152  	gb.SetText("GroupBox")
   153  	gb.SetSize(100, 100)
   154  
   155  	return gb
   156  }