- /* If it is already the current font, just return. */
- if (fontsel->font_index == row)
- return;
-
- fontsel->font_index = row;
- gtk_font_selection_show_available_styles (fontsel);
- gtk_font_selection_select_best_style (fontsel, TRUE);
-}
-
-
-static gint
-gtk_font_selection_on_clist_key_press (GtkWidget *clist,
- GdkEventKey *event,
- GtkFontSelection *fontsel)
-{
-#ifdef FONTSEL_DEBUG
- g_message("In on_clist_key_press\n");
-#endif
- if (event->keyval == GDK_Up)
- return gtk_font_selection_select_next (fontsel, clist, -1);
- else if (event->keyval == GDK_Down)
- return gtk_font_selection_select_next (fontsel, clist, 1);
- else
- return FALSE;
-}
-
-
-static gboolean
-gtk_font_selection_select_next (GtkFontSelection *fontsel,
- GtkWidget *clist,
- gint step)
-{
- GList *selection;
- gint current_row, row;
-
- selection = GTK_CLIST(clist)->selection;
- if (!selection)
- return FALSE;
- current_row = GPOINTER_TO_INT (selection->data);
-
- /* Stop the normal clist key handler from being run. */
- gtk_signal_emit_stop_by_name (GTK_OBJECT (clist), "key_press_event");
-
- for (row = current_row + step;
- row >= 0 && row < GTK_CLIST(clist)->rows;
- row += step)
- {
- /* If this is the style clist, make sure that the item is not a charset
- entry. */
- if (clist == fontsel->font_style_clist)
- if (GPOINTER_TO_INT (gtk_clist_get_row_data(GTK_CLIST(clist), row)) == -1)
- continue;
-
- /* Now we've found the row to select. */
- if (gtk_clist_row_is_visible(GTK_CLIST(clist), row)
- != GTK_VISIBILITY_FULL)
- gtk_clist_moveto(GTK_CLIST(clist), row, -1, (step < 0) ? 0 : 1, 0);
- gtk_clist_select_row(GTK_CLIST(clist), row, 0);
- break;
- }
- return TRUE;
-}
-
-
-/* This fills the font style clist with all the possible style combinations
- for the current font family. */
-static void
-gtk_font_selection_show_available_styles (GtkFontSelection *fontsel)
-{
- FontInfo *font;
- FontStyle *styles;
- gint style, tmpstyle, row;
- gint weight_index, slant_index, set_width_index, spacing_index;
- gint charset_index;
- gchar *weight, *slant, *set_width, *spacing;
- gchar *charset = NULL;
- gchar *new_item;
- gchar buffer[XLFD_MAX_FIELD_LEN * 6 + 2];
- GdkColor *inactive_fg, *inactive_bg;
- gboolean show_charset;
-
-#ifdef FONTSEL_DEBUG
- g_message("In show_available_styles\n");
-#endif
- font = &fontsel_info->font_info[fontsel->font_index];
- styles = &fontsel_info->font_styles[font->style_index];
-
- gtk_clist_freeze (GTK_CLIST(fontsel->font_style_clist));
- gtk_clist_clear (GTK_CLIST(fontsel->font_style_clist));
-
- /* First we mark all visible styles as not having been displayed yet,
- and check if every style has the same charset. If not then we will
- display the charset in the list before the styles. */
- show_charset = FALSE;
- charset_index = -1;
- for (style = 0; style < font->nstyles; style++)
- {
- if (gtk_font_selection_style_visible(fontsel, font, style))
- {
- styles[style].flags &= ~GTK_FONT_DISPLAYED;
-
- if (charset_index == -1)
- charset_index = styles[style].properties[CHARSET];
- else if (charset_index != styles[style].properties[CHARSET])
- show_charset = TRUE;
- }
- else
- styles[style].flags |= GTK_FONT_DISPLAYED;
- }
-
- /* Step through the undisplayed styles, finding the next charset which
- hasn't been displayed yet. Then display the charset on one line, if
- necessary, and the visible styles indented beneath it. */
- inactive_fg = &fontsel->font_style_clist->style->fg[GTK_STATE_INSENSITIVE];
- inactive_bg = &fontsel->font_style_clist->style->bg[GTK_STATE_INSENSITIVE];
-
- for (style = 0; style < font->nstyles; style++)
- {
- if (styles[style].flags & GTK_FONT_DISPLAYED)
- continue;
-
- if (show_charset)
- {
- charset_index = styles[style].properties[CHARSET];
- charset = fontsel_info->properties[CHARSET] [charset_index];
- row = gtk_clist_append(GTK_CLIST(fontsel->font_style_clist),
- &charset);
- gtk_clist_set_row_data(GTK_CLIST(fontsel->font_style_clist), row,
- (gpointer) -1);
- if (GTK_WIDGET_REALIZED (fontsel->font_style_clist))
- {
- gtk_clist_set_foreground(GTK_CLIST(fontsel->font_style_clist),
- row, inactive_fg);
- gtk_clist_set_background(GTK_CLIST(fontsel->font_style_clist),
- row, inactive_bg);
- }
- }
-
- for (tmpstyle = style; tmpstyle < font->nstyles; tmpstyle++)
- {
- if (styles[tmpstyle].flags & GTK_FONT_DISPLAYED
- || charset_index != styles[tmpstyle].properties[CHARSET])
- continue;
-
- styles[tmpstyle].flags |= GTK_FONT_DISPLAYED;
-
- weight_index = styles[tmpstyle].properties[WEIGHT];
- slant_index = styles[tmpstyle].properties[SLANT];
- set_width_index = styles[tmpstyle].properties[SET_WIDTH];
- spacing_index = styles[tmpstyle].properties[SPACING];
- weight = fontsel_info->properties[WEIGHT] [weight_index];
- slant = fontsel_info->properties[SLANT] [slant_index];
- set_width = fontsel_info->properties[SET_WIDTH][set_width_index];
- spacing = fontsel_info->properties[SPACING] [spacing_index];
-
- /* Convert '(nil)' weights to 'regular', since it looks nicer. */
- 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 = 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 = 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));
- if (slant)
- {
- strcat(buffer, " ");
- strcat(buffer, _(slant));
- }
- if (set_width)
- {
- strcat(buffer, " ");
- strcat(buffer, _(set_width));
- }
- if (spacing)
- {
- strcat(buffer, " ");
- strcat(buffer, _(spacing));
- }
-
- new_item = buffer;
- row = gtk_clist_append(GTK_CLIST(fontsel->font_style_clist),
- &new_item);
- if (show_charset)
- gtk_clist_set_shift(GTK_CLIST(fontsel->font_style_clist), row, 0,
- 0, 4);
- gtk_clist_set_row_data(GTK_CLIST(fontsel->font_style_clist), row,
- GINT_TO_POINTER (tmpstyle));
- }
- }
-
- gtk_clist_thaw (GTK_CLIST(fontsel->font_style_clist));
-}
-
-
-/* This selects a style when the user selects a font. It just uses the first
- available style at present. I was thinking of trying to maintain the
- selected style, e.g. bold italic, when the user selects different fonts.
- However, the interface is so easy to use now I'm not sure it's worth it.
- Note: This will load a font. */
-static void
-gtk_font_selection_select_best_style(GtkFontSelection *fontsel,
- gboolean use_first)
-{
- FontInfo *font;
- FontStyle *styles;
- gint row, prop, style, matched;
- gint best_matched = -1, best_style = -1, best_row = -1;
-
-#ifdef FONTSEL_DEBUG
- g_message("In select_best_style\n");
-#endif
- font = &fontsel_info->font_info[fontsel->font_index];
- styles = &fontsel_info->font_styles[font->style_index];
-
- for (row = 0; row < GTK_CLIST(fontsel->font_style_clist)->rows; row++)
- {
- style = GPOINTER_TO_INT (gtk_clist_get_row_data (GTK_CLIST (fontsel->font_style_clist), row));
- /* Skip charset rows. */
- if (style == -1)
- continue;
-
- /* If we just want the first style, we've got it. */
- if (use_first)
- {
- best_style = style;
- best_row = row;
- break;
- }
-
- matched = 0;
- for (prop = 0; prop < GTK_NUM_STYLE_PROPERTIES; prop++)
- {
- if (fontsel->property_values[prop] == styles[style].properties[prop])
- matched++;
- }
- if (matched > best_matched)
- {
- best_matched = matched;
- best_style = style;
- best_row = row;
- }
- }
- g_return_if_fail (best_style != -1);
- g_return_if_fail (best_row != -1);
-
- fontsel->style = best_style;
-
- for (prop = 0; prop < GTK_NUM_STYLE_PROPERTIES; prop++)
- fontsel->property_values[prop] = styles[fontsel->style].properties[prop];
-
- gtk_clist_select_row(GTK_CLIST(fontsel->font_style_clist), best_row, 0);
- if (gtk_clist_row_is_visible(GTK_CLIST(fontsel->font_style_clist), best_row)
- != GTK_VISIBILITY_FULL)
- gtk_clist_moveto(GTK_CLIST(fontsel->font_style_clist), best_row, -1,
- 0.5, 0);
- gtk_font_selection_show_available_sizes (fontsel);
- gtk_font_selection_select_best_size (fontsel);
-}
-
-
-/* This is called when a style is selected in the list. */
-static void
-gtk_font_selection_select_style (GtkWidget *w,
- gint row,
- gint column,
- GdkEventButton *bevent,
- gpointer data)
-{
- GtkFontSelection *fontsel;
- FontInfo *font_info;
- FontInfo *font;
- FontStyle *styles;
- gint style, prop;
- gchar *text;
-
-#ifdef FONTSEL_DEBUG
- g_message("In select_style\n");
-#endif
- fontsel = GTK_FONT_SELECTION(data);
- font_info = fontsel_info->font_info;
- font = &font_info[fontsel->font_index];
- styles = &fontsel_info->font_styles[font->style_index];
-
- if (bevent && !GTK_WIDGET_HAS_FOCUS (w))
- gtk_widget_grab_focus (w);
-
- /* The style index is stored in the row data, so we just need to copy
- the style values into the fontsel and reload the font. */
- style = GPOINTER_TO_INT (gtk_clist_get_row_data(GTK_CLIST(fontsel->font_style_clist), row));
-
- /* Don't allow selection of charset rows. */
- if (style == -1)
- {
- gtk_clist_unselect_row(GTK_CLIST(fontsel->font_style_clist), row, 0);
- return;
- }
-
- gtk_clist_get_text(GTK_CLIST(fontsel->font_style_clist), row, 0, &text);
- gtk_entry_set_text(GTK_ENTRY(fontsel->font_style_entry), text);
-
- for (prop = 0; prop < GTK_NUM_STYLE_PROPERTIES; prop++)
- fontsel->property_values[prop] = styles[style].properties[prop];
-
- if (fontsel->style == style)
- return;
-
- fontsel->style = style;
- gtk_font_selection_show_available_sizes (fontsel);
- gtk_font_selection_select_best_size (fontsel);
-}
-
-
-/* This shows all the available sizes in the size clist, according to the
- current metric and the current font & style. */
-static void
-gtk_font_selection_show_available_sizes (GtkFontSelection *fontsel)
-{
- FontInfo *font;
- FontStyle *styles, *style;
- const guint16 *standard_sizes;
- guint16 *bitmapped_sizes;
- gint nstandard_sizes, nbitmapped_sizes;
- gchar buffer[16], *size;
- gfloat bitmap_size_float = 0.;
- guint16 bitmap_size = 0;
- gboolean can_match;
- gint type_filter;
-
-#ifdef FONTSEL_DEBUG
- g_message("In show_available_sizes\n");
-#endif
- font = &fontsel_info->font_info[fontsel->font_index];
- styles = &fontsel_info->font_styles[font->style_index];
- style = &styles[fontsel->style];
-
- standard_sizes = font_sizes;
- nstandard_sizes = sizeof(font_sizes) / sizeof(font_sizes[0]);
- if (fontsel->metric == GTK_FONT_METRIC_POINTS)
- {
- bitmapped_sizes = &fontsel_info->point_sizes[style->point_sizes_index];
- nbitmapped_sizes = style->npoint_sizes;
- }
- else
- {
- bitmapped_sizes = &fontsel_info->pixel_sizes[style->pixel_sizes_index];
- nbitmapped_sizes = style->npixel_sizes;
- }
-
- /* Only show the standard sizes if a scalable font is available. */
- type_filter = fontsel->filters[GTK_FONT_FILTER_BASE].font_type
- & fontsel->filters[GTK_FONT_FILTER_USER].font_type;
-
- if (!((style->flags & GTK_FONT_SCALABLE_BITMAP
- && type_filter & GTK_FONT_SCALABLE_BITMAP)
- || (style->flags & GTK_FONT_SCALABLE
- && type_filter & GTK_FONT_SCALABLE)))
- nstandard_sizes = 0;
-
- gtk_clist_freeze (GTK_CLIST(fontsel->size_clist));
- gtk_clist_clear (GTK_CLIST(fontsel->size_clist));
-
- /* Interleave the standard sizes with the bitmapped sizes so we get a list
- of ascending sizes. If the metric is points, we have to convert the
- decipoints to points. */
- while (nstandard_sizes || nbitmapped_sizes)
- {
- 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 (can_match && nstandard_sizes && nbitmapped_sizes
- && *standard_sizes == bitmap_size)
- {
- sprintf(buffer, "%i *", *standard_sizes);
- standard_sizes++;
- nstandard_sizes--;
- bitmapped_sizes++;
- nbitmapped_sizes--;
- }
- else if (nstandard_sizes
- && (!nbitmapped_sizes
- || (gfloat)*standard_sizes < bitmap_size_float))
- {
- sprintf(buffer, "%i", *standard_sizes);
- standard_sizes++;
- nstandard_sizes--;
- }
- else
- {
- if (fontsel->metric == GTK_FONT_METRIC_POINTS)
- {
- if (*bitmapped_sizes % 10 == 0)
- sprintf(buffer, "%i *", *bitmapped_sizes / 10);
- else
- sprintf(buffer, "%i.%i *", *bitmapped_sizes / 10,
- *bitmapped_sizes % 10);
- }
- else
- {
- sprintf(buffer, "%i *", *bitmapped_sizes);
- }
- bitmapped_sizes++;
- nbitmapped_sizes--;
- }
- size = buffer;
- gtk_clist_append(GTK_CLIST(fontsel->size_clist), &size);
- }
- gtk_clist_thaw (GTK_CLIST(fontsel->size_clist));
-}
-
-
-/* If the user hits return in the font size entry, we change to the new font
- size. */
-static gint
-gtk_font_selection_size_key_press (GtkWidget *w,
- GdkEventKey *event,
- gpointer data)
-{
- GtkFontSelection *fontsel;
- gint new_size;
- gfloat new_size_float;
- gchar *text;
-
-#ifdef FONTSEL_DEBUG
- g_message("In size_key_press\n");
-#endif
- fontsel = GTK_FONT_SELECTION(data);
-
- if (event->keyval == GDK_Return)
- {
- text = gtk_entry_get_text (GTK_ENTRY (fontsel->size_entry));
- if (fontsel->metric == GTK_FONT_METRIC_PIXELS)
- {
- new_size = atoi (text);
- if (new_size < 2)
- new_size = 2;
- }
- else
- {
- new_size_float = atof (text) * 10;
- new_size = (gint) new_size_float;
- if (new_size < 20)
- new_size = 20;
- }
-
- /* Remember that this size was set explicitly. */
- fontsel->selected_size = new_size;
-
- /* Check if the font size has changed, and return if it hasn't. */
- if (fontsel->size == new_size)
- return TRUE;
-
- fontsel->size = new_size;
- gtk_font_selection_select_best_size (fontsel);
- return TRUE;
- }
-
- return FALSE;
-}
-
-
-/* This tries to select the closest size to the current size, though it
- may have to change the size if only unscaled bitmaps are available.
- Note: this will load a font. */
-static void
-gtk_font_selection_select_best_size(GtkFontSelection *fontsel)
-{
- FontInfo *font;
- FontStyle *styles, *style;
- gchar *text;
- gint row, best_row = 0, size, size_fraction, best_size = 0, nmatched;
- gboolean found = FALSE;
- gchar buffer[32];
- GList *selection;
- gint type_filter;
-
-#ifdef FONTSEL_DEBUG
- g_message("In select_best_size\n");
-#endif
- font = &fontsel_info->font_info[fontsel->font_index];
- styles = &fontsel_info->font_styles[font->style_index];
- style = &styles[fontsel->style];
-
- /* Find the closest size available in the size clist. If the exact size is
- in the list set found to TRUE. */
- for (row = 0; row < GTK_CLIST(fontsel->size_clist)->rows; row++)
- {
- gtk_clist_get_text(GTK_CLIST(fontsel->size_clist), row, 0, &text);
- nmatched = sscanf(text, "%i.%i", &size, &size_fraction);
- if (fontsel->metric == GTK_FONT_METRIC_POINTS)
- {
- size *= 10;
- if (nmatched == 2)
- size += size_fraction;
- }
-
- if (size == fontsel->selected_size)
- {
- found = TRUE;
- best_size = size;
- best_row = row;
- break;
- }
- else if (best_size == 0
- || abs(size - fontsel->selected_size)
- < (abs(best_size - fontsel->selected_size)))
- {
- best_size = size;
- best_row = row;
- }
- }
-
- /* If we aren't scaling bitmapped fonts and this is a bitmapped font, we
- need to use the closest size found. */
- type_filter = fontsel->filters[GTK_FONT_FILTER_BASE].font_type
- & fontsel->filters[GTK_FONT_FILTER_USER].font_type;
-
- if (!((style->flags & GTK_FONT_SCALABLE_BITMAP
- && type_filter & GTK_FONT_SCALABLE_BITMAP)
- || (style->flags & GTK_FONT_SCALABLE
- && type_filter & GTK_FONT_SCALABLE)))
- found = TRUE;
-
- if (found)
- {
- fontsel->size = best_size;
- gtk_clist_moveto(GTK_CLIST(fontsel->size_clist), best_row, -1, 0.5, 0);
- gtk_clist_select_row(GTK_CLIST(fontsel->size_clist), best_row, 0);
- }
- else
- {
- fontsel->size = fontsel->selected_size;
- selection = GTK_CLIST(fontsel->size_clist)->selection;
- if (selection)
- gtk_clist_unselect_row(GTK_CLIST(fontsel->size_clist),
- GPOINTER_TO_INT (selection->data), 0);
- gtk_clist_moveto(GTK_CLIST(fontsel->size_clist), best_row, -1, 0.5, 0);
-
- /* Show the size in the size entry. */
- if (fontsel->metric == GTK_FONT_METRIC_PIXELS)
- sprintf(buffer, "%i", fontsel->size);
- else
- {
- if (fontsel->size % 10 == 0)
- sprintf(buffer, "%i", fontsel->size / 10);
- else
- sprintf(buffer, "%i.%i", fontsel->size / 10, fontsel->size % 10);
- }
- gtk_entry_set_text (GTK_ENTRY (fontsel->size_entry), buffer);
- }
- gtk_font_selection_load_font (fontsel);
-}
-
-
-/* This is called when a size is selected in the list. */
-static void
-gtk_font_selection_select_size (GtkWidget *w,
- gint row,
- gint column,
- GdkEventButton *bevent,
- gpointer data)
-{
- GtkFontSelection *fontsel;
- gint new_size;
- gchar *text;
- gchar buffer[16];
- gint i;
-
-#ifdef FONTSEL_DEBUG
- g_message("In select_size\n");
-#endif
- fontsel = GTK_FONT_SELECTION(data);
-
- if (bevent && !GTK_WIDGET_HAS_FOCUS (w))
- gtk_widget_grab_focus (w);
-
- /* Copy the size from the clist to the size entry, but without the bitmapped
- marker ('*'). */
- gtk_clist_get_text(GTK_CLIST(fontsel->size_clist), row, 0, &text);
- i = 0;
- while (i < 15 && (text[i] == '.' || (text[i] >= '0' && text[i] <= '9')))
- {
- buffer[i] = text[i];
- i++;
- }
- buffer[i] = '\0';
- gtk_entry_set_text(GTK_ENTRY(fontsel->size_entry), buffer);
-
- /* Check if the font size has changed, and return if it hasn't. */
- new_size = atoi(text);
- if (fontsel->metric == GTK_FONT_METRIC_POINTS)
- new_size *= 10;
-
- if (fontsel->size == new_size)
- return;
-
- /* If the size was selected by the user we set the selected_size. */
- fontsel->selected_size = new_size;
-
- fontsel->size = new_size;
- gtk_font_selection_load_font (fontsel);
-}
-
-
-/* This is called when the pixels or points radio buttons are pressed. */
-static void
-gtk_font_selection_metric_callback (GtkWidget *w,
- gpointer data)
-{
- GtkFontSelection *fontsel = GTK_FONT_SELECTION(data);
-
-#ifdef FONTSEL_DEBUG
- g_message("In metric_callback\n");
-#endif
- if (GTK_TOGGLE_BUTTON(fontsel->pixels_button)->active)
- {
- if (fontsel->metric == GTK_FONT_METRIC_PIXELS)
- return;
- fontsel->metric = GTK_FONT_METRIC_PIXELS;
- fontsel->size = (fontsel->size + 5) / 10;
- fontsel->selected_size = (fontsel->selected_size + 5) / 10;
- }
- else
- {
- if (fontsel->metric == GTK_FONT_METRIC_POINTS)
- return;
- fontsel->metric = GTK_FONT_METRIC_POINTS;
- fontsel->size *= 10;
- fontsel->selected_size *= 10;
- }
- if (fontsel->font_index != -1)
- {
- gtk_font_selection_show_available_sizes (fontsel);
- gtk_font_selection_select_best_size (fontsel);
- }
-}
-
-
-/* This searches the given property table and returns the index of the given
- string, or 0, which is the wildcard '*' index, if it's not found. */
-static guint16
-gtk_font_selection_field_to_index (gchar **table,
- gint ntable,
- gchar *field)
-{
- gint i;
-
- for (i = 0; i < ntable; i++)
- if (strcmp (field, table[i]) == 0)
- return i;
-
- return 0;
-}
-
-
-
-/* This attempts to load the current font, and returns TRUE if it succeeds. */
-static gboolean
-gtk_font_selection_load_font (GtkFontSelection *fontsel)
-{
- GdkFont *font;
- gchar *fontname, *label_text;
-
- if (fontsel->font)
- gdk_font_unref (fontsel->font);
- fontsel->font = NULL;
-
- /* If no family has been selected yet, just return FALSE. */
- if (fontsel->font_index == -1)
- return FALSE;
-
- fontname = gtk_font_selection_get_font_name (fontsel);
- if (fontname)
- {
-#ifdef FONTSEL_DEBUG
- g_message("Loading: %s\n", fontname);
-#endif
- font = gdk_font_load (fontname);
- g_free(fontname);
-
- if (font)
- {
- fontsel->font = font;
- /* Make sure the message label is empty, but don't change it unless
- it's necessary as it results in a resize of the whole window! */
- gtk_label_get(GTK_LABEL(fontsel->message_label), &label_text);
- if (strcmp(label_text, ""))
- gtk_label_set_text(GTK_LABEL(fontsel->message_label), "");
- gtk_font_selection_update_preview (fontsel);
- return TRUE;
- }
- else
- {
- gtk_label_set_text(GTK_LABEL(fontsel->message_label),
- _("The selected font is not available."));
- }
- }
- else
- {
- gtk_label_set_text(GTK_LABEL(fontsel->message_label),
- _("The selected font is not a valid font."));
- }
-
- return FALSE;
-}
-
-
-/* This sets the font in the preview entry to the selected font, and tries to
- make sure that the preview entry is a reasonable size, i.e. so that the
- text can be seen with a bit of space to spare. But it tries to avoid
- resizing the entry every time the font changes.
- This also used to shrink the preview if the font size was decreased, but
- that made it awkward if the user wanted to resize the window themself. */
-static void
-gtk_font_selection_update_preview (GtkFontSelection *fontsel)
-{
- GtkWidget *preview_entry;
- GtkStyle *style;
- gint text_height, new_height;
- gchar *text;
-#ifdef GDK_WINDOWING_X11
- XFontStruct *xfs;
-#endif
-
-#ifdef FONTSEL_DEBUG
- g_message("In update_preview\n");
-#endif
- style = gtk_style_new ();
- gdk_font_unref (style->font);
- style->font = fontsel->font;
- gdk_font_ref (style->font);
-
- preview_entry = fontsel->preview_entry;
- gtk_widget_set_style (preview_entry, style);
- gtk_style_unref(style);
-
- text_height = preview_entry->style->font->ascent
- + preview_entry->style->font->descent;
- /* We don't ever want to be over MAX_PREVIEW_HEIGHT pixels high. */
- new_height = text_height + 20;
- if (new_height < INITIAL_PREVIEW_HEIGHT)
- new_height = INITIAL_PREVIEW_HEIGHT;
- if (new_height > MAX_PREVIEW_HEIGHT)
- new_height = MAX_PREVIEW_HEIGHT;
-
- if ((preview_entry->requisition.height < text_height + 10)
- || (preview_entry->requisition.height > text_height + 40))
- gtk_widget_set_usize(preview_entry, -1, new_height);
-
- /* This sets the preview text, if it hasn't been set already. */
- text = gtk_entry_get_text(GTK_ENTRY(fontsel->preview_entry));
- if (strlen(text) == 0)
- gtk_entry_set_text(GTK_ENTRY(fontsel->preview_entry), PREVIEW_TEXT);
- gtk_entry_set_position(GTK_ENTRY(fontsel->preview_entry), 0);
-
-#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);
- 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
-}
-
-
-static void
-gtk_font_selection_switch_page (GtkWidget *w,
- GtkNotebookPage *page,
- gint page_num,
- gpointer data)
-{
- GtkFontSelection *fontsel = GTK_FONT_SELECTION(data);
-
- /* This function strangely gets called when the window is destroyed,
- so we check here to see if the notebook is visible. */
- if (!GTK_WIDGET_VISIBLE(w))
- return;
-
- if (page_num == 0)
- gtk_font_selection_update_filter(fontsel);
- else if (page_num == 1)
- gtk_font_selection_show_font_info(fontsel);
-}
-
-
-static void
-gtk_font_selection_show_font_info (GtkFontSelection *fontsel)
-{
-#ifdef GDK_WINDOWING_X11
- Atom font_atom, atom;
- Bool status;
-#endif
- char *name;
- gchar *fontname;
- gchar field_buffer[XLFD_MAX_FIELD_LEN];
- gchar *field;
- gint i;
- gboolean shown_actual_fields = FALSE;
-
- fontname = gtk_font_selection_get_font_name(fontsel);
- gtk_entry_set_text(GTK_ENTRY(fontsel->requested_font_name),
- fontname ? fontname : "");
-
- gtk_clist_freeze (GTK_CLIST(fontsel->info_clist));
- for (i = 0; i < GTK_XLFD_NUM_FIELDS; i++)
- {
- if (fontname)
- field = gtk_font_selection_get_xlfd_field (fontname, i, field_buffer);
- else
- field = NULL;
- if (field)
- {
- 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, 1,
- field ? field : "");
- }
-#ifdef GDK_WINDOWING_X11
- if (fontsel->font)
- {
- font_atom = XInternAtom(GDK_DISPLAY(), "FONT", True);
- if (font_atom != None)
- {
- status = XGetFontProperty(GDK_FONT_XFONT(fontsel->font), font_atom,
- &atom);
- if (status == True)
- {
- 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);
- }
- }
- }
-#elif defined (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), "");
- for (i = 0; i < GTK_XLFD_NUM_FIELDS; i++)
- {
- gtk_clist_set_text(GTK_CLIST(fontsel->info_clist), i, 2,
- fontname ? _("(unknown)") : "");
- }
- }
- gtk_clist_thaw (GTK_CLIST(fontsel->info_clist));
- g_free(fontname);
-}
-
-
-static gchar*
-gtk_font_selection_expand_slant_code(gchar *slant)
-{
- if (!g_strcasecmp(slant, "r")) return(_("roman"));
- else if (!g_strcasecmp(slant, "i")) return(_("italic"));
- else if (!g_strcasecmp(slant, "o")) return(_("oblique"));
- else if (!g_strcasecmp(slant, "ri")) return(_("reverse italic"));
- else if (!g_strcasecmp(slant, "ro")) return(_("reverse oblique"));
- else if (!g_strcasecmp(slant, "ot")) return(_("other"));
- return slant;
-}