forked from lxn/walk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
78 lines (60 loc) · 1.9 KB
/
image.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
// 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 (
"strings"
"github.com/lxn/win"
)
type Image interface {
draw(hdc win.HDC, location Point) error
drawStretched(hdc win.HDC, bounds Rectangle) error
Dispose()
Size() Size
}
func NewImageFromFile(filePath string) (Image, error) {
if strings.HasSuffix(filePath, ".ico") {
return NewIconFromFile(filePath)
} else if strings.HasSuffix(filePath, ".emf") {
return NewMetafileFromFile(filePath)
}
return NewBitmapFromFile(filePath)
}
type PaintFuncImage struct {
size96dpi Size
paint func(canvas *Canvas, bounds Rectangle) error
dispose func()
}
func NewPaintFuncImage(size Size, paint func(canvas *Canvas, bounds Rectangle) error) *PaintFuncImage {
return &PaintFuncImage{size96dpi: size, paint: paint}
}
func NewPaintFuncImageWithDispose(size Size, paint func(canvas *Canvas, bounds Rectangle) error, dispose func()) *PaintFuncImage {
return &PaintFuncImage{size96dpi: size, paint: paint, dispose: dispose}
}
func (pfi *PaintFuncImage) draw(hdc win.HDC, location Point) error {
dpi := dpiForHDC(hdc)
size := SizeFrom96DPI(pfi.size96dpi, dpi)
return pfi.drawStretched(hdc, Rectangle{location.X, location.Y, size.Width, size.Height})
}
func (pfi *PaintFuncImage) drawStretched(hdc win.HDC, bounds Rectangle) error {
canvas, err := newCanvasFromHDC(hdc)
if err != nil {
return err
}
defer canvas.Dispose()
return pfi.drawStretchedOnCanvas(canvas, bounds)
}
func (pfi *PaintFuncImage) drawStretchedOnCanvas(canvas *Canvas, bounds Rectangle) error {
bounds = RectangleTo96DPI(bounds, canvas.dpix)
return pfi.paint(canvas, bounds)
}
func (pfi *PaintFuncImage) Dispose() {
if pfi.dispose != nil {
pfi.dispose()
pfi.dispose = nil
}
}
func (pfi *PaintFuncImage) Size() Size {
return pfi.size96dpi
}