-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageviewer.go
155 lines (130 loc) · 2.82 KB
/
imageviewer.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
// 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.
package main
import (
"log"
"path"
"strings"
)
import (
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
)
func main() {
mw := new(MyMainWindow)
var openAction *walk.Action
if _, err := (MainWindow{
AssignTo: &mw.MainWindow,
Title: "Walk Image Viewer Example",
MenuItems: []MenuItem{
Menu{
Text: "&File",
Items: []MenuItem{
Action{
AssignTo: &openAction,
Text: "&Open",
Image: "../img/open.png",
OnTriggered: mw.openAction_Triggered,
},
Separator{},
Action{
Text: "Exit",
OnTriggered: func() { mw.Close() },
},
},
},
Menu{
Text: "&Help",
Items: []MenuItem{
Action{
Text: "About",
OnTriggered: mw.aboutAction_Triggered,
},
},
},
},
ToolBarItems: []MenuItem{
ActionRef{&openAction},
},
MinSize: Size{320, 240},
Size: Size{800, 600},
Layout: VBox{MarginsZero: true},
Children: []Widget{
TabWidget{
AssignTo: &mw.tabWidget,
},
},
}.Run()); err != nil {
log.Fatal(err)
}
}
type MyMainWindow struct {
*walk.MainWindow
tabWidget *walk.TabWidget
prevFilePath string
}
func (mw *MyMainWindow) openAction_Triggered() {
if err := mw.openImage(); err != nil {
log.Print(err)
}
}
func (mw *MyMainWindow) openImage() error {
dlg := new(walk.FileDialog)
dlg.FilePath = mw.prevFilePath
dlg.Filter = "Image Files (*.emf;*.bmp;*.exif;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.emf;*.bmp;*.exif;*.gif;*.jpeg;*.jpg;*.png;*.tiff"
dlg.Title = "Select an Image"
if ok, err := dlg.ShowOpen(mw); err != nil {
return err
} else if !ok {
return nil
}
mw.prevFilePath = dlg.FilePath
img, err := walk.NewImageFromFile(dlg.FilePath)
if err != nil {
return err
}
var succeeded bool
defer func() {
if !succeeded {
img.Dispose()
}
}()
page, err := walk.NewTabPage()
if err != nil {
return err
}
if page.SetTitle(path.Base(strings.Replace(dlg.FilePath, "\\", "/", -1))); err != nil {
return err
}
page.SetLayout(walk.NewHBoxLayout())
defer func() {
if !succeeded {
page.Dispose()
}
}()
imageView, err := walk.NewImageView(page)
if err != nil {
return err
}
defer func() {
if !succeeded {
imageView.Dispose()
}
}()
imageView.SetMode(walk.ImageViewModeShrink)
if err := imageView.SetImage(img); err != nil {
return err
}
if err := mw.tabWidget.Pages().Add(page); err != nil {
return err
}
if err := mw.tabWidget.SetCurrentIndex(mw.tabWidget.Pages().Len() - 1); err != nil {
return err
}
succeeded = true
return nil
}
func (mw *MyMainWindow) aboutAction_Triggered() {
walk.MsgBox(mw, "About", "Walk Image Viewer Example", walk.MsgBoxIconInformation)
}