| 1 |
/* This is directly taken from gtk+/demos/gtk-demo/foreigndrawing.c |
| 2 |
* Gtk+ is license under LGPLv2+ |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#include "ut_assert.h" |
| 8 |
|
| 9 |
#include "xap_GtkStyle.h" |
| 10 |
|
| 11 |
static void |
| 12 |
append_element (GtkWidgetPath *path, |
| 13 |
const char *selector) |
| 14 |
{ |
| 15 |
#if 0 |
| 16 |
static const struct { |
| 17 |
const char *name; |
| 18 |
GtkStateFlags state_flag; |
| 19 |
} pseudo_classes[] = { |
| 20 |
{ "active", GTK_STATE_FLAG_ACTIVE }, |
| 21 |
{ "hover", GTK_STATE_FLAG_PRELIGHT }, |
| 22 |
{ "selected", GTK_STATE_FLAG_SELECTED }, |
| 23 |
{ "disabled", GTK_STATE_FLAG_INSENSITIVE }, |
| 24 |
{ "indeterminate", GTK_STATE_FLAG_INCONSISTENT }, |
| 25 |
{ "focus", GTK_STATE_FLAG_FOCUSED }, |
| 26 |
{ "backdrop", GTK_STATE_FLAG_BACKDROP }, |
| 27 |
{ "dir(ltr)", GTK_STATE_FLAG_DIR_LTR }, |
| 28 |
{ "dir(rtl)", GTK_STATE_FLAG_DIR_RTL }, |
| 29 |
{ "link", GTK_STATE_FLAG_LINK }, |
| 30 |
{ "visited", GTK_STATE_FLAG_VISITED }, |
| 31 |
{ "checked", GTK_STATE_FLAG_CHECKED }, |
| 32 |
{ "drop(active)", GTK_STATE_FLAG_DROP_ACTIVE } |
| 33 |
}; |
| 34 |
guint i; |
| 35 |
#endif |
| 36 |
const char *next; |
| 37 |
char *name; |
| 38 |
char type; |
| 39 |
|
| 40 |
next = strpbrk (selector, "#.:"); |
| 41 |
if (next == NULL) |
| 42 |
next = selector + strlen (selector); |
| 43 |
|
| 44 |
name = g_strndup (selector, next - selector); |
| 45 |
if (g_ascii_isupper (selector[0])) |
| 46 |
{ |
| 47 |
GType gtype; |
| 48 |
gtype = g_type_from_name (name); |
| 49 |
if (gtype == G_TYPE_INVALID) |
| 50 |
{ |
| 51 |
g_critical ("Unknown type name `%s'", name); |
| 52 |
g_free (name); |
| 53 |
return; |
| 54 |
} |
| 55 |
gtk_widget_path_append_type (path, gtype); |
| 56 |
} |
| 57 |
else |
| 58 |
{ |
| 59 |
#if 0 |
| 60 |
/* Omit type, we're using name */ |
| 61 |
gtk_widget_path_append_type (path, G_TYPE_NONE); |
| 62 |
gtk_widget_path_iter_set_object_name (path, -1, name); |
| 63 |
#else |
| 64 |
UT_ASSERT(UT_SHOULD_NOT_HAPPEN); |
| 65 |
#endif |
| 66 |
} |
| 67 |
g_free (name); |
| 68 |
|
| 69 |
while (*next != '\0') |
| 70 |
{ |
| 71 |
type = *next; |
| 72 |
selector = next + 1; |
| 73 |
next = strpbrk (selector, "#.:"); |
| 74 |
if (next == NULL) |
| 75 |
next = selector + strlen (selector); |
| 76 |
name = g_strndup (selector, next - selector); |
| 77 |
|
| 78 |
switch (type) |
| 79 |
{ |
| 80 |
case '#': |
| 81 |
gtk_widget_path_iter_set_name (path, -1, name); |
| 82 |
break; |
| 83 |
|
| 84 |
case '.': |
| 85 |
gtk_widget_path_iter_add_class (path, -1, name); |
| 86 |
break; |
| 87 |
|
| 88 |
case ':': |
| 89 |
#if 0 |
| 90 |
for (i = 0; i < G_N_ELEMENTS (pseudo_classes); i++) |
| 91 |
{ |
| 92 |
if (g_str_equal (pseudo_classes[i].name, name)) |
| 93 |
{ |
| 94 |
gtk_widget_path_iter_set_state ( |
| 95 |
path, |
| 96 |
-1, |
| 97 |
(GtkStateFlags)(gtk_widget_path_iter_get_state (path, -1) |
| 98 |
| pseudo_classes[i].state_flag)); |
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
if (i == G_N_ELEMENTS (pseudo_classes)) |
| 103 |
g_critical ("Unknown pseudo-class :%s", name); |
| 104 |
#else |
| 105 |
// you reached it, check what you passed to XAP_GtkStyle_get_style() |
| 106 |
// this require a version of Gtk that is too recent. |
| 107 |
// XXX eventually fix when we bump the requirements. |
| 108 |
UT_ASSERT(UT_SHOULD_NOT_HAPPEN); |
| 109 |
#endif |
| 110 |
break; |
| 111 |
|
| 112 |
default: |
| 113 |
g_assert_not_reached (); |
| 114 |
break; |
| 115 |
} |
| 116 |
|
| 117 |
g_free (name); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
static GtkStyleContext * |
| 122 |
create_context_for_path (GtkWidgetPath *path, |
| 123 |
GtkStyleContext * /*parent*/) |
| 124 |
{ |
| 125 |
GtkStyleContext *context; |
| 126 |
|
| 127 |
context = gtk_style_context_new (); |
| 128 |
gtk_style_context_set_path (context, path); |
| 129 |
#if 0 // Gtk 3.4 minimum - no parent set anyway |
| 130 |
gtk_style_context_set_parent (context, parent); |
| 131 |
#endif |
| 132 |
/* Unfortunately, we have to explicitly set the state again here |
| 133 |
* for it to take effect |
| 134 |
*/ |
| 135 |
#if 0 // we haven't set a state anyway. |
| 136 |
gtk_style_context_set_state (context, gtk_widget_path_iter_get_state (path, -1)); |
| 137 |
#endif |
| 138 |
gtk_widget_path_free (path); |
| 139 |
|
| 140 |
return context; |
| 141 |
} |
| 142 |
|
| 143 |
GtkStyleContext * |
| 144 |
XAP_GtkStyle_get_style (GtkStyleContext *parent, |
| 145 |
const char *selector) |
| 146 |
{ |
| 147 |
GtkWidgetPath *path; |
| 148 |
|
| 149 |
if (parent) |
| 150 |
path = gtk_widget_path_copy (gtk_style_context_get_path (parent)); |
| 151 |
else |
| 152 |
path = gtk_widget_path_new (); |
| 153 |
|
| 154 |
append_element (path, selector); |
| 155 |
|
| 156 |
return create_context_for_path (path, parent); |
| 157 |
} |
| 158 |
|