Port gtk3-demo to GtkStyleContext

This commit is contained in:
Matthias Clasen 2010-11-24 22:40:19 -05:00 committed by Carlos Garnacho
parent bbca96044e
commit 9b1118a35d
7 changed files with 86 additions and 87 deletions

View File

@ -9,35 +9,37 @@
static GtkWidget *window = NULL; static GtkWidget *window = NULL;
static GtkWidget *da; static GtkWidget *da;
static GdkColor color; static GdkRGBA color;
static GtkWidget *frame; static GtkWidget *frame;
/* Expose callback for the drawing area /* draw callback for the drawing area
*/ */
static gboolean static gboolean
draw_callback (GtkWidget *widget, draw_callback (GtkWidget *widget,
cairo_t *cr, cairo_t *cr,
gpointer data) gpointer data)
{ {
GtkStyle *style; GtkStyleContext *context;
GdkRGBA *bg;
style = gtk_widget_get_style (widget); context = gtk_widget_get_style_context (widget);
gtk_style_context_get (context, 0, "background-color", &bg, NULL);
gdk_cairo_set_source_color (cr, &style->bg[GTK_STATE_NORMAL]); gdk_cairo_set_source_rgba (cr, bg);
cairo_paint (cr); cairo_paint (cr);
gdk_rgba_free (bg);
return TRUE; return TRUE;
} }
static void static void
change_color_callback (GtkWidget *button, change_color_callback (GtkWidget *button,
gpointer data) gpointer data)
{ {
GtkWidget *dialog; GtkWidget *dialog;
GtkColorSelection *colorsel; GtkColorSelection *colorsel;
GtkColorSelectionDialog *selection_dialog; GtkColorSelectionDialog *selection_dialog;
gint response; gint response;
dialog = gtk_color_selection_dialog_new ("Changing color"); dialog = gtk_color_selection_dialog_new ("Changing color");
gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (window)); gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (window));
@ -45,20 +47,19 @@ change_color_callback (GtkWidget *button,
selection_dialog = GTK_COLOR_SELECTION_DIALOG (dialog); selection_dialog = GTK_COLOR_SELECTION_DIALOG (dialog);
colorsel = GTK_COLOR_SELECTION (gtk_color_selection_dialog_get_color_selection (selection_dialog)); colorsel = GTK_COLOR_SELECTION (gtk_color_selection_dialog_get_color_selection (selection_dialog));
gtk_color_selection_set_previous_color (colorsel, &color); gtk_color_selection_set_previous_rgba (colorsel, &color);
gtk_color_selection_set_current_color (colorsel, &color); gtk_color_selection_set_current_rgba (colorsel, &color);
gtk_color_selection_set_has_palette (colorsel, TRUE); gtk_color_selection_set_has_palette (colorsel, TRUE);
response = gtk_dialog_run (GTK_DIALOG (dialog)); response = gtk_dialog_run (GTK_DIALOG (dialog));
if (response == GTK_RESPONSE_OK) if (response == GTK_RESPONSE_OK)
{ {
gtk_color_selection_get_current_color (colorsel, gtk_color_selection_get_current_rgba (colorsel, &color);
&color);
gtk_widget_override_background_color (da, 0, &color);
gtk_widget_modify_bg (da, GTK_STATE_NORMAL, &color);
} }
gtk_widget_destroy (dialog); gtk_widget_destroy (dialog);
} }
@ -68,20 +69,21 @@ do_colorsel (GtkWidget *do_widget)
GtkWidget *vbox; GtkWidget *vbox;
GtkWidget *button; GtkWidget *button;
GtkWidget *alignment; GtkWidget *alignment;
if (!window) if (!window)
{ {
color.red = 0; color.red = 0;
color.blue = 65535; color.blue = 1;
color.green = 0; color.green = 0;
color.alpha = 1;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_screen (GTK_WINDOW (window), gtk_window_set_screen (GTK_WINDOW (window),
gtk_widget_get_screen (do_widget)); gtk_widget_get_screen (do_widget));
gtk_window_set_title (GTK_WINDOW (window), "Color Selection"); gtk_window_set_title (GTK_WINDOW (window), "Color Selection");
g_signal_connect (window, "destroy", g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed), &window); G_CALLBACK (gtk_widget_destroyed), &window);
gtk_container_set_border_width (GTK_CONTAINER (window), 8); gtk_container_set_border_width (GTK_CONTAINER (window), 8);
@ -92,33 +94,32 @@ do_colorsel (GtkWidget *do_widget)
/* /*
* Create the color swatch area * Create the color swatch area
*/ */
frame = gtk_frame_new (NULL); frame = gtk_frame_new (NULL);
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN); gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
da = gtk_drawing_area_new (); da = gtk_drawing_area_new ();
g_signal_connect (da, "draw", g_signal_connect (da, "draw", G_CALLBACK (draw_callback), NULL);
G_CALLBACK (draw_callback), NULL);
/* set a minimum size */ /* set a minimum size */
gtk_widget_set_size_request (da, 200, 200); gtk_widget_set_size_request (da, 200, 200);
/* set the color */ /* set the color */
gtk_widget_modify_bg (da, GTK_STATE_NORMAL, &color); gtk_widget_override_background_color (da, 0, &color);
gtk_container_add (GTK_CONTAINER (frame), da); gtk_container_add (GTK_CONTAINER (frame), da);
alignment = gtk_alignment_new (1.0, 0.5, 0.0, 0.0); alignment = gtk_alignment_new (1.0, 0.5, 0.0, 0.0);
button = gtk_button_new_with_mnemonic ("_Change the above color"); button = gtk_button_new_with_mnemonic ("_Change the above color");
gtk_container_add (GTK_CONTAINER (alignment), button); gtk_container_add (GTK_CONTAINER (alignment), button);
gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0); gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
g_signal_connect (button, "clicked", g_signal_connect (button, "clicked",
G_CALLBACK (change_color_callback), NULL); G_CALLBACK (change_color_callback), NULL);
} }
if (!gtk_widget_get_visible (window)) if (!gtk_widget_get_visible (window))

View File

@ -281,18 +281,18 @@ G_DEFINE_TYPE_WITH_CODE (MaskEntry, mask_entry, GTK_TYPE_ENTRY,
static void static void
mask_entry_set_background (MaskEntry *entry) mask_entry_set_background (MaskEntry *entry)
{ {
static const GdkColor error_color = { 0, 65535, 60000, 60000 }; static const GdkRGBA error_color = { 1.0, 0.9, 0.9, 1.0 };
if (entry->mask) if (entry->mask)
{ {
if (!g_regex_match_simple (entry->mask, gtk_entry_get_text (GTK_ENTRY (entry)), 0, 0)) if (!g_regex_match_simple (entry->mask, gtk_entry_get_text (GTK_ENTRY (entry)), 0, 0))
{ {
gtk_widget_modify_base (GTK_WIDGET (entry), GTK_STATE_NORMAL, &error_color); gtk_widget_override_color (GTK_WIDGET (entry), 0, &error_color);
return; return;
} }
} }
gtk_widget_modify_base (GTK_WIDGET (entry), GTK_STATE_NORMAL, NULL); gtk_widget_override_color (GTK_WIDGET (entry), 0, NULL);
} }

View File

@ -751,7 +751,7 @@ create_text (GtkTextBuffer **buffer,
if (is_source) if (is_source)
{ {
font_desc = pango_font_description_from_string ("monospace"); font_desc = pango_font_description_from_string ("monospace");
gtk_widget_modify_font (text_view, font_desc); gtk_widget_override_font (text_view, font_desc);
pango_font_description_free (font_desc); pango_font_description_free (font_desc);
gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (text_view), gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (text_view),

View File

@ -230,7 +230,7 @@ gtk_rotated_bin_realize (GtkWidget *widget)
{ {
GtkRotatedBin *bin = GTK_ROTATED_BIN (widget); GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
GtkAllocation allocation; GtkAllocation allocation;
GtkStyle *style; GtkStyleContext *context;
GdkWindow *window; GdkWindow *window;
GdkWindowAttr attributes; GdkWindowAttr attributes;
gint attributes_mask; gint attributes_mask;
@ -291,9 +291,9 @@ gtk_rotated_bin_realize (GtkWidget *widget)
G_CALLBACK (offscreen_window_from_parent), bin); G_CALLBACK (offscreen_window_from_parent), bin);
gtk_widget_style_attach (widget); gtk_widget_style_attach (widget);
style = gtk_widget_get_style (widget); context = gtk_widget_get_style_context (widget);
gtk_style_set_background (style, window, GTK_STATE_NORMAL); gtk_style_context_set_background (context, window);
gtk_style_set_background (style, bin->offscreen_window, GTK_STATE_NORMAL); gtk_style_context_set_background (context, bin->offscreen_window);
gdk_window_show (bin->offscreen_window); gdk_window_show (bin->offscreen_window);
} }
@ -542,12 +542,11 @@ gtk_rotated_bin_draw (GtkWidget *widget,
} }
if (gtk_cairo_should_draw_window (cr, bin->offscreen_window)) if (gtk_cairo_should_draw_window (cr, bin->offscreen_window))
{ {
gtk_paint_flat_box (gtk_widget_get_style (widget), cr, gtk_render_background (gtk_widget_get_style_context (widget),
GTK_STATE_NORMAL, GTK_SHADOW_NONE, cr,
widget, "blah", 0, 0,
0, 0, gdk_window_get_width (bin->offscreen_window),
gdk_window_get_width (bin->offscreen_window), gdk_window_get_height (bin->offscreen_window));
gdk_window_get_height (bin->offscreen_window));
if (bin->child) if (bin->child)
gtk_container_propagate_draw (GTK_CONTAINER (widget), gtk_container_propagate_draw (GTK_CONTAINER (widget),
@ -575,7 +574,7 @@ do_offscreen_window (GtkWidget *do_widget)
if (!window) if (!window)
{ {
GtkWidget *bin, *vbox, *scale, *button; GtkWidget *bin, *vbox, *scale, *button;
GdkColor black; GdkRGBA black;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_screen (GTK_WINDOW (window), gtk_window_set_screen (GTK_WINDOW (window),
@ -585,8 +584,8 @@ do_offscreen_window (GtkWidget *do_widget)
g_signal_connect (window, "destroy", g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed), &window); G_CALLBACK (gtk_widget_destroyed), &window);
gdk_color_parse ("black", &black); gdk_rgba_parse (&black, "black");
gtk_widget_modify_bg (window, GTK_STATE_NORMAL, &black); gtk_widget_override_background_color (window, 0, &black);
gtk_container_set_border_width (GTK_CONTAINER (window), 10); gtk_container_set_border_width (GTK_CONTAINER (window), 10);
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);

View File

@ -168,7 +168,7 @@ gtk_mirror_bin_realize (GtkWidget *widget)
{ {
GtkMirrorBin *bin = GTK_MIRROR_BIN (widget); GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
GtkAllocation allocation; GtkAllocation allocation;
GtkStyle *style; GtkStyleContext *context;
GdkWindow *window; GdkWindow *window;
GdkWindowAttr attributes; GdkWindowAttr attributes;
gint attributes_mask; gint attributes_mask;
@ -229,9 +229,9 @@ gtk_mirror_bin_realize (GtkWidget *widget)
G_CALLBACK (offscreen_window_from_parent), bin); G_CALLBACK (offscreen_window_from_parent), bin);
gtk_widget_style_attach (widget); gtk_widget_style_attach (widget);
style = gtk_widget_get_style (widget); context = gtk_widget_get_style_context (widget);
gtk_style_set_background (style, window, GTK_STATE_NORMAL); gtk_style_context_set_background (context, window);
gtk_style_set_background (style, bin->offscreen_window, GTK_STATE_NORMAL); gtk_style_context_set_background (context, bin->offscreen_window);
gdk_window_show (bin->offscreen_window); gdk_window_show (bin->offscreen_window);
} }
@ -451,12 +451,11 @@ gtk_mirror_bin_draw (GtkWidget *widget,
} }
else if (gtk_cairo_should_draw_window (cr, bin->offscreen_window)) else if (gtk_cairo_should_draw_window (cr, bin->offscreen_window))
{ {
gtk_paint_flat_box (gtk_widget_get_style (widget), cr, gtk_render_background (gtk_widget_get_style_context (widget),
GTK_STATE_NORMAL, GTK_SHADOW_NONE, cr,
widget, "blah", 0, 0,
0, 0, gdk_window_get_width (bin->offscreen_window),
gdk_window_get_width (bin->offscreen_window), gdk_window_get_height (bin->offscreen_window));
gdk_window_get_height (bin->offscreen_window));
if (bin->child) if (bin->child)
gtk_container_propagate_draw (GTK_CONTAINER (widget), gtk_container_propagate_draw (GTK_CONTAINER (widget),

View File

@ -18,17 +18,17 @@ const char text[] = "I ♥ GTK+";
static void static void
fancy_shape_renderer (cairo_t *cr, fancy_shape_renderer (cairo_t *cr,
PangoAttrShape *attr, PangoAttrShape *attr,
gboolean do_path, gboolean do_path,
gpointer data) gpointer data)
{ {
double x, y; double x, y;
cairo_get_current_point (cr, &x, &y); cairo_get_current_point (cr, &x, &y);
cairo_translate (cr, x, y); cairo_translate (cr, x, y);
cairo_scale (cr, cairo_scale (cr,
(double) attr->ink_rect.width / PANGO_SCALE, (double) attr->ink_rect.width / PANGO_SCALE,
(double) attr->ink_rect.height / PANGO_SCALE); (double) attr->ink_rect.height / PANGO_SCALE);
switch (GPOINTER_TO_UINT (attr->data)) switch (GPOINTER_TO_UINT (attr->data))
{ {
@ -36,9 +36,9 @@ fancy_shape_renderer (cairo_t *cr,
{ {
cairo_move_to (cr, .5, .0); cairo_move_to (cr, .5, .0);
cairo_line_to (cr, .9, -.4); cairo_line_to (cr, .9, -.4);
cairo_curve_to (cr, 1.1, -.8, .5, -.9, .5, -.5); cairo_curve_to (cr, 1.1, -.8, .5, -.9, .5, -.5);
cairo_curve_to (cr, .5, -.9, -.1, -.8, .1, -.4); cairo_curve_to (cr, .5, -.9, -.1, -.8, .1, -.4);
cairo_close_path (cr); cairo_close_path (cr);
} }
break; break;
} }
@ -60,8 +60,8 @@ create_fancy_attr_list_for_layout (PangoLayout *layout)
/* Get font metrics and prepare fancy shape size */ /* Get font metrics and prepare fancy shape size */
metrics = pango_context_get_metrics (pango_layout_get_context (layout), metrics = pango_context_get_metrics (pango_layout_get_context (layout),
pango_layout_get_font_description (layout), pango_layout_get_font_description (layout),
NULL); NULL);
ascent = pango_font_metrics_get_ascent (metrics); ascent = pango_font_metrics_get_ascent (metrics);
logical_rect.x = 0; logical_rect.x = 0;
logical_rect.width = ascent; logical_rect.width = ascent;
@ -77,9 +77,9 @@ create_fancy_attr_list_for_layout (PangoLayout *layout)
PangoAttribute *attr; PangoAttribute *attr;
attr = pango_attr_shape_new_with_data (&ink_rect, attr = pango_attr_shape_new_with_data (&ink_rect,
&logical_rect, &logical_rect,
GUINT_TO_POINTER (g_utf8_get_char (p)), GUINT_TO_POINTER (g_utf8_get_char (p)),
NULL, NULL); NULL, NULL);
attr->start_index = p - text; attr->start_index = p - text;
attr->end_index = attr->start_index + strlen (HEART); attr->end_index = attr->start_index + strlen (HEART);
@ -93,7 +93,7 @@ create_fancy_attr_list_for_layout (PangoLayout *layout)
static gboolean static gboolean
rotated_text_draw (GtkWidget *widget, rotated_text_draw (GtkWidget *widget,
cairo_t *cr, cairo_t *cr,
gpointer data) gpointer data)
{ {
#define RADIUS 150 #define RADIUS 150
#define N_WORDS 5 #define N_WORDS 5
@ -119,8 +119,8 @@ rotated_text_draw (GtkWidget *widget,
height = gtk_widget_get_allocated_height (widget); height = gtk_widget_get_allocated_height (widget);
device_radius = MIN (width, height) / 2.; device_radius = MIN (width, height) / 2.;
cairo_translate (cr, cairo_translate (cr,
device_radius + (width - 2 * device_radius) / 2, device_radius + (width - 2 * device_radius) / 2,
device_radius + (height - 2 * device_radius) / 2); device_radius + (height - 2 * device_radius) / 2);
cairo_scale (cr, device_radius / RADIUS, device_radius / RADIUS); cairo_scale (cr, device_radius / RADIUS, device_radius / RADIUS);
/* Create and a subtle gradient source and use it. */ /* Create and a subtle gradient source and use it. */
@ -132,8 +132,8 @@ rotated_text_draw (GtkWidget *widget,
/* Create a PangoContext and set up our shape renderer */ /* Create a PangoContext and set up our shape renderer */
context = gtk_widget_create_pango_context (widget); context = gtk_widget_create_pango_context (widget);
pango_cairo_context_set_shape_renderer (context, pango_cairo_context_set_shape_renderer (context,
fancy_shape_renderer, fancy_shape_renderer,
NULL, NULL); NULL, NULL);
/* Create a PangoLayout, set the text, font, and attributes */ /* Create a PangoLayout, set the text, font, and attributes */
layout = pango_layout_new (context); layout = pango_layout_new (context);
@ -181,11 +181,11 @@ do_rotated_text (GtkWidget *do_widget)
PangoLayout *layout; PangoLayout *layout;
PangoAttrList *attrs; PangoAttrList *attrs;
const GdkColor white = { 0, 0xffff, 0xffff, 0xffff }; const GdkRGBA white = { 1.0, 1.0, 1.0, 1.0 };
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_screen (GTK_WINDOW (window), gtk_window_set_screen (GTK_WINDOW (window),
gtk_widget_get_screen (do_widget)); gtk_widget_get_screen (do_widget));
gtk_window_set_title (GTK_WINDOW (window), "Rotated Text"); gtk_window_set_title (GTK_WINDOW (window), "Rotated Text");
gtk_window_set_default_size (GTK_WINDOW (window), 4 * RADIUS, 2 * RADIUS); gtk_window_set_default_size (GTK_WINDOW (window), 4 * RADIUS, 2 * RADIUS);
g_signal_connect (window, "destroy", G_CALLBACK (gtk_widget_destroyed), &window); g_signal_connect (window, "destroy", G_CALLBACK (gtk_widget_destroyed), &window);
@ -200,10 +200,10 @@ do_rotated_text (GtkWidget *do_widget)
gtk_container_add (GTK_CONTAINER (box), drawing_area); gtk_container_add (GTK_CONTAINER (box), drawing_area);
/* This overrides the background color from the theme */ /* This overrides the background color from the theme */
gtk_widget_modify_bg (drawing_area, GTK_STATE_NORMAL, &white); gtk_widget_override_background_color (drawing_area, 0, &white);
g_signal_connect (drawing_area, "draw", g_signal_connect (drawing_area, "draw",
G_CALLBACK (rotated_text_draw), NULL); G_CALLBACK (rotated_text_draw), NULL);
/* And a label */ /* And a label */
@ -215,8 +215,8 @@ do_rotated_text (GtkWidget *do_widget)
/* Set up fancy stuff on the label */ /* Set up fancy stuff on the label */
layout = gtk_label_get_layout (GTK_LABEL (label)); layout = gtk_label_get_layout (GTK_LABEL (label));
pango_cairo_context_set_shape_renderer (pango_layout_get_context (layout), pango_cairo_context_set_shape_renderer (pango_layout_get_context (layout),
fancy_shape_renderer, fancy_shape_renderer,
NULL, NULL); NULL, NULL);
attrs = create_fancy_attr_list_for_layout (layout); attrs = create_fancy_attr_list_for_layout (layout);
gtk_label_set_attributes (GTK_LABEL (label), attrs); gtk_label_set_attributes (GTK_LABEL (label), attrs);
pango_attr_list_unref (attrs); pango_attr_list_unref (attrs);

View File

@ -561,7 +561,7 @@ recursive_attach_view (int depth,
{ {
GtkWidget *child_view; GtkWidget *child_view;
GtkWidget *event_box; GtkWidget *event_box;
GdkColor color; GdkRGBA color;
GtkWidget *align; GtkWidget *align;
if (depth > 4) if (depth > 4)
@ -571,8 +571,8 @@ recursive_attach_view (int depth,
/* Event box is to add a black border around each child view */ /* Event box is to add a black border around each child view */
event_box = gtk_event_box_new (); event_box = gtk_event_box_new ();
gdk_color_parse ("black", &color); gdk_rgba_parse (&color, "black");
gtk_widget_modify_bg (event_box, GTK_STATE_NORMAL, &color); gtk_widget_override_background_color (event_box, 0, &color);
align = gtk_alignment_new (0.5, 0.5, 1.0, 1.0); align = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
gtk_container_set_border_width (GTK_CONTAINER (align), 1); gtk_container_set_border_width (GTK_CONTAINER (align), 1);