summaryrefslogtreecommitdiff
path: root/examples/example-2.c
blob: 60c70d9467a46f43ff680514b0d1a04b7e4d308a (plain)
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
/* vim: set ts=8 sw=8 noexpandtab: */

#include <stdlib.h>
#include <cairo.h>
#include <ccss-cairo/ccss-cairo.h>
#include <gtk/gtk.h>
#include "config.h"

typedef struct {
	char const	*type_name;
	ptrdiff_t	 instance;
	char const	*inline_css;
} nodeinfo_t;

static char const *
get_type (ccss_node_t const *self)
{
	nodeinfo_t *info = ccss_node_get_user_data (self);

	return info->type_name;
}

static ptrdiff_t
get_instance (ccss_node_t const *self)
{
	nodeinfo_t *info = ccss_node_get_user_data (self);

	return info->instance;
}

static char const *
get_style (ccss_node_t const	*self,
	   unsigned int		 descriptor)
{
	nodeinfo_t *info = ccss_node_get_user_data (self);

	return info->inline_css;
}

static ccss_node_class_t _node_class = {
	.is_a			= NULL,
	.get_container		= NULL,
	.get_base_style		= NULL,
	.get_instance		= get_instance,
	.get_id			= NULL,
	.get_type		= get_type,
	.get_classes		= NULL,
	.get_pseudo_classes	= NULL,
	.get_attribute		= NULL,
	.get_style		= get_style,
	.get_viewport		= NULL,
	.release		= NULL
};

static gboolean
expose_cb (GtkWidget		*widget,
	   GdkEventExpose	*event,
	   ccss_style_t const	*style)
{
	cairo_t *cr;

	cr = gdk_cairo_create (widget->window);

	ccss_cairo_style_draw_rectangle (style, cr, 
					 widget->allocation.x + 10,
					 widget->allocation.y + 10,
					 widget->allocation.width - 20, 
					 widget->allocation.height - 20);

	cairo_destroy (cr);

	return FALSE;
}

static char const _css[] = "			\
	box { 					\
		background-color: grey;		\
		border: 3px solid black;	\
		border-top: 3px dotted red;	\
		border-radius: 3px;		\
	}					\
";

int
main (int	  argc,
      char	**argv)
{
	ccss_grammar_t		*grammar;
	ccss_stylesheet_t	*stylesheet;
	ccss_style_t		*style;
	ccss_node_t		*node;
	GtkWidget		*window;
	GtkWidget		*area;
	nodeinfo_t		 info;

	gtk_init (&argc, &argv);

	grammar = ccss_cairo_grammar_create ();
	stylesheet = ccss_grammar_create_stylesheet_from_buffer (grammar,
							_css, sizeof (_css),
							NULL);
	info.type_name = "box";
	info.instance = 0xdeadbeef;
	info.inline_css = "background-color: yellow";
	node = ccss_node_create (&_node_class,
				 CCSS_NODE_CLASS_N_METHODS (_node_class),
				 &info);
	style = ccss_stylesheet_query (stylesheet, node);
	ccss_node_destroy (node);
	g_assert (style);

#ifdef CCSS_DEBUG
	ccss_style_dump (style);
#endif

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_default_size (GTK_WINDOW (window), 160, 90);
	g_signal_connect (G_OBJECT (window), "delete-event", 
			  G_CALLBACK (gtk_main_quit), NULL);

	area = gtk_drawing_area_new ();
	gtk_container_add (GTK_CONTAINER (window), area);
	g_signal_connect (G_OBJECT (area), "expose-event",
			  G_CALLBACK (expose_cb), style);

	gtk_widget_show_all (window);
	gtk_main ();

	ccss_style_destroy (style);
	ccss_stylesheet_destroy (stylesheet);
	ccss_grammar_destroy (grammar);

	return EXIT_SUCCESS;
}