-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwindow.go
208 lines (176 loc) · 5.34 KB
/
window.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
package gocoa
// #cgo CFLAGS: -x objective-c
// #cgo LDFLAGS: -framework Cocoa
// #include "window.h"
import "C"
import "unsafe"
// WindowEvent - different window delegate Events
type WindowEvent int
const (
didResize WindowEvent = 0
didMove WindowEvent = 1
didMiniaturize WindowEvent = 2
didDeminiaturize WindowEvent = 3
)
// EventHandler - handler functions that accepts the updated window as parameter
type EventHandler func(wnd *Window)
// Window is just that.
type Window struct {
title string
x int
y int
w int
h int
callbacks map[WindowEvent]EventHandler
winPtr unsafe.Pointer
}
// Screen the screen of the window.
type Screen struct {
X int
Y int
}
var windows []*Window
// NewWindow constructs and returns a new window.
func NewWindow(title string, x int, y int, w int, h int) *Window {
windowID := len(windows)
cTitle := C.CString(title)
defer C.free(unsafe.Pointer(cTitle))
wnd := &Window{
title: title,
x: x,
y: y,
w: w,
h: h,
callbacks: make(map[WindowEvent]EventHandler),
winPtr: C.Window_New(C.int(windowID), C.int(x), C.int(y), C.int(w), C.int(h), cTitle)}
windows = append(windows, wnd)
return wnd
}
// NewCenteredWindow constructs and returns a new window.
func NewCenteredWindow(title string, w int, h int) *Window {
windowID := len(windows)
cTitle := C.CString(title)
defer C.free(unsafe.Pointer(cTitle))
wnd := &Window{
title: title,
w: w,
h: h,
callbacks: make(map[WindowEvent]EventHandler),
winPtr: C.Centered_Window_New(C.int(windowID), C.int(w), C.int(h), cTitle)}
wnd.x = int(C.Screen_Center_X(wnd.winPtr))
wnd.y = int(C.Screen_Center_Y(wnd.winPtr))
windows = append(windows, wnd)
return wnd
}
// GetScreen - returns the screen dimensions
func (wnd *Window) GetScreen() *Screen {
return &Screen{
X: int(C.Screen_X(wnd.winPtr)),
Y: int(C.Screen_Y(wnd.winPtr))}
}
// MakeKeyAndOrderFront moves the window to the front of the screen list, within its
// level and it shows the window.
func (wnd *Window) MakeKeyAndOrderFront() {
C.Window_MakeKeyAndOrderFront(wnd.winPtr)
}
// AddButton adds a Button to the window.
func (wnd *Window) AddButton(btn *Button) {
C.Window_AddButton(wnd.winPtr, btn.buttonPtr)
}
// AddDatePicker adds a DatePicker to the window.
func (wnd *Window) AddDatePicker(datePicker *DatePicker) {
C.Window_AddDatePicker(wnd.winPtr, datePicker.datePickerPtr)
}
// AddTextView - adds a Button to the window.
func (wnd *Window) AddTextView(tv *TextView) {
C.Window_AddTextView(wnd.winPtr, tv.textViewPtr)
}
// AddTextField - adds a Button to the window.
func (wnd *Window) AddTextField(tv *TextField) {
C.Window_AddTextField(wnd.winPtr, tv.textFieldPtr)
}
// AddTextField - adds a Button to the window.
func (wnd *Window) AddLabel(tv *TextField) {
C.Window_AddTextField(wnd.winPtr, tv.textFieldPtr)
}
// AddProgressIndicator adds a ProgressIndicator to the window.
func (wnd *Window) AddProgressIndicator(indicator *ProgressIndicator) {
C.Window_AddProgressIndicator(wnd.winPtr, indicator.progressIndicatorPtr)
}
// AddImageView adds an ImageView to the window.
func (wnd *Window) AddImageView(imageView *ImageView) {
C.Window_AddImageView(wnd.winPtr, imageView.imageViewPtr)
}
func (wnd *Window) AddSlider(slider *Slider) {
C.Window_AddSlider(wnd.winPtr, slider.sliderPtr)
}
func (wnd *Window) AddComboBox(comboBox *ComboBox) {
C.Window_AddComboBox(wnd.winPtr, comboBox.comboBoxPtr)
}
// Update - forces the whole window to repaint
func (wnd *Window) Update() {
C.Window_Update(wnd.winPtr)
}
func (wnd *Window) SetTitle(title string) {
cTitle := C.CString(title)
defer C.free(unsafe.Pointer(cTitle))
C.Window_SetTitle(wnd.winPtr, cTitle)
}
func (wnd *Window) SetMiniaturizeButtonEnabled(enabled bool) {
if enabled {
C.Window_SetMiniaturizeButtonEnabled(wnd.winPtr, C.int(1))
} else {
C.Window_SetMiniaturizeButtonEnabled(wnd.winPtr, C.int(0))
}
}
func (wnd *Window) SetZoomButtonEnabled(enabled bool) {
if enabled {
C.Window_SetZoomButtonEnabled(wnd.winPtr, C.int(1))
} else {
C.Window_SetZoomButtonEnabled(wnd.winPtr, C.int(0))
}
}
func (wnd *Window) SetCloseButtonEnabled(enabled bool) {
if enabled {
C.Window_SetCloseButtonEnabled(wnd.winPtr, C.int(1))
} else {
C.Window_SetCloseButtonEnabled(wnd.winPtr, C.int(0))
}
}
func (wnd *Window) SetAllowsResizing(allowsResizing bool) {
if allowsResizing {
C.Window_SetAllowsResizing(wnd.winPtr, C.int(1))
} else {
C.Window_SetAllowsResizing(wnd.winPtr, C.int(0))
}
}
func (wnd *Window) AddDefaultQuitMenu() {
C.Window_AddDefaultQuitMenu(wnd.winPtr)
}
func (wnd *Window) OnDidResize(fn EventHandler) {
wnd.callbacks[didResize] = fn
}
func (wnd *Window) OnDidMiniaturize(fn EventHandler) {
wnd.callbacks[didMiniaturize] = fn
}
func (wnd *Window) OnDidDeminiaturize(fn EventHandler) {
wnd.callbacks[didDeminiaturize] = fn
}
func (wnd *Window) OnDidMove(fn EventHandler) {
wnd.callbacks[didMove] = fn
}
//export onWindowEvent
func onWindowEvent(id C.int, eventID C.int, x C.int, y C.int, w C.int, h C.int) {
windowID := int(id)
event := WindowEvent(eventID)
if windowID < len(windows) && windows[windowID].callbacks[event] != nil {
wnd := windows[windowID]
windows[windowID].callbacks[event](&Window{
title: wnd.title,
x: int(x),
y: int(y),
w: int(w),
h: int(h),
winPtr: wnd.winPtr})
}
}