From 0e47daf640588b6c3bc237d83406da2cbd10f912 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rafa=C5=82=20Mu=C5=BCy=C5=82o?= <galtgendo@gmail.com>
Date: Sun, 26 Aug 2012 20:18:11 +0200
Subject: [PATCH] fix applying font style

---
 client/gui-gtk-3.0/canvas.c    |  4 ++--
 client/gui-gtk-3.0/gui_main.c  | 21 +++++++++++-------
 client/gui-gtk-3.0/gui_main.h  |  6 +++---
 client/gui-gtk-3.0/gui_stuff.c | 48 +++++++++++++++---------------------------
 client/gui-gtk-3.0/gui_stuff.h |  2 +-
 5 files changed, 36 insertions(+), 45 deletions(-)

diff --git a/client/gui-gtk-3.0/canvas.c b/client/gui-gtk-3.0/canvas.c
index 8b08d09..6151e06 100644
--- a/client/gui-gtk-3.0/canvas.c
+++ b/client/gui-gtk-3.0/canvas.c
@@ -319,14 +319,14 @@ void canvas_put_curved_line(struct canvas *pcanvas,
 
 static PangoLayout *layout;
 static struct {
-  GtkStyle **styles;
+  PangoFontDescription **styles;
   bool shadowed;
 } fonts[FONT_COUNT] = {
   {&city_names_style, TRUE},
   {&city_productions_style, TRUE},
   {&reqtree_text_style, FALSE}
 };
-#define FONT(font) ((*fonts[font].styles)->font_desc)
+#define FONT(font) (*fonts[font].styles)
 
 /****************************************************************************
   Return the size of the given text in the given font.  This size should
diff --git a/client/gui-gtk-3.0/gui_main.c b/client/gui-gtk-3.0/gui_main.c
index 507762e..bd081a1 100644
--- a/client/gui-gtk-3.0/gui_main.c
+++ b/client/gui-gtk-3.0/gui_main.c
@@ -125,9 +125,9 @@ GtkWidget *map_widget;
 static GtkWidget *bottom_hpaned;
 
 int city_names_font_size = 0, city_productions_font_size = 0;
-GtkStyle *city_names_style = NULL;
-GtkStyle *city_productions_style = NULL;
-GtkStyle *reqtree_text_style = NULL;
+PangoFontDescription *city_names_style = NULL;
+PangoFontDescription *city_productions_style = NULL;
+PangoFontDescription *reqtree_text_style = NULL;
 
 GtkWidget *main_frame_civ_name;
 GtkWidget *main_label_info;
@@ -220,11 +220,11 @@ void set_city_names_font_sizes(int my_city_names_font_size,
   city_names_font_size = my_city_names_font_size;
   city_productions_font_size = my_city_productions_font_size;
   if (city_names_style) {
-    pango_font_description_set_size(city_names_style->font_desc,
+    pango_font_description_set_size(city_names_style,
                                     PANGO_SCALE * city_names_font_size);
   }
   if (city_productions_style) {
-    pango_font_description_set_size(city_productions_style->font_desc,
+    pango_font_description_set_size(city_productions_style,
                                     PANGO_SCALE * city_productions_font_size);
   }
 }
@@ -1562,6 +1562,8 @@ static void migrate_options_from_gtk2(void)
 **************************************************************************/
 void ui_main(int argc, char **argv)
 {
+  GtkSettings *settings;
+  const gchar *def_font_name;
   guint sig;
 
   parse_options(argc, argv);
@@ -1611,16 +1613,19 @@ void ui_main(int argc, char **argv)
     }
   } options_iterate_end;
 
+  settings = gtk_settings_get_for_screen(gtk_widget_get_screen(toplevel));
+  g_object_get(settings, "gtk-font-name", &def_font_name, NULL);
+
   if (NULL == city_names_style) {
-    city_names_style = gtk_style_new();
+    city_names_style = pango_font_description_from_string(def_font_name);
     log_error("city_names_style should have been set by options.");
   }
   if (NULL == city_productions_style) {
-    city_productions_style = gtk_style_new();
+    city_productions_style = pango_font_description_from_string(def_font_name);
     log_error("city_productions_style should have been set by options.");
   }
   if (NULL == reqtree_text_style) {
-    reqtree_text_style = gtk_style_new();
+    reqtree_text_style = pango_font_description_from_string(def_font_name);
     log_error("reqtree_text_style should have been set by options.");
   }
 
diff --git a/client/gui-gtk-3.0/gui_main.h b/client/gui-gtk-3.0/gui_main.h
index d0875ad..07e0f7f 100644
--- a/client/gui-gtk-3.0/gui_main.h
+++ b/client/gui-gtk-3.0/gui_main.h
@@ -21,9 +21,9 @@
 /* network string charset conversion */
 gchar *ntoh_str(const gchar *netstr);
 
-extern GtkStyle *city_names_style;
-extern GtkStyle *city_productions_style;
-extern GtkStyle *reqtree_text_style;
+extern PangoFontDescription *city_names_style;
+extern PangoFontDescription *city_productions_style;
+extern PangoFontDescription *reqtree_text_style;
 
 #define single_tile_pixmap (mapview.single_tile->pixmap)
 
diff --git a/client/gui-gtk-3.0/gui_stuff.c b/client/gui-gtk-3.0/gui_stuff.c
index 3bc781b..8404874 100644
--- a/client/gui-gtk-3.0/gui_stuff.c
+++ b/client/gui-gtk-3.0/gui_stuff.c
@@ -1002,48 +1002,34 @@ void gui_dialog_set_return_dialog(struct gui_dialog *dlg,
 **************************************************************************/
 void gui_update_font(const char *font_name, const char *font_value)
 {
-  char str[512];
-
-  fc_snprintf(str, sizeof(str),
-              "style \"ext-%s\" {\n"
-              "  font_name = \"%s\"\n"
-              "}\n"
-              "\n"
-              "widget \"Freeciv*.%s\" style \"ext-%s\"",
-              font_name, font_value, font_name, font_name);
-
-  gtk_rc_parse_string(str);
+  char *str;
+  GtkCssProvider *provider;
+
+  str = g_strdup_printf("#Freeciv #%s { font: %s;}", font_name, font_value);
+
+  provider = gtk_css_provider_new();
+  gtk_css_provider_load_from_data(GTK_CSS_PROVIDER(provider),
+    str, -1, NULL);
+  gtk_style_context_add_provider_for_screen(
+    gtk_widget_get_screen(toplevel), GTK_STYLE_PROVIDER(provider),
+    GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+  g_free(str);
 }
 
 /****************************************************************************
   Update a font option which is not attached to a widget.
 ****************************************************************************/
 void gui_update_font_full(const char *font_name, const char *font_value,
-                          GtkStyle **pstyle)
+                          PangoFontDescription **font_desc)
 {
-  GtkSettings *settings;
-  GdkScreen *screen;
-  GtkStyle *style;
-  char buf[64];
+  PangoFontDescription *f_desc;
 
   gui_update_font(font_name, font_value);
 
-  screen = gtk_widget_get_screen(toplevel);
-  settings = gtk_settings_get_for_screen(screen);
+  f_desc = pango_font_description_from_string(font_value);
+  pango_font_description_free(*font_desc);
 
-  fc_snprintf(buf, sizeof(buf), "Freeciv*.%s", font_name);
-  style = gtk_rc_get_style_by_paths(settings, buf, NULL, G_TYPE_NONE);
-
-  if (style) {
-    g_object_ref(style);
-  } else {
-    style = gtk_style_new();
-  }
-
-  if (*pstyle) {
-    g_object_unref(*pstyle);
-  }
-  *pstyle = style;
+  *font_desc = f_desc;
 }
 
 /****************************************************************************
diff --git a/client/gui-gtk-3.0/gui_stuff.h b/client/gui-gtk-3.0/gui_stuff.h
index aa051b2..7b0f315 100644
--- a/client/gui-gtk-3.0/gui_stuff.h
+++ b/client/gui-gtk-3.0/gui_stuff.h
@@ -124,7 +124,7 @@ void gui_dialog_set_return_dialog(struct gui_dialog *dlg,
                                   struct gui_dialog *return_dialog);
 
 void gui_update_font_full(const char *font_name, const char *font_value,
-                          GtkStyle **pstyle);
+                          PangoFontDescription **font_desc);
 
 void disable_gobject_callback(GObject *obj, GCallback cb);
 void enable_gobject_callback(GObject *obj, GCallback cb);
-- 
1.7.12

