2014-07-15 21:29:45 -05:00
// 15 july 2014
package ui
import (
2014-07-17 14:10:26 -05:00
"unsafe"
2014-07-15 21:29:45 -05:00
)
2014-07-17 21:16:32 -05:00
// #include "winapi_windows.h"
import "C"
2014-07-15 21:29:45 -05:00
type widgetbase struct {
2014-07-17 21:16:32 -05:00
hwnd C . HWND
2014-07-25 16:34:45 -05:00
parent C . HWND
2014-07-15 21:29:45 -05:00
}
2014-07-17 23:22:21 -05:00
func newWidget ( class C . LPCWSTR , style C . DWORD , extstyle C . DWORD ) * widgetbase {
2014-07-15 21:29:45 -05:00
return & widgetbase {
2014-07-17 21:16:32 -05:00
hwnd : C . newWidget ( class , style , extstyle ) ,
2014-07-15 21:29:45 -05:00
}
}
// these few methods are embedded by all the various Controls since they all will do the same thing
2014-07-25 16:34:45 -05:00
func ( w * widgetbase ) setParent ( win C . HWND ) {
C . controlSetParent ( w . hwnd , win )
w . parent = win
2014-07-15 21:29:45 -05:00
}
2014-07-25 11:45:56 -05:00
func ( w * widgetbase ) containerShow ( ) {
C . ShowWindow ( w . hwnd , C . SW_SHOW )
}
func ( w * widgetbase ) containerHide ( ) {
C . ShowWindow ( w . hwnd , C . SW_HIDE )
}
2014-07-15 21:29:45 -05:00
// don't embed these as exported; let each Control decide if it should
2014-07-19 08:44:32 -05:00
func ( w * widgetbase ) text ( ) string {
return getWindowText ( w . hwnd )
2014-07-15 21:29:45 -05:00
}
2014-07-19 08:44:32 -05:00
func ( w * widgetbase ) settext ( text string ) {
C . setWindowText ( w . hwnd , toUTF16 ( text ) )
2014-07-15 21:29:45 -05:00
}
type button struct {
* widgetbase
2014-07-17 14:10:26 -05:00
clicked * event
2014-07-15 21:29:45 -05:00
}
2014-07-17 21:16:32 -05:00
var buttonclass = toUTF16 ( "BUTTON" )
2014-07-15 21:29:45 -05:00
2014-07-21 20:34:52 -05:00
func startNewButton ( text string , style C . DWORD ) * button {
2014-07-19 08:44:32 -05:00
w := newWidget ( buttonclass ,
2014-07-21 20:34:52 -05:00
style | C . WS_TABSTOP ,
2014-07-19 08:44:32 -05:00
0 )
C . setWindowText ( w . hwnd , toUTF16 ( text ) )
C . controlSetControlFont ( w . hwnd )
b := & button {
widgetbase : w ,
clicked : newEvent ( ) ,
2014-07-15 21:29:45 -05:00
}
2014-07-21 20:34:52 -05:00
return b
}
func newButton ( text string ) * button {
b := startNewButton ( text , C . BS_PUSHBUTTON )
C . setButtonSubclass ( b . hwnd , unsafe . Pointer ( b ) )
2014-07-19 08:44:32 -05:00
return b
2014-07-15 21:29:45 -05:00
}
2014-07-19 08:44:32 -05:00
func ( b * button ) OnClicked ( e func ( ) ) {
b . clicked . set ( e )
2014-07-15 21:29:45 -05:00
}
2014-07-19 08:44:32 -05:00
func ( b * button ) Text ( ) string {
2014-07-15 21:29:45 -05:00
return b . text ( )
}
2014-07-19 08:44:32 -05:00
func ( b * button ) SetText ( text string ) {
2014-07-19 09:07:42 -05:00
b . settext ( text )
2014-07-15 21:29:45 -05:00
}
2014-07-17 14:10:26 -05:00
2014-07-17 21:16:32 -05:00
//export buttonClicked
func buttonClicked ( data unsafe . Pointer ) {
b := ( * button ) ( data )
b . clicked . fire ( )
println ( "button clicked" )
2014-07-17 14:10:26 -05:00
}
2014-07-21 20:34:52 -05:00
type checkbox struct {
* button
}
func newCheckbox ( text string ) * checkbox {
c := & checkbox {
// 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)
button : startNewButton ( text , C . BS_CHECKBOX ) ,
}
C . setCheckboxSubclass ( c . hwnd , unsafe . Pointer ( c ) )
return c
}
func ( c * checkbox ) Checked ( ) bool {
if C . checkboxChecked ( c . hwnd ) == C . FALSE {
return false
}
return true
}
func ( c * checkbox ) SetChecked ( checked bool ) {
if checked {
C . checkboxSetChecked ( c . hwnd , C . TRUE )
return
}
C . checkboxSetChecked ( c . hwnd , C . FALSE )
}
//export checkboxToggled
func checkboxToggled ( data unsafe . Pointer ) {
c := ( * checkbox ) ( data )
c . clicked . fire ( )
println ( "checkbox toggled" )
}
2014-07-26 05:57:11 -05:00
type textField struct {
* widgetbase
}
var editclass = toUTF16 ( "EDIT" )
func startNewTextField ( style C . DWORD ) * textField {
w := newWidget ( editclass ,
2014-07-26 06:02:19 -05:00
style | C . ES_AUTOHSCROLL | C . ES_LEFT | C . ES_NOHIDESEL | C . WS_BORDER | C . WS_TABSTOP ,
C . WS_EX_CLIENTEDGE ) // shows visual style-themed edit control borders (TODO get credit from old source)
2014-07-26 05:57:11 -05:00
C . controlSetControlFont ( w . hwnd )
return & textField {
widgetbase : w ,
}
}
func newTextField ( ) * textField {
return startNewTextField ( 0 )
}
func newPasswordField ( ) * textField {
return startNewTextField ( C . ES_PASSWORD )
}
func ( t * textField ) Text ( ) string {
return t . text ( )
}
func ( t * textField ) SetText ( text string ) {
t . settext ( text )
}