gtk2/demos/gtk-demo/css_shadows.c

151 lines
4.7 KiB
C
Raw Normal View History

/* Theming/Shadows
2012-09-21 16:50:15 +00:00
*
* This demo shows how to use CSS shadows.
*/
#include <gtk/gtk.h>
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2012-09-21 16:50:15 +00:00
static void
show_parsing_error (GtkCssProvider *provider,
GtkCssSection *section,
const GError *error,
GtkTextBuffer *buffer)
{
const GtkCssLocation *start_location, *end_location;
2012-09-21 16:50:15 +00:00
GtkTextIter start, end;
const char *tag_name;
start_location = gtk_css_section_get_start_location (section);
2012-09-21 16:50:15 +00:00
gtk_text_buffer_get_iter_at_line_index (buffer,
&start,
start_location->lines,
start_location->line_bytes);
end_location = gtk_css_section_get_end_location (section);
2012-09-21 16:50:15 +00:00
gtk_text_buffer_get_iter_at_line_index (buffer,
&end,
end_location->lines,
end_location->line_bytes);
2012-09-21 16:50:15 +00:00
if (error->domain == GTK_CSS_PARSER_WARNING)
2012-09-21 16:50:15 +00:00
tag_name = "warning";
else
tag_name = "error";
gtk_text_buffer_apply_tag_by_name (buffer, tag_name, &start, &end);
}
2012-09-21 16:50:15 +00:00
static void
css_text_changed (GtkTextBuffer *buffer,
GtkCssProvider *provider)
{
GtkTextIter start, end;
char *text;
gtk_text_buffer_get_start_iter (buffer, &start);
gtk_text_buffer_get_end_iter (buffer, &end);
gtk_text_buffer_remove_all_tags (buffer, &start, &end);
text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
gtk_css_provider_load_from_data (provider, text, -1);
2012-09-21 16:50:15 +00:00
g_free (text);
}
static void
apply_css (GtkWidget *widget, GtkStyleProvider *provider)
{
GtkWidget *child;
2012-09-21 16:50:15 +00:00
gtk_style_context_add_provider (gtk_widget_get_style_context (widget), provider, G_MAXUINT);
for (child = gtk_widget_get_first_child (widget);
child != NULL;
child = gtk_widget_get_next_sibling (child))
apply_css (child, provider);
2012-09-21 16:50:15 +00:00
}
static GtkWidget *
2012-09-21 16:50:15 +00:00
create_toolbar (void)
{
GtkWidget *toolbar;
2020-02-03 03:52:15 +00:00
GtkWidget *item;
2012-09-21 16:50:15 +00:00
2020-02-03 03:52:15 +00:00
toolbar = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
2012-09-21 16:50:15 +00:00
gtk_widget_set_valign (toolbar, GTK_ALIGN_CENTER);
2020-02-03 03:52:15 +00:00
item = gtk_button_new_from_icon_name ("go-next");
gtk_box_append (GTK_BOX (toolbar), item);
2012-09-21 16:50:15 +00:00
2020-02-03 03:52:15 +00:00
item = gtk_button_new_from_icon_name ("go-previous");
gtk_box_append (GTK_BOX (toolbar), item);
2012-09-21 16:50:15 +00:00
2020-02-03 03:52:15 +00:00
item = gtk_button_new_with_label ("Hello World");
gtk_box_append (GTK_BOX (toolbar), item);
2012-09-21 16:50:15 +00:00
return toolbar;
}
GtkWidget *
do_css_shadows (GtkWidget *do_widget)
{
static GtkWidget *window = NULL;
2012-09-21 16:50:15 +00:00
if (!window)
{
GtkWidget *paned, *container, *child;
GtkStyleProvider *provider;
GtkTextBuffer *text;
GBytes *bytes;
window = gtk_window_new ();
gtk_window_set_title (GTK_WINDOW (window), "Shadows");
2012-09-21 16:50:15 +00:00
gtk_window_set_transient_for (GTK_WINDOW (window), GTK_WINDOW (do_widget));
gtk_window_set_default_size (GTK_WINDOW (window), 400, 300);
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
2012-09-21 16:50:15 +00:00
paned = gtk_paned_new (GTK_ORIENTATION_VERTICAL);
gtk_window_set_child (GTK_WINDOW (window), paned);
2012-09-21 16:50:15 +00:00
child = create_toolbar ();
2020-05-08 06:19:18 +00:00
gtk_paned_set_start_child (GTK_PANED (paned), child);
gtk_paned_set_resize_start_child (GTK_PANED (paned), FALSE);
2012-09-21 16:50:15 +00:00
text = gtk_text_buffer_new (NULL);
gtk_text_buffer_create_tag (text,
"warning",
"underline", PANGO_UNDERLINE_SINGLE,
NULL);
gtk_text_buffer_create_tag (text,
"error",
"underline", PANGO_UNDERLINE_ERROR,
NULL);
provider = GTK_STYLE_PROVIDER (gtk_css_provider_new ());
container = gtk_scrolled_window_new ();
2020-05-08 06:19:18 +00:00
gtk_paned_set_end_child (GTK_PANED (paned), container);
2012-09-21 16:50:15 +00:00
child = gtk_text_view_new_with_buffer (text);
2020-05-08 06:19:18 +00:00
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (container), child);
g_signal_connect (text, "changed",
G_CALLBACK (css_text_changed), provider);
2012-09-21 16:50:15 +00:00
bytes = g_resources_lookup_data ("/css_shadows/gtk.css", 0, NULL);
gtk_text_buffer_set_text (text, g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes));
g_bytes_unref (bytes);
2012-09-21 16:50:15 +00:00
g_signal_connect (provider,
"parsing-error",
G_CALLBACK (show_parsing_error),
gtk_text_view_get_buffer (GTK_TEXT_VIEW (child)));
apply_css (window, provider);
}
if (!gtk_widget_get_visible (window))
2017-01-19 09:02:04 +00:00
gtk_widget_show (window);
2012-09-21 16:50:15 +00:00
else
gtk_window_destroy (GTK_WINDOW (window));
2012-09-21 16:50:15 +00:00
return window;
}