]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkfontsel.c
Update gcc build instructions. Mention gettext is GPL.
[~andy/gtk] / gtk / gtkfontsel.c
index baae35da732d0d6a08a13a01d98c9bdc0542316b..36eaf9b4d087cd3125028bf4bfcbea523ee98e91 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
 
+/*
+ * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
+ * file for a list of people on the GTK+ Team.  See the ChangeLog
+ * files for a list of changes.  These files are distributed with
+ * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
+ */
+
 /*
  * Limits:
  *
  * Debugging: compile with -DFONTSEL_DEBUG for lots of debugging output.
  */
 
-
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
-#include <X11/Xlib.h>
 
-#include "gdk/gdkx.h"
+#include "gdk/gdk.h"
+/* Protect against the CHARSET struct in Win32 */
+#if GDK_WINDOWING == GDK_WINDOWING_WIN32
+# define CHARSET CHARSETstruct
+#endif
+#include "gdkx.h"
+#if GDK_WINDOWING == GDK_WINDOWING_WIN32
+# undef CHARSET
+#endif
 #include "gdk/gdkkeysyms.h"
 
 #include "gtkbutton.h"
@@ -388,6 +401,13 @@ static gint    gtk_font_selection_dialog_on_configure(GtkWidget      *widget,
                                                      GdkEventConfigure *event,
                                                      GtkFontSelectionDialog *fsd);
 
+#if GDK_WINDOWING == GDK_WINDOWING_WIN32
+static char *logfont_to_xlfd (const LOGFONT *lfp,
+                             int size,
+                             int res,
+                             int avg_width);
+#endif
+
 static GtkWindowClass *font_selection_parent_class = NULL;
 static GtkNotebookClass *font_selection_dialog_parent_class = NULL;
 
@@ -658,14 +678,14 @@ gtk_font_selection_init(GtkFontSelection *fontsel)
   gtk_widget_show (fontsel->points_button);
   gtk_box_pack_start (GTK_BOX (hbox2), fontsel->points_button, FALSE, TRUE, 0);
   if (INITIAL_METRIC == GTK_FONT_METRIC_POINTS)
-    gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(fontsel->points_button),
+    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fontsel->points_button),
                                TRUE);
   
   fontsel->pixels_button = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(fontsel->points_button), _("Pixels"));
   gtk_widget_show (fontsel->pixels_button);
   gtk_box_pack_start (GTK_BOX (hbox2), fontsel->pixels_button, FALSE, TRUE, 0);
   if (INITIAL_METRIC == GTK_FONT_METRIC_PIXELS)
-    gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(fontsel->pixels_button),
+    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fontsel->pixels_button),
                                TRUE);
   
   gtk_signal_connect(GTK_OBJECT(fontsel->points_button), "toggled",
@@ -731,7 +751,7 @@ gtk_font_selection_init(GtkFontSelection *fontsel)
   row_text[2] = "";
   for (i = 0; i < GTK_XLFD_NUM_FIELDS; i++)
     {
-      row_text[0] = gettext(xlfd_field_names[i]);
+      row_text[0] = _(xlfd_field_names[i]);
       gtk_clist_append(GTK_CLIST(fontsel->info_clist), row_text);
       gtk_clist_set_shift(GTK_CLIST(fontsel->info_clist), i, 0, 0, 4);
       gtk_clist_set_shift(GTK_CLIST(fontsel->info_clist), i, 1, 0, 4);
@@ -798,13 +818,13 @@ gtk_font_selection_init(GtkFontSelection *fontsel)
   gtk_box_pack_start (GTK_BOX (hbox), hbox2, FALSE, TRUE, 0);
 
   fontsel->type_bitmaps_button = gtk_check_button_new_with_label (_("Bitmap"));
-  gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (fontsel->type_bitmaps_button), TRUE);
+  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fontsel->type_bitmaps_button), TRUE);
   gtk_widget_show (fontsel->type_bitmaps_button);
   gtk_box_pack_start (GTK_BOX (hbox2), fontsel->type_bitmaps_button,
                      FALSE, TRUE, 0);
 
   fontsel->type_scalable_button = gtk_check_button_new_with_label (_("Scalable"));
-  gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (fontsel->type_scalable_button), TRUE);
+  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fontsel->type_scalable_button), TRUE);
   gtk_widget_show (fontsel->type_scalable_button);
   gtk_box_pack_start (GTK_BOX (hbox2), fontsel->type_scalable_button,
                      FALSE, TRUE, 0);
@@ -824,7 +844,7 @@ gtk_font_selection_init(GtkFontSelection *fontsel)
       gint left = filter_positions[prop][0];
       gint top = filter_positions[prop][1];
       
-      label = gtk_label_new(gettext(xlfd_field_names[xlfd_index[prop]]));
+      label = gtk_label_new(_(xlfd_field_names[xlfd_index[prop]]));
       gtk_misc_set_alignment (GTK_MISC (label), 0.0, 1.0);
       gtk_misc_set_padding (GTK_MISC (label), 0, 2);
       gtk_widget_show(label);
@@ -880,16 +900,16 @@ gtk_font_selection_init(GtkFontSelection *fontsel)
       /* Insert the property names, expanded, and in sorted order.
         But we make sure that the wildcard '*' is first. */
       gtk_clist_freeze (GTK_CLIST(clist));
-      property = "*";
+      property = N_("*");
       gtk_clist_append(GTK_CLIST(clist), &property);
       
       for (i = 1; i < fontsel_info->nproperties[prop]; i++) {
-       property = fontsel_info->properties[prop][i];
+       property = _(fontsel_info->properties[prop][i]);
        if (prop == SLANT)
          property = gtk_font_selection_expand_slant_code(property);
        else if (prop == SPACING)
          property = gtk_font_selection_expand_spacing_code(property);
-       
+
        inserted = FALSE;
        for (row = 1; row < GTK_CLIST(clist)->rows; row++)
          {
@@ -1205,42 +1225,42 @@ gtk_font_selection_show_available_styles (GtkFontSelection *fontsel)
          spacing   = fontsel_info->properties[SPACING]  [spacing_index];
          
          /* Convert '(nil)' weights to 'regular', since it looks nicer. */
-         if      (!g_strcasecmp(weight, "(nil)"))      weight = _("regular");
+         if      (!g_strcasecmp(weight, N_("(nil)")))  weight = N_("regular");
          
          /* We don't show default values or (nil) in the other properties. */
          if      (!g_strcasecmp(slant, "r"))        slant = NULL;
          else if (!g_strcasecmp(slant, "(nil)"))    slant = NULL;
-         else if (!g_strcasecmp(slant, "i"))        slant = _("italic");
-         else if (!g_strcasecmp(slant, "o"))        slant = _("oblique");
-         else if (!g_strcasecmp(slant, "ri"))       slant = _("reverse italic");
-         else if (!g_strcasecmp(slant, "ro"))       slant = _("reverse oblique");
-         else if (!g_strcasecmp(slant, "ot"))       slant = _("other");
+         else if (!g_strcasecmp(slant, "i"))        slant = N_("italic");
+         else if (!g_strcasecmp(slant, "o"))        slant = N_("oblique");
+         else if (!g_strcasecmp(slant, "ri"))       slant = N_("reverse italic");
+         else if (!g_strcasecmp(slant, "ro"))       slant = N_("reverse oblique");
+         else if (!g_strcasecmp(slant, "ot"))       slant = N_("other");
          
          if      (!g_strcasecmp(set_width, "normal")) set_width = NULL;
          else if (!g_strcasecmp(set_width, "(nil)"))  set_width = NULL;
          
          if      (!g_strcasecmp(spacing, "p"))        spacing = NULL;
          else if (!g_strcasecmp(spacing, "(nil)"))    spacing = NULL;
-         else if (!g_strcasecmp(spacing, "m"))        spacing = _("[M]");
-         else if (!g_strcasecmp(spacing, "c"))        spacing = _("[C]");
+         else if (!g_strcasecmp(spacing, "m"))        spacing = N_("[M]");
+         else if (!g_strcasecmp(spacing, "c"))        spacing = N_("[C]");
          
          /* Add the strings together, making sure there is 1 space between
             them */
-         strcpy(buffer, weight);
+         strcpy(buffer, _(weight));
          if (slant)
            {
              strcat(buffer, " ");
-             strcat(buffer, slant);
+             strcat(buffer, _(slant));
            }
          if (set_width)
            {
              strcat(buffer, " ");
-             strcat(buffer, set_width);
+             strcat(buffer, _(set_width));
            }
          if (spacing)
            {
              strcat(buffer, " ");
-             strcat(buffer, spacing);
+             strcat(buffer, _(spacing));
            }
          
          new_item = buffer;
@@ -1384,10 +1404,11 @@ gtk_font_selection_show_available_sizes (GtkFontSelection *fontsel)
   FontInfo *font;
   FontStyle *styles, *style;
   const guint16 *standard_sizes;
-  guint16 *bitmapped_sizes, bitmap_size;
+  guint16 *bitmapped_sizes;
   gint nstandard_sizes, nbitmapped_sizes;
   gchar buffer[16], *size;
-  gfloat bitmap_size_float;
+  gfloat bitmap_size_float = 0.;
+  guint16 bitmap_size = 0;
   gboolean can_match;
   gint type_filter;
   
@@ -1430,18 +1451,20 @@ gtk_font_selection_show_available_sizes (GtkFontSelection *fontsel)
   while (nstandard_sizes || nbitmapped_sizes)
     {
       can_match = TRUE;
-      if (fontsel->metric == GTK_FONT_METRIC_POINTS)
-       {
-         if (*bitmapped_sizes % 10 != 0)
-           can_match = FALSE;
-         bitmap_size = *bitmapped_sizes / 10;
-         bitmap_size_float = *bitmapped_sizes / 10;
-       }
-      else
-       {
-         bitmap_size = *bitmapped_sizes;
-         bitmap_size_float = *bitmapped_sizes;
-       }
+
+      if (nbitmapped_sizes)
+       if (fontsel->metric == GTK_FONT_METRIC_POINTS)
+         {
+           if (*bitmapped_sizes % 10 != 0)
+             can_match = FALSE;
+           bitmap_size = *bitmapped_sizes / 10;
+           bitmap_size_float = *bitmapped_sizes / 10;
+         }
+       else
+         {
+           bitmap_size = *bitmapped_sizes;
+           bitmap_size_float = *bitmapped_sizes;
+         }
       
       if (can_match && nstandard_sizes && nbitmapped_sizes
          && *standard_sizes == bitmap_size)
@@ -1793,8 +1816,10 @@ gtk_font_selection_update_preview (GtkFontSelection *fontsel)
   GtkStyle *style;
   gint text_height, new_height;
   gchar *text;
+#if GDK_WINDOWING == GDK_WINDOWING_X11
   XFontStruct *xfs;
-  
+#endif
+
 #ifdef FONTSEL_DEBUG
   g_message("In update_preview\n");
 #endif
@@ -1826,12 +1851,14 @@ gtk_font_selection_update_preview (GtkFontSelection *fontsel)
     gtk_entry_set_text(GTK_ENTRY(fontsel->preview_entry), PREVIEW_TEXT);
   gtk_entry_set_position(GTK_ENTRY(fontsel->preview_entry), 0);
   
+#if GDK_WINDOWING == GDK_WINDOWING_X11
   /* If this is a 2-byte font display a message to say it may not be
      displayed properly. */
   xfs = GDK_FONT_XFONT(fontsel->font);
   if (xfs->min_byte1 != 0 || xfs->max_byte1 != 0)
     gtk_label_set_text(GTK_LABEL(fontsel->message_label),
                       _("This is a 2-byte font and may not be displayed correctly."));
+#endif
 }
 
 
@@ -1858,8 +1885,10 @@ gtk_font_selection_switch_page (GtkWidget       *w,
 static void
 gtk_font_selection_show_font_info (GtkFontSelection *fontsel)
 {
+#if GDK_WINDOWING == GDK_WINDOWING_X11
   Atom font_atom, atom;
   Bool status;
+#endif
   char *name;
   gchar *fontname;
   gchar field_buffer[XLFD_MAX_FIELD_LEN];
@@ -1888,7 +1917,7 @@ gtk_font_selection_show_font_info (GtkFontSelection *fontsel)
       gtk_clist_set_text(GTK_CLIST(fontsel->info_clist), i, 1,
                         field ? field : "");
     }
-  
+#if GDK_WINDOWING == GDK_WINDOWING_X11
   if (fontsel->font)
     {
       font_atom = XInternAtom(GDK_DISPLAY(), "FONT", True);
@@ -1917,6 +1946,33 @@ gtk_font_selection_show_font_info (GtkFontSelection *fontsel)
            }
        }
     }
+#elif GDK_WINDOWING == GDK_WINDOWING_WIN32
+  if (fontsel->font)
+    {
+      LOGFONT logfont;
+
+      if (GetObject (GDK_FONT_XFONT (fontsel->font),
+                    sizeof (LOGFONT), &logfont) > 0)
+       {
+         name = logfont_to_xlfd (&logfont, logfont.lfHeight, -1, 0);
+         gtk_entry_set_text (GTK_ENTRY(fontsel->actual_font_name),name);
+         for (i = 0; i < GTK_XLFD_NUM_FIELDS; i++)
+           {
+             field = gtk_font_selection_get_xlfd_field (name, i,
+                                                        field_buffer);
+             if (i == XLFD_SLANT)
+               field = gtk_font_selection_expand_slant_code(field);
+             else if (i == XLFD_SPACING)
+               field = gtk_font_selection_expand_spacing_code(field);
+             gtk_clist_set_text(GTK_CLIST(fontsel->info_clist), i, 2,
+                                field ? field : "");
+           }
+         shown_actual_fields = TRUE;
+         g_free (name);
+       }
+    }
+  
+#endif
   if (!shown_actual_fields)
     {
       gtk_entry_set_text(GTK_ENTRY(fontsel->actual_font_name), "");
@@ -2326,11 +2382,11 @@ gtk_font_selection_reset_filter      (GtkWidget      *w,
 
   base_font_type = fontsel->filters[GTK_FONT_FILTER_BASE].font_type;
   if (base_font_type & GTK_FONT_BITMAP)
-    gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (fontsel->type_bitmaps_button), TRUE);
+    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fontsel->type_bitmaps_button), TRUE);
   if (base_font_type & GTK_FONT_SCALABLE)
-    gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (fontsel->type_scalable_button), TRUE);
+    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fontsel->type_scalable_button), TRUE);
   if (base_font_type & GTK_FONT_SCALABLE_BITMAP)
-    gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (fontsel->type_scaled_bitmaps_button), FALSE);
+    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fontsel->type_scaled_bitmaps_button), FALSE);
   
   for (prop = 0; prop < GTK_NUM_FONT_PROPERTIES; prop++)
     gtk_clist_select_row(GTK_CLIST(fontsel->filter_clists[prop]), 0, 0);
@@ -2433,7 +2489,7 @@ gtk_font_selection_set_filter     (GtkFontSelection *fontsel,
              filter_string = filter_strings[prop][i];
              for (j = 0; j < fontsel_info->nproperties[prop]; j++)
                {
-                 property = fontsel_info->properties[prop][j];
+                 property = _(fontsel_info->properties[prop][j]);
                  property_alt = NULL;
                  if (prop == SLANT)
                    property_alt = gtk_font_selection_expand_slant_code(property);
@@ -2461,47 +2517,47 @@ gtk_font_selection_set_filter   (GtkFontSelection *fontsel,
       if (font_type & GTK_FONT_BITMAP)
        {
          gtk_widget_set_sensitive (fontsel->type_bitmaps_button, TRUE);
-         gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (fontsel->type_bitmaps_button), user_font_type & GTK_FONT_BITMAP);
+         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fontsel->type_bitmaps_button), user_font_type & GTK_FONT_BITMAP);
        }
       else
        {
          gtk_widget_set_sensitive (fontsel->type_bitmaps_button, FALSE);
-         gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (fontsel->type_bitmaps_button), FALSE);
+         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fontsel->type_bitmaps_button), FALSE);
        }
       
       if (font_type & GTK_FONT_SCALABLE)
        {
          gtk_widget_set_sensitive (fontsel->type_scalable_button, TRUE);
-         gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (fontsel->type_scalable_button), user_font_type & GTK_FONT_SCALABLE);
+         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fontsel->type_scalable_button), user_font_type & GTK_FONT_SCALABLE);
        }
       else
        {
          gtk_widget_set_sensitive (fontsel->type_scalable_button, FALSE);
-         gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (fontsel->type_scalable_button), FALSE);
+         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fontsel->type_scalable_button), FALSE);
        }
 
       if (font_type & GTK_FONT_SCALABLE_BITMAP)
        {
          gtk_widget_set_sensitive (fontsel->type_scaled_bitmaps_button, TRUE);
-         gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (fontsel->type_scaled_bitmaps_button), user_font_type & GTK_FONT_SCALABLE_BITMAP);
+         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fontsel->type_scaled_bitmaps_button), user_font_type & GTK_FONT_SCALABLE_BITMAP);
        }
       else
        {
          gtk_widget_set_sensitive (fontsel->type_scaled_bitmaps_button, FALSE);
-         gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (fontsel->type_scaled_bitmaps_button), FALSE);
+         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fontsel->type_scaled_bitmaps_button), FALSE);
        }
     }
   else
     {
       base_font_type = fontsel->filters[GTK_FONT_FILTER_BASE].font_type;
       if (base_font_type & GTK_FONT_BITMAP)
-       gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (fontsel->type_bitmaps_button), font_type & GTK_FONT_BITMAP);
+       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fontsel->type_bitmaps_button), font_type & GTK_FONT_BITMAP);
 
       if (base_font_type & GTK_FONT_SCALABLE)
-       gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (fontsel->type_scalable_button), font_type & GTK_FONT_SCALABLE);
+       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fontsel->type_scalable_button), font_type & GTK_FONT_SCALABLE);
 
       if (base_font_type & GTK_FONT_SCALABLE_BITMAP)
-       gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (fontsel->type_scaled_bitmaps_button), font_type & GTK_FONT_SCALABLE_BITMAP);
+       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fontsel->type_scaled_bitmaps_button), font_type & GTK_FONT_SCALABLE_BITMAP);
 
       /* If the user filter is not the default, make the 'Reset Filter' button
         sensitive. */
@@ -2592,6 +2648,190 @@ gtk_font_selection_filter_state (GtkFontSelection *fontsel,
 }
 
 
+#if GDK_WINDOWING == GDK_WINDOWING_WIN32
+
+static gint num_fonts;
+static gint font_names_size;
+static gchar **xfontnames;
+static HDC hdc;
+
+static char *
+logfont_to_xlfd (const LOGFONT *lfp,
+                int size,
+                int res,
+                int avg_width)
+{
+  const gchar *weight;
+  const gchar *registry, *encoding;
+  int point_size;
+  static int logpixelsy = 0;
+  gchar facename[LF_FACESIZE*3];
+  gchar *p, *q;
+
+  if (logpixelsy == 0)
+    {
+      HDC hdc = GetDC (NULL);
+      logpixelsy = GetDeviceCaps (hdc, LOGPIXELSY);
+      ReleaseDC (NULL, hdc);
+    }
+
+  /* Don't use _() here, only N_(), the actual translation is done elsewhere */   
+  if (lfp->lfWeight >= FW_HEAVY)
+    weight = N_("heavy");
+  else if (lfp->lfWeight >= FW_EXTRABOLD)
+    weight = N_("extrabold");
+  else if (lfp->lfWeight >= FW_BOLD)
+    weight = N_("bold");
+#ifdef FW_DEMIBOLD
+  else if (lfp->lfWeight >= FW_DEMIBOLD)
+    weight = N_("demibold");
+#endif
+  else if (lfp->lfWeight >= FW_MEDIUM)
+    weight = N_("medium");
+  else if (lfp->lfWeight >= FW_NORMAL)
+    weight = N_("normal");
+  else if (lfp->lfWeight >= FW_LIGHT)
+    weight = N_("light");
+  else if (lfp->lfWeight >= FW_EXTRALIGHT)
+    weight = N_("extralight");
+  else if (lfp->lfWeight >= FW_THIN)
+    weight = N_("thin");
+  else
+    weight = N_("regular");
+
+  if (lfp->lfCharSet == ANSI_CHARSET)
+    {
+      registry = "iso8859";
+      encoding = "1";
+    }
+  else
+    {
+      registry = "windows";
+      if (lfp->lfCharSet == DEFAULT_CHARSET)
+       encoding = "default";
+      else if (lfp->lfCharSet == SYMBOL_CHARSET)
+       encoding = "symbol";
+      else if (lfp->lfCharSet == SHIFTJIS_CHARSET)
+       encoding = "shiftjis";
+      else if (lfp->lfCharSet == GB2312_CHARSET)
+       encoding = "gb2312";
+      else if (lfp->lfCharSet == HANGEUL_CHARSET)
+       encoding = "hangeul";
+      else if (lfp->lfCharSet == CHINESEBIG5_CHARSET)
+       encoding = "chinesebig5";
+      else if (lfp->lfCharSet == OEM_CHARSET)
+       encoding = "oem";
+#ifdef JOHAB_CHARSET
+      else if (lfp->lfCharSet == JOHAB_CHARSET)
+       encoding = "johab";
+#endif
+      else if (lfp->lfCharSet == HEBREW_CHARSET)
+       encoding = "hebrew";
+      else if (lfp->lfCharSet == ARABIC_CHARSET)
+       encoding = "arabic";
+      else if (lfp->lfCharSet == GREEK_CHARSET)
+       encoding = "greek";
+      else if (lfp->lfCharSet == TURKISH_CHARSET)
+       encoding = "turkish";
+      else if (lfp->lfCharSet == THAI_CHARSET)
+       encoding = "thai";
+      else if (lfp->lfCharSet == EASTEUROPE_CHARSET)
+       encoding = "easteurope";
+      else if (lfp->lfCharSet == RUSSIAN_CHARSET)
+       encoding = "russian";
+      else if (lfp->lfCharSet == MAC_CHARSET)
+       encoding = "mac";
+      else if (lfp->lfCharSet == BALTIC_CHARSET)
+       encoding = "baltic";
+      else
+       encoding = "unknown";
+    }
+  
+  point_size = (int) (((double) size/logpixelsy) * 720.);
+
+  if (res == -1)
+    res = logpixelsy;
+
+  /* Replace illegal characters with hex escapes. */
+  p = facename;
+  q = lfp->lfFaceName;
+  while (*q)
+    {
+      if (*q == '-' || *q == '*' || *q == '?' || *q == '%')
+       p += sprintf (p, "%%%.02x", *q);
+      else
+       *p++ = *q;
+      q++;
+    }
+  *p = '\0';
+
+  return  g_strdup_printf
+    ("-%s-%s-%s-%s-%s-%s-%d-%d-%d-%d-%s-%d-%s-%s",
+     "unknown", 
+     facename,
+     weight,
+     (lfp->lfItalic ?
+      ((lfp->lfPitchAndFamily & 0xF0) == FF_ROMAN
+       || (lfp->lfPitchAndFamily & 0xF0) == FF_SCRIPT ?
+       "i" : "o") : "r"),
+     "normal",
+     "",
+     size,
+     point_size,
+     res,
+     res,
+     ((lfp->lfPitchAndFamily & 0x03) == FIXED_PITCH ? "m" : "p"),
+     avg_width,
+     registry, encoding);
+}
+
+int CALLBACK
+InnerEnumFontFamExProc (const LOGFONT *lfp,
+                       const TEXTMETRIC *metrics,
+                       DWORD fontType,
+                       LPARAM lParam)
+{
+  int size;
+
+  if (fontType == TRUETYPE_FONTTYPE)
+    {
+      size = 0;
+    }
+  else
+    {
+      size = lfp->lfHeight;
+    }
+
+  num_fonts++;
+  if (num_fonts == font_names_size)
+    {
+      font_names_size *= 2;
+      xfontnames = g_realloc (xfontnames, font_names_size * sizeof (gchar *));
+    }
+  xfontnames[num_fonts-1] =
+    logfont_to_xlfd (lfp, size, 0, 0);
+  return 1;
+}
+
+int CALLBACK
+EnumFontFamExProc (const LOGFONT *lfp,
+                  const TEXTMETRIC *metrics,
+                  DWORD fontType,
+                  LPARAM lParam)
+{
+  if (fontType == TRUETYPE_FONTTYPE)
+    {
+      LOGFONT lf = *lfp;
+      lf.lfPitchAndFamily = 0;
+      EnumFontFamiliesEx (hdc, &lf, InnerEnumFontFamExProc, 0, 0);
+    }
+  else
+    InnerEnumFontFamExProc (lfp, metrics, fontType, lParam);
+  return 1;
+}
+
+#endif
+
 /*****************************************************************************
  * These functions all deal with creating the main class arrays containing
  * the data about all available fonts.
@@ -2599,11 +2839,18 @@ gtk_font_selection_filter_state (GtkFontSelection *fontsel,
 static void
 gtk_font_selection_get_fonts (void)
 {
+#if GDK_WINDOWING == GDK_WINDOWING_X11
   gchar **xfontnames;
+  gint num_fonts;
+#elif GDK_WINDOWING == GDK_WINDOWING_WIN32
+  LOGFONT logfont;
+#else
+  gint num_fonts = 0;
+  gchar **xfontnames = NULL;
+#endif
   GSList **fontnames;
   gchar *fontname;
   GSList * temp_list;
-  gint num_fonts;
   gint i, prop, style, size;
   gint npixel_sizes = 0, npoint_sizes = 0;
   FontInfo *font;
@@ -2616,7 +2863,8 @@ gtk_font_selection_get_fonts (void)
   guint16 *pixel_sizes, *point_sizes, *tmp_sizes;
   
   fontsel_info = g_new (GtkFontSelInfo, 1);
-  
+
+#if GDK_WINDOWING == GDK_WINDOWING_X11
   /* Get a maximum of MAX_FONTS fontnames from the X server.
      Use "-*" as the pattern rather than "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" since
      the latter may result in fonts being returned which don't actually exist.
@@ -2626,6 +2874,18 @@ gtk_font_selection_get_fonts (void)
   if (num_fonts == MAX_FONTS)
     g_warning(_("MAX_FONTS exceeded. Some fonts may be missing."));
   
+#elif GDK_WINDOWING == GDK_WINDOWING_WIN32
+  num_fonts = 0;
+  hdc = GetDC (NULL);
+  font_names_size = 100;
+  xfontnames = g_new (gchar *, font_names_size);
+  logfont.lfCharSet = DEFAULT_CHARSET;
+  logfont.lfFaceName[0] = '\0';
+  logfont.lfPitchAndFamily = 0;
+  EnumFontFamiliesEx (hdc, &logfont, EnumFontFamExProc, 0, 0);
+  ReleaseDC (NULL, hdc);
+#endif
+
   /* The maximum size of all these tables is the number of font names
      returned. We realloc them later when we know exactly how many
      unique entries there are. */
@@ -2867,8 +3127,14 @@ gtk_font_selection_get_fonts (void)
   fontsel_info->point_sizes = g_realloc(fontsel_info->point_sizes,
                                        sizeof(guint16) * npoint_sizes);
   g_free(fontnames);
+
+#if GDK_WINDOWING == GDK_WINDOWING_X11
   XFreeFontNames (xfontnames);
-  
+#elif GDK_WINDOWING == GDK_WINDOWING_WIN32
+  for (i = 0; i < num_fonts; i++)
+    g_free (xfontnames[i]);
+  g_free (xfontnames);
+#endif
   
   /* Debugging Output */
   /* This outputs all FontInfos. */
@@ -3163,7 +3429,7 @@ gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
        size = 20;
       fontsel->size = fontsel->selected_size = size;
       fontsel->metric = GTK_FONT_METRIC_POINTS;
-      gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(fontsel->points_button),
+      gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fontsel->points_button),
                                  TRUE);
       if (size % 10 == 0)
        sprintf (buffer, "%i", size / 10);
@@ -3179,7 +3445,7 @@ gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
        size = 2;
       fontsel->size = fontsel->selected_size = size;
       fontsel->metric = GTK_FONT_METRIC_PIXELS;
-      gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(fontsel->pixels_button),
+      gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fontsel->pixels_button),
                                  TRUE);
       sprintf (buffer, "%i", size);
     }
@@ -3363,7 +3629,6 @@ gtk_font_selection_create_xlfd (gint                size,
 {
   gchar buffer[16];
   gchar *pixel_size = "*", *point_size = "*", *fontname;
-  gint length;
   
   if (size <= 0)
     return NULL;
@@ -3374,18 +3639,10 @@ gtk_font_selection_create_xlfd (gint              size,
   else
     point_size = buffer;
   
-  /* Note: be careful here - don't overrun the allocated memory. */
-  length = strlen(foundry) + strlen(family) + strlen(weight) + strlen(slant)
-    + strlen(set_width) + strlen(pixel_size) + strlen(point_size)
-    + strlen(spacing) + strlen(charset)
-    + 1 + 1 + 1 + 1 + 1 + 3 + 1 + 5 + 3
-    + 1 /* for the terminating '\0'. */;
-  
-  fontname = g_new(gchar, length);
-  /* **NOTE**: If you change this string please change length above! */
-  sprintf(fontname, "-%s-%s-%s-%s-%s-*-%s-%s-*-*-%s-*-%s",
-         foundry, family, weight, slant, set_width, pixel_size,
-         point_size, spacing, charset);
+  fontname = g_strdup_printf ("-%s-%s-%s-%s-%s-*-%s-%s-*-*-%s-*-%s",
+                             foundry, family, weight, slant,
+                             set_width, pixel_size, point_size,
+                             spacing, charset);
   return fontname;
 }