-
Notifications
You must be signed in to change notification settings - Fork 0
/
gohFunc.go
186 lines (168 loc) · 5.21 KB
/
gohFunc.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
// gohFunc.go
// Source file auto-generated on Sun, 14 Jul 2019 16:40:30 using Gotk3ObjHandler v1.3 ©2019 H.F.M
/*
This program comes with absolutely no warranty. See the The MIT License (MIT) for details:
https://opensource.org/licenses/mit-license.php
*/
package main
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/gotk3/gotk3/gdk"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)
/*******************************************************/
/* Functions declarations, used to initialize objects */
/*****************************************************/
// newBuilder: initialise builder with glade xml string
func newBuilder(varPath interface{}) (err error) {
if mainObjects.mainUiBuilder, err = gtk.BuilderNew(); err == nil {
if Gtk3Interface, err := getBytesFromVarAsset(varPath); err == nil {
err = mainObjects.mainUiBuilder.AddFromString(string(Gtk3Interface))
}
}
return err
}
// loadObject: Load GtkObject to be transtyped ...
func loadObject(name string) (newObj glib.IObject) {
var err error
newObj, err = mainObjects.mainUiBuilder.GetObject(name)
if err != nil {
log.Panic(err)
}
return newObj
}
// WindowDestroy: is the triggered handler when closing/destroying the gui window.
func windowDestroy() {
// Doing something before quit.
err = mainOptions.Write() /* Update mainOptions with values of gtk conctrols and write to file */
if err != nil {
fmt.Printf("%s\n%v\n", "Writing options error.", err)
}
// Bye ...
gtk.MainQuit()
}
/*************************************************/
/* Images functions, used to initialize objects */
/***********************************************/
// setWinIcon: Set Icon to GtkWindow objects
func setWinIcon(object *gtk.Window, varPath interface{}) {
if inPixbuf, err := getPixBuff(varPath); err == nil {
object.SetIcon(inPixbuf)
} else {
if len(varPath.(string)) != 0 {
fmt.Printf("An error occurred on image: %s\n%v\n", varPath, err.Error())
}
}
}
// setImage: Set Image to GtkImage objects
func setImage(object *gtk.Image, varPath interface{}) {
if inPixbuf, err := getPixBuff(varPath); err == nil {
object.SetFromPixbuf(inPixbuf)
return
} else {
if len(varPath.(string)) != 0 {
fmt.Printf("An error occurred on image: %s\n%v\n", varPath, err.Error())
}
}
object.Hide()
}
// setButtonImage: Set Icon to GtkButton objects
func setButtonImage(object *gtk.Button, varPath interface{}) {
if inPixbuf, err := getPixBuff(varPath); err == nil {
if image, err := gtk.ImageNewFromPixbuf(inPixbuf); err == nil {
object.SetImage(image)
object.SetAlwaysShowImage(true)
return
}
}
if err != nil {
if len(varPath.(string)) != 0 {
fmt.Printf("An error occurred on image: %s\n%v\n", varPath, err.Error())
}
}
}
// getPixBuff: Get gtk.Pixbuff image representation from file or []byte, depending on type
func getPixBuff(varPath interface{}) (outPixbuf *gdk.Pixbuf, err error) {
switch reflect.TypeOf(varPath).String() {
case "string":
outPixbuf, err = gdk.PixbufNewFromFile(varPath.(string))
case "[]uint8":
pbLoader, err := gdk.PixbufLoaderNew()
if err == nil {
outPixbuf, err = pbLoader.WriteAndReturnPixbuf(varPath.([]byte))
}
}
return outPixbuf, err
}
/***************************************/
/* Embedded data conversion functions */
/* Used to make variable content */
/* available in go-source */
/***********************************/
// getBytesFromVarAsset: Get []byte representation from file or asset, depending on type
func getBytesFromVarAsset(varPath interface{}) (outBytes []byte, err error) {
// outBytes = new([]byte)
var rBytes []byte
switch reflect.TypeOf(varPath).String() {
case "string":
rBytes, err = ioutil.ReadFile(varPath.(string))
case "[]uint8":
rBytes = varPath.([]byte)
}
return rBytes, err
}
// HexToBytes: Convert Gzip Hex to []byte used for embedded binary in source code
func HexToBytes(varPath string, gzipData []byte) (outByte []byte) {
if r, err := gzip.NewReader(bytes.NewBuffer(gzipData)); err == nil {
var bBuffer bytes.Buffer
if _, err = io.Copy(&bBuffer, r); err == nil {
if err = r.Close(); err == nil {
return bBuffer.Bytes()
}
}
}
if err != nil {
fmt.Printf("An error occurred while reading: %s\n%v\n", varPath, err.Error())
}
return outByte
}
/*******************************/
/* Simplified files Functions */
/*****************************/
// tempMake: Make temporary directory
func tempMake(prefix string) (dir string) {
if dir, err = ioutil.TempDir("", prefix+"-"); err != nil {
log.Fatal(err)
}
return dir + string(os.PathSeparator)
}
// getAbsRealPath: Retrieve app current realpath and options filenme
func getAbsRealPath() (absoluteRealPath, optFilename string) {
absoluteBaseName, err := os.Executable()
if err != nil {
log.Fatal(err)
}
base := filepath.Base(absoluteBaseName)
splited := strings.Split(base, ".")
length := len(splited)
if length == 1 {
optFilename = base
} else {
splited = splited[:length-1]
optFilename = strings.Join(splited, ".")
}
return filepath.Dir(absoluteBaseName) + string(os.PathSeparator),
filepath.Dir(absoluteBaseName) + string(os.PathSeparator) + optFilename + ".opt"
}
// Used as fake function for signals section
func blankNotify() {}