gtkfontsel.c 34.9 KB
Newer Older
Owen Taylor's avatar
Owen Taylor committed
1 2 3
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
4
 * Massively updated for Pango by Owen Taylor, May 2000
Owen Taylor's avatar
Owen Taylor committed
5 6 7 8 9
 * GtkFontSelection widget for Gtk+, by Damon Chaplin, May 1998.
 * Based on the GnomeFontSelector widget, by Elliot Lee, but major changes.
 * The GnomeFontSelector was derived from app/text_tool.c in the GIMP.
 *
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
Owen Taylor's avatar
Owen Taylor committed
11 12 13 14 15 16
 * 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
17
 * Lesser General Public License for more details.
Owen Taylor's avatar
Owen Taylor committed
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
Owen Taylor's avatar
Owen Taylor committed
20 21 22 23 24
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

25
/*
26
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
27 28 29 30 31
 * 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/. 
 */

Owen Taylor's avatar
Owen Taylor committed
32 33 34 35 36
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

37
#include "gdk/gdk.h"
Owen Taylor's avatar
Owen Taylor committed
38 39
#include "gdk/gdkkeysyms.h"

40 41
#include "gtkfontsel.h"

Owen Taylor's avatar
Owen Taylor committed
42 43 44 45 46 47 48
#include "gtkbutton.h"
#include "gtkclist.h"
#include "gtkentry.h"
#include "gtkframe.h"
#include "gtkhbbox.h"
#include "gtkhbox.h"
#include "gtklabel.h"
49
#include "gtkrc.h"
Owen Taylor's avatar
Owen Taylor committed
50
#include "gtksignal.h"
51
#include "gtkstock.h"
Owen Taylor's avatar
Owen Taylor committed
52 53
#include "gtktable.h"
#include "gtkvbox.h"
54
#include "gtkscrolledwindow.h"
55
#include "gtkintl.h"
Owen Taylor's avatar
Owen Taylor committed
56 57 58 59 60 61 62 63 64 65 66 67

/* This is the default text shown in the preview entry, though the user
   can set it. Remember that some fonts only have capital letters. */
#define PREVIEW_TEXT "abcdefghijk ABCDEFGHIJK"

/* This is the initial and maximum height of the preview entry (it expands
   when large font sizes are selected). Initial height is also the minimum. */
#define INITIAL_PREVIEW_HEIGHT 44
#define MAX_PREVIEW_HEIGHT 300

/* These are the sizes of the font, style & size clists. */
#define FONT_LIST_HEIGHT	136
Damon Chaplin's avatar
""  
Damon Chaplin committed
68 69
#define FONT_LIST_WIDTH		190
#define FONT_STYLE_LIST_WIDTH	170
Owen Taylor's avatar
Owen Taylor committed
70 71
#define FONT_SIZE_LIST_WIDTH	60

72 73 74 75 76
/* These are what we use as the standard font sizes, for the size clist.
 */
static const guint16 font_sizes[] = {
  8, 9, 10, 11, 12, 13, 14, 16, 18, 20, 22, 24, 26, 28,
  32, 36, 40, 48, 56, 64, 72
Owen Taylor's avatar
Owen Taylor committed
77 78
};

79 80 81 82 83 84 85
enum {
   PROP_0,
   PROP_FONT_NAME,
   PROP_FONT,
   PROP_PREVIEW_TEXT
};

Owen Taylor's avatar
Owen Taylor committed
86
static void    gtk_font_selection_class_init	     (GtkFontSelectionClass *klass);
87 88 89 90 91 92 93 94
static void    gtk_font_selection_set_property       (GObject         *object,
						      guint            prop_id,
						      const GValue    *value,
						      GParamSpec      *pspec);
static void    gtk_font_selection_get_property       (GObject         *object,
						      guint            prop_id,
						      GValue          *value,
						      GParamSpec      *pspec);
95 96
static void    gtk_font_selection_init		     (GtkFontSelection      *fontsel);
static void    gtk_font_selection_finalize	     (GObject               *object);
Owen Taylor's avatar
Owen Taylor committed
97 98

/* These are the callbacks & related functions. */
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
static void     gtk_font_selection_select_font           (GtkWidget        *w,
							  gint              row,
							  gint              column,
							  GdkEventButton   *bevent,
							  gpointer          data);
static void     gtk_font_selection_show_available_fonts  (GtkFontSelection *fs);

static void     gtk_font_selection_show_available_styles (GtkFontSelection *fs);
static void     gtk_font_selection_select_best_style     (GtkFontSelection *fs,
							  gboolean          use_first);
static void     gtk_font_selection_select_style          (GtkWidget        *w,
							  gint              row,
							  gint              column,
							  GdkEventButton   *bevent,
							  gpointer          data);

static void     gtk_font_selection_select_best_size      (GtkFontSelection *fs);
static void     gtk_font_selection_show_available_sizes  (GtkFontSelection *fs);
117
static void     gtk_font_selection_size_activate         (GtkWidget        *w,
118 119 120 121 122 123 124 125 126 127 128
							  gpointer          data);
static void     gtk_font_selection_select_size           (GtkWidget        *w,
							  gint              row,
							  gint              column,
							  GdkEventButton   *bevent,
							  gpointer          data);

static void     gtk_font_selection_expose_list           (GtkWidget        *w,
							  GdkEventExpose   *event,
							  gpointer          data);

129 130
static void     gtk_font_selection_preview_changed       (GtkWidget        *entry,
							  GtkFontSelection *fontsel);
Owen Taylor's avatar
Owen Taylor committed
131 132

/* Misc. utility functions. */
133
static void     gtk_font_selection_load_font         (GtkFontSelection *fs);
Owen Taylor's avatar
Owen Taylor committed
134 135 136 137 138 139 140 141 142 143 144
static void    gtk_font_selection_update_preview     (GtkFontSelection *fs);

/* FontSelectionDialog */
static void    gtk_font_selection_dialog_class_init  (GtkFontSelectionDialogClass *klass);
static void    gtk_font_selection_dialog_init	     (GtkFontSelectionDialog *fontseldiag);

static gint    gtk_font_selection_dialog_on_configure(GtkWidget      *widget,
						      GdkEventConfigure *event,
						      GtkFontSelectionDialog *fsd);

static GtkWindowClass *font_selection_parent_class = NULL;
145
static GtkVBoxClass *font_selection_dialog_parent_class = NULL;
Owen Taylor's avatar
Owen Taylor committed
146

Tim Janik's avatar
Tim Janik committed
147
GtkType
Owen Taylor's avatar
Owen Taylor committed
148 149
gtk_font_selection_get_type()
{
Tim Janik's avatar
Tim Janik committed
150
  static GtkType font_selection_type = 0;
151
  
Owen Taylor's avatar
Owen Taylor committed
152 153
  if(!font_selection_type)
    {
154
      static const GtkTypeInfo fontsel_type_info =
Owen Taylor's avatar
Owen Taylor committed
155 156
      {
	"GtkFontSelection",
Tim Janik's avatar
Tim Janik committed
157 158
	sizeof (GtkFontSelection),
	sizeof (GtkFontSelectionClass),
Owen Taylor's avatar
Owen Taylor committed
159 160
	(GtkClassInitFunc) gtk_font_selection_class_init,
	(GtkObjectInitFunc) gtk_font_selection_init,
161 162
	/* reserved_1 */ NULL,
	/* reserved_2 */ NULL,
163
        (GtkClassInitFunc) NULL,
Owen Taylor's avatar
Owen Taylor committed
164
      };
165
      
166
      font_selection_type = gtk_type_unique (GTK_TYPE_VBOX,
Tim Janik's avatar
Tim Janik committed
167
					     &fontsel_type_info);
Owen Taylor's avatar
Owen Taylor committed
168
    }
169
  
Owen Taylor's avatar
Owen Taylor committed
170 171 172 173 174 175
  return font_selection_type;
}

static void
gtk_font_selection_class_init(GtkFontSelectionClass *klass)
{
176
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
177
  
178
  font_selection_parent_class = gtk_type_class (GTK_TYPE_VBOX);
179
  
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  gobject_class->set_property = gtk_font_selection_set_property;
  gobject_class->get_property = gtk_font_selection_get_property;
   
  g_object_class_install_property (gobject_class,
                                   PROP_FONT_NAME,
                                   g_param_spec_string ("font_name",
                                                        _("Font name"),
                                                        _("The X string that represents this font."),
                                                        NULL,
                                                        G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
				   PROP_FONT,
				   g_param_spec_boxed ("font",
						       _("Font"),
						       _("The GdkFont that is currently selected."),
						       GDK_TYPE_FONT,
						       G_PARAM_READABLE));
  g_object_class_install_property (gobject_class,
                                   PROP_PREVIEW_TEXT,
                                   g_param_spec_string ("preview_text",
                                                        _("Preview text"),
                                                        _("The text to display in order to demonstrate the selected font."),
                                                        PREVIEW_TEXT,
                                                        G_PARAM_READWRITE));
204
  gobject_class->finalize = gtk_font_selection_finalize;
Owen Taylor's avatar
Owen Taylor committed
205 206
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
static void 
gtk_font_selection_set_property (GObject         *object,
				 guint            prop_id,
				 const GValue    *value,
				 GParamSpec      *pspec)
{
  GtkFontSelection *fontsel;

  fontsel = GTK_FONT_SELECTION (object);

  switch (prop_id)
    {
    case PROP_FONT_NAME:
      gtk_font_selection_set_font_name (fontsel, g_value_get_string (value));
      break;
    case PROP_PREVIEW_TEXT:
      gtk_font_selection_set_preview_text (fontsel, g_value_get_string (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void gtk_font_selection_get_property (GObject         *object,
					     guint            prop_id,
					     GValue          *value,
					     GParamSpec      *pspec)
{
  GtkFontSelection *fontsel;

  fontsel = GTK_FONT_SELECTION (object);

  switch (prop_id)
    {
    case PROP_FONT_NAME:
      g_value_set_string (value, gtk_font_selection_get_font_name (fontsel));
      break;
    case PROP_FONT:
      g_value_set_object (value, G_OBJECT (gtk_font_selection_get_font (fontsel)));
      break;
    case PROP_PREVIEW_TEXT:
      g_value_set_string (value, gtk_font_selection_get_preview_text (fontsel));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}


Owen Taylor's avatar
Owen Taylor committed
258 259 260
static void
gtk_font_selection_init(GtkFontSelection *fontsel)
{
261
  GtkWidget *scrolled_win;
Owen Taylor's avatar
Owen Taylor committed
262
  GtkWidget *text_frame;
263 264
  GtkWidget *text_box;
  GtkWidget *table, *label;
265

266
  fontsel->font_desc = pango_font_description_from_string ("sans 12");
267
  
Owen Taylor's avatar
Owen Taylor committed
268 269 270 271
  /* Create the table of font, style & size. */
  table = gtk_table_new (3, 3, FALSE);
  gtk_widget_show (table);
  gtk_table_set_col_spacings(GTK_TABLE(table), 8);
272
  gtk_box_pack_start (GTK_BOX (fontsel), table, TRUE, TRUE, 0);
273

274
  fontsel->font_entry = gtk_entry_new ();
Owen Taylor's avatar
Owen Taylor committed
275 276 277 278 279
  gtk_entry_set_editable(GTK_ENTRY(fontsel->font_entry), FALSE);
  gtk_widget_set_usize (fontsel->font_entry, 20, -1);
  gtk_widget_show (fontsel->font_entry);
  gtk_table_attach (GTK_TABLE (table), fontsel->font_entry, 0, 1, 1, 2,
		    GTK_FILL, 0, 0, 0);
280 281 282
  
  fontsel->font_style_entry = gtk_entry_new ();
  gtk_entry_set_editable (GTK_ENTRY(fontsel->font_style_entry), FALSE);
Owen Taylor's avatar
Owen Taylor committed
283 284 285 286
  gtk_widget_set_usize (fontsel->font_style_entry, 20, -1);
  gtk_widget_show (fontsel->font_style_entry);
  gtk_table_attach (GTK_TABLE (table), fontsel->font_style_entry, 1, 2, 1, 2,
		    GTK_FILL, 0, 0, 0);
287 288
  
  fontsel->size_entry = gtk_entry_new ();
Owen Taylor's avatar
Owen Taylor committed
289 290 291 292
  gtk_widget_set_usize (fontsel->size_entry, 20, -1);
  gtk_widget_show (fontsel->size_entry);
  gtk_table_attach (GTK_TABLE (table), fontsel->size_entry, 2, 3, 1, 2,
		    GTK_FILL, 0, 0, 0);
293 294
  gtk_signal_connect (GTK_OBJECT (fontsel->size_entry), "activate",
		      (GtkSignalFunc) gtk_font_selection_size_activate,
Owen Taylor's avatar
Owen Taylor committed
295
		      fontsel);
296
  
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
  fontsel->font_label = gtk_label_new_with_mnemonic (_("_Family:"));
  gtk_label_set_mnemonic_widget (GTK_LABEL (fontsel->font_label),
                                 fontsel->font_entry);
  gtk_misc_set_alignment (GTK_MISC (fontsel->font_label), 0.0, 0.5);
  gtk_widget_show (fontsel->font_label);
  gtk_table_attach (GTK_TABLE (table), fontsel->font_label, 0, 1, 0, 1,
		    GTK_FILL, 0, 0, 0);  
  label = gtk_label_new_with_mnemonic (_("_Style:"));
  gtk_label_set_mnemonic_widget (GTK_LABEL (label),
                                 fontsel->font_style_entry);
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_widget_show (label);
  gtk_table_attach (GTK_TABLE (table), label, 1, 2, 0, 1,
		    GTK_FILL, 0, 0, 0);
  label = gtk_label_new_with_mnemonic (_("Si_ze:"));
  gtk_label_set_mnemonic_widget (GTK_LABEL (label),
                                 fontsel->size_entry);
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_widget_show (label);
  gtk_table_attach (GTK_TABLE (table), label, 2, 3, 0, 1,
		    GTK_FILL, 0, 0, 0);
  
  
Owen Taylor's avatar
Owen Taylor committed
320
  /* Create the clists  */
321 322
  fontsel->font_clist = gtk_clist_new (1);
  gtk_clist_column_titles_hide (GTK_CLIST (fontsel->font_clist));
Damon Chaplin's avatar
""  
Damon Chaplin committed
323
  gtk_clist_set_column_auto_resize (GTK_CLIST (fontsel->font_clist), 0, TRUE);
324
  scrolled_win = gtk_scrolled_window_new (NULL, NULL);
Damon Chaplin's avatar
""  
Damon Chaplin committed
325
  gtk_widget_set_usize (scrolled_win, FONT_LIST_WIDTH, FONT_LIST_HEIGHT);
326 327 328
  gtk_container_add (GTK_CONTAINER (scrolled_win), fontsel->font_clist);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
				  GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
Owen Taylor's avatar
Owen Taylor committed
329
  gtk_widget_show(fontsel->font_clist);
330 331 332
  gtk_widget_show(scrolled_win);

  gtk_table_attach (GTK_TABLE (table), scrolled_win, 0, 1, 2, 3,
Owen Taylor's avatar
Owen Taylor committed
333 334
		    GTK_EXPAND | GTK_FILL,
		    GTK_EXPAND | GTK_FILL, 0, 0);
335
  
336 337
  fontsel->font_style_clist = gtk_clist_new (1);
  gtk_clist_column_titles_hide (GTK_CLIST (fontsel->font_style_clist));
Damon Chaplin's avatar
""  
Damon Chaplin committed
338 339
  gtk_clist_set_column_auto_resize (GTK_CLIST (fontsel->font_style_clist),
				    0, TRUE);
340
  scrolled_win = gtk_scrolled_window_new (NULL, NULL);
Damon Chaplin's avatar
""  
Damon Chaplin committed
341
  gtk_widget_set_usize (scrolled_win, FONT_STYLE_LIST_WIDTH, -1);
342 343 344
  gtk_container_add (GTK_CONTAINER (scrolled_win), fontsel->font_style_clist);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
				  GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
Owen Taylor's avatar
Owen Taylor committed
345
  gtk_widget_show(fontsel->font_style_clist);
346 347
  gtk_widget_show(scrolled_win);
  gtk_table_attach (GTK_TABLE (table), scrolled_win, 1, 2, 2, 3,
Owen Taylor's avatar
Owen Taylor committed
348 349
		    GTK_EXPAND | GTK_FILL,
		    GTK_EXPAND | GTK_FILL, 0, 0);
350
  
351
  fontsel->size_clist = gtk_clist_new (1);
Owen Taylor's avatar
Owen Taylor committed
352
  gtk_clist_column_titles_hide (GTK_CLIST(fontsel->size_clist));
Damon Chaplin's avatar
""  
Damon Chaplin committed
353
  gtk_clist_set_column_width (GTK_CLIST(fontsel->size_clist), 0, 20);
354
  scrolled_win = gtk_scrolled_window_new (NULL, NULL);
Damon Chaplin's avatar
""  
Damon Chaplin committed
355
  gtk_widget_set_usize (scrolled_win, FONT_SIZE_LIST_WIDTH, -1);
356 357 358
  gtk_container_add (GTK_CONTAINER (scrolled_win), fontsel->size_clist);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
				  GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
359 360
  gtk_widget_show (fontsel->size_clist);
  gtk_widget_show (scrolled_win);
361
  gtk_table_attach (GTK_TABLE (table), scrolled_win, 2, 3, 2, 3,
Owen Taylor's avatar
Owen Taylor committed
362
		    GTK_FILL, GTK_FILL, 0, 0);
363
  
Owen Taylor's avatar
Owen Taylor committed
364 365
  /* Insert the fonts. If there exist fonts with the same family but
     different foundries, then the foundry name is appended in brackets. */
366
  gtk_font_selection_show_available_fonts(fontsel);
367
  
Owen Taylor's avatar
Owen Taylor committed
368 369 370 371
  gtk_signal_connect (GTK_OBJECT (fontsel->font_clist), "select_row",
		      GTK_SIGNAL_FUNC(gtk_font_selection_select_font),
		      fontsel);
  GTK_WIDGET_SET_FLAGS (fontsel->font_clist, GTK_CAN_FOCUS);
372 373 374 375

  gtk_signal_connect_after (GTK_OBJECT (fontsel->font_clist), "expose_event",
			    GTK_SIGNAL_FUNC(gtk_font_selection_expose_list),
			    fontsel);
376
  
377 378
  gtk_font_selection_show_available_styles (fontsel);
  
Owen Taylor's avatar
Owen Taylor committed
379 380 381 382
  gtk_signal_connect (GTK_OBJECT (fontsel->font_style_clist), "select_row",
		      GTK_SIGNAL_FUNC(gtk_font_selection_select_style),
		      fontsel);
  GTK_WIDGET_SET_FLAGS (fontsel->font_style_clist, GTK_CAN_FOCUS);
383 384

  gtk_font_selection_show_available_sizes (fontsel);
385
  
Owen Taylor's avatar
Owen Taylor committed
386 387 388 389
  gtk_signal_connect (GTK_OBJECT (fontsel->size_clist), "select_row",
		      GTK_SIGNAL_FUNC(gtk_font_selection_select_size),
		      fontsel);
  GTK_WIDGET_SET_FLAGS (fontsel->size_clist, GTK_CAN_FOCUS);
390
  
Owen Taylor's avatar
Owen Taylor committed
391
  /* create the text entry widget */
392
  text_frame = gtk_frame_new (_("Preview:"));
Owen Taylor's avatar
Owen Taylor committed
393 394
  gtk_widget_show (text_frame);
  gtk_frame_set_shadow_type(GTK_FRAME(text_frame), GTK_SHADOW_ETCHED_IN);
395
  gtk_box_pack_start (GTK_BOX (fontsel), text_frame,
Owen Taylor's avatar
Owen Taylor committed
396
		      FALSE, TRUE, 0);
397
  
Owen Taylor's avatar
Owen Taylor committed
398 399 400 401
  /* This is just used to get a 4-pixel space around the preview entry. */
  text_box = gtk_hbox_new (FALSE, 0);
  gtk_widget_show (text_box);
  gtk_container_add (GTK_CONTAINER (text_frame), text_box);
402
  gtk_container_set_border_width (GTK_CONTAINER (text_box), 4);
403
  
Owen Taylor's avatar
Owen Taylor committed
404 405
  fontsel->preview_entry = gtk_entry_new ();
  gtk_widget_show (fontsel->preview_entry);
406 407 408
  gtk_signal_connect (GTK_OBJECT (fontsel->preview_entry), "changed",
		      (GtkSignalFunc) gtk_font_selection_preview_changed,
		      fontsel);
Owen Taylor's avatar
Owen Taylor committed
409 410 411
  gtk_widget_set_usize (fontsel->preview_entry, -1, INITIAL_PREVIEW_HEIGHT);
  gtk_box_pack_start (GTK_BOX (text_box), fontsel->preview_entry,
		      TRUE, TRUE, 0);
412

413
  gtk_font_selection_update_preview (fontsel);
Owen Taylor's avatar
Owen Taylor committed
414 415 416 417 418 419
}

GtkWidget *
gtk_font_selection_new()
{
  GtkFontSelection *fontsel;
420
  
Tim Janik's avatar
Tim Janik committed
421
  fontsel = gtk_type_new (GTK_TYPE_FONT_SELECTION);
422
  
Owen Taylor's avatar
Owen Taylor committed
423 424 425 426
  return GTK_WIDGET (fontsel);
}

static void
427
gtk_font_selection_finalize (GObject *object)
Owen Taylor's avatar
Owen Taylor committed
428 429
{
  GtkFontSelection *fontsel;
430
  
Owen Taylor's avatar
Owen Taylor committed
431 432
  g_return_if_fail (object != NULL);
  g_return_if_fail (GTK_IS_FONT_SELECTION (object));
433
  
Owen Taylor's avatar
Owen Taylor committed
434
  fontsel = GTK_FONT_SELECTION (object);
435 436 437

  pango_font_description_free (fontsel->font_desc);

Owen Taylor's avatar
Owen Taylor committed
438 439
  if (fontsel->font)
    gdk_font_unref (fontsel->font);
440
  
441 442
  if (G_OBJECT_CLASS (font_selection_parent_class)->finalize)
    (* G_OBJECT_CLASS (font_selection_parent_class)->finalize) (object);
Owen Taylor's avatar
Owen Taylor committed
443 444
}

445 446 447 448 449 450
static void
gtk_font_selection_preview_changed (GtkWidget        *entry,
				    GtkFontSelection *fontsel)
{
  g_object_notify (G_OBJECT (fontsel), "preview_text");
}
Owen Taylor's avatar
Owen Taylor committed
451 452 453 454 455 456 457 458 459 460 461

/* This is called when the clist is exposed. Here we scroll to the current
   font if necessary. */
static void
gtk_font_selection_expose_list (GtkWidget		*widget,
				GdkEventExpose		*event,
				gpointer		 data)
{
  GtkFontSelection *fontsel;
  GList *selection;
  gint index;
462
  
Owen Taylor's avatar
Owen Taylor committed
463
#ifdef FONTSEL_DEBUG
464
  g_message("In expose_list\n");
Owen Taylor's avatar
Owen Taylor committed
465 466
#endif
  fontsel = GTK_FONT_SELECTION(data);
467
  
468 469 470 471 472 473 474 475 476
  /* Try to scroll the font family clist to the selected item */
  selection = GTK_CLIST(fontsel->font_clist)->selection;
  if (selection)
    {
      index = GPOINTER_TO_INT (selection->data);
      if (gtk_clist_row_is_visible(GTK_CLIST(fontsel->font_clist), index)
	  != GTK_VISIBILITY_FULL)
	gtk_clist_moveto(GTK_CLIST(fontsel->font_clist), index, -1, 0.5, 0);
    }
477
      
478 479 480 481 482 483 484 485 486 487
  /* Try to scroll the font style clist to the selected item */
  selection = GTK_CLIST(fontsel->font_style_clist)->selection;
  if (selection)
    {
      index = GPOINTER_TO_INT (selection->data);
      if (gtk_clist_row_is_visible(GTK_CLIST(fontsel->font_style_clist), index)
	  != GTK_VISIBILITY_FULL)
	gtk_clist_moveto(GTK_CLIST(fontsel->font_style_clist), index, -1,
			 0.5, 0);
    }
488
      
489 490 491 492 493 494 495 496
  /* Try to scroll the font size clist to the selected item */
  selection = GTK_CLIST(fontsel->size_clist)->selection;
  if (selection)
    {
      index = GPOINTER_TO_INT (selection->data);
      if (gtk_clist_row_is_visible(GTK_CLIST(fontsel->size_clist), index)
	  != GTK_VISIBILITY_FULL)
      gtk_clist_moveto(GTK_CLIST(fontsel->size_clist), index, -1, 0.5, 0);
Owen Taylor's avatar
Owen Taylor committed
497 498 499
    }
}

500
/* This is called when a family is selected in the list. */
Damon Chaplin's avatar
""  
Damon Chaplin committed
501
static void
502 503 504 505 506
gtk_font_selection_select_font (GtkWidget      *w,
				gint	        row,
				gint	        column,
				GdkEventButton *bevent,
				gpointer        data)
Damon Chaplin's avatar
""  
Damon Chaplin committed
507 508
{
  GtkFontSelection *fontsel;
509 510 511
  gchar *family_name;
  gint index;
  
Damon Chaplin's avatar
""  
Damon Chaplin committed
512 513
  fontsel = GTK_FONT_SELECTION (data);

514
  if (GTK_CLIST (fontsel->font_clist)->selection)
Damon Chaplin's avatar
""  
Damon Chaplin committed
515
    {
516 517 518 519
      index = GPOINTER_TO_INT (GTK_CLIST (fontsel->font_clist)->selection->data);

      if (gtk_clist_get_text (GTK_CLIST (fontsel->font_clist), index, 0, &family_name) &&
	  strcasecmp (fontsel->font_desc->family_name, family_name) != 0)
Damon Chaplin's avatar
""  
Damon Chaplin committed
520
	{
521 522
	  g_free (fontsel->font_desc->family_name);
	  fontsel->font_desc->family_name  = g_strdup (family_name);
523

Havoc Pennington's avatar
Havoc Pennington committed
524
	  gtk_entry_set_text (GTK_ENTRY (fontsel->font_entry), family_name);
525 526 527
	  
	  gtk_font_selection_show_available_styles (fontsel);
	  gtk_font_selection_select_best_style (fontsel, TRUE);
Damon Chaplin's avatar
""  
Damon Chaplin committed
528 529 530 531
	}
    }
}

532 533 534 535 536
static int
cmp_strings (const void *a, const void *b)
{
  return strcasecmp (*(const char **)a, *(const char **)b);
}
Damon Chaplin's avatar
""  
Damon Chaplin committed
537

Owen Taylor's avatar
Owen Taylor committed
538
static void
539
gtk_font_selection_show_available_fonts (GtkFontSelection *fontsel)
Owen Taylor's avatar
Owen Taylor committed
540
{
541 542 543
  gchar **families;
  int n_families, i;

544 545
  pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)),
			       &families, &n_families);
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
  qsort (families, n_families, sizeof(char *), cmp_strings);

  gtk_clist_freeze (GTK_CLIST (fontsel->font_clist));
  gtk_clist_clear (GTK_CLIST (fontsel->font_clist));

  for (i=0; i<n_families; i++)
    {
      gtk_clist_append (GTK_CLIST (fontsel->font_clist), &families[i]);

      if (!strcasecmp (families[i], fontsel->font_desc->family_name))
	{
	  gtk_clist_select_row (GTK_CLIST(fontsel->font_clist), i, 0);
	  gtk_entry_set_text(GTK_ENTRY(fontsel->font_entry), families[i]);
	}
    }
561
  
562
  gtk_clist_thaw (GTK_CLIST(fontsel->font_clist));
Owen Taylor's avatar
Owen Taylor committed
563

564 565
  pango_font_map_free_families (families, n_families);
}
Owen Taylor's avatar
Owen Taylor committed
566

567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
static int
compare_font_descriptions (const PangoFontDescription *a, const PangoFontDescription *b)
{
  int val = strcasecmp (a->family_name, b->family_name);
  if (val != 0)
    return val;

  if (a->weight != b->weight)
    return a->weight - b->weight;

  if (a->style != b->style)
    return a->style - b->style;
  
  if (a->stretch != b->stretch)
    return a->stretch - b->stretch;

  if (a->variant != b->variant)
    return a->variant - b->variant;

  return 0;
}

static int
font_description_sort_func (const void *a, const void *b)
{
  return compare_font_descriptions (*(PangoFontDescription **)a, *(PangoFontDescription **)b);
}
Owen Taylor's avatar
Owen Taylor committed
594 595 596 597 598 599

/* 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)
{
600 601
  PangoFontDescription **descs;
  int n_descs, i;
602
  gint match_row = -1;
603
  gchar *str;
604
  
605 606
  pango_context_list_fonts (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)),
			    fontsel->font_desc->family_name, &descs, &n_descs);
607 608 609 610 611 612
  qsort (descs, n_descs, sizeof(PangoFontDescription *), font_description_sort_func);

  gtk_clist_freeze (GTK_CLIST (fontsel->font_style_clist));
  gtk_clist_clear (GTK_CLIST (fontsel->font_style_clist));

  for (i=0; i<n_descs; i++)
Owen Taylor's avatar
Owen Taylor committed
613
    {
614 615 616 617 618 619 620 621 622 623 624 625 626 627
      PangoFontDescription tmp_desc;

      tmp_desc = *descs[i];
      tmp_desc.family_name = NULL;
      tmp_desc.size = 0;

      str = pango_font_description_to_string (&tmp_desc);
      gtk_clist_append (GTK_CLIST (fontsel->font_style_clist), &str);

      if (descs[i]->weight == fontsel->font_desc->weight &&
	  descs[i]->style == fontsel->font_desc->style &&
	  descs[i]->stretch == fontsel->font_desc->stretch &&
	  descs[i]->variant == fontsel->font_desc->variant)
	match_row = i;
628
      
629
      g_free (str);
Owen Taylor's avatar
Owen Taylor committed
630
    }
631 632

  gtk_clist_select_row (GTK_CLIST (fontsel->font_style_clist), match_row, 0);
633 634 635 636 637
  if (match_row >= 0)
    {
      gtk_clist_get_text (GTK_CLIST (fontsel->font_style_clist), match_row, 0, &str);
      gtk_entry_set_text (GTK_ENTRY (fontsel->font_style_entry), str);
    }
638
  
Owen Taylor's avatar
Owen Taylor committed
639
  gtk_clist_thaw (GTK_CLIST(fontsel->font_style_clist));
640 641

  pango_font_descriptions_free (descs, n_descs);
Owen Taylor's avatar
Owen Taylor committed
642 643 644 645 646 647 648 649 650
}


/* 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
651 652
gtk_font_selection_select_best_style(GtkFontSelection *fontsel,
				     gboolean	       use_first)
Owen Taylor's avatar
Owen Taylor committed
653
{
654
  gint best_row = 0;
655
  
Damon Chaplin's avatar
""  
Damon Chaplin committed
656 657
  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)
658
      != GTK_VISIBILITY_FULL)
Damon Chaplin's avatar
""  
Damon Chaplin committed
659 660
    gtk_clist_moveto(GTK_CLIST(fontsel->font_style_clist), best_row, -1,
		     0.5, 0);
Owen Taylor's avatar
Owen Taylor committed
661 662 663 664 665 666 667 668 669 670 671 672 673
  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)
{
674 675
  GtkFontSelection *fontsel = GTK_FONT_SELECTION (data);
  PangoFontDescription *tmp_desc;
Owen Taylor's avatar
Owen Taylor committed
676
  gchar *text;
677
  gint index;
678
  
679 680 681 682
  if (bevent && !GTK_WIDGET_HAS_FOCUS (w))
    gtk_widget_grab_focus (w);
  
  if (GTK_CLIST (fontsel->font_style_clist)->selection)
683
    {
684
      index = GPOINTER_TO_INT (GTK_CLIST (fontsel->font_style_clist)->selection->data);
685

686 687 688 689 690 691 692 693 694 695 696
      if (gtk_clist_get_text (GTK_CLIST (fontsel->font_style_clist), index, 0, &text))
	{
	  tmp_desc = pango_font_description_from_string (text);
	  
	  fontsel->font_desc->style = tmp_desc->style;
	  fontsel->font_desc->variant = tmp_desc->variant;
	  fontsel->font_desc->weight = tmp_desc->weight;
	  fontsel->font_desc->stretch = tmp_desc->stretch;
	  
	  pango_font_description_free (tmp_desc);
	}
697 698
    }

699 700
  gtk_font_selection_show_available_sizes (fontsel);
  gtk_font_selection_select_best_size (fontsel);
701 702
}

703 704
static void
gtk_font_selection_show_available_sizes (GtkFontSelection *fontsel)
705
{
706 707 708
  gint i;
  gchar buffer[128];
  gchar *size;
709

710 711 712
  /* Insert the standard font sizes */
  gtk_clist_freeze (GTK_CLIST (fontsel->size_clist));
  gtk_clist_clear (GTK_CLIST (fontsel->size_clist));
713

714
  for (i = 0; i < G_N_ELEMENTS (font_sizes); i++)
715
    {
716 717 718 719 720
      sprintf(buffer, "%i", font_sizes[i]);
      size = buffer;
      gtk_clist_append (GTK_CLIST(fontsel->size_clist), &size);
      if (font_sizes[i] * PANGO_SCALE == fontsel->font_desc->size)
	gtk_clist_select_row(GTK_CLIST(fontsel->size_clist), i, 0);
721
    }
722
  gtk_clist_thaw (GTK_CLIST(fontsel->size_clist));
723

724 725
  sprintf (buffer, "%i", fontsel->font_desc->size / PANGO_SCALE);
  gtk_entry_set_text (GTK_ENTRY(fontsel->size_entry), buffer);
726 727
}

Owen Taylor's avatar
Owen Taylor committed
728
static void
729
gtk_font_selection_select_best_size (GtkFontSelection *fontsel)
Owen Taylor's avatar
Owen Taylor committed
730
{
731 732
  gtk_font_selection_load_font (fontsel);  
}
733

734 735
/* If the user hits return in the font size entry, we change to the new font
   size. */
736 737 738
static void
gtk_font_selection_size_activate (GtkWidget   *w,
                                  gpointer     data)
739 740 741 742
{
  GtkFontSelection *fontsel;
  gint new_size;
  gchar *text;
743
  
744
  fontsel = GTK_FONT_SELECTION (data);
745 746 747

  text = gtk_entry_get_text (GTK_ENTRY (fontsel->size_entry));
  new_size = atoi (text) * PANGO_SCALE;
748
  
749
  if (fontsel->font_desc->size != new_size)
Owen Taylor's avatar
Owen Taylor committed
750
    {
751 752
      fontsel->font_desc->size = new_size;
      gtk_font_selection_load_font (fontsel);
Owen Taylor's avatar
Owen Taylor committed
753 754 755
    }
}

756
/* This is called when a size is selected in the list. */
Owen Taylor's avatar
Owen Taylor committed
757
static void
758 759 760 761 762
gtk_font_selection_select_size (GtkWidget      *w,
				gint	        row,
				gint	        column,
				GdkEventButton *bevent,
				gpointer        data)
Owen Taylor's avatar
Owen Taylor committed
763
{
764 765 766 767 768
  GtkFontSelection *fontsel;
  gint new_size;
  gchar *text;
  
  fontsel = GTK_FONT_SELECTION (data);
769
  
770 771
  if (bevent && !GTK_WIDGET_HAS_FOCUS (w))
    gtk_widget_grab_focus (w);
772
  
773 774
  gtk_clist_get_text(GTK_CLIST(fontsel->size_clist), row, 0, &text);
  new_size = atoi (text) * PANGO_SCALE;
775
  
776
  if (fontsel->font_desc->size != new_size)
Owen Taylor's avatar
Owen Taylor committed
777
    {
778 779 780 781
      /* If the size was selected by the user we set the selected_size. */
      fontsel->font_desc->size = new_size;

      gtk_font_selection_load_font (fontsel);
Owen Taylor's avatar
Owen Taylor committed
782 783 784
    }
}

785 786 787 788 789 790 791 792 793
static void
gtk_font_selection_load_font (GtkFontSelection *fontsel)
{
  if (fontsel->font)
    gdk_font_unref (fontsel->font);
  fontsel->font = NULL;

  gtk_font_selection_update_preview (fontsel);
}
Owen Taylor's avatar
Owen Taylor committed
794

795 796 797 798 799 800 801 802
/* 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)
Owen Taylor's avatar
Owen Taylor committed
803
{
804 805 806 807 808 809 810
  GtkRcStyle *rc_style;
  gint new_height;
  GtkRequisition old_requisition;
  GtkWidget *preview_entry = fontsel->preview_entry;
  gchar *text;

  gtk_widget_get_child_requisition (preview_entry, &old_requisition);
811
  
812 813 814 815 816 817 818 819 820
  rc_style = gtk_rc_style_new ();
  rc_style->font_desc = pango_font_description_copy (fontsel->font_desc);
  gtk_widget_modify_style (preview_entry, rc_style);
  gtk_rc_style_unref (rc_style);

  gtk_widget_size_request (preview_entry, NULL);
  
  /* We don't ever want to be over MAX_PREVIEW_HEIGHT pixels high. */
  new_height = CLAMP (preview_entry->requisition.height, INITIAL_PREVIEW_HEIGHT, MAX_PREVIEW_HEIGHT);
Owen Taylor's avatar
Owen Taylor committed
821

822 823 824 825 826 827 828 829 830
  if (new_height > old_requisition.height || new_height < old_requisition.height - 30)
    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(preview_entry));
  if (strlen(text) == 0)
    gtk_entry_set_text(GTK_ENTRY(preview_entry), PREVIEW_TEXT);
  gtk_entry_set_position(GTK_ENTRY(preview_entry), 0);
}
Owen Taylor's avatar
Owen Taylor committed
831 832 833 834 835 836 837 838

/*****************************************************************************
 * These functions are the main public interface for getting/setting the font.
 *****************************************************************************/

GdkFont*
gtk_font_selection_get_font (GtkFontSelection *fontsel)
{
839 840 841
  if (!fontsel->font)
    fontsel->font = gdk_font_from_description (fontsel->font_desc);
  
Owen Taylor's avatar
Owen Taylor committed
842 843 844 845 846 847 848
  return fontsel->font;
}


gchar *
gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
{
849
  return pango_font_description_to_string (fontsel->font_desc);
Owen Taylor's avatar
Owen Taylor committed
850 851 852 853 854 855 856
}


/* This sets the current font, selecting the appropriate clist rows.
   First we check the fontname is valid and try to find the font family
   - i.e. the name in the main list. If we can't find that, then just return.
   Next we try to set each of the properties according to the fontname.
857
   Finally we select the font family & style in the clists. */
Owen Taylor's avatar
Owen Taylor committed
858 859 860 861
gboolean
gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
				  const gchar      *fontname)
{
862 863 864 865 866
  PangoFontDescription *new_desc;
  PangoFontDescription **descs;
  int n_descs, i;
  gboolean found = FALSE;

Owen Taylor's avatar
Owen Taylor committed
867 868
  g_return_val_if_fail (fontsel != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
869
  
870
  new_desc = pango_font_description_from_string (fontname);
Owen Taylor's avatar
Owen Taylor committed
871

872
  /* Check to make sure that this is in the list of allowed fonts */
Owen Taylor's avatar
Owen Taylor committed
873

874 875
  pango_context_list_fonts (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)),
			    new_desc->family_name, &descs, &n_descs);
876 877

  for (i=0; i<n_descs; i++)
Owen Taylor's avatar
Owen Taylor committed
878
    {
879 880 881 882
      if (descs[i]->weight == new_desc->weight &&
	  descs[i]->style == new_desc->style &&
	  descs[i]->stretch == new_desc->stretch &&
	  descs[i]->variant == new_desc->variant)
Damon Chaplin's avatar
""  
Damon Chaplin committed
883
	{
884 885
	  found = TRUE;
	  break;
Damon Chaplin's avatar
""  
Damon Chaplin committed
886
	}
Owen Taylor's avatar
Owen Taylor committed
887
    }
888 889 890 891 892 893 894 895 896

  pango_font_descriptions_free (descs, n_descs);

  if (!found)
    return FALSE;

  pango_font_description_free (fontsel->font_desc);
  fontsel->font_desc = new_desc;

897 898
  g_object_notify (G_OBJECT (fontsel), "font_name");
  g_object_notify (G_OBJECT (fontsel), "font");
899
  return TRUE;
Owen Taylor's avatar
Owen Taylor committed
900 901 902 903 904 905 906 907
}


/* This returns the text in the preview entry. You should copy the returned
   text if you need it. */
gchar*
gtk_font_selection_get_preview_text  (GtkFontSelection *fontsel)
{
908
  return gtk_entry_get_text (GTK_ENTRY (fontsel->preview_entry));
Owen Taylor's avatar
Owen Taylor committed
909 910 911 912 913 914 915 916
}


/* This sets the text in the preview entry. */
void
gtk_font_selection_set_preview_text  (GtkFontSelection *fontsel,
				      const gchar	  *text)
{
917
  gtk_entry_set_text (GTK_ENTRY (fontsel->preview_entry), text);
Owen Taylor's avatar
Owen Taylor committed
918 919 920 921 922 923
}

/*****************************************************************************
 * GtkFontSelectionDialog
 *****************************************************************************/

924
GtkType
Tim Janik's avatar
Tim Janik committed
925
gtk_font_selection_dialog_get_type (void)
Owen Taylor's avatar
Owen Taylor committed
926 927
{
  static guint font_selection_dialog_type = 0;
928
  
Owen Taylor's avatar
Owen Taylor committed
929 930 931 932 933 934 935 936 937
  if (!font_selection_dialog_type)
    {
      GtkTypeInfo fontsel_diag_info =
      {
	"GtkFontSelectionDialog",
	sizeof (GtkFontSelectionDialog),
	sizeof (GtkFontSelectionDialogClass),
	(GtkClassInitFunc) gtk_font_selection_dialog_class_init,
	(GtkObjectInitFunc) gtk_font_selection_dialog_init,
938 939
	/* reserved_1 */ NULL,
	/* reserved_2 */ NULL,
940
        (GtkClassInitFunc) NULL,
Owen Taylor's avatar
Owen Taylor committed
941
      };
942
      
943 944
      font_selection_dialog_type = gtk_type_unique (GTK_TYPE_DIALOG,
                                                    &fontsel_diag_info);
Owen Taylor's avatar
Owen Taylor committed
945
    }
946
  
Owen Taylor's avatar
Owen Taylor committed
947 948 949 950 951 952 953
  return font_selection_dialog_type;
}

static void
gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
{
  GtkObjectClass *object_class;
954
  
Owen Taylor's avatar
Owen Taylor committed
955
  object_class = (GtkObjectClass*) klass;
956
  
957
  font_selection_dialog_parent_class = gtk_type_class (GTK_TYPE_DIALOG);
Owen Taylor's avatar
Owen Taylor committed
958 959 960 961 962
}

static void
gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
{
963 964 965 966
  GtkDialog *dialog;

  dialog = GTK_DIALOG (fontseldiag);
  
Owen Taylor's avatar
Owen Taylor committed
967 968
  fontseldiag->dialog_width = -1;
  fontseldiag->auto_resize = TRUE;
969
  
Owen Taylor's avatar
Owen Taylor committed
970 971 972 973
  gtk_widget_set_events(GTK_WIDGET(fontseldiag), GDK_STRUCTURE_MASK);
  gtk_signal_connect (GTK_OBJECT (fontseldiag), "configure_event",
		      (GtkSignalFunc) gtk_font_selection_dialog_on_configure,
		      fontseldiag);
974
  
975
  gtk_container_set_border_width (GTK_CONTAINER (fontseldiag), 4);
Owen Taylor's avatar
Owen Taylor committed
976
  gtk_window_set_policy(GTK_WINDOW(fontseldiag), FALSE, TRUE, TRUE);
977
  
978
  fontseldiag->main_vbox = dialog->vbox;
979
  
Owen Taylor's avatar
Owen Taylor committed
980
  fontseldiag->fontsel = gtk_font_selection_new();
981
  gtk_container_set_border_width (GTK_CONTAINER (fontseldiag->fontsel), 4);
Owen Taylor's avatar
Owen Taylor committed
982 983 984
  gtk_widget_show (fontseldiag->fontsel);
  gtk_box_pack_start (GTK_BOX (fontseldiag->main_vbox),
		      fontseldiag->fontsel, TRUE, TRUE, 0);
985
  
Owen Taylor's avatar
Owen Taylor committed
986
  /* Create the action area */
987
  fontseldiag->action_area = dialog->action_area;
988
  
989 990 991 992
  fontseldiag->ok_button = gtk_dialog_add_button (dialog,
                                                  GTK_STOCK_BUTTON_OK,
                                                  GTK_RESPONSE_OK);
  gtk_widget_grab_default (fontseldiag->ok_button);
993
  
994 995 996 997 998
  fontseldiag->apply_button = gtk_dialog_add_button (dialog,
                                                     GTK_STOCK_BUTTON_APPLY,
                                                     GTK_RESPONSE_APPLY);
  gtk_widget_hide (fontseldiag->apply_button);

999
  
1000 1001 1002 1003 1004 1005 1006
  fontseldiag->cancel_button = gtk_dialog_add_button (dialog,
                                                      GTK_STOCK_BUTTON_CANCEL,
                                                      GTK_RESPONSE_CANCEL);

  gtk_window_set_title (GTK_WINDOW (fontseldiag),
                        _("Font Selection"));

Owen Taylor's avatar
Owen Taylor committed
1007 1008 1009 1010 1011 1012
}

GtkWidget*
gtk_font_selection_dialog_new	(const gchar	  *title)
{
  GtkFontSelectionDialog *fontseldiag;
1013
  
Tim Janik's avatar
Tim Janik committed
1014
  fontseldiag = gtk_type_new (GTK_TYPE_FONT_SELECTION_DIALOG);
1015 1016 1017

  if (title)
    gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
1018
  
Owen Taylor's avatar
Owen Taylor committed
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
  return GTK_WIDGET (fontseldiag);
}

gchar*
gtk_font_selection_dialog_get_font_name	(GtkFontSelectionDialog *fsd)
{
  return gtk_font_selection_get_font_name(GTK_FONT_SELECTION(fsd->fontsel));
}

GdkFont*
gtk_font_selection_dialog_get_font	(GtkFontSelectionDialog *fsd)
{
  return gtk_font_selection_get_font(GTK_FONT_SELECTION(fsd->fontsel));
}

gboolean
gtk_font_selection_dialog_set_font_name	(GtkFontSelectionDialog *fsd,
					 const gchar	  *fontname)
{
  return gtk_font_selection_set_font_name(GTK_FONT_SELECTION(fsd->fontsel),
					  fontname);
1040 1041
}

Owen Taylor's avatar
Owen Taylor committed
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
gchar*
gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
{
  return gtk_font_selection_get_preview_text(GTK_FONT_SELECTION(fsd->fontsel));
}

void
gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
					    const gchar	  *text)
{
  gtk_font_selection_set_preview_text(GTK_FONT_SELECTION(fsd->fontsel), text);
}


/* This turns auto-shrink off if the user resizes the width of the dialog.
   It also turns it back on again if the user resizes it back to its normal
   width. */
static gint
gtk_font_selection_dialog_on_configure (GtkWidget         *widget,
					GdkEventConfigure *event,
					GtkFontSelectionDialog *fsd)
{
  /* This sets the initial width. */
  if (fsd->dialog_width == -1)
    fsd->dialog_width = event->width;
  else if (fsd->auto_resize && fsd->dialog_width != event->width)
    {
      fsd->auto_resize = FALSE;
      gtk_window_set_policy(GTK_WINDOW(fsd), FALSE, TRUE, FALSE);
    }
  else if (!fsd->auto_resize && fsd->dialog_width == event->width)
    {
      fsd->auto_resize = TRUE;
      gtk_window_set_policy(GTK_WINDOW(fsd), FALSE, TRUE, TRUE);
    }
1077
  
Owen Taylor's avatar
Owen Taylor committed
1078 1079
  return FALSE;
}