From 944ffc1a1f1bae8812ee4d0ddda786aea09bb617 Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Tue, 14 Jul 2015 18:53:05 +0200 Subject: Make button size themeable indirectly through font settings --- rcm-client-receive.c | 53 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/rcm-client-receive.c b/rcm-client-receive.c index 710c8c4..3dd7f20 100644 --- a/rcm-client-receive.c +++ b/rcm-client-receive.c @@ -69,10 +69,42 @@ button_press_event_cb(GtkButton *button, gpointer user_data) } } -#define REMOTE_BUTTON_WIDTH 64 -#define REMOTE_BUTTON_HEIGHT 64 -#define REMOTE_BUTTON_PADDING 2 +/* Hacky McHack - Estimate the size needed for a button with ABCDEF */ +static int +calculate_img_size(GtkWidget *button) +{ + static bool first = true; + static int size; + cairo_surface_t *surface; + cairo_t *ct; + PangoLayout *layout; + PangoFontDescription *font_description; + if (!first) + return size; + + surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1); + ct = cairo_create(surface); + 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, + NULL); + + pango_layout_set_font_description(layout, font_description); + pango_font_description_free(font_description); + pango_layout_set_text(layout, "ABCDEF", -1); + pango_layout_get_pixel_size(layout, &size, NULL); + g_object_unref(layout); + cairo_destroy(ct); + cairo_surface_destroy(surface); + + first = false; + return size; +} + +#define REMOTE_BUTTON_PADDING 2.0f static void create_button_img(GtkWidget *button, const char *keycode) { @@ -88,7 +120,8 @@ create_button_img(GtkWidget *button, const char *keycode) double scale = 1.0; GdkRGBA *color; gint pw, ph; - + int img_size; + for (lik = &linux_input_keycodes[0]; lik->name; lik++) if (keycode && !strcmp(lik->name, keycode)) break; @@ -101,9 +134,9 @@ create_button_img(GtkWidget *button, const char *keycode) if (lik->cs) goto out; + img_size = calculate_img_size(button); surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, - REMOTE_BUTTON_WIDTH, - REMOTE_BUTTON_HEIGHT); + img_size, img_size); ct = cairo_create(surface); layout = pango_cairo_create_layout(ct); @@ -123,12 +156,12 @@ create_button_img(GtkWidget *button, const char *keycode) 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_x = ((float)img_size - REMOTE_BUTTON_PADDING * 2.0f) / (float)pw; + scale_y = ((float)img_size - REMOTE_BUTTON_PADDING * 2.0f) / (float)ph; scale = scale_x < scale ? scale_x : scale; scale = scale_y < scale ? scale_y : 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; + x = (float)img_size / 2.0f - ((float)pw / 2.0f) * scale; + y = (float)img_size / 2.0f - ((float)ph / 2.0f) * scale; cairo_move_to(ct, x, y); cairo_scale(ct, scale, scale); pango_cairo_show_layout(ct, layout); -- cgit v1.2.3