2014-02-08 22:51:11 -06:00
// 8 february 2014
2014-03-12 20:55:45 -05:00
2014-02-19 10:41:10 -06:00
package ui
2014-02-08 22:51:11 -06:00
import (
2014-02-11 16:38:38 -06:00
"fmt"
2014-02-08 22:51:11 -06:00
"syscall"
"unsafe"
2014-02-12 19:51:07 -06:00
)
2014-06-03 09:52:53 -05:00
var (
stdWndClass = toUTF16 ( "gouiwnd" )
2014-02-11 16:38:38 -06:00
)
var (
2014-05-30 13:13:47 -05:00
_defWindowProc = user32 . NewProc ( "DefWindowProcW" )
2014-02-11 16:38:38 -06:00
)
2014-05-30 13:13:47 -05:00
func defWindowProc ( hwnd _HWND , uMsg uint32 , wParam _WPARAM , lParam _LPARAM ) _LRESULT {
r1 , _ , _ := _defWindowProc . Call (
uintptr ( hwnd ) ,
uintptr ( uMsg ) ,
uintptr ( wParam ) ,
uintptr ( lParam ) )
return _LRESULT ( r1 )
}
2014-05-30 16:59:29 -05:00
// don't worry about error returns from GetWindowLongPtr()/SetWindowLongPtr()
// see comments of http://blogs.msdn.com/b/oldnewthing/archive/2014/02/03/10496248.aspx
func getWindowLongPtr ( hwnd _HWND , what uintptr ) uintptr {
r1 , _ , _ := _getWindowLongPtr . Call (
uintptr ( hwnd ) ,
what )
return r1
}
func setWindowLongPtr ( hwnd _HWND , what uintptr , value uintptr ) {
_setWindowLongPtr . Call (
uintptr ( hwnd ) ,
what ,
value )
}
// we can store a pointer in extra space provided by Windows
// we'll store sysData there
// see http://blogs.msdn.com/b/oldnewthing/archive/2005/03/03/384285.aspx
2014-05-30 17:03:57 -05:00
2014-05-30 16:59:29 -05:00
func getSysData ( hwnd _HWND ) * sysData {
return ( * sysData ) ( unsafe . Pointer ( getWindowLongPtr ( hwnd , negConst ( _GWLP_USERDATA ) ) ) )
}
func storeSysData ( hwnd _HWND , uMsg uint32 , wParam _WPARAM , lParam _LPARAM ) _LRESULT {
// we can get the lpParam from CreateWindowEx() in WM_NCCREATE and WM_CREATE
// we can freely skip any messages that come prior
// see http://blogs.msdn.com/b/oldnewthing/archive/2005/04/22/410773.aspx and http://blogs.msdn.com/b/oldnewthing/archive/2014/02/03/10496248.aspx (note the date on the latter one!)
if uMsg == _WM_NCCREATE {
// the lpParam to CreateWindowEx() is the first uintptr of the CREATESTRUCT
// so rather than create that whole structure, we'll just grab the uintptr at the address pointed to by lParam
cs := ( * uintptr ) ( unsafe . Pointer ( lParam ) )
saddr := * cs
setWindowLongPtr ( hwnd , negConst ( _GWLP_USERDATA ) , saddr )
2014-05-30 22:28:41 -05:00
// also set s.hwnd here so it can be used by other window messages right away
s := ( * sysData ) ( unsafe . Pointer ( saddr ) )
s . hwnd = hwnd
2014-05-30 16:59:29 -05:00
}
2014-06-04 00:17:59 -05:00
// then regardless of what happens, defer to DefWindowProc() (if you trace the execution of the above links, this is what they do)
2014-05-30 16:59:29 -05:00
return defWindowProc ( hwnd , uMsg , wParam , lParam )
}
2014-05-30 20:49:55 -05:00
func stdWndProc ( hwnd _HWND , uMsg uint32 , wParam _WPARAM , lParam _LPARAM ) _LRESULT {
s := getSysData ( hwnd )
if s == nil { // not yet saved
return storeSysData ( hwnd , uMsg , wParam , lParam )
}
switch uMsg {
case _WM_COMMAND :
id := _HMENU ( wParam . LOWORD ( ) )
s . childrenLock . Lock ( )
ss := s . children [ id ]
s . childrenLock . Unlock ( )
switch ss . ctype {
case c_button :
if wParam . HIWORD ( ) == _BN_CLICKED {
ss . signal ( )
2014-02-12 10:32:17 -06:00
}
2014-05-30 20:49:55 -05:00
}
return 0
case _WM_GETMINMAXINFO :
mm := lParam . MINMAXINFO ( )
// ... minimum size
_ = mm
return 0
case _WM_SIZE :
if s . resize != nil {
var r _RECT
r1 , _ , err := _getClientRect . Call (
uintptr ( hwnd ) ,
uintptr ( unsafe . Pointer ( & r ) ) )
if r1 == 0 {
panic ( "GetClientRect failed: " + err . Error ( ) )
2014-02-13 04:28:26 -06:00
}
2014-05-30 20:49:55 -05:00
// top-left corner is (0,0) so no need for winheight
s . doResize ( int ( r . left ) , int ( r . top ) , int ( r . right ) , int ( r . bottom ) , 0 )
// TODO use the Defer movement functions here?
// TODO redraw window and all children here?
2014-02-12 10:32:17 -06:00
}
2014-05-30 20:49:55 -05:00
return 0
case _WM_CLOSE :
s . signal ( )
return 0
default :
return defWindowProc ( hwnd , uMsg , wParam , lParam )
2014-02-11 16:38:38 -06:00
}
2014-05-30 20:49:55 -05:00
panic ( fmt . Sprintf ( "stdWndProc message %d did not return: internal bug in ui library" , uMsg ) )
2014-02-08 22:51:11 -06:00
}
2014-02-11 16:38:38 -06:00
type _WNDCLASS struct {
2014-02-08 22:51:11 -06:00
style uint32
2014-02-09 13:59:37 -06:00
lpfnWndProc uintptr
2014-02-21 01:27:59 -06:00
cbClsExtra int32 // originally int
cbWndExtra int32 // originally int
2014-02-11 16:38:38 -06:00
hInstance _HANDLE
hIcon _HANDLE
hCursor _HANDLE
hbrBackground _HBRUSH
2014-02-08 22:51:11 -06:00
lpszMenuName * uint16
2014-02-11 18:52:39 -06:00
lpszClassName uintptr
2014-02-08 22:51:11 -06:00
}
2014-02-12 19:51:07 -06:00
var (
icon , cursor _HANDLE
)
var (
_registerClass = user32 . NewProc ( "RegisterClassW" )
)
2014-05-30 20:49:55 -05:00
func registerStdWndClass ( ) ( err error ) {
2014-02-12 19:51:07 -06:00
wc := & _WNDCLASS {
2014-06-03 09:52:53 -05:00
lpszClassName : utf16ToArg ( stdWndClass ) ,
2014-05-30 20:49:55 -05:00
lpfnWndProc : syscall . NewCallback ( stdWndProc ) ,
2014-02-12 19:51:07 -06:00
hInstance : hInstance ,
hIcon : icon ,
hCursor : cursor ,
hbrBackground : _HBRUSH ( _COLOR_BTNFACE + 1 ) ,
}
2014-05-30 20:49:55 -05:00
r1 , _ , err := _registerClass . Call ( uintptr ( unsafe . Pointer ( wc ) ) )
if r1 == 0 { // failure
return err
2014-02-12 19:51:07 -06:00
}
2014-05-30 20:49:55 -05:00
return nil
2014-02-12 19:51:07 -06:00
}
2014-05-25 14:09:01 -05:00
// no need to use/recreate MAKEINTRESOURCE() here as the Windows constant generator already took care of that because Microsoft's headers do already
2014-02-12 19:51:07 -06:00
func initWndClassInfo ( ) ( err error ) {
2014-02-11 18:18:03 -06:00
r1 , _ , err := user32 . NewProc ( "LoadIconW" ) . Call (
2014-02-11 16:38:38 -06:00
uintptr ( _NULL ) ,
2014-05-25 14:09:01 -05:00
uintptr ( _IDI_APPLICATION ) )
2014-02-11 18:27:58 -06:00
if r1 == 0 { // failure
2014-02-11 16:38:38 -06:00
return fmt . Errorf ( "error getting window icon: %v" , err )
2014-02-08 22:51:11 -06:00
}
2014-02-12 19:51:07 -06:00
icon = _HANDLE ( r1 )
2014-02-11 18:18:03 -06:00
r1 , _ , err = user32 . NewProc ( "LoadCursorW" ) . Call (
2014-02-11 16:38:38 -06:00
uintptr ( _NULL ) ,
2014-05-25 14:09:01 -05:00
uintptr ( _IDC_ARROW ) )
2014-02-11 18:27:58 -06:00
if r1 == 0 { // failure
2014-02-11 16:38:38 -06:00
return fmt . Errorf ( "error getting window cursor: %v" , err )
2014-02-08 22:51:11 -06:00
}
2014-02-12 19:51:07 -06:00
cursor = _HANDLE ( r1 )
2014-02-08 22:51:11 -06:00
2014-02-11 16:38:38 -06:00
return nil
2014-02-08 22:51:11 -06:00
}