gimpfontmenu.c 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimpfontmenu.c
 * Copyright (C) 2003  Sven Neumann  <sven@gimp.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include "gimp.h"
#include "gimpui.h"

28 29
#include "libgimp-intl.h"

30

31
#define FONT_SELECT_DATA_KEY  "gimp-font-selct-data"
32

33 34 35 36

typedef struct _FontSelect FontSelect;

struct _FontSelect
37
{
38 39
  gchar               *title;
  GimpRunFontCallback  callback;
40 41
  gpointer             data;

42 43
  GtkWidget           *button;
  GtkWidget           *label;
44

45
  gchar               *font_name;      /* Local copy */
46

47 48
  const gchar         *temp_font_callback;
};
49 50


51
/*  local function prototypes  */
52

53 54 55 56 57 58 59
static void   gimp_font_select_widget_callback (const gchar *name,
                                                gboolean     closing,
                                                gpointer     data);
static void   gimp_font_select_widget_clicked  (GtkWidget   *widget,
                                                FontSelect  *font_sel);
static void   gimp_font_select_widget_destroy  (GtkWidget   *widget,
                                                FontSelect  *font_sel);
60

61 62

/**
63 64 65
 * gimp_font_select_widget_new:
 * @title:     Title of the dialog to use or %NULL means to use the default
 *             title.
66
 * @font_name: Initial font name.
67 68
 * @callback:  A function to call when the selected font changes.
 * @data:      A pointer to arbitary data to be used in the call to @callback.
69
 *
70 71
 * Creates a new #GtkWidget that completely controls the selection of
 * a font.  This widget is suitable for placement in a table in a
72 73
 * plug-in dialog.
 *
74
 * Returns: A #GtkWidget that you can use in your UI.
75
 */
76
GtkWidget *
77
gimp_font_select_widget_new (const gchar         *title,
78
                             const gchar         *font_name,
79 80
                             GimpRunFontCallback  callback,
                             gpointer             data)
81
{
82 83 84
  FontSelect *font_sel;
  GtkWidget  *hbox;
  GtkWidget  *image;
85

86
  g_return_val_if_fail (callback != NULL, NULL);
87

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  if (! title)
    title = _("Font Selection");

  font_sel = g_new0 (FontSelect, 1);

  font_sel->title    = g_strdup (title);
  font_sel->callback = callback;
  font_sel->data     = data;

  font_sel->font_name = g_strdup (font_name);

  font_sel->button = gtk_button_new ();

  g_signal_connect (font_sel->button, "clicked",
                    G_CALLBACK (gimp_font_select_widget_clicked),
                    font_sel);
  g_signal_connect (font_sel->button, "destroy",
                    G_CALLBACK (gimp_font_select_widget_destroy),
                    font_sel);
107 108

  hbox = gtk_hbox_new (FALSE, 4);
109
  gtk_container_add (GTK_CONTAINER (font_sel->button), hbox);
110
  gtk_widget_show (hbox);
111 112 113 114

  font_sel->label = gtk_label_new (font_name);
  gtk_box_pack_start (GTK_BOX (hbox), font_sel->label, TRUE, TRUE, 4);
  gtk_widget_show (font_sel->label);
115 116 117 118 119 120

  image = gtk_image_new_from_stock (GTK_STOCK_SELECT_FONT,
                                    GTK_ICON_SIZE_BUTTON);
  gtk_box_pack_end (GTK_BOX (hbox), image, FALSE, FALSE, 4);
  gtk_widget_show (image);

121 122
  g_object_set_data (G_OBJECT (font_sel->button),
                     FONT_SELECT_DATA_KEY, font_sel);
123

124
  return font_sel->button;
125 126 127
}

/**
128
 * gimp_font_select_widget_close:
129 130 131 132 133
 * @widget: A font select widget.
 *
 * Closes the popup window associated with @widget.
 */
void
134
gimp_font_select_widget_close (GtkWidget *widget)
135
{
136
  FontSelect *font_sel;
137

138
  font_sel = g_object_get_data (G_OBJECT (widget), FONT_SELECT_DATA_KEY);
139

140 141 142
  g_return_if_fail (font_sel != NULL);

  if (font_sel->temp_font_callback)
143
    {
144 145
      gimp_font_select_destroy (font_sel->temp_font_callback);
      font_sel->temp_font_callback = NULL;
146 147 148 149
    }
}

/**
150 151
 * gimp_font_select_widget_set;
 * @widget:    A font select widget.
152
 * @font_name: Font name to set; %NULL means no change.
153
 *
154 155 156
 * Sets the current font for the font select widget.  Calls the
 * callback function if one was supplied in the call to
 * gimp_font_select_widget_new().
157 158
 */
void
159 160
gimp_font_select_widget_set (GtkWidget   *widget,
                             const gchar *font_name)
161
{
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  FontSelect *font_sel;

  font_sel = g_object_get_data (G_OBJECT (widget), FONT_SELECT_DATA_KEY);

  g_return_if_fail (font_sel != NULL);

  if (font_sel->temp_font_callback)
    gimp_fonts_set_popup (font_sel->temp_font_callback, font_name);
  else
    gimp_font_select_widget_callback (font_name, FALSE, font_sel);
}


/*  private functions  */

static void
gimp_font_select_widget_callback (const gchar *name,
                                  gboolean     closing,
                                  gpointer     data)
{
  FontSelect *font_sel = (FontSelect *) data;

  g_free (font_sel->font_name);
  font_sel->font_name = g_strdup (name);

  gtk_label_set_text (GTK_LABEL (font_sel->label), name);

  if (font_sel->callback)
    font_sel->callback (name, closing, font_sel->data);
191

192 193 194 195 196 197 198 199 200
  if (closing)
    font_sel->temp_font_callback = NULL;
}

static void
gimp_font_select_widget_clicked (GtkWidget  *widget,
                                 FontSelect *font_sel)
{
  if (font_sel->temp_font_callback)
201
    {
202
      /*  calling gimp_fonts_set_popup() raises the dialog  */
203
      gimp_fonts_set_popup (font_sel->temp_font_callback, font_sel->font_name);
204 205 206 207 208 209 210 211
    }
  else
    {
      font_sel->temp_font_callback =
        gimp_font_select_new (font_sel->title,
                              font_sel->font_name,
                              gimp_font_select_widget_callback,
                              font_sel);
212 213 214
    }
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228
static void
gimp_font_select_widget_destroy (GtkWidget  *widget,
                                 FontSelect *font_sel)
{
  if (font_sel->temp_font_callback)
    {
      gimp_font_select_destroy (font_sel->temp_font_callback);
      font_sel->temp_font_callback = NULL;
    }

  g_free (font_sel->title);
  g_free (font_sel->font_name);
  g_free (font_sel);
}