diff options
author | David Härdeman <david@hardeman.nu> | 2015-07-14 18:24:01 +0200 |
---|---|---|
committer | David Härdeman <david@hardeman.nu> | 2015-07-14 18:24:01 +0200 |
commit | d45f4f380e8dfca977ca2d5a19787a2e2b57e2f7 (patch) | |
tree | 4b227d25d70f9377d09838d45bd139371f2e732c | |
parent | 57ab666fe4cd9de19c0ddd92186bd0b8ccadf37d (diff) |
Make the remote button text themeable
-rw-r--r-- | rcm-client-receive.c | 47 |
1 files changed, 28 insertions, 19 deletions
diff --git a/rcm-client-receive.c b/rcm-client-receive.c index c31df5c..710c8c4 100644 --- a/rcm-client-receive.c +++ b/rcm-client-receive.c @@ -72,7 +72,6 @@ button_press_event_cb(GtkButton *button, gpointer user_data) #define REMOTE_BUTTON_WIDTH 64 #define REMOTE_BUTTON_HEIGHT 64 #define REMOTE_BUTTON_PADDING 2 -#define REMOTE_BUTTON_FONT_SIZE 15 static void create_button_img(GtkWidget *button, const char *keycode) @@ -80,14 +79,15 @@ create_button_img(GtkWidget *button, const char *keycode) struct linux_input_keycode *lik; cairo_surface_t *surface; cairo_t *ct; - cairo_text_extents_t text_extents; - cairo_font_extents_t font_extents; + PangoLayout *layout; + PangoFontDescription *font_description; GtkWidget *img; const char *label = keycode; double x, y; double scale_x, scale_y; double scale = 1.0; - GdkRGBA color; + GdkRGBA *color; + gint pw, ph; for (lik = &linux_input_keycodes[0]; lik->name; lik++) if (keycode && !strcmp(lik->name, keycode)) @@ -105,25 +105,34 @@ create_button_img(GtkWidget *button, const char *keycode) REMOTE_BUTTON_WIDTH, REMOTE_BUTTON_HEIGHT); ct = cairo_create(surface); - /* FIXME: Make this themeable */ - cairo_set_font_size(ct, REMOTE_BUTTON_FONT_SIZE); - cairo_text_extents(ct, label, &text_extents); - /* font extents so the vertial alignment is the same for all buttons */ - cairo_font_extents(ct, &font_extents); - scale_x = (REMOTE_BUTTON_WIDTH - REMOTE_BUTTON_PADDING * 2) / text_extents.width; - scale_y = (REMOTE_BUTTON_HEIGHT - REMOTE_BUTTON_PADDING * 2) / font_extents.height; + layout = pango_cairo_create_layout(ct); + + gtk_style_context_get(gtk_widget_get_style_context(button), + gtk_widget_get_state_flags(button), + GTK_STYLE_PROPERTY_FONT, &font_description, + GTK_STYLE_PROPERTY_COLOR, &color, + NULL); + + pango_layout_set_font_description(layout, font_description); + pango_font_description_free(font_description); + + gdk_cairo_set_source_rgba(ct, color); + gdk_rgba_free(color); + + pango_layout_set_text(layout, label, -1); + + pango_layout_get_pixel_size(layout, &pw, &ph); + + scale_x = ((float)REMOTE_BUTTON_WIDTH - (float)REMOTE_BUTTON_PADDING * 2.0f) / (float)pw; + scale_y = ((float)REMOTE_BUTTON_HEIGHT - (float)REMOTE_BUTTON_PADDING * 2.0f) / (float)ph; scale = scale_x < scale ? scale_x : scale; scale = scale_y < scale ? scale_y : scale; - x = REMOTE_BUTTON_WIDTH / 2 - (text_extents.x_bearing + text_extents.width / 2.0) * scale; - y = REMOTE_BUTTON_HEIGHT / 2 - (font_extents.descent - font_extents.height / 2.0) * scale; + x = (float)REMOTE_BUTTON_WIDTH / 2.0f - ((float)pw / 2.0f) * scale; + y = (float)REMOTE_BUTTON_HEIGHT / 2.0f - ((float)ph / 2.0f) * scale; cairo_move_to(ct, x, y); cairo_scale(ct, scale, scale); - gtk_style_context_get_color(gtk_widget_get_style_context(button), - gtk_widget_get_state_flags(button), - &color); - gdk_cairo_set_source_rgba(ct, &color); - cairo_show_text(ct, label); - + pango_cairo_show_layout(ct, layout); + g_object_unref(layout); cairo_destroy(ct); lik->cs = surface; |