OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [exp/] [wingui/] [gui.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
// +build windows
6
 
7
package main
8
 
9
import (
10
        "fmt"
11
        "os"
12
        "syscall"
13
        "unsafe"
14
)
15
 
16
// some help functions
17
 
18
func abortf(format string, a ...interface{}) {
19
        fmt.Fprintf(os.Stdout, format, a...)
20
        os.Exit(1)
21
}
22
 
23
func abortErrNo(funcname string, err error) {
24
        errno, _ := err.(syscall.Errno)
25
        abortf("%s failed: %d %s\n", funcname, uint32(errno), err)
26
}
27
 
28
// global vars
29
 
30
var (
31
        mh syscall.Handle
32
        bh syscall.Handle
33
)
34
 
35
// WinProc called by windows to notify us of all windows events we might be interested in.
36
func WndProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) (rc uintptr) {
37
        switch msg {
38
        case WM_CREATE:
39
                var e error
40
                // CreateWindowEx
41
                bh, e = CreateWindowEx(
42
                        0,
43
                        syscall.StringToUTF16Ptr("button"),
44
                        syscall.StringToUTF16Ptr("Quit"),
45
                        WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
46
                        75, 70, 140, 25,
47
                        hwnd, 1, mh, 0)
48
                if e != nil {
49
                        abortErrNo("CreateWindowEx", e)
50
                }
51
                fmt.Printf("button handle is %x\n", bh)
52
                rc = DefWindowProc(hwnd, msg, wparam, lparam)
53
        case WM_COMMAND:
54
                switch syscall.Handle(lparam) {
55
                case bh:
56
                        e := PostMessage(hwnd, WM_CLOSE, 0, 0)
57
                        if e != nil {
58
                                abortErrNo("PostMessage", e)
59
                        }
60
                default:
61
                        rc = DefWindowProc(hwnd, msg, wparam, lparam)
62
                }
63
        case WM_CLOSE:
64
                DestroyWindow(hwnd)
65
        case WM_DESTROY:
66
                PostQuitMessage(0)
67
        default:
68
                rc = DefWindowProc(hwnd, msg, wparam, lparam)
69
        }
70
        //fmt.Printf("WndProc(0x%08x, %d, 0x%08x, 0x%08x) (%d)\n", hwnd, msg, wparam, lparam, rc)
71
        return
72
}
73
 
74
func rungui() int {
75
        var e error
76
 
77
        // GetModuleHandle
78
        mh, e = GetModuleHandle(nil)
79
        if e != nil {
80
                abortErrNo("GetModuleHandle", e)
81
        }
82
 
83
        // Get icon we're going to use.
84
        myicon, e := LoadIcon(0, IDI_APPLICATION)
85
        if e != nil {
86
                abortErrNo("LoadIcon", e)
87
        }
88
 
89
        // Get cursor we're going to use.
90
        mycursor, e := LoadCursor(0, IDC_ARROW)
91
        if e != nil {
92
                abortErrNo("LoadCursor", e)
93
        }
94
 
95
        // Create callback
96
        wproc := syscall.NewCallback(WndProc)
97
 
98
        // RegisterClassEx
99
        wcname := syscall.StringToUTF16Ptr("myWindowClass")
100
        var wc Wndclassex
101
        wc.Size = uint32(unsafe.Sizeof(wc))
102
        wc.WndProc = wproc
103
        wc.Instance = mh
104
        wc.Icon = myicon
105
        wc.Cursor = mycursor
106
        wc.Background = COLOR_BTNFACE + 1
107
        wc.MenuName = nil
108
        wc.ClassName = wcname
109
        wc.IconSm = myicon
110
        if _, e := RegisterClassEx(&wc); e != nil {
111
                abortErrNo("RegisterClassEx", e)
112
        }
113
 
114
        // CreateWindowEx
115
        wh, e := CreateWindowEx(
116
                WS_EX_CLIENTEDGE,
117
                wcname,
118
                syscall.StringToUTF16Ptr("My window"),
119
                WS_OVERLAPPEDWINDOW,
120
                CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
121
                0, 0, mh, 0)
122
        if e != nil {
123
                abortErrNo("CreateWindowEx", e)
124
        }
125
        fmt.Printf("main window handle is %x\n", wh)
126
 
127
        // ShowWindow
128
        ShowWindow(wh, SW_SHOWDEFAULT)
129
 
130
        // UpdateWindow
131
        if e := UpdateWindow(wh); e != nil {
132
                abortErrNo("UpdateWindow", e)
133
        }
134
 
135
        // Process all windows messages until WM_QUIT.
136
        var m Msg
137
        for {
138
                r, e := GetMessage(&m, 0, 0, 0)
139
                if e != nil {
140
                        abortErrNo("GetMessage", e)
141
                }
142
                if r == 0 {
143
                        // WM_QUIT received -> get out
144
                        break
145
                }
146
                TranslateMessage(&m)
147
                DispatchMessage(&m)
148
        }
149
        return int(m.Wparam)
150
}
151
 
152
func main() {
153
        rc := rungui()
154
        os.Exit(rc)
155
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.