-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageview.go
162 lines (120 loc) · 3.02 KB
/
imageview.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
// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package walk
import (
"github.com/lxn/win"
"math"
"strconv"
)
var imageViewBackgroundBrush, _ = NewSystemColorBrush(win.COLOR_APPWORKSPACE)
type ImageView struct {
*CustomWidget
image Image
imageChangedPublisher EventPublisher
margin int
marginChangedPublisher EventPublisher
}
func NewImageView(parent Container) (*ImageView, error) {
iv := new(ImageView)
cw, err := NewCustomWidget(parent, 0, func(canvas *Canvas, updateBounds Rectangle) error {
return iv.drawImage(canvas, updateBounds)
})
if err != nil {
return nil, err
}
iv.CustomWidget = cw
iv.window = iv
iv.SetInvalidatesOnResize(true)
iv.SetPaintMode(PaintNoErase)
iv.MustRegisterProperty("Image", NewProperty(
func() interface{} {
return iv.Image()
},
func(v interface{}) error {
var img Image
switch val := v.(type) {
case Image:
img = val
case int:
var err error
if img, err = Resources.Image(strconv.Itoa(val)); err != nil {
return err
}
case string:
var err error
if img, err = Resources.Image(val); err != nil {
return err
}
default:
return ErrInvalidType
}
return iv.SetImage(img)
},
iv.imageChangedPublisher.Event()))
iv.MustRegisterProperty("Margin", NewProperty(
func() interface{} {
return iv.Margin()
},
func(v interface{}) error {
return iv.SetMargin(v.(int))
},
iv.MarginChanged()))
return iv, nil
}
func (iv *ImageView) Image() Image {
return iv.image
}
func (iv *ImageView) SetImage(value Image) error {
if value == iv.image {
return nil
}
iv.image = value
_, isMetafile := value.(*Metafile)
iv.SetClearsBackground(isMetafile)
err := iv.Invalidate()
iv.imageChangedPublisher.Publish()
return err
}
func (iv *ImageView) ImageChanged() *Event {
return iv.imageChangedPublisher.Event()
}
func (iv *ImageView) Margin() int {
return iv.margin
}
func (iv *ImageView) SetMargin(margin int) error {
if margin == iv.margin {
return nil
}
iv.margin = margin
err := iv.Invalidate()
iv.marginChangedPublisher.Publish()
return err
}
func (iv *ImageView) MarginChanged() *Event {
return iv.marginChangedPublisher.Event()
}
func (iv *ImageView) drawImage(canvas *Canvas, updateBounds Rectangle) error {
if iv.image == nil {
return nil
}
cb := iv.ClientBounds()
canvas.FillRectangle(imageViewBackgroundBrush, cb)
cb.Width -= iv.margin * 2
cb.Height -= iv.margin * 2
s := iv.image.Size()
var scale float64
if s.Width > cb.Width || s.Height > cb.Height {
sx := float64(cb.Width) / float64(s.Width)
sy := float64(cb.Height) / float64(s.Height)
scale = math.Min(sx, sy)
} else {
scale = 1.0
}
w := int(float64(s.Width) * scale)
h := int(float64(s.Height) * scale)
x := iv.margin + (cb.Width-w)/2
y := iv.margin + (cb.Height-h)/2
return canvas.DrawImageStretched(iv.image, Rectangle{X: x, Y: y, Width: w, Height: h})
}