1// Same copyright and license as the rest of the files in this project
2// This file contains style related functions and structures
3
4package gtk
5
6// #include <gtk/gtk.h>
7// #include "gtk.go.h"
8import "C"
9import (
10	"unsafe"
11
12	"github.com/gotk3/gotk3/gdk"
13	"github.com/gotk3/gotk3/glib"
14)
15
16type StyleProviderPriority int
17
18const (
19	STYLE_PROVIDER_PRIORITY_FALLBACK    StyleProviderPriority = C.GTK_STYLE_PROVIDER_PRIORITY_FALLBACK
20	STYLE_PROVIDER_PRIORITY_THEME                             = C.GTK_STYLE_PROVIDER_PRIORITY_THEME
21	STYLE_PROVIDER_PRIORITY_SETTINGS                          = C.GTK_STYLE_PROVIDER_PRIORITY_SETTINGS
22	STYLE_PROVIDER_PRIORITY_APPLICATION                       = C.GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
23	STYLE_PROVIDER_PRIORITY_USER                              = C.GTK_STYLE_PROVIDER_PRIORITY_USER
24)
25
26/*
27 * GtkStyleContext
28 */
29
30// StyleContext is a representation of GTK's GtkStyleContext.
31type StyleContext struct {
32	*glib.Object
33}
34
35// native returns a pointer to the underlying GtkStyleContext.
36func (v *StyleContext) native() *C.GtkStyleContext {
37	if v == nil || v.Object == nil {
38		return nil
39	}
40	p := unsafe.Pointer(v.GObject)
41	return C.toGtkStyleContext(p)
42}
43
44func wrapStyleContext(obj *glib.Object) *StyleContext {
45	if obj == nil {
46		return nil
47	}
48
49	return &StyleContext{obj}
50}
51
52func (v *StyleContext) AddClass(class_name string) {
53	cstr := C.CString(class_name)
54	defer C.free(unsafe.Pointer(cstr))
55
56	C.gtk_style_context_add_class(v.native(), (*C.gchar)(cstr))
57}
58
59func (v *StyleContext) RemoveClass(class_name string) {
60	cstr := C.CString(class_name)
61	defer C.free(unsafe.Pointer(cstr))
62
63	C.gtk_style_context_remove_class(v.native(), (*C.gchar)(cstr))
64}
65
66func fromNativeStyleContext(c *C.GtkStyleContext) (*StyleContext, error) {
67	if c == nil {
68		return nil, nilPtrErr
69	}
70
71	obj := glib.Take(unsafe.Pointer(c))
72	return wrapStyleContext(obj), nil
73}
74
75// GetStyleContext is a wrapper around gtk_widget_get_style_context().
76func (v *Widget) GetStyleContext() (*StyleContext, error) {
77	return fromNativeStyleContext(C.gtk_widget_get_style_context(v.native()))
78}
79
80// GetParent is a wrapper around gtk_style_context_get_parent().
81func (v *StyleContext) GetParent() (*StyleContext, error) {
82	return fromNativeStyleContext(C.gtk_style_context_get_parent(v.native()))
83}
84
85// GetProperty is a wrapper around gtk_style_context_get_property().
86func (v *StyleContext) GetProperty(property string, state StateFlags) (interface{}, error) {
87	cstr := (*C.gchar)(C.CString(property))
88	defer C.free(unsafe.Pointer(cstr))
89
90	var gval C.GValue
91	C.gtk_style_context_get_property(v.native(), cstr, C.GtkStateFlags(state), &gval)
92	val := glib.ValueFromNative(unsafe.Pointer(&gval))
93	return val.GoValue()
94}
95
96// GetStyleProperty is a wrapper around gtk_style_context_get_style_property().
97func (v *StyleContext) GetStyleProperty(property string) (interface{}, error) {
98	cstr := (*C.gchar)(C.CString(property))
99	defer C.free(unsafe.Pointer(cstr))
100
101	var gval C.GValue
102	C.gtk_style_context_get_style_property(v.native(), cstr, &gval)
103	val := glib.ValueFromNative(unsafe.Pointer(&gval))
104	return val.GoValue()
105}
106
107// GetScreen is a wrapper around gtk_style_context_get_screen().
108func (v *StyleContext) GetScreen() (*gdk.Screen, error) {
109	c := C.gtk_style_context_get_screen(v.native())
110	if c == nil {
111		return nil, nilPtrErr
112	}
113
114	d := &gdk.Screen{glib.Take(unsafe.Pointer(c))}
115	return d, nil
116}
117
118// GetState is a wrapper around gtk_style_context_get_state().
119func (v *StyleContext) GetState() StateFlags {
120	return StateFlags(C.gtk_style_context_get_state(v.native()))
121}
122
123// GetColor is a wrapper around gtk_style_context_get_color().
124func (v *StyleContext) GetColor(state StateFlags) *gdk.RGBA {
125	gdkColor := gdk.NewRGBA()
126	C.gtk_style_context_get_color(v.native(), C.GtkStateFlags(state), (*C.GdkRGBA)(unsafe.Pointer(gdkColor.Native())))
127	return gdkColor
128}
129
130// LookupColor is a wrapper around gtk_style_context_lookup_color().
131func (v *StyleContext) LookupColor(colorName string) (*gdk.RGBA, bool) {
132	cstr := (*C.gchar)(C.CString(colorName))
133	defer C.free(unsafe.Pointer(cstr))
134	gdkColor := gdk.NewRGBA()
135	ret := C.gtk_style_context_lookup_color(v.native(), cstr, (*C.GdkRGBA)(unsafe.Pointer(gdkColor.Native())))
136	return gdkColor, gobool(ret)
137}
138
139// StyleContextResetWidgets is a wrapper around gtk_style_context_reset_widgets().
140func StyleContextResetWidgets(v *gdk.Screen) {
141	C.gtk_style_context_reset_widgets((*C.GdkScreen)(unsafe.Pointer(v.Native())))
142}
143
144// Restore is a wrapper around gtk_style_context_restore().
145func (v *StyleContext) Restore() {
146	C.gtk_style_context_restore(v.native())
147}
148
149// Save is a wrapper around gtk_style_context_save().
150func (v *StyleContext) Save() {
151	C.gtk_style_context_save(v.native())
152}
153
154// SetParent is a wrapper around gtk_style_context_set_parent().
155func (v *StyleContext) SetParent(p *StyleContext) {
156	C.gtk_style_context_set_parent(v.native(), p.native())
157}
158
159// HasClass is a wrapper around gtk_style_context_has_class().
160func (v *StyleContext) HasClass(className string) bool {
161	cstr := C.CString(className)
162	defer C.free(unsafe.Pointer(cstr))
163
164	return gobool(C.gtk_style_context_has_class(v.native(), (*C.gchar)(cstr)))
165}
166
167// SetScreen is a wrapper around gtk_style_context_set_screen().
168func (v *StyleContext) SetScreen(s *gdk.Screen) {
169	C.gtk_style_context_set_screen(v.native(), (*C.GdkScreen)(unsafe.Pointer(s.Native())))
170}
171
172// SetState is a wrapper around gtk_style_context_set_state().
173func (v *StyleContext) SetState(state StateFlags) {
174	C.gtk_style_context_set_state(v.native(), C.GtkStateFlags(state))
175}
176
177type IStyleProvider interface {
178	toStyleProvider() *C.GtkStyleProvider
179}
180
181// AddProvider is a wrapper around gtk_style_context_add_provider().
182func (v *StyleContext) AddProvider(provider IStyleProvider, prio uint) {
183	C.gtk_style_context_add_provider(v.native(), provider.toStyleProvider(), C.guint(prio))
184}
185
186// AddProviderForScreen is a wrapper around gtk_style_context_add_provider_for_screen().
187func AddProviderForScreen(s *gdk.Screen, provider IStyleProvider, prio uint) {
188	C.gtk_style_context_add_provider_for_screen((*C.GdkScreen)(unsafe.Pointer(s.Native())), provider.toStyleProvider(), C.guint(prio))
189}
190
191// RemoveProvider is a wrapper around gtk_style_context_remove_provider().
192func (v *StyleContext) RemoveProvider(provider IStyleProvider) {
193	C.gtk_style_context_remove_provider(v.native(), provider.toStyleProvider())
194}
195
196// RemoveProviderForScreen is a wrapper around gtk_style_context_remove_provider_for_screen().
197func RemoveProviderForScreen(s *gdk.Screen, provider IStyleProvider) {
198	C.gtk_style_context_remove_provider_for_screen((*C.GdkScreen)(unsafe.Pointer(s.Native())), provider.toStyleProvider())
199}
200
201// GtkStyleContext * 	gtk_style_context_new ()
202// void 	gtk_style_context_get ()
203// GtkTextDirection 	gtk_style_context_get_direction ()
204// GtkJunctionSides 	gtk_style_context_get_junction_sides ()
205// const GtkWidgetPath * 	gtk_style_context_get_path ()
206// GdkFrameClock * 	gtk_style_context_get_frame_clock ()
207// void 	gtk_style_context_get_style ()
208// void 	gtk_style_context_get_style_valist ()
209// void 	gtk_style_context_get_valist ()
210// GtkCssSection * 	gtk_style_context_get_section ()
211// void 	gtk_style_context_get_background_color ()
212// void 	gtk_style_context_get_border_color ()
213// void 	gtk_style_context_get_border ()
214// void 	gtk_style_context_get_padding ()
215// void 	gtk_style_context_get_margin ()
216// const PangoFontDescription * 	gtk_style_context_get_font ()
217// void 	gtk_style_context_invalidate ()
218// gboolean 	gtk_style_context_state_is_running ()
219// GtkIconSet * 	gtk_style_context_lookup_icon_set ()
220// void 	gtk_style_context_cancel_animations ()
221// void 	gtk_style_context_scroll_animations ()
222// void 	gtk_style_context_notify_state_change ()
223// void 	gtk_style_context_pop_animatable_region ()
224// void 	gtk_style_context_push_animatable_region ()
225// void 	gtk_style_context_set_background ()
226// void 	gtk_style_context_set_direction ()
227// void 	gtk_style_context_set_junction_sides ()
228// void 	gtk_style_context_set_path ()
229// void 	gtk_style_context_add_region ()
230// void 	gtk_style_context_remove_region ()
231// gboolean 	gtk_style_context_has_region ()
232// GList * 	gtk_style_context_list_regions ()
233// void 	gtk_style_context_set_frame_clock ()
234// void 	gtk_style_context_set_scale ()
235// gint 	gtk_style_context_get_scale ()
236// GList * 	gtk_style_context_list_classes ()
237