From 89f635fee1bd0af3bdbf96e2545e0140ea99afc9 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Thu, 5 Feb 2015 18:52:59 +0100 Subject: [PATCH] css: Add -gtk-dpi CSS property hack This property is necessary to ensure widgets automatically update after the text scale factor is changed desktop-wide. And if I'm already doing a property like this, I can make it overridable. So now you can override the dpi per-widget with CSS like GtkSwitch { -gtk-dpi: 48; } if you want to debug things. Long-term, we want to get rid of this property and insist on dpi being 96 everywhere and people can change the font size to get larger fonts. --- gtk/gtkcssinitialvalue.c | 13 +++++++++++++ gtk/gtkcssstylepropertyimpl.c | 18 +++++++++++++++++- gtk/gtkcsstypesprivate.h | 1 + gtk/gtkwidget.c | 8 ++++++-- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/gtk/gtkcssinitialvalue.c b/gtk/gtkcssinitialvalue.c index 5e2aa91da5..0a16cb940c 100644 --- a/gtk/gtkcssinitialvalue.c +++ b/gtk/gtkcssinitialvalue.c @@ -23,6 +23,7 @@ #include "gtkcssnumbervalueprivate.h" #include "gtkcssstringvalueprivate.h" #include "gtkcssstylepropertyprivate.h" +#include "gtksettingsprivate.h" #include "gtkstyleproviderprivate.h" struct _GtkCssValue { @@ -49,6 +50,18 @@ gtk_css_value_initial_compute (GtkCssValue *value, switch (property_id) { + case GTK_CSS_PROPERTY_DPI: + settings = _gtk_style_provider_private_get_settings (provider); + if (settings) + { + GdkScreen *screen = _gtk_settings_get_screen (settings); + double resolution = gdk_screen_get_resolution (screen); + + if (resolution > 0.0) + return _gtk_css_number_value_new (resolution, GTK_CSS_NUMBER); + } + break; + case GTK_CSS_PROPERTY_FONT_FAMILY: settings = _gtk_style_provider_private_get_settings (provider); if (settings) diff --git a/gtk/gtkcssstylepropertyimpl.c b/gtk/gtkcssstylepropertyimpl.c index 9f90184d9f..056c54ff82 100644 --- a/gtk/gtkcssstylepropertyimpl.c +++ b/gtk/gtkcssstylepropertyimpl.c @@ -731,6 +731,13 @@ background_image_value_assign (GtkCssStyleProperty *property, return _gtk_css_array_value_new (css_image_value_assign (property, value)); } +static GtkCssValue * +dpi_parse (GtkCssStyleProperty *property, + GtkCssParser *parser) +{ + return _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_NUMBER); +} + static GtkCssValue * font_size_parse (GtkCssStyleProperty *property, GtkCssParser *parser) @@ -959,7 +966,7 @@ icon_theme_value_parse (GtkCssStyleProperty *property, void _gtk_css_style_property_init_properties (void) { - /* Initialize "color" and "font-size" first, + /* Initialize "color", "-gtk-dpi" and "font-size" first, * so that when computing values later they are * done first. That way, 'currentColor' and font * sizes in em can be looked up properly */ @@ -972,6 +979,15 @@ _gtk_css_style_property_init_properties (void) color_query, color_assign, _gtk_css_color_value_new_rgba (1, 1, 1, 1)); + gtk_css_style_property_register ("-gtk-dpi", + GTK_CSS_PROPERTY_DPI, + G_TYPE_NONE, + GTK_STYLE_PROPERTY_INHERIT | GTK_STYLE_PROPERTY_ANIMATED, + GTK_CSS_AFFECTS_FONT | GTK_CSS_AFFECTS_TEXT | GTK_CSS_AFFECTS_SIZE, + dpi_parse, + NULL, + NULL, + _gtk_css_number_value_new (96.0, GTK_CSS_NUMBER)); gtk_css_style_property_register ("font-size", GTK_CSS_PROPERTY_FONT_SIZE, G_TYPE_DOUBLE, diff --git a/gtk/gtkcsstypesprivate.h b/gtk/gtkcsstypesprivate.h index 1f96f7d7bb..2308453eab 100644 --- a/gtk/gtkcsstypesprivate.h +++ b/gtk/gtkcsstypesprivate.h @@ -110,6 +110,7 @@ typedef enum { enum { /*< skip >*/ GTK_CSS_PROPERTY_COLOR, + GTK_CSS_PROPERTY_DPI, GTK_CSS_PROPERTY_FONT_SIZE, GTK_CSS_PROPERTY_ICON_THEME, GTK_CSS_PROPERTY_BACKGROUND_COLOR, diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c index f6c83bb7ea..2a9769b1e7 100644 --- a/gtk/gtkwidget.c +++ b/gtk/gtkwidget.c @@ -10286,6 +10286,12 @@ update_pango_context (GtkWidget *widget, PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL); pango_font_description_free (font_desc); + + pango_cairo_context_set_resolution (context, + _gtk_css_number_value_get ( + _gtk_style_context_peek_property (style_context, + GTK_CSS_PROPERTY_DPI), + 100)); } static void @@ -10302,8 +10308,6 @@ gtk_widget_update_pango_context (GtkWidget *widget) screen = gtk_widget_get_screen_unchecked (widget); if (screen) { - pango_cairo_context_set_resolution (context, - gdk_screen_get_resolution (screen)); pango_cairo_context_set_font_options (context, gdk_screen_get_font_options (screen)); }