2014-08-02 21:35:58 -05:00
// 15 july 2014
package ui
import (
"unsafe"
)
// #include "winapi_windows.h"
import "C"
type checkbox struct {
2014-10-02 09:05:53 -05:00
_hwnd C . HWND
_textlen C . LONG
toggled * event
2014-08-02 21:35:58 -05:00
}
func newCheckbox ( text string ) * checkbox {
2014-08-03 08:18:35 -05:00
// don't use BS_AUTOCHECKBOX here because it creates problems when refocusing (see http://blogs.msdn.com/b/oldnewthing/archive/2014/05/22/10527522.aspx)
// we'll handle actually toggling the check state ourselves (see controls_windows.c)
2014-08-03 20:52:21 -05:00
hwnd := C . newControl ( buttonclass ,
2014-10-02 09:05:53 -05:00
C . BS_CHECKBOX | C . WS_TABSTOP ,
2014-08-03 08:18:35 -05:00
0 )
2014-08-02 21:35:58 -05:00
c := & checkbox {
2014-10-02 09:05:53 -05:00
_hwnd : hwnd ,
toggled : newEvent ( ) ,
2014-08-02 21:35:58 -05:00
}
2014-08-03 20:52:21 -05:00
c . SetText ( text )
C . controlSetControlFont ( c . _hwnd )
C . setCheckboxSubclass ( c . _hwnd , unsafe . Pointer ( c ) )
2014-08-02 21:35:58 -05:00
return c
}
2014-08-03 08:18:35 -05:00
func ( c * checkbox ) OnToggled ( e func ( ) ) {
c . toggled . set ( e )
}
func ( c * checkbox ) Text ( ) string {
2014-08-03 20:52:21 -05:00
return baseText ( c )
2014-08-03 08:18:35 -05:00
}
func ( c * checkbox ) SetText ( text string ) {
2014-08-03 20:52:21 -05:00
baseSetText ( c , text )
2014-08-03 08:18:35 -05:00
}
2014-08-02 21:35:58 -05:00
func ( c * checkbox ) Checked ( ) bool {
2014-08-03 20:52:21 -05:00
return C . checkboxChecked ( c . _hwnd ) != C . FALSE
2014-08-02 21:35:58 -05:00
}
func ( c * checkbox ) SetChecked ( checked bool ) {
if checked {
2014-08-03 20:52:21 -05:00
C . checkboxSetChecked ( c . _hwnd , C . TRUE )
2014-08-02 21:35:58 -05:00
return
}
2014-08-03 20:52:21 -05:00
C . checkboxSetChecked ( c . _hwnd , C . FALSE )
2014-08-02 21:35:58 -05:00
}
//export checkboxToggled
func checkboxToggled ( data unsafe . Pointer ) {
c := ( * checkbox ) ( data )
2014-08-03 08:18:35 -05:00
c . toggled . fire ( )
2014-08-02 21:35:58 -05:00
}
2014-08-03 20:52:21 -05:00
func ( c * checkbox ) hwnd ( ) C . HWND {
return c . _hwnd
}
func ( c * checkbox ) textlen ( ) C . LONG {
return c . _textlen
}
func ( c * checkbox ) settextlen ( len C . LONG ) {
c . _textlen = len
}
2014-08-03 08:18:35 -05:00
func ( c * checkbox ) setParent ( p * controlParent ) {
2014-08-03 20:52:21 -05:00
basesetParent ( c , p )
2014-08-03 08:18:35 -05:00
}
func ( c * checkbox ) allocate ( x int , y int , width int , height int , d * sizing ) [ ] * allocation {
return baseallocate ( c , x , y , width , height , d )
}
2014-08-02 21:35:58 -05:00
const (
// from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
checkboxHeight = 10
// from http://msdn.microsoft.com/en-us/library/windows/desktop/bb226818%28v=vs.85%29.aspx
checkboxXFromLeftOfBoxToLeftOfLabel = 12
)
2014-08-03 08:18:35 -05:00
func ( c * checkbox ) preferredSize ( d * sizing ) ( width , height int ) {
2014-08-03 20:52:21 -05:00
return fromdlgunitsX ( checkboxXFromLeftOfBoxToLeftOfLabel , d ) + int ( c . _textlen ) ,
2014-08-02 21:35:58 -05:00
fromdlgunitsY ( checkboxHeight , d )
}
2014-08-03 08:18:35 -05:00
func ( c * checkbox ) commitResize ( a * allocation , d * sizing ) {
2014-08-03 20:52:21 -05:00
basecommitResize ( c , a , d )
2014-08-03 08:18:35 -05:00
}
func ( c * checkbox ) getAuxResizeInfo ( d * sizing ) {
2014-08-03 19:42:45 -05:00
basegetAuxResizeInfo ( c , d )
2014-08-03 08:18:35 -05:00
}