diff options
author | Robert Staudinger <robsta@gnome.org> | 2008-10-31 18:14:11 +0100 |
---|---|---|
committer | Robert Staudinger <robsta@gnome.org> | 2008-10-31 18:14:11 +0100 |
commit | 07f3fa37aa6bcb788a5c2d928b2666e57b861a99 (patch) | |
tree | 8ab80ec372a546d6344e321072f93169e41582c6 /examples | |
parent | 997893eb5c21d376ba4d4ed1f456415b83be9a76 (diff) |
* TODO: remember inline styling memory management facts.
* ccss/ccss-parser.c (walk_selector): associate the block to the
selector, so the styling can be applied.
* ccss/ccss-stylesheet.c (ccss_stylesheet_query): do not free the
block(s), they need to live as long as the style.
* examples/Makefile.am:
* examples/example-2.c (get_type), (get_instance), (get_style),
(expose_cb), (main):
New example illustrating the use of inline styling.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Makefile.am | 6 | ||||
-rw-r--r-- | examples/example-2.c | 112 |
2 files changed, 117 insertions, 1 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am index a9f1235..15d787e 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -8,7 +8,11 @@ AM_LDFLAGS = \ $(CCSS_LIBS) \ ../ccss/libccss-1.la -noinst_PROGRAMS = example-1 +noinst_PROGRAMS = \ + example-1 \ + example-2 example_1_SOURCES = example-1.c +example_2_SOURCES = example-2.c + diff --git a/examples/example-2.c b/examples/example-2.c new file mode 100644 index 0000000..328b0f2 --- /dev/null +++ b/examples/example-2.c @@ -0,0 +1,112 @@ + +#include <stdlib.h> +#include <cairo.h> +#include <ccss/ccss.h> +#include <gtk/gtk.h> + +typedef struct { + ccss_node_t parent; + char const *type_name; + ptrdiff_t instance; + char const *inline_css; +} node_t; + +static char const * +get_type (node_t const *self) +{ + return self->type_name; +} + +static ptrdiff_t +get_instance (node_t const *self) +{ + return self->instance; +} + +static char const * +get_style (node_t const *self) +{ + return self->inline_css; +} + +static ccss_node_class_t _node_class = { + .get_type = (ccss_node_get_type_f) get_type, + .get_instance = (ccss_node_get_instance_f) get_instance, + .get_style = (ccss_node_get_style_f) get_style +}; + +static gboolean +expose_cb (GtkWidget *widget, + GdkEventExpose *event, + ccss_style_t const *style) +{ + cairo_t *cr; + + cr = gdk_cairo_create (widget->window); + + ccss_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_stylesheet_t *stylesheet; + ccss_style_t *style; + node_t node; + GtkWidget *window; + gboolean ret; + + gtk_init (&argc, &argv); + ccss_init (NULL); + + stylesheet = ccss_stylesheet_new_from_buffer (_css, sizeof (_css)); + + ccss_node_init ((ccss_node_t *) &node, &_node_class); + node.type_name = "box"; + node.instance = 0xdeadbeef; + node.inline_css = "background-color: yellow"; + + style = ccss_style_new (); + ret = ccss_stylesheet_query (stylesheet, (ccss_node_t const *) &node, + style); + g_assert (ret); + + ccss_style_dump (style); + + window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_default_size (GTK_WINDOW (window), 160, 90); + gtk_widget_set_app_paintable (window, TRUE); + g_signal_connect (G_OBJECT (window), "delete-event", + G_CALLBACK (gtk_main_quit), NULL); + g_signal_connect (G_OBJECT (window), "expose-event", + G_CALLBACK (expose_cb), style); + + gtk_widget_show_all (window); + gtk_main (); + + ccss_style_free (style); + ccss_stylesheet_free (stylesheet); + + ccss_shutdown (); + + return EXIT_SUCCESS; +} + |