/
gio.go
221 lines (181 loc) · 4.65 KB
/
gio.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
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
package gio
// #cgo pkg-config: gio-2.0 glib-2.0 gobject-2.0
// #include <gio/gio.h>
// #include <stdlib.h>
// #include "gio.go.h"
import "C"
import (
"errors"
"runtime"
"unsafe"
"github.com/gotk3/gotk3/glib"
)
func init() {
tm := []glib.TypeMarshaler{
{glib.Type(C.g_icon_get_type()), marshalIcon},
{glib.Type(C.g_file_get_type()), marshalFile},
{glib.Type(C.g_file_icon_get_type()), marshalFileIcon},
}
glib.RegisterGValueMarshalers(tm)
}
/*
* Unexported vars
*/
var nilPtrErr = errors.New("cgo returned unexpected nil pointer")
/*
* GIcon
*/
// Icon is a representation of GIO's GIcon.
// Interface for icons
type Icon struct {
*glib.Object
}
// native returns a pointer to the underlying GIcon.
func (v *Icon) native() *C.GIcon {
if v == nil || v.GObject == nil {
return nil
}
p := unsafe.Pointer(v.GObject)
return C.toGIcon(p)
}
// NativePrivate: to be used inside Gotk3 only.
func (v *Icon) NativePrivate() *C.GIcon {
if v == nil || v.GObject == nil {
return nil
}
p := unsafe.Pointer(v.GObject)
return C.toGIcon(p)
}
// Native returns a pointer to the underlying GIcon.
func (v *Icon) Native() uintptr {
return uintptr(unsafe.Pointer(v.native()))
}
func marshalIcon(p uintptr) (interface{}, error) {
c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p)))
obj := glib.Take(unsafe.Pointer(c))
return wrapIcon(obj), nil
}
func wrapIcon(obj *glib.Object) *Icon {
return &Icon{obj}
}
// TODO I dont know how to handle it ...
/*
guint
g_icon_hash (gconstpointer icon);
*/
// Equal is a wrapper around g_icon_equal().
func (v *Icon) Equal(icon *Icon) bool {
return gobool(C.g_icon_equal(v.native(), icon.native()))
}
// ToString is a wrapper around g_icon_to_string().
func (v *Icon) ToString() string {
return goString(C.g_icon_to_string(v.native()))
}
// IconNewForString is a wrapper around g_icon_new_for_string().
func IconNewForString(str string) (*Icon, error) {
cstr := C.CString(str)
defer C.free(unsafe.Pointer(cstr))
var err *C.GError
c := C.g_icon_new_for_string((*C.char)(cstr), &err)
if c == nil {
defer C.g_error_free(err)
return nil, errors.New(C.GoString((*C.char)(err.message)))
}
obj := &glib.Object{glib.ToGObject(unsafe.Pointer(c))}
i := &Icon{obj}
runtime.SetFinalizer(i, func(_ interface{}) { obj.Unref() })
return i, nil
}
// TODO Requiere GVariant
/*
GVariant * g_icon_serialize ()
GIcon * g_icon_deserialize ()
*/
/*
* GFileIcon
*/
// FileIcon is a representation of GIO's GFileIcon.
type FileIcon struct {
*glib.Object
}
// native returns a pointer to the underlying GFileIcon.
func (v *FileIcon) native() *C.GFileIcon {
if v == nil || v.GObject == nil {
return nil
}
p := unsafe.Pointer(v.GObject)
return C.toGFileIcon(p)
}
// NativePrivate: to be used inside Gotk3 only.
func (v *FileIcon) NativePrivate() *C.GFileIcon {
if v == nil || v.GObject == nil {
return nil
}
p := unsafe.Pointer(v.GObject)
return C.toGFileIcon(p)
}
// Native returns a pointer to the underlying GFileIcon.
func (v *FileIcon) Native() uintptr {
return uintptr(unsafe.Pointer(v.native()))
}
func marshalFileIcon(p uintptr) (interface{}, error) {
c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p)))
obj := glib.Take(unsafe.Pointer(c))
return wrapFileIcon(obj), nil
}
func wrapFileIcon(obj *glib.Object) *FileIcon {
return &FileIcon{obj}
}
// FileIconNew is a wrapper around g_file_icon_new().
func FileIconNew(file *File) (*Icon, error) {
c := C.g_file_icon_new(file.native())
if c == nil {
return nil, nilPtrErr
}
return wrapIcon(glib.Take(unsafe.Pointer(c))), nil
}
/*
* GFile
*/
// File is a representation of GIO's GFile.
type File struct {
*glib.Object
}
// native returns a pointer to the underlying GFile.
func (v *File) native() *C.GFile {
if v == nil || v.GObject == nil {
return nil
}
p := unsafe.Pointer(v.GObject)
return C.toGFile(p)
}
// NativePrivate: to be used inside Gotk3 only.
func (v *File) NativePrivate() *C.GFile {
if v == nil || v.GObject == nil {
return nil
}
p := unsafe.Pointer(v.GObject)
return C.toGFile(p)
}
// Native returns a pointer to the underlying GFile.
func (v *File) Native() uintptr {
return uintptr(unsafe.Pointer(v.native()))
}
func marshalFile(p uintptr) (interface{}, error) {
c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p)))
obj := glib.Take(unsafe.Pointer(c))
return wrapFile(obj), nil
}
func wrapFile(obj *glib.Object) *File {
return &File{obj}
}
// FileNewForPath is a wrapper around g_file_new_for_path().
func FileNewForPath(path string) (*File, error) {
cstr := (*C.char)(C.CString(path))
defer C.free(unsafe.Pointer(cstr))
c := C.g_file_new_for_path(cstr)
if c == nil {
return nil, nilPtrErr
}
return wrapFile(glib.Take(unsafe.Pointer(c))), nil
}