]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkfontsel.c
Merges from gtk-1-2
[~andy/gtk] / gtk / gtkfontsel.c
index c665bae116d0bf05f4cff7062bee07f8420d0412..4c2318c2c0f1e70366b81dda0a23f8eee003211d 100644 (file)
 #include <ctype.h>
 
 #include "gdk/gdk.h"
+/* Protect against the CHARSET struct in Win32 */
+#ifdef GDK_WINDOWING_WIN32
+# define CHARSET CHARSETstruct
+#endif
 #include "gdkx.h"
+#ifdef GDK_WINDOWING_WIN32
+# undef CHARSET
+#endif
 #include "gdk/gdkkeysyms.h"
 
 #include "gtkbutton.h"
@@ -394,7 +401,7 @@ static gint    gtk_font_selection_dialog_on_configure(GtkWidget      *widget,
                                                      GdkEventConfigure *event,
                                                      GtkFontSelectionDialog *fsd);
 
-#if GDK_WINDOWING == GDK_WINDOWING_WIN32
+#ifdef GDK_WINDOWING_WIN32
 static char *logfont_to_xlfd (const LOGFONT *lfp,
                              int size,
                              int res,
@@ -893,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++)
          {
@@ -1218,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;
@@ -1446,18 +1453,20 @@ gtk_font_selection_show_available_sizes (GtkFontSelection *fontsel)
       can_match = TRUE;
 
       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 (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)
@@ -1751,6 +1760,7 @@ gtk_font_selection_load_font (GtkFontSelection *fontsel)
 {
   GdkFont *font;
   gchar *fontname, *label_text;
+  XFontStruct *xfs;
   
   if (fontsel->font)
     gdk_font_unref (fontsel->font);
@@ -1766,7 +1776,22 @@ gtk_font_selection_load_font (GtkFontSelection *fontsel)
 #ifdef FONTSEL_DEBUG
       g_message("Loading: %s\n", fontname);
 #endif
+#ifndef GDK_WINDOWING_WIN32
       font = gdk_font_load (fontname);
+      xfs = GDK_FONT_XFONT(font);
+      if (xfs->min_byte1 != 0 || xfs->max_byte1 != 0)
+       {
+         gchar *tmp_name;
+         
+         gdk_font_unref (font);
+         tmp_name = g_strconcat (fontname, ",*", NULL);
+         font = gdk_fontset_load (tmp_name);
+         g_free(tmp_name);
+       }
+#else
+      /* Load as a fontset so that gtkentry uses wide chars for it */
+      font = gdk_fontset_load (fontname);
+#endif
       g_free(fontname);
       
       if (font)
@@ -1809,7 +1834,7 @@ gtk_font_selection_update_preview (GtkFontSelection *fontsel)
   GtkStyle *style;
   gint text_height, new_height;
   gchar *text;
-#if GDK_WINDOWING == GDK_WINDOWING_X11
+#ifdef GDK_WINDOWING_X11
   XFontStruct *xfs;
 #endif
 
@@ -1844,7 +1869,7 @@ 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
+#ifdef 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);
@@ -1878,7 +1903,7 @@ gtk_font_selection_switch_page (GtkWidget       *w,
 static void
 gtk_font_selection_show_font_info (GtkFontSelection *fontsel)
 {
-#if GDK_WINDOWING == GDK_WINDOWING_X11
+#ifdef GDK_WINDOWING_X11
   Atom font_atom, atom;
   Bool status;
 #endif
@@ -1910,36 +1935,47 @@ 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
+#ifdef GDK_WINDOWING_X11
   if (fontsel->font)
     {
-      font_atom = XInternAtom(GDK_DISPLAY(), "FONT", True);
-      if (font_atom != None)
+      font_atom = gdk_atom_intern ("FONT", FALSE);
+
+      if (fontsel->font->type == GDK_FONT_FONTSET)
+       {
+         XFontStruct **font_structs;
+         gint num_fonts;
+         gchar **font_names;
+         
+         num_fonts = XFontsOfFontSet (GDK_FONT_XFONT(fontsel->font),
+                                      &font_structs, &font_names);
+         status = XGetFontProperty(font_structs[0], font_atom, &atom);
+       }
+      else
        {
          status = XGetFontProperty(GDK_FONT_XFONT(fontsel->font), font_atom,
                                    &atom);
-         if (status == True)
+       }
+
+      if (status == True)
+       {
+         name = gdk_atom_name (atom);
+         gtk_entry_set_text (GTK_ENTRY (fontsel->actual_font_name), name);
+         
+         for (i = 0; i < GTK_XLFD_NUM_FIELDS; i++)
            {
-             name = XGetAtomName(GDK_DISPLAY(), atom);
-             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;
-             XFree(name);
+             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);
        }
     }
-#elif GDK_WINDOWING == GDK_WINDOWING_WIN32
+#elif defined (GDK_WINDOWING_WIN32)
   if (fontsel->font)
     {
       LOGFONT logfont;
@@ -2482,7 +2518,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);
@@ -2641,7 +2677,7 @@ gtk_font_selection_filter_state (GtkFontSelection *fontsel,
 }
 
 
-#if GDK_WINDOWING == GDK_WINDOWING_WIN32
+#ifdef GDK_WINDOWING_WIN32
 
 static gint num_fonts;
 static gint font_names_size;
@@ -2659,7 +2695,8 @@ logfont_to_xlfd (const LOGFONT *lfp,
   int point_size;
   static int logpixelsy = 0;
   gchar facename[LF_FACESIZE*3];
-  gchar *p, *q;
+  gchar *p;
+  const gchar *q;
 
   if (logpixelsy == 0)
     {
@@ -2668,26 +2705,29 @@ logfont_to_xlfd (const LOGFONT *lfp,
       ReleaseDC (NULL, hdc);
     }
 
+  /* Don't use _() here, only N_(), the actual translation is done elsewhere */   
   if (lfp->lfWeight >= FW_HEAVY)
-    weight = "heavy";
+    weight = N_("heavy");
   else if (lfp->lfWeight >= FW_EXTRABOLD)
-    weight = "extrabold";
+    weight = N_("extrabold");
   else if (lfp->lfWeight >= FW_BOLD)
-    weight = "bold";
+    weight = N_("bold");
+#ifdef FW_DEMIBOLD
   else if (lfp->lfWeight >= FW_DEMIBOLD)
-    weight = "demibold";
+    weight = N_("demibold");
+#endif
   else if (lfp->lfWeight >= FW_MEDIUM)
-    weight = "medium";
+    weight = N_("medium");
   else if (lfp->lfWeight >= FW_NORMAL)
-    weight = "normal";
+    weight = N_("normal");
   else if (lfp->lfWeight >= FW_LIGHT)
-    weight = "light";
+    weight = N_("light");
   else if (lfp->lfWeight >= FW_EXTRALIGHT)
-    weight = "extralight";
+    weight = N_("extralight");
   else if (lfp->lfWeight >= FW_THIN)
-    weight = "thin";
+    weight = N_("thin");
   else
-    weight = "regular";
+    weight = N_("regular");
 
   if (lfp->lfCharSet == ANSI_CHARSET)
     {
@@ -2711,8 +2751,10 @@ logfont_to_xlfd (const LOGFONT *lfp,
        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)
@@ -2827,11 +2869,14 @@ EnumFontFamExProc (const LOGFONT *lfp,
 static void
 gtk_font_selection_get_fonts (void)
 {
-#if GDK_WINDOWING == GDK_WINDOWING_X11
+#ifdef GDK_WINDOWING_X11
   gchar **xfontnames;
   gint num_fonts;
-#elif GDK_WINDOWING == GDK_WINDOWING_WIN32
+#elif defined (GDK_WINDOWING_WIN32)
   LOGFONT logfont;
+#else
+  gint num_fonts = 0;
+  gchar **xfontnames = NULL;
 #endif
   GSList **fontnames;
   gchar *fontname;
@@ -2849,7 +2894,7 @@ gtk_font_selection_get_fonts (void)
   
   fontsel_info = g_new (GtkFontSelInfo, 1);
 
-#if GDK_WINDOWING == GDK_WINDOWING_X11
+#ifdef 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.
@@ -2859,7 +2904,7 @@ 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
+#elif defined (GDK_WINDOWING_WIN32)
   num_fonts = 0;
   hdc = GetDC (NULL);
   font_names_size = 100;
@@ -2899,17 +2944,8 @@ gtk_font_selection_get_fonts (void)
   fontsel_info->nfonts = 0;
   for (i = 0; i < num_fonts; i++)
     {
-#ifdef FONTSEL_DEBUG
-      g_message("%s\n", xfontnames[i]);
-#endif
       if (gtk_font_selection_is_xlfd_font_name (xfontnames[i]))
        gtk_font_selection_insert_font (fontnames, &fontsel_info->nfonts, xfontnames[i]);
-      else
-       {
-#ifdef FONTSEL_DEBUG
-         g_warning("Skipping invalid font: %s", xfontnames[i]);
-#endif
-       }
     }
   
   
@@ -3113,9 +3149,9 @@ gtk_font_selection_get_fonts (void)
                                        sizeof(guint16) * npoint_sizes);
   g_free(fontnames);
 
-#if GDK_WINDOWING == GDK_WINDOWING_X11
+#ifdef GDK_WINDOWING_X11
   XFreeFontNames (xfontnames);
-#elif GDK_WINDOWING == GDK_WINDOWING_WIN32
+#elif defined (GDK_WINDOWING_WIN32)
   for (i = 0; i < num_fonts; i++)
     g_free (xfontnames[i]);
   g_free (xfontnames);
@@ -3560,6 +3596,9 @@ gtk_font_selection_get_xlfd_field (const gchar *fontname,
 {
   const gchar *t1, *t2;
   gint countdown, len, num_dashes;
+#ifdef GDK_WINDOWING_WIN32
+  gchar *p;
+#endif
   
   if (!fontname)
     return NULL;
@@ -3587,9 +3626,27 @@ gtk_font_selection_get_xlfd_field (const gchar *fontname,
        return NULL;
       strncpy (buffer, t1, len);
       buffer[len] = 0;
-
+#ifdef GDK_WINDOWING_X11
       /* Convert to lower case. */
       g_strdown (buffer);
+#elif defined (GDK_WINDOWING_WIN32)
+      /* Check for hex escapes in font family */
+      if (field_num == XLFD_FAMILY)
+       {
+         p = buffer;
+         while (*p)
+           {
+             if (*p == '%' && isxdigit (p[1]) && isxdigit (p[2]))
+               {
+                 guint c;
+                 sscanf (p+1, "%2x", &c);
+                 *p = c;
+                 strcpy (p+1, p+3);
+               }
+             p++;
+           }
+       }
+#endif
     }
   else
     strcpy(buffer, "(nil)");
@@ -3614,7 +3671,11 @@ gtk_font_selection_create_xlfd (gint               size,
 {
   gchar buffer[16];
   gchar *pixel_size = "*", *point_size = "*", *fontname;
-  
+  gchar *fam = family;
+#ifdef GDK_WINDOWING_WIN32 
+  gchar *p, *q;
+#endif
+
   if (size <= 0)
     return NULL;
   
@@ -3624,10 +3685,27 @@ gtk_font_selection_create_xlfd (gint              size,
   else
     point_size = buffer;
   
+#ifdef GDK_WINDOWING_WIN32
+  fam = g_malloc (strlen (family) * 3 + 1);
+  p = fam;
+  q = family;
+  while (*q)
+    {
+      if (*q == '-' || *q == '*' || *q == '?' || *q == '%')
+       p += sprintf (p, "%%%.02x", *q);
+      else
+       *p++ = *q;
+      q++;
+    }
+  *p = '\0';
+#endif
   fontname = g_strdup_printf ("-%s-%s-%s-%s-%s-*-%s-%s-*-*-%s-*-%s",
-                             foundry, family, weight, slant,
+                             foundry, fam, weight, slant,
                              set_width, pixel_size, point_size,
                              spacing, charset);
+#ifdef GDK_WINDOWING_WIN32
+  g_free (fam);
+#endif
   return fontname;
 }
 
@@ -3637,7 +3715,7 @@ gtk_font_selection_create_xlfd (gint                size,
  * GtkFontSelectionDialog
  *****************************************************************************/
 
-guint
+GtkType
 gtk_font_selection_dialog_get_type (void)
 {
   static guint font_selection_dialog_type = 0;