-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow_linux.go
364 lines (316 loc) · 9.18 KB
/
mainwindow_linux.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package goey
import (
"image"
"bitbucket.org/rj/goey/base"
"bitbucket.org/rj/goey/dialog"
"bitbucket.org/rj/goey/internal/syscall"
"bitbucket.org/rj/goey/loop"
"github.com/gotk3/gotk3/gdk"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)
var (
vscrollbarWidth base.Length
)
func init() {
gtk.Init(nil)
}
func boolToPolicy(value bool) gtk.PolicyType {
if value {
return gtk.POLICY_ALWAYS
}
return gtk.POLICY_NEVER
}
type windowImpl struct {
handle *gtk.Window
scroll *gtk.ScrolledWindow
layout *gtk.Layout
child base.Element
childMinSize base.Size
horizontalScroll bool
horizontalScrollVisible bool
verticalScroll bool
verticalScrollVisible bool
onClosing func() bool
shClosing glib.SignalHandle
}
func newWindow(title string, child base.Widget) (*Window, error) {
// Create a new GTK window
app, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
if err != nil {
return nil, err
}
loop.AddLockCount(1)
scroll, err := gtk.ScrolledWindowNew(nil, nil)
if err != nil {
return nil, err
}
scroll.SetPolicy(gtk.POLICY_NEVER, gtk.POLICY_NEVER)
app.Add(scroll)
layout, err := gtk.LayoutNew(nil, nil)
if err != nil {
return nil, err
}
scroll.Add(layout)
retval := &Window{windowImpl{
handle: app,
scroll: scroll,
layout: layout,
}}
app.SetTitle(title)
app.SetBorderWidth(0)
app.Connect("destroy", mainwindowOnDestroy, retval)
app.Connect("size-allocate", mainwindowOnSizeAllocate, retval)
app.SetDefaultSize(func() (int, int) {
w, h := sizeDefaults()
return int(w), int(h)
}())
return retval, nil
}
func windowOnClosing(widget *gtk.Window, _ *gdk.Event, w *windowImpl) bool {
return w.onClosing()
}
func (w *windowImpl) onSize() {
if w.child == nil {
return
}
// Update the global DPI
base.DPI.X, base.DPI.Y = 96, 96
width, height := w.handle.GetSize()
clientSize := base.Size{base.FromPixelsX(width), base.FromPixelsY(height)}
size := w.layoutChild(clientSize)
if w.horizontalScroll && w.verticalScroll {
// Show scroll bars if necessary.
w.showScrollV(size.Height, clientSize.Height)
ok := w.showScrollH(size.Width, clientSize.Width)
// Adding horizontal scroll take vertical space, so we need to check
// again for vertical scroll.
if ok {
_, height := w.handle.GetSize()
w.showScrollV(size.Height, base.FromPixelsY(height))
}
} else if w.verticalScroll {
// Show scroll bars if necessary.
ok := w.showScrollV(size.Height, clientSize.Height)
if ok {
width, height := w.handle.GetSize()
clientSize := base.Size{base.FromPixelsX(width), base.FromPixelsY(height)}
size = w.layoutChild(clientSize)
}
} else if w.horizontalScroll {
// Show scroll bars if necessary.
ok := w.showScrollH(size.Width, clientSize.Width)
if ok {
width, height := w.handle.GetSize()
clientSize := base.Size{base.FromPixelsX(width), base.FromPixelsY(height)}
size = w.layoutChild(clientSize)
}
}
w.layout.SetSize(uint(size.Width.PixelsX()), uint(size.Height.PixelsY()))
bounds := base.Rectangle{
base.Point{}, base.Point{size.Width, size.Height},
}
w.child.SetBounds(bounds)
}
func (w *windowImpl) control() base.Control {
return base.Control{&w.layout.Container}
}
func (w *windowImpl) close() {
if w.handle != nil {
w.handle.Destroy()
w.handle = nil
}
}
func (w *windowImpl) message(m *dialog.Message) {
title, _ := w.handle.GetTitle()
// TODO: Error handling for above
m.WithTitle(title)
m.WithParent(w.handle)
}
func (w *windowImpl) openfiledialog(m *dialog.OpenFile) {
m.WithParent(w.handle)
}
func (w *windowImpl) savefiledialog(m *dialog.SaveFile) {
m.WithParent(w.handle)
}
// Screenshot returns an image of the window, as displayed on screen.
func (w *windowImpl) Screenshot() (image.Image, error) {
screen, err := w.handle.GetScreen()
if err != nil {
return nil, err
}
rw, err := screen.GetRootWindow()
if err != nil {
return nil, err
}
pixbuf := syscall.PixbufGetFromWindow(rw, w.handle)
if pixbuf == nil {
panic("nil pointer")
}
// Convert the pixbuf to a image.Image.
img := pixbufToImage(pixbuf)
return img, nil
}
func get_vscrollbar_width(window *gtk.Window) (base.Length, error) {
if vscrollbarWidth != 0 {
return vscrollbarWidth, nil
}
oldChild, err := window.GetChild()
if err != nil {
return 0, err
}
window.Remove(oldChild)
sb, err := gtk.ScrollbarNew(gtk.ORIENTATION_VERTICAL, nil)
if err != nil {
return 0, err
}
window.Add(sb)
sb.Show()
_, retval := sb.GetPreferredWidth()
sb.Destroy()
window.Add(oldChild)
vscrollbarWidth = base.FromPixelsX(retval)
return vscrollbarWidth, nil
}
func (w *windowImpl) setChildPost() {
// Redo the layout so the children are placed.
if w.child != nil {
// Update the global DPI
base.DPI.X, base.DPI.Y = 96, 96
// Constrain window size
w.updateWindowMinSize()
// Properties may have changed sizes, so we need to do layout.
w.onSize()
} else {
// Ensure that the scrollbars are hidden.
w.scroll.SetPolicy(gtk.POLICY_NEVER, gtk.POLICY_NEVER)
}
}
func (w *windowImpl) setScroll(horz, vert bool) {
w.horizontalScroll = horz
w.verticalScroll = vert
// Hide the scrollbars as a reset
w.scroll.SetPolicy(gtk.POLICY_NEVER, gtk.POLICY_NEVER)
w.horizontalScrollVisible = false
w.verticalScrollVisible = false
// Redo layout to account for new box constraints, and show
// scrollbars if necessary
w.onSize()
}
func (w *windowImpl) show() {
w.handle.ShowAll()
}
func (w *windowImpl) showScrollH(width base.Length, clientWidth base.Length) bool {
if width > clientWidth {
if !w.horizontalScrollVisible {
// Show the scrollbar
w.scroll.SetPolicy(gtk.POLICY_ALWAYS, boolToPolicy(w.verticalScrollVisible))
w.horizontalScrollVisible = true
return true
}
} else if w.horizontalScrollVisible {
// Remove the scroll bar
w.scroll.SetPolicy(gtk.POLICY_NEVER, boolToPolicy(w.verticalScrollVisible))
w.horizontalScrollVisible = false
return true
}
return false
}
func (w *windowImpl) showScrollV(height base.Length, clientHeight base.Length) bool {
if height > clientHeight {
if !w.verticalScrollVisible {
// Show the scrollbar
w.scroll.SetPolicy(boolToPolicy(w.horizontalScrollVisible), gtk.POLICY_ALWAYS)
w.verticalScrollVisible = true
return true
}
} else if w.verticalScrollVisible {
// Remove the scroll bar
w.scroll.SetPolicy(boolToPolicy(w.horizontalScrollVisible), gtk.POLICY_NEVER)
w.verticalScrollVisible = false
return true
}
return false
}
func (w *windowImpl) setIcon(img image.Image) error {
pixbuf, _, err := imageToPixbuf(img)
if err != nil {
return err
}
w.handle.SetIcon(pixbuf)
return nil
}
func (w *windowImpl) setOnClosing(callback func() bool) {
w.onClosing = callback
w.shClosing = setSignalHandler(&w.handle.Widget, w.shClosing, callback != nil, "delete-event", windowOnClosing, w)
}
func (w *windowImpl) setTitle(value string) error {
w.handle.SetTitle(value)
return nil
}
func (w *windowImpl) title() (string, error) {
return w.handle.GetTitle()
}
func (w *windowImpl) updateWindowMinSize() {
// Determine the extra width and height required for borders, title bar,
// and scrollbars
dx, dy := 0, 0
if w.verticalScroll {
// TODO: Measure scrollbar width
dx += 15
}
if w.horizontalScroll {
// TODO: Measure scrollbar height
dy += 15
}
// If there is no child, then we just need enough space for the window chrome.
if w.child == nil {
w.handle.SetSizeRequest(dx, dy)
return
}
request := image.Point{}
// Determine the minimum size (in pixels) for the child of the window
if w.horizontalScroll && w.verticalScroll {
width := w.child.MinIntrinsicWidth(base.Inf)
height := w.child.MinIntrinsicHeight(base.Inf)
request.X = width.PixelsX() + dx
request.Y = height.PixelsY() + dy
} else if w.horizontalScroll {
height := w.child.MinIntrinsicHeight(base.Inf)
size := w.child.Layout(base.TightHeight(height))
request.X = size.Width.PixelsX() + dx
request.Y = height.PixelsY() + dy
} else if w.verticalScroll {
width := w.child.MinIntrinsicWidth(base.Inf)
size := w.child.Layout(base.TightWidth(width))
request.X = width.PixelsX() + dx
request.Y = size.Height.PixelsY() + dy
} else {
width := w.child.MinIntrinsicWidth(base.Inf)
height := w.child.MinIntrinsicHeight(base.Inf)
size1 := w.child.Layout(base.TightWidth(width))
size2 := w.child.Layout(base.TightHeight(height))
request.X = max(width, size2.Width).PixelsX() + dx
request.Y = max(height, size1.Height).PixelsY() + dy
}
// If scrolling is enabled for either direction, we can relax the
// minimum window size. These limits are fairly arbitrary, but we do need to
// leave enough space for the scroll bars.
if limit := (120 * DIP).PixelsX(); w.horizontalScroll && request.X > limit {
request.X = limit
}
if limit := (120 * DIP).PixelsY(); w.verticalScroll && request.Y > limit {
request.Y = limit
}
w.handle.SetSizeRequest(request.X, request.Y)
}
func mainwindowOnDestroy(widget *gtk.Window, mw *Window) {
// Clear handle from the struct so that we dont' risk pointing to a
// non existent window.
mw.handle = nil
// Release lock count on the GUI event loop.
loop.AddLockCount(-1)
}
func mainwindowOnSizeAllocate(widget *gtk.Window, rect uintptr, mw *Window) {
mw.onSize()
}