2016-03-26 02:39:53 +00:00
|
|
|
/* Theming/Multiple Backgrounds
|
2012-05-24 18:17:25 +00:00
|
|
|
*
|
2020-09-13 18:59:33 +00:00
|
|
|
* GTK themes are written using CSS. Every widget is build of multiple items
|
2012-05-24 18:17:25 +00:00
|
|
|
* that you can style very similarly to a regular website.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2022-10-09 02:54:48 +00:00
|
|
|
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
|
|
|
|
2012-05-24 18:17:25 +00:00
|
|
|
static void
|
|
|
|
show_parsing_error (GtkCssProvider *provider,
|
|
|
|
GtkCssSection *section,
|
|
|
|
const GError *error,
|
|
|
|
GtkTextBuffer *buffer)
|
|
|
|
{
|
2019-04-10 03:18:14 +00:00
|
|
|
const GtkCssLocation *start_location, *end_location;
|
2012-05-24 18:17:25 +00:00
|
|
|
GtkTextIter start, end;
|
|
|
|
const char *tag_name;
|
|
|
|
|
2019-04-10 03:18:14 +00:00
|
|
|
start_location = gtk_css_section_get_start_location (section);
|
2012-05-24 18:17:25 +00:00
|
|
|
gtk_text_buffer_get_iter_at_line_index (buffer,
|
|
|
|
&start,
|
2019-04-10 03:18:14 +00:00
|
|
|
start_location->lines,
|
|
|
|
start_location->line_bytes);
|
|
|
|
end_location = gtk_css_section_get_end_location (section);
|
2012-05-24 18:17:25 +00:00
|
|
|
gtk_text_buffer_get_iter_at_line_index (buffer,
|
|
|
|
&end,
|
2019-04-10 03:18:14 +00:00
|
|
|
end_location->lines,
|
|
|
|
end_location->line_bytes);
|
|
|
|
|
2012-05-24 18:17:25 +00:00
|
|
|
|
2019-03-22 03:20:17 +00:00
|
|
|
if (error->domain == GTK_CSS_PARSER_WARNING)
|
2012-05-24 18:17:25 +00:00
|
|
|
tag_name = "warning";
|
|
|
|
else
|
|
|
|
tag_name = "error";
|
|
|
|
|
|
|
|
gtk_text_buffer_apply_tag_by_name (buffer, tag_name, &start, &end);
|
|
|
|
}
|
2015-06-20 01:59:15 +00:00
|
|
|
|
2012-05-24 18:17:25 +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);
|
2016-10-17 14:09:12 +00:00
|
|
|
gtk_css_provider_load_from_data (provider, text, -1);
|
2012-05-24 18:17:25 +00:00
|
|
|
g_free (text);
|
|
|
|
}
|
|
|
|
|
2016-11-18 05:16:38 +00:00
|
|
|
static void
|
|
|
|
drawing_area_draw (GtkDrawingArea *da,
|
|
|
|
cairo_t *cr,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
gpointer data)
|
2012-05-24 18:17:25 +00:00
|
|
|
{
|
2016-11-18 05:16:38 +00:00
|
|
|
GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (da));
|
|
|
|
|
|
|
|
gtk_render_background (context, cr, 0, 0, width, height);
|
|
|
|
gtk_render_frame (context, cr, 0, 0, width, height);
|
2012-05-24 18:17:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
apply_css (GtkWidget *widget, GtkStyleProvider *provider)
|
|
|
|
{
|
2020-05-09 12:26:52 +00:00
|
|
|
GtkWidget *child;
|
|
|
|
|
2012-05-24 18:17:25 +00:00
|
|
|
gtk_style_context_add_provider (gtk_widget_get_style_context (widget), provider, G_MAXUINT);
|
2020-05-09 12:26:52 +00:00
|
|
|
for (child = gtk_widget_get_first_child (widget);
|
|
|
|
child != NULL;
|
|
|
|
child = gtk_widget_get_next_sibling (child))
|
|
|
|
apply_css (child, provider);
|
2012-05-24 18:17:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
do_css_multiplebgs (GtkWidget *do_widget)
|
|
|
|
{
|
2015-06-28 13:23:28 +00:00
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
|
2012-05-24 18:17:25 +00:00
|
|
|
if (!window)
|
|
|
|
{
|
2020-05-21 20:24:56 +00:00
|
|
|
GtkWidget *paned, *overlay, *child, *sw;
|
2012-05-24 18:17:25 +00:00
|
|
|
GtkStyleProvider *provider;
|
|
|
|
GtkTextBuffer *text;
|
|
|
|
GBytes *bytes;
|
2012-05-30 18:28:16 +00:00
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2015-06-20 01:59:15 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Multiple Backgrounds");
|
2012-05-24 18:17:25 +00:00
|
|
|
gtk_window_set_transient_for (GTK_WINDOW (window), GTK_WINDOW (do_widget));
|
|
|
|
gtk_window_set_default_size (GTK_WINDOW (window), 400, 300);
|
2020-05-09 16:03:11 +00:00
|
|
|
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
|
2012-05-24 18:17:25 +00:00
|
|
|
|
2020-05-21 20:24:56 +00:00
|
|
|
overlay = gtk_overlay_new ();
|
|
|
|
gtk_window_set_child (GTK_WINDOW (window), overlay);
|
2012-05-24 18:17:25 +00:00
|
|
|
|
|
|
|
child = gtk_drawing_area_new ();
|
|
|
|
gtk_widget_set_name (child, "canvas");
|
2016-11-18 05:16:38 +00:00
|
|
|
gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (child),
|
|
|
|
drawing_area_draw,
|
|
|
|
NULL, NULL);
|
2020-05-21 20:24:56 +00:00
|
|
|
gtk_overlay_set_child (GTK_OVERLAY (overlay), child);
|
2012-05-24 18:17:25 +00:00
|
|
|
|
|
|
|
child = gtk_button_new ();
|
2020-05-21 20:24:56 +00:00
|
|
|
gtk_overlay_add_overlay (GTK_OVERLAY (overlay), child);
|
2012-05-24 18:17:25 +00:00
|
|
|
gtk_widget_set_name (child, "bricks-button");
|
|
|
|
gtk_widget_set_halign (child, GTK_ALIGN_CENTER);
|
|
|
|
gtk_widget_set_valign (child, GTK_ALIGN_CENTER);
|
2013-06-27 16:32:24 +00:00
|
|
|
gtk_widget_set_size_request (child, 250, 84);
|
2012-05-24 18:17:25 +00:00
|
|
|
|
|
|
|
paned = gtk_paned_new (GTK_ORIENTATION_VERTICAL);
|
2020-05-21 20:24:56 +00:00
|
|
|
gtk_overlay_add_overlay (GTK_OVERLAY (overlay), paned);
|
2012-05-24 18:17:25 +00:00
|
|
|
|
|
|
|
/* Need a filler so we get a handle */
|
|
|
|
child = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
2020-05-08 06:19:18 +00:00
|
|
|
gtk_paned_set_start_child (GTK_PANED (paned), child);
|
2012-05-24 18:17:25 +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 ());
|
2012-05-30 18:28:16 +00:00
|
|
|
|
2020-06-24 15:25:09 +00:00
|
|
|
sw = gtk_scrolled_window_new ();
|
2020-05-21 20:24:56 +00:00
|
|
|
gtk_paned_set_end_child (GTK_PANED (paned), sw);
|
2012-05-24 18:17:25 +00:00
|
|
|
child = gtk_text_view_new_with_buffer (text);
|
2020-05-21 20:24:56 +00:00
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), child);
|
2012-05-24 18:17:25 +00:00
|
|
|
g_signal_connect (text,
|
|
|
|
"changed",
|
|
|
|
G_CALLBACK (css_text_changed),
|
|
|
|
provider);
|
|
|
|
|
2013-01-30 20:11:32 +00:00
|
|
|
bytes = g_resources_lookup_data ("/css_multiplebgs/css_multiplebgs.css", 0, NULL);
|
2012-05-24 18:17:25 +00:00
|
|
|
gtk_text_buffer_set_text (text, g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes));
|
2012-09-16 04:48:18 +00:00
|
|
|
g_bytes_unref (bytes);
|
2012-05-24 18:17:25 +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-05-24 18:17:25 +00:00
|
|
|
else
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2012-05-24 18:17:25 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|