-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
213 lines (190 loc) · 4.62 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// 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"
"image"
"image/color"
"os"
"unsafe"
"github.com/alexbrainman/gowingui/gui"
"github.com/alexbrainman/gowingui/subclass"
"github.com/alexbrainman/gowingui/uit"
"github.com/alexbrainman/gowingui/winapi"
)
// printClientRect prints control's c client rectangle with prefix title.
func printClientRect(c gui.Control) {
fmt.Printf("%s=%v\n", c.Text(), c.ClientRect())
}
// mvto moves rectangle r by x, y.
func mvto(r image.Rectangle, x, y int) image.Rectangle {
return r.Add(image.Pt(x, y))
}
// MainWindow represents main window.
type MainWindow struct {
w *gui.Window // main window
quit *gui.Button // quit button
edit *gui.Edit // edit control
label *gui.Label // label control
change *gui.Button // change button
panel *gui.Panel // panel
}
func NewMainWindow() (*MainWindow, error) {
c, err := gui.RegisterWindowClass("my class")
if err != nil {
return nil, err
}
var m MainWindow
m.w, err = c.CreateWindow("My window", mvto(image.Rect(0, 0, 300, 400), 200, 100))
if err != nil {
return nil, err
}
printClientRect(m.w)
err = m.w.AddHandler(func(p *subclass.Params) uintptr {
switch p.Msg {
case winapi.WM_GETMINMAXINFO:
i := (*winapi.MinMaxInfo)(unsafe.Pointer(p.Lparam))
i.MinTrackSize.X = 200
i.MinTrackSize.Y = 300
i.MaxTrackSize.X = 400
i.MaxTrackSize.Y = 500
return 0
// quit the app once w window is closed
case winapi.WM_CLOSE:
m.w.Destroy()
return 0
case winapi.WM_NCDESTROY:
uit.M.Stop()
}
return p.CallDefaultProc()
})
if err != nil {
return nil, err
}
r := image.Rect(0, 0, 120, 25) // default button size
m.quit, err = m.w.AddButton(100, "Quit", mvto(r, 160, 30))
if err != nil {
return nil, err
}
printClientRect(m.quit)
err = m.quit.AddClick(func() {
// clicking quit button will close window
m.w.Close()
})
if err != nil {
return nil, err
}
m.edit, err = m.w.AddEdit(0, "Some text", mvto(r, 20, 30))
if err != nil {
return nil, err
}
printClientRect(m.edit)
m.label, err = m.w.AddLabel(0, "Alex", mvto(r, 40, 100))
if err != nil {
return nil, err
}
printClientRect(m.label)
m.change, err = m.w.AddButton(101, "Change my text", mvto(r, 160, 100))
if err != nil {
return nil, err
}
printClientRect(m.change)
err = m.change.AddClick(func() {
m.change.SetText(m.edit.Text())
if m.edit.IsEnabled() {
m.edit.Disable()
} else {
m.edit.Enable()
}
})
if err != nil {
return nil, err
}
m.panel, err = m.w.AddPanel(m.PanelRect())
if err != nil {
return nil, err
}
printClientRect(m.panel)
err = m.panel.AddPaint(func(c *gui.PaintCanvas) {
c.SetTextColor(color.RGBA{0, 0, 255, 0})
c.SetBkColor(color.RGBA{0, 255, 0, 0})
// TODO: font can, probably, be opened once at the program start
f, err := gui.OpenFont("Broadway", 0, winapi.FW_BOLD, gui.Italic|gui.Underline)
if err != nil {
panic(err)
}
defer f.Close()
c.SetFont(f)
c.TextOut(image.Point{10, 20}, "Hello \U00000421\U00000430\U00000448\U00000430")
})
if err != nil {
return nil, err
}
// track main window resizing
delta := m.w.ClientRect().Max.X - m.quit.Rect().Min.X
err = m.w.AddResize(func(width, height int) {
r := m.quit.Rect()
newdelta := m.w.ClientRect().Max.X - r.Min.X
shift := image.Point{newdelta - delta, 0}
m.quit.SetRect(r.Add(shift))
m.quit.Invalidate()
m.change.SetRect(m.change.Rect().Add(shift))
m.change.Invalidate()
m.panel.SetRect(m.PanelRect())
m.panel.Invalidate()
})
if err != nil {
return nil, err
}
// using go statement here, just to show that we can
go func() {
// track hovering over change button
bp := func(p *subclass.Params) uintptr {
if p.Msg == winapi.WM_SETCURSOR {
var p winapi.Point
uit.M.GetCursorPos(&p)
m.label.SetText(fmt.Sprintf("(x=%d,y=%d)\n", p.X, p.Y))
}
return p.CallDefaultProc()
}
p, err := subclass.New(m.change.Handle, bp)
if err != nil {
panic("subclass.New: " + err.Error())
}
defer p.Remove()
select {}
}()
return &m, nil
}
func (m *MainWindow) PanelRect() image.Rectangle {
return image.Rect(
m.edit.Rect().Min.X,
m.change.Rect().Max.Y+10,
m.quit.Rect().Max.X,
m.w.ClientRect().Dy()-10)
}
func rungui() (int, error) {
m, err := NewMainWindow()
if err != nil {
return 0, err
}
m.w.Show()
err = m.w.Update()
if err != nil {
return 0, err
}
rc, err := uit.M.Wait()
if err != nil {
return 0, err
}
return rc, nil
}
func main() {
rc, err := rungui()
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
os.Exit(rc)
}