From ddfb5c760345b03c16c1559bd3e83cbafb8b11c3 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Wed, 12 Feb 2014 11:29:20 -0500 Subject: [PATCH] Added buttons. --- control.go | 2 +- ...controls_windows.go => controls_windows.go | 95 ++++++++++--------- main.go | 12 ++- sysdata.go | 3 +- sysdata_windows.go | 20 ++-- window.go | 5 +- 6 files changed, 75 insertions(+), 62 deletions(-) rename unmigrated/controls_windows.go => controls_windows.go (85%) diff --git a/control.go b/control.go index b0a79a0..54f92d1 100644 --- a/control.go +++ b/control.go @@ -10,5 +10,5 @@ import ( type Control interface { apply() error setParent(c Control) - setParentWindow(w *Window) + parentWindow() *Window } diff --git a/unmigrated/controls_windows.go b/controls_windows.go similarity index 85% rename from unmigrated/controls_windows.go rename to controls_windows.go index f2c778b..06d7317 100644 --- a/unmigrated/controls_windows.go +++ b/controls_windows.go @@ -9,65 +9,65 @@ import ( // Button styles. const ( // from winuser.h - BS_PUSHBUTTON = 0x00000000 - BS_DEFPUSHBUTTON = 0x00000001 - BS_CHECKBOX = 0x00000002 - BS_AUTOCHECKBOX = 0x00000003 - BS_RADIOBUTTON = 0x00000004 - BS_3STATE = 0x00000005 - BS_AUTO3STATE = 0x00000006 - BS_GROUPBOX = 0x00000007 - BS_USERBUTTON = 0x00000008 - BS_AUTORADIOBUTTON = 0x00000009 - BS_PUSHBOX = 0x0000000A - BS_OWNERDRAW = 0x0000000B - BS_TYPEMASK = 0x0000000F - BS_LEFTTEXT = 0x00000020 - BS_TEXT = 0x00000000 - BS_ICON = 0x00000040 - BS_BITMAP = 0x00000080 - BS_LEFT = 0x00000100 - BS_RIGHT = 0x00000200 - BS_CENTER = 0x00000300 - BS_TOP = 0x00000400 - BS_BOTTOM = 0x00000800 - BS_VCENTER = 0x00000C00 - BS_PUSHLIKE = 0x00001000 - BS_MULTILINE = 0x00002000 - BS_NOTIFY = 0x00004000 - BS_FLAT = 0x00008000 - BS_RIGHTBUTTON = BS_LEFTTEXT + _BS_PUSHBUTTON = 0x00000000 + _BS_DEFPUSHBUTTON = 0x00000001 + _BS_CHECKBOX = 0x00000002 + _BS_AUTOCHECKBOX = 0x00000003 + _BS_RADIOBUTTON = 0x00000004 + _BS_3STATE = 0x00000005 + _BS_AUTO3STATE = 0x00000006 + _BS_GROUPBOX = 0x00000007 + _BS_USERBUTTON = 0x00000008 + _BS_AUTORADIOBUTTON = 0x00000009 + _BS_PUSHBOX = 0x0000000A + _BS_OWNERDRAW = 0x0000000B + _BS_TYPEMASK = 0x0000000F + _BS_LEFTTEXT = 0x00000020 + _BS_TEXT = 0x00000000 + _BS_ICON = 0x00000040 + _BS_BITMAP = 0x00000080 + _BS_LEFT = 0x00000100 + _BS_RIGHT = 0x00000200 + _BS_CENTER = 0x00000300 + _BS_TOP = 0x00000400 + _BS_BOTTOM = 0x00000800 + _BS_VCENTER = 0x00000C00 + _BS_PUSHLIKE = 0x00001000 + _BS_MULTILINE = 0x00002000 + _BS_NOTIFY = 0x00004000 + _BS_FLAT = 0x00008000 + _BS_RIGHTBUTTON = _BS_LEFTTEXT // from commctrl.h -// BS_SPLITBUTTON = 0x0000000C // Windows Vista and newer and(/or?) comctl6 only -// BS_DEFSPLITBUTTON = 0x0000000D // Windows Vista and newer and(/or?) comctl6 only -// BS_COMMANDLINK = 0x0000000E // Windows Vista and newer and(/or?) comctl6 only -// BS_DEFCOMMANDLINK = 0x0000000F // Windows Vista and newer and(/or?) comctl6 only +// _BS_SPLITBUTTON = 0x0000000C // Windows Vista and newer and(/or?) comctl6 only +// _BS_DEFSPLITBUTTON = 0x0000000D // Windows Vista and newer and(/or?) comctl6 only +// _BS_COMMANDLINK = 0x0000000E // Windows Vista and newer and(/or?) comctl6 only +// _BS_DEFCOMMANDLINK = 0x0000000F // Windows Vista and newer and(/or?) comctl6 only ) // Button WM_COMMAND notifications. const ( // from winuser.h - BN_CLICKED = 0 - BN_PAINT = 1 - BN_HILITE = 2 - BN_UNHILITE = 3 - BN_DISABLE = 4 - BN_DOUBLECLICKED = 5 - BN_PUSHED = BN_HILITE - BN_UNPUSHED = BN_UNHILITE - BN_DBLCLK = BN_DOUBLECLICKED - BN_SETFOCUS = 6 - BN_KILLFOCUS = 7 + _BN_CLICKED = 0 + _BN_PAINT = 1 + _BN_HILITE = 2 + _BN_UNHILITE = 3 + _BN_DISABLE = 4 + _BN_DOUBLECLICKED = 5 + _BN_PUSHED = _BN_HILITE + _BN_UNPUSHED = _BN_UNHILITE + _BN_DBLCLK = _BN_DOUBLECLICKED + _BN_SETFOCUS = 6 + _BN_KILLFOCUS = 7 ) // Button check states. const ( // from winuser.h - BST_UNCHECKED = 0x0000 - BST_CHECKED = 0x0001 - BST_INDETERMINATE = 0x0002 + _BST_UNCHECKED = 0x0000 + _BST_CHECKED = 0x0001 + _BST_INDETERMINATE = 0x0002 ) - +/* var ( checkDlgButton = user32.NewProc("CheckDlgButton") checkRadioButton = user32.NewProc("CheckRadioButton") @@ -402,3 +402,4 @@ const ( STN_ENABLE = 2 STN_DISABLE = 3 ) +*/ diff --git a/main.go b/main.go index 70caf80..09fae17 100644 --- a/main.go +++ b/main.go @@ -4,11 +4,21 @@ package main func main() { w := NewWindow("Main Window", 320, 240) w.Closing = make(chan struct{}) + b := NewButton("Click Me") + w.SetControl(b) err := w.Open() if err != nil { panic(err) } - <-w.Closing +mainloop: + for { + select { + case <-w.Closing: + break mainloop + case <-b.Clicked: + println("clicked") + } + } w.Close() } diff --git a/sysdata.go b/sysdata.go index 824df0c..1d4ecbd 100644 --- a/sysdata.go +++ b/sysdata.go @@ -7,7 +7,8 @@ import ( // The sysData type contains all system data. It provides the system-specific underlying implementation. It is guaranteed to have the following by embedding: type cSysData struct { - ctype int + ctype int + parentWindow *sysData // for Window closing chan struct{} diff --git a/sysdata_windows.go b/sysdata_windows.go index 554ae19..586b320 100644 --- a/sysdata_windows.go +++ b/sysdata_windows.go @@ -23,8 +23,8 @@ type classData struct { mkid bool } -//const controlstyle = _WS_CHILD | _WS_VISIBLE | _WS_TABSTOP -//const controlxstyle = 0 +const controlstyle = _WS_CHILD | _WS_VISIBLE | _WS_TABSTOP +const controlxstyle = 0 var classTypes = [nctypes]*classData{ c_window: &classData{ @@ -32,12 +32,12 @@ var classTypes = [nctypes]*classData{ style: _WS_OVERLAPPEDWINDOW, xstyle: 0, }, -// c_button: &classData{ -// name: "BUTTON" -// style: _BS_PUSHBUTTON | controlstyle, -// xstyle: 0 | controlxstyle, -// mkid: true, -// }, + c_button: &classData{ + name: "BUTTON", + style: _BS_PUSHBUTTON | controlstyle, + xstyle: 0 | controlxstyle, + mkid: true, + }, } var ( @@ -56,8 +56,10 @@ func (s *sysData) make(initText string, initWidth int, initHeight int) (err erro ret := make(chan uiret) defer close(ret) ct := classTypes[s.ctype] + pwin := uintptr(_NULL) if ct.mkid { s.cid = nextID() + pwin = uintptr(s.parentWindow.hwnd) } uitask <- &uimsg{ call: _createWindowEx, @@ -70,7 +72,7 @@ func (s *sysData) make(initText string, initWidth int, initHeight int) (err erro uintptr(_CW_USEDEFAULT), uintptr(initWidth), uintptr(initHeight), - uintptr(_NULL), // TODO parent + pwin, uintptr(s.cid), uintptr(hInstance), uintptr(_NULL), diff --git a/window.go b/window.go index b5971c6..780414b 100644 --- a/window.go +++ b/window.go @@ -47,7 +47,6 @@ func (w *Window) SetControl(control Control) (err error) { } w.control = control w.control.setParent(w) - w.control.setParentWindow(w) return nil } @@ -112,6 +111,6 @@ func (w *Window) apply() error { func (w *Window) setParent(c Control) { panic("Window.setParent() should never be called") } -func (w *Window) setParentWindow(w2 *Window) { - panic("Window.setParent() should never be called") +func (w *Window) parentWindow() *Window { + panic("Window.parentWindow() should never be called") }