-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewport.go
More file actions
335 lines (291 loc) · 10.6 KB
/
viewport.go
File metadata and controls
335 lines (291 loc) · 10.6 KB
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
// Copyright (c) 2023 The Go-Curses Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ctk
import (
"github.com/go-curses/cdk"
cenums "github.com/go-curses/cdk/lib/enums"
cmath "github.com/go-curses/cdk/lib/math"
"github.com/go-curses/cdk/lib/paint"
"github.com/go-curses/cdk/lib/ptypes"
"github.com/go-curses/cdk/memphis"
"github.com/go-curses/ctk/lib/enums"
)
const TypeViewport cdk.CTypeTag = "ctk-viewport"
func init() {
_ = cdk.TypesManager.AddType(TypeViewport, func() interface{} { return MakeViewport() })
}
// Viewport Hierarchy:
// Object
// +- Widget
// +- Container
// +- Bin
// +- Viewport
//
// The Viewport widget acts as an adaptor class, implementing scrollability
// for child widgets that lack their own scrolling capabilities. Use Viewport
// to scroll child widgets such as Table, Box, and so on. If a widget has
// native scrolling abilities, such as TextView, TreeView IconView, it can
// be added to a ScrolledWindow with Container.Add. If a widget does not, you
// must first add the widget to a Viewport, then add the viewport to the
// scrolled window. The convenience function ScrolledWindow.AddWithViewport does
// exactly this, so you can ignore the presence of the viewport.
type Viewport interface {
Bin
GetHAdjustment() (adjustment Adjustment)
SetHAdjustment(adjustment Adjustment)
GetVAdjustment() (adjustment Adjustment)
SetVAdjustment(adjustment Adjustment)
GetShadowType() (shadowType enums.ShadowType)
SetShadowType(shadowType enums.ShadowType)
GetBinWindow() (value Window)
GetViewWindow() (value Window)
}
var _ Viewport = (*CViewport)(nil)
// The CViewport structure implements the Viewport interface and is exported
// to facilitate type embedding with custom implementations. No member variables
// are exported as the interface methods are the only intended means of
// interacting with Viewport objects.
type CViewport struct {
CBin
hAdjustment Adjustment
vAdjustment Adjustment
}
// MakeViewport is used by the Buildable system to construct a new Viewport.
func MakeViewport() Viewport {
return NewViewport(nil, nil)
}
// NewViewport is the constructor for new Viewport instances.
func NewViewport(hAdjustment, vAdjustment Adjustment) Viewport {
v := new(CViewport)
v.hAdjustment = hAdjustment
v.vAdjustment = vAdjustment
v.Init()
return v
}
// Init initializes a Viewport object. This must be called at least once to
// set up the necessary defaults and allocate any memory structures. Calling
// this more than once is safe though unnecessary. Only the first call will
// result in any effect upon the Viewport instance. Init is used in the
// NewViewport constructor and only necessary when implementing a derivative
// Viewport type.
func (v *CViewport) Init() (already bool) {
if v.InitTypeItem(TypeViewport, v) {
return true
}
v.CBin.Init()
v.flags = enums.NULL_WIDGET_FLAG
v.SetFlags(enums.SENSITIVE | enums.PARENT_SENSITIVE | enums.APP_PAINTABLE)
if v.vAdjustment == nil {
v.vAdjustment = NewAdjustment(0, 0, 0, 0, 0, 0)
}
if v.hAdjustment == nil {
v.hAdjustment = NewAdjustment(0, 0, 0, 0, 0, 0)
}
_ = v.InstallProperty(PropertyHAdjustment, cdk.StructProperty, true, v.hAdjustment)
_ = v.InstallProperty(PropertyViewportShadowType, cdk.StructProperty, true, nil)
_ = v.InstallProperty(PropertyVAdjustment, cdk.StructProperty, true, v.vAdjustment)
v.Connect(SignalResize, ViewportResizeHandle, v.resize)
v.Connect(SignalDraw, ViewportDrawHandle, v.draw)
return false
}
// GetHAdjustment returns the horizontal adjustment of the viewport.
// See: SetHAdjustment()
func (v *CViewport) GetHAdjustment() (adjustment Adjustment) {
var ok bool
if value, err := v.GetStructProperty(PropertyHAdjustment); err != nil {
v.LogErr(err)
} else if adjustment, ok = value.(*CAdjustment); !ok {
v.LogError("value stored in %v property is not of *CAdjustment type: %v (%T)", PropertyHAdjustment, value, value)
}
return
}
// SetHAdjustment replaces the horizontal adjustment of the viewport with the
// given adjustment.
func (v *CViewport) SetHAdjustment(adjustment Adjustment) {
if err := v.SetStructProperty(PropertyHAdjustment, adjustment); err != nil {
v.LogErr(err)
} else {
v.hAdjustment = adjustment
}
}
// GetVAdjustment returns the vertical adjustment of the viewport.
// See: SetVAdjustment()
func (v *CViewport) GetVAdjustment() (adjustment Adjustment) {
var ok bool
if value, err := v.GetStructProperty(PropertyVAdjustment); err != nil {
v.LogErr(err)
} else if adjustment, ok = value.(*CAdjustment); !ok {
v.LogError("value stored in %v property is not of *CAdjustment type: %v (%T)", PropertyVAdjustment, value, value)
}
return
}
// SetHAdjustment replaces the horizontal adjustment of the viewport with the
// given adjustment.
func (v *CViewport) SetVAdjustment(adjustment Adjustment) {
if err := v.SetStructProperty(PropertyVAdjustment, adjustment); err != nil {
v.LogErr(err)
} else {
v.vAdjustment = adjustment
}
}
// GetShadowType returns the shadow type of the Viewport.
// See: SetShadowType()
//
// Note that usage of this within CTK is unimplemented at this time
func (v *CViewport) GetShadowType() (shadowType enums.ShadowType) {
var ok bool
if value, err := v.GetStructProperty(PropertyViewportShadowType); err != nil {
v.LogErr(err)
} else if shadowType, ok = value.(enums.ShadowType); !ok {
v.LogError("value stored in %v property is not of ShadowType type: %v (%T)", PropertyViewportShadowType, value, value)
}
return
}
// SetShadowType updates the shadow type of the viewport.
//
// Note that usage of this within CTK is unimplemented at this time
func (v *CViewport) SetShadowType(shadowType enums.ShadowType) {
if err := v.SetStructProperty(PropertyViewportShadowType, shadowType); err != nil {
v.LogErr(err)
}
}
// GetBinWindow returns the bin window of the Viewport.
//
// Note that usage of this within CTK is unimplemented at this time
func (v *CViewport) GetBinWindow() (value Window) {
v.LogWarn("method unimplemented")
return nil
}
// GetViewWindow returns the view window of the Viewport.
//
// Note that usage of this within CTK is unimplemented at this time
func (v *CViewport) GetViewWindow() (value Window) {
v.LogError("method unimplemented")
return nil
}
func (v *CViewport) resize(data []interface{}, argv ...interface{}) cenums.EventFlag {
alloc := v.GetAllocation()
child := v.GetChild()
horizontal, vertical := v.GetHAdjustment(), v.GetVAdjustment()
if alloc.W == 0 || alloc.H == 0 {
if child != nil {
child.SetAllocation(ptypes.MakeRectangle(0, 0))
child.Resize()
}
if horizontal != nil {
horizontal.Configure(0, 0, 0, 0, 0, 0)
}
if vertical != nil {
vertical.Configure(0, 0, 0, 0, 0, 0)
}
return cenums.EVENT_STOP
}
hValue, hLower, hUpper, hStepIncrement, hPageIncrement, hPageSize := 0, 0, 0, 0, 0, 0
vValue, vLower, vUpper, vStepIncrement, vPageIncrement, vPageSize := 0, 0, 0, 0, 0, 0
if child != nil {
childSize := ptypes.NewRectangle(child.GetSizeRequest())
if childSize.W <= -1 {
childSize.W = alloc.W
}
if childSize.H <= -1 {
childSize.H = alloc.H
}
hChanged, vChanged := false, false
if childSize.W >= alloc.W {
delta := childSize.W - alloc.W
hLower, hUpper, hStepIncrement, hPageIncrement, hPageSize = 0, delta, 1, alloc.W/2, alloc.W
if horizontal != nil {
hValue = cmath.ClampI(horizontal.GetValue(), 0, hUpper)
horizontal.Configure(hValue, hLower, hUpper, hStepIncrement, hPageIncrement, hPageSize)
}
hChanged = true
}
if childSize.H >= alloc.H {
delta := childSize.H - alloc.H
vLower, vUpper, vStepIncrement, vPageIncrement, vPageSize = 0, delta, 1, alloc.H/2, alloc.H
if vertical != nil {
vValue = cmath.ClampI(vertical.GetValue(), 0, vUpper)
vertical.Configure(vValue, vLower, vUpper, vStepIncrement, vPageIncrement, vPageSize)
}
vChanged = true
}
origin := v.GetOrigin()
childOrigin := child.GetOrigin()
if hChanged {
childOrigin.X = origin.X - hValue
}
if vChanged {
childOrigin.Y = origin.Y - vValue
}
child.SetOrigin(childOrigin.X, childOrigin.Y)
child.SetAllocation(*childSize)
child.Resize()
// v.LogDebug("child resized: origin=%v, alloc=%v", child.GetOrigin(), child.GetAllocation())
if hChanged {
if horizontal != nil {
horizontal.Changed()
}
}
if vChanged {
if vertical != nil {
vertical.Changed()
}
}
}
v.Invalidate()
return cenums.EVENT_STOP
}
func (v *CViewport) draw(data []interface{}, argv ...interface{}) cenums.EventFlag {
if surface, ok := argv[1].(*memphis.CSurface); ok {
size := v.GetAllocation()
if !v.IsVisible() || size.W <= 0 || size.H <= 0 {
v.LogTrace("not visible, zero width or zero height")
return cenums.EVENT_PASS
}
surface.Fill(v.GetThemeRequest())
if child := v.GetChild(); child != nil {
child.Draw()
if err := surface.Composite(child.ObjectID()); err != nil {
v.LogError("composite error: %v", err)
}
}
if debug, _ := v.GetBoolProperty(cdk.PropertyDebug); debug {
surface.DebugBox(paint.ColorSilver, v.ObjectInfo())
}
return cenums.EVENT_STOP
}
return cenums.EVENT_PASS
}
// The Adjustment that determines the values of the horizontal position
// for this viewport.
// Flags: Read / Write / Construct
const PropertyViewportHAdjustment cdk.Property = "hadjustment"
// Determines how the shadowed box around the viewport is drawn.
// Flags: Read / Write
// Default value: GTK_SHADOW_IN
const PropertyViewportShadowType cdk.Property = "shadow-type"
// The Adjustment that determines the values of the vertical position for
// this viewport.
// Flags: Read / Write / Construct
const PropertyViewportVAdjustment cdk.Property = "vadjustment"
// Set the scroll adjustments for the viewport. Usually scrolled containers
// like ScrolledWindow will emit this signal to connect two instances of
// Scrollbar to the scroll directions of the Viewport.
// Listener function arguments:
// vertical Adjustment the vertical GtkAdjustment
// arg2 Adjustment
const SignalSetScrollAdjustments cdk.Signal = "set-scroll-adjustments"
const ViewportInvalidateHandle = "viewport-invalidate-handler"
const ViewportResizeHandle = "viewport-resize-handler"
const ViewportDrawHandle = "viewport-draw-handler"