summaryrefslogtreecommitdiff
path: root/src/pkg/exp/wingui
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/exp/wingui')
-rw-r--r--src/pkg/exp/wingui/Makefile29
-rw-r--r--src/pkg/exp/wingui/gui.go153
-rw-r--r--src/pkg/exp/wingui/winapi.go131
-rw-r--r--src/pkg/exp/wingui/zwinapi.go211
4 files changed, 524 insertions, 0 deletions
diff --git a/src/pkg/exp/wingui/Makefile b/src/pkg/exp/wingui/Makefile
new file mode 100644
index 000000000..00f7d234d
--- /dev/null
+++ b/src/pkg/exp/wingui/Makefile
@@ -0,0 +1,29 @@
+# Copyright 2011 The Go Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style
+# license that can be found in the LICENSE file.
+
+GOOS=windows
+
+include ../../../Make.inc
+
+LD:=$(LD) -Hwindowsgui
+
+TARG=wingui
+
+GOFILES=\
+ gui.go\
+ winapi.go\
+ zwinapi.go\
+
+include ../../../Make.cmd
+
+zwinapi.go: winapi.go
+ $(GOROOT)/src/pkg/syscall/mksyscall_windows.pl $< \
+ | sed 's/^package.*syscall$$/package main/' \
+ | sed '/^import/a \
+ import "syscall"' \
+ | sed 's/Syscall/syscall.Syscall/' \
+ | sed 's/NewLazyDLL/syscall.NewLazyDLL/' \
+ | sed 's/EINVAL/syscall.EINVAL/' \
+ | gofmt \
+ > $@
diff --git a/src/pkg/exp/wingui/gui.go b/src/pkg/exp/wingui/gui.go
new file mode 100644
index 000000000..cf392934c
--- /dev/null
+++ b/src/pkg/exp/wingui/gui.go
@@ -0,0 +1,153 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "fmt"
+ "syscall"
+ "os"
+ "unsafe"
+)
+
+// some help functions
+
+func abortf(format string, a ...interface{}) {
+ fmt.Fprintf(os.Stdout, format, a...)
+ os.Exit(1)
+}
+
+func abortErrNo(funcname string, err int) {
+ abortf("%s failed: %d %s\n", funcname, err, syscall.Errstr(err))
+}
+
+// global vars
+
+var (
+ mh uint32
+ bh uint32
+)
+
+// WinProc called by windows to notify us of all windows events we might be interested in.
+func WndProc(hwnd, msg uint32, wparam, lparam int32) uintptr {
+ var rc int32
+ switch msg {
+ case WM_CREATE:
+ var e int
+ // CreateWindowEx
+ bh, e = CreateWindowEx(
+ 0,
+ syscall.StringToUTF16Ptr("button"),
+ syscall.StringToUTF16Ptr("Quit"),
+ WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
+ 75, 70, 140, 25,
+ hwnd, 1, mh, 0)
+ if e != 0 {
+ abortErrNo("CreateWindowEx", e)
+ }
+ fmt.Printf("button handle is %x\n", bh)
+ rc = DefWindowProc(hwnd, msg, wparam, lparam)
+ case WM_COMMAND:
+ switch uint32(lparam) {
+ case bh:
+ e := PostMessage(hwnd, WM_CLOSE, 0, 0)
+ if e != 0 {
+ abortErrNo("PostMessage", e)
+ }
+ default:
+ rc = DefWindowProc(hwnd, msg, wparam, lparam)
+ }
+ case WM_CLOSE:
+ DestroyWindow(hwnd)
+ case WM_DESTROY:
+ PostQuitMessage(0)
+ default:
+ rc = DefWindowProc(hwnd, msg, wparam, lparam)
+ }
+ //fmt.Printf("WndProc(0x%08x, %d, 0x%08x, 0x%08x) (%d)\n", hwnd, msg, wparam, lparam, rc)
+ return uintptr(rc)
+}
+
+func rungui() int {
+ var e int
+
+ // GetModuleHandle
+ mh, e = GetModuleHandle(nil)
+ if e != 0 {
+ abortErrNo("GetModuleHandle", e)
+ }
+
+ // Get icon we're going to use.
+ myicon, e := LoadIcon(0, IDI_APPLICATION)
+ if e != 0 {
+ abortErrNo("LoadIcon", e)
+ }
+
+ // Get cursor we're going to use.
+ mycursor, e := LoadCursor(0, IDC_ARROW)
+ if e != 0 {
+ abortErrNo("LoadCursor", e)
+ }
+
+ // Create callback
+ wproc := syscall.NewCallback(WndProc)
+
+ // RegisterClassEx
+ wcname := syscall.StringToUTF16Ptr("myWindowClass")
+ var wc Wndclassex
+ wc.Size = uint32(unsafe.Sizeof(wc))
+ wc.WndProc = wproc
+ wc.Instance = mh
+ wc.Icon = myicon
+ wc.Cursor = mycursor
+ wc.Background = COLOR_BTNFACE + 1
+ wc.MenuName = nil
+ wc.ClassName = wcname
+ wc.IconSm = myicon
+ if _, e := RegisterClassEx(&wc); e != 0 {
+ abortErrNo("RegisterClassEx", e)
+ }
+
+ // CreateWindowEx
+ wh, e := CreateWindowEx(
+ WS_EX_CLIENTEDGE,
+ wcname,
+ syscall.StringToUTF16Ptr("My window"),
+ WS_OVERLAPPEDWINDOW,
+ CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
+ 0, 0, mh, 0)
+ if e != 0 {
+ abortErrNo("CreateWindowEx", e)
+ }
+ fmt.Printf("main window handle is %x\n", wh)
+
+ // ShowWindow
+ ShowWindow(wh, SW_SHOWDEFAULT)
+
+ // UpdateWindow
+ if e := UpdateWindow(wh); e != 0 {
+ abortErrNo("UpdateWindow", e)
+ }
+
+ // Process all windows messages until WM_QUIT.
+ var m Msg
+ for {
+ r, e := GetMessage(&m, 0, 0, 0)
+ if e != 0 {
+ abortErrNo("GetMessage", e)
+ }
+ if r == 0 {
+ // WM_QUIT received -> get out
+ break
+ }
+ TranslateMessage(&m)
+ DispatchMessage(&m)
+ }
+ return int(m.Wparam)
+}
+
+func main() {
+ rc := rungui()
+ os.Exit(rc)
+}
diff --git a/src/pkg/exp/wingui/winapi.go b/src/pkg/exp/wingui/winapi.go
new file mode 100644
index 000000000..31b57a2cc
--- /dev/null
+++ b/src/pkg/exp/wingui/winapi.go
@@ -0,0 +1,131 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "unsafe"
+)
+
+type Wndclassex struct {
+ Size uint32
+ Style uint32
+ WndProc uintptr
+ ClsExtra int32
+ WndExtra int32
+ Instance uint32
+ Icon uint32
+ Cursor uint32
+ Background uint32
+ MenuName *uint16
+ ClassName *uint16
+ IconSm uint32
+}
+
+type Point struct {
+ X int32
+ Y int32
+}
+
+type Msg struct {
+ Hwnd uint32
+ Message uint32
+ Wparam int32
+ Lparam int32
+ Time uint32
+ Pt Point
+}
+
+const (
+ // Window styles
+ WS_OVERLAPPED = 0
+ WS_POPUP = 0x80000000
+ WS_CHILD = 0x40000000
+ WS_MINIMIZE = 0x20000000
+ WS_VISIBLE = 0x10000000
+ WS_DISABLED = 0x8000000
+ WS_CLIPSIBLINGS = 0x4000000
+ WS_CLIPCHILDREN = 0x2000000
+ WS_MAXIMIZE = 0x1000000
+ WS_CAPTION = WS_BORDER | WS_DLGFRAME
+ WS_BORDER = 0x800000
+ WS_DLGFRAME = 0x400000
+ WS_VSCROLL = 0x200000
+ WS_HSCROLL = 0x100000
+ WS_SYSMENU = 0x80000
+ WS_THICKFRAME = 0x40000
+ WS_GROUP = 0x20000
+ WS_TABSTOP = 0x10000
+ WS_MINIMIZEBOX = 0x20000
+ WS_MAXIMIZEBOX = 0x10000
+ WS_TILED = WS_OVERLAPPED
+ WS_ICONIC = WS_MINIMIZE
+ WS_SIZEBOX = WS_THICKFRAME
+ // Common Window Styles
+ WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX
+ WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW
+ WS_POPUPWINDOW = WS_POPUP | WS_BORDER | WS_SYSMENU
+ WS_CHILDWINDOW = WS_CHILD
+
+ WS_EX_CLIENTEDGE = 0x200
+
+ // Some windows messages
+ WM_CREATE = 1
+ WM_DESTROY = 2
+ WM_CLOSE = 16
+ WM_COMMAND = 273
+
+ // Some button control styles
+ BS_DEFPUSHBUTTON = 1
+
+ // Some color constants
+ COLOR_WINDOW = 5
+ COLOR_BTNFACE = 15
+
+ // Default window position
+ CW_USEDEFAULT = 0x80000000 - 0x100000000
+
+ // Show window default style
+ SW_SHOWDEFAULT = 10
+)
+
+var (
+ // Some globally known cursors
+ IDC_ARROW = MakeIntResource(32512)
+ IDC_IBEAM = MakeIntResource(32513)
+ IDC_WAIT = MakeIntResource(32514)
+ IDC_CROSS = MakeIntResource(32515)
+
+ // Some globally known icons
+ IDI_APPLICATION = MakeIntResource(32512)
+ IDI_HAND = MakeIntResource(32513)
+ IDI_QUESTION = MakeIntResource(32514)
+ IDI_EXCLAMATION = MakeIntResource(32515)
+ IDI_ASTERISK = MakeIntResource(32516)
+ IDI_WINLOGO = MakeIntResource(32517)
+ IDI_WARNING = IDI_EXCLAMATION
+ IDI_ERROR = IDI_HAND
+ IDI_INFORMATION = IDI_ASTERISK
+)
+
+//sys GetModuleHandle(modname *uint16) (handle uint32, errno int) = GetModuleHandleW
+//sys RegisterClassEx(wndclass *Wndclassex) (atom uint16, errno int) = user32.RegisterClassExW
+//sys CreateWindowEx(exstyle uint32, classname *uint16, windowname *uint16, style uint32, x int32, y int32, width int32, height int32, wndparent uint32, menu uint32, instance uint32, param uintptr) (hwnd uint32, errno int) = user32.CreateWindowExW
+//sys DefWindowProc(hwnd uint32, msg uint32, wparam int32, lparam int32) (lresult int32) = user32.DefWindowProcW
+//sys DestroyWindow(hwnd uint32) (errno int) = user32.DestroyWindow
+//sys PostQuitMessage(exitcode int32) = user32.PostQuitMessage
+//sys ShowWindow(hwnd uint32, cmdshow int32) (wasvisible bool) = user32.ShowWindow
+//sys UpdateWindow(hwnd uint32) (errno int) = user32.UpdateWindow
+//sys GetMessage(msg *Msg, hwnd uint32, MsgFilterMin uint32, MsgFilterMax uint32) (ret int32, errno int) [failretval==-1] = user32.GetMessageW
+//sys TranslateMessage(msg *Msg) (done bool) = user32.TranslateMessage
+//sys DispatchMessage(msg *Msg) (ret int32) = user32.DispatchMessageW
+//sys LoadIcon(instance uint32, iconname *uint16) (icon uint32, errno int) = user32.LoadIconW
+//sys LoadCursor(instance uint32, cursorname *uint16) (cursor uint32, errno int) = user32.LoadCursorW
+//sys SetCursor(cursor uint32) (precursor uint32, errno int) = user32.SetCursor
+//sys SendMessage(hwnd uint32, msg uint32, wparam int32, lparam int32) (lresult int32) = user32.SendMessageW
+//sys PostMessage(hwnd uint32, msg uint32, wparam int32, lparam int32) (errno int) = user32.PostMessageW
+
+func MakeIntResource(id uint16) *uint16 {
+ return (*uint16)(unsafe.Pointer(uintptr(id)))
+}
diff --git a/src/pkg/exp/wingui/zwinapi.go b/src/pkg/exp/wingui/zwinapi.go
new file mode 100644
index 000000000..4c009dd69
--- /dev/null
+++ b/src/pkg/exp/wingui/zwinapi.go
@@ -0,0 +1,211 @@
+// mksyscall_windows.pl winapi.go
+// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
+
+package main
+
+import "unsafe"
+import "syscall"
+
+var (
+ modkernel32 = syscall.NewLazyDLL("kernel32.dll")
+ moduser32 = syscall.NewLazyDLL("user32.dll")
+
+ procGetModuleHandleW = modkernel32.NewProc("GetModuleHandleW")
+ procRegisterClassExW = moduser32.NewProc("RegisterClassExW")
+ procCreateWindowExW = moduser32.NewProc("CreateWindowExW")
+ procDefWindowProcW = moduser32.NewProc("DefWindowProcW")
+ procDestroyWindow = moduser32.NewProc("DestroyWindow")
+ procPostQuitMessage = moduser32.NewProc("PostQuitMessage")
+ procShowWindow = moduser32.NewProc("ShowWindow")
+ procUpdateWindow = moduser32.NewProc("UpdateWindow")
+ procGetMessageW = moduser32.NewProc("GetMessageW")
+ procTranslateMessage = moduser32.NewProc("TranslateMessage")
+ procDispatchMessageW = moduser32.NewProc("DispatchMessageW")
+ procLoadIconW = moduser32.NewProc("LoadIconW")
+ procLoadCursorW = moduser32.NewProc("LoadCursorW")
+ procSetCursor = moduser32.NewProc("SetCursor")
+ procSendMessageW = moduser32.NewProc("SendMessageW")
+ procPostMessageW = moduser32.NewProc("PostMessageW")
+)
+
+func GetModuleHandle(modname *uint16) (handle uint32, errno int) {
+ r0, _, e1 := syscall.Syscall(procGetModuleHandleW.Addr(), 1, uintptr(unsafe.Pointer(modname)), 0, 0)
+ handle = uint32(r0)
+ if handle == 0 {
+ if e1 != 0 {
+ errno = int(e1)
+ } else {
+ errno = syscall.EINVAL
+ }
+ } else {
+ errno = 0
+ }
+ return
+}
+
+func RegisterClassEx(wndclass *Wndclassex) (atom uint16, errno int) {
+ r0, _, e1 := syscall.Syscall(procRegisterClassExW.Addr(), 1, uintptr(unsafe.Pointer(wndclass)), 0, 0)
+ atom = uint16(r0)
+ if atom == 0 {
+ if e1 != 0 {
+ errno = int(e1)
+ } else {
+ errno = syscall.EINVAL
+ }
+ } else {
+ errno = 0
+ }
+ return
+}
+
+func CreateWindowEx(exstyle uint32, classname *uint16, windowname *uint16, style uint32, x int32, y int32, width int32, height int32, wndparent uint32, menu uint32, instance uint32, param uintptr) (hwnd uint32, errno int) {
+ r0, _, e1 := syscall.Syscall12(procCreateWindowExW.Addr(), 12, uintptr(exstyle), uintptr(unsafe.Pointer(classname)), uintptr(unsafe.Pointer(windowname)), uintptr(style), uintptr(x), uintptr(y), uintptr(width), uintptr(height), uintptr(wndparent), uintptr(menu), uintptr(instance), uintptr(param))
+ hwnd = uint32(r0)
+ if hwnd == 0 {
+ if e1 != 0 {
+ errno = int(e1)
+ } else {
+ errno = syscall.EINVAL
+ }
+ } else {
+ errno = 0
+ }
+ return
+}
+
+func DefWindowProc(hwnd uint32, msg uint32, wparam int32, lparam int32) (lresult int32) {
+ r0, _, _ := syscall.Syscall6(procDefWindowProcW.Addr(), 4, uintptr(hwnd), uintptr(msg), uintptr(wparam), uintptr(lparam), 0, 0)
+ lresult = int32(r0)
+ return
+}
+
+func DestroyWindow(hwnd uint32) (errno int) {
+ r1, _, e1 := syscall.Syscall(procDestroyWindow.Addr(), 1, uintptr(hwnd), 0, 0)
+ if int(r1) == 0 {
+ if e1 != 0 {
+ errno = int(e1)
+ } else {
+ errno = syscall.EINVAL
+ }
+ } else {
+ errno = 0
+ }
+ return
+}
+
+func PostQuitMessage(exitcode int32) {
+ syscall.Syscall(procPostQuitMessage.Addr(), 1, uintptr(exitcode), 0, 0)
+ return
+}
+
+func ShowWindow(hwnd uint32, cmdshow int32) (wasvisible bool) {
+ r0, _, _ := syscall.Syscall(procShowWindow.Addr(), 2, uintptr(hwnd), uintptr(cmdshow), 0)
+ wasvisible = bool(r0 != 0)
+ return
+}
+
+func UpdateWindow(hwnd uint32) (errno int) {
+ r1, _, e1 := syscall.Syscall(procUpdateWindow.Addr(), 1, uintptr(hwnd), 0, 0)
+ if int(r1) == 0 {
+ if e1 != 0 {
+ errno = int(e1)
+ } else {
+ errno = syscall.EINVAL
+ }
+ } else {
+ errno = 0
+ }
+ return
+}
+
+func GetMessage(msg *Msg, hwnd uint32, MsgFilterMin uint32, MsgFilterMax uint32) (ret int32, errno int) {
+ r0, _, e1 := syscall.Syscall6(procGetMessageW.Addr(), 4, uintptr(unsafe.Pointer(msg)), uintptr(hwnd), uintptr(MsgFilterMin), uintptr(MsgFilterMax), 0, 0)
+ ret = int32(r0)
+ if ret == -1 {
+ if e1 != 0 {
+ errno = int(e1)
+ } else {
+ errno = syscall.EINVAL
+ }
+ } else {
+ errno = 0
+ }
+ return
+}
+
+func TranslateMessage(msg *Msg) (done bool) {
+ r0, _, _ := syscall.Syscall(procTranslateMessage.Addr(), 1, uintptr(unsafe.Pointer(msg)), 0, 0)
+ done = bool(r0 != 0)
+ return
+}
+
+func DispatchMessage(msg *Msg) (ret int32) {
+ r0, _, _ := syscall.Syscall(procDispatchMessageW.Addr(), 1, uintptr(unsafe.Pointer(msg)), 0, 0)
+ ret = int32(r0)
+ return
+}
+
+func LoadIcon(instance uint32, iconname *uint16) (icon uint32, errno int) {
+ r0, _, e1 := syscall.Syscall(procLoadIconW.Addr(), 2, uintptr(instance), uintptr(unsafe.Pointer(iconname)), 0)
+ icon = uint32(r0)
+ if icon == 0 {
+ if e1 != 0 {
+ errno = int(e1)
+ } else {
+ errno = syscall.EINVAL
+ }
+ } else {
+ errno = 0
+ }
+ return
+}
+
+func LoadCursor(instance uint32, cursorname *uint16) (cursor uint32, errno int) {
+ r0, _, e1 := syscall.Syscall(procLoadCursorW.Addr(), 2, uintptr(instance), uintptr(unsafe.Pointer(cursorname)), 0)
+ cursor = uint32(r0)
+ if cursor == 0 {
+ if e1 != 0 {
+ errno = int(e1)
+ } else {
+ errno = syscall.EINVAL
+ }
+ } else {
+ errno = 0
+ }
+ return
+}
+
+func SetCursor(cursor uint32) (precursor uint32, errno int) {
+ r0, _, e1 := syscall.Syscall(procSetCursor.Addr(), 1, uintptr(cursor), 0, 0)
+ precursor = uint32(r0)
+ if precursor == 0 {
+ if e1 != 0 {
+ errno = int(e1)
+ } else {
+ errno = syscall.EINVAL
+ }
+ } else {
+ errno = 0
+ }
+ return
+}
+
+func SendMessage(hwnd uint32, msg uint32, wparam int32, lparam int32) (lresult int32) {
+ r0, _, _ := syscall.Syscall6(procSendMessageW.Addr(), 4, uintptr(hwnd), uintptr(msg), uintptr(wparam), uintptr(lparam), 0, 0)
+ lresult = int32(r0)
+ return
+}
+
+func PostMessage(hwnd uint32, msg uint32, wparam int32, lparam int32) (errno int) {
+ r1, _, e1 := syscall.Syscall6(procPostMessageW.Addr(), 4, uintptr(hwnd), uintptr(msg), uintptr(wparam), uintptr(lparam), 0, 0)
+ if int(r1) == 0 {
+ if e1 != 0 {
+ errno = int(e1)
+ } else {
+ errno = syscall.EINVAL
+ }
+ } else {
+ errno = 0
+ }
+ return
+}