widget-font-selector.c 6.52 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
/*
 * A font selector widget.  This is a simplified version of the
 * GnomePrint font selector widget.
 *
 * Author:
 *   Miguel de Icaza (miguel@gnu.org)
 */
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include "widget-font-selector.h"
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>

/* 
 * Inside this file we define a number of aliases for the
 * FontSelector object, a short hand for it is FS
 *
 * I do want to avoid typing.
 */ 
#define FS(x) FONT_SELECTOR(x)
typedef FontSelector Fs;
typedef FontSelectorClass FsClass;

static GtkHBoxClass *fs_parent_class;

static void
reload_preview (FontSelector *fs)
{
	char *name = gtk_entry_get_text (GTK_ENTRY (fs->font_name_entry));
	GtkStyle *style;
	GnomeFont *gnome_font;
	GnomeDisplayFont *display_font;
	
	gnome_font = gnome_font_new_closest (
		name,
		fs->is_bold ? GNOME_FONT_BOLD : GNOME_FONT_BOOK,
		fs->is_italic,
		fs->size);
	if (!gnome_font){
		g_warning ("Uh oh, can not get the font!");
		return;
	}

	display_font = gnome_font_get_display_font (gnome_font);
	if (!display_font){
		g_warning ("Uh oh, can not get the display font");
		return;
	}

	if (!display_font->gdk_font){
		gtk_object_unref (GTK_OBJECT (gnome_font));
		return;
	}

	fs->gnome_font = gnome_font;
	fs->display_font = display_font;
		
	style = gtk_style_new ();
	gdk_font_unref (style->font);
	style->font = fs->display_font->gdk_font;
	gdk_font_ref (style->font);

	gtk_widget_set_style (fs->font_preview, style);
	gtk_style_unref (style);
}

static void
font_selected (GtkCList *font_list, int col, int row, GdkEvent *event, FontSelector *fs)
{
	int row;
	gchar *text;
	
	if (font_list->selection == NULL)
		return;

	row = GPOINTER_TO_INT (font_list->selection->data);
	gtk_clist_get_text (font_list, row, 0, &text);
	printf ("Text at %d, %s\n", row, text);
	gtk_entry_set_text (GTK_ENTRY (fs->font_name_entry), text);

	reload_preview (fs);
}

static void
fs_fill_font_name_list (FontSelector *fs)
{
	GnomeFontClass *gfc = gtk_type_class (gnome_font_get_type ());
	GList *font_list = gnome_font_family_list (gfc);
	GList *l;
	
	for (l = font_list; l; l = l->next){
		char *name = l->data;
		char *array [1];

		array [0] = name;
		
		gtk_clist_append (GTK_CLIST (fs->font_name_list), array);
	}
	gtk_signal_connect (
		GTK_OBJECT (fs->font_name_list), "select_row",
		GTK_SIGNAL_FUNC(font_selected), fs);
}

static char *styles [] = {
	N_("Normal"),
	N_("Bold"),
	N_("Bold italic"),
	N_("Italic"),
	NULL
};

static void
style_selected (GtkCList *style_list, int col, int row, GdkEvent *event, FontSelector *fs)
{
	int row;
	
	if (style_list->selection == NULL)
		return;

	row = GPOINTER_TO_INT (style_list->selection->data);
	switch (row){
	case 0:
		fs->is_bold = fs->is_italic = FALSE;
		break;
	case 1:
		fs->is_bold = TRUE;
		fs->is_italic = FALSE;
		break;

	case 2:
		fs->is_bold = fs->is_italic = TRUE;
		break;
	case 3:
		fs->is_italic = TRUE;
		fs->is_bold = FALSE;
		break;
	}

	gtk_entry_set_text (GTK_ENTRY (fs->font_style_entry), _(styles [row]));
	reload_preview (fs);
}

static void
fs_fill_font_style_list (FontSelector *fs)
{
	GtkCList *style_list = GTK_CLIST (fs->font_style_list);
	int i;
	
	for (i = 0; styles [i] != NULL; i++){
		char *array [1];
		array [0] = _(styles [i]);
		
		gtk_clist_append (style_list, array);
	}
	gtk_signal_connect (
		GTK_OBJECT (fs->font_style_list), "select_row",
		GTK_SIGNAL_FUNC(style_selected), fs);
}

static void
size_selected (GtkCList *size_list, int col, int row, GdkEvent *event, FontSelector *fs)
{
	int row;
	gchar *text;

	if (size_list->selection == NULL)
		return;

	row = GPOINTER_TO_INT (size_list->selection->data);
	gtk_clist_get_text (size_list, row, 0, &text);
	gtk_entry_set_text (GTK_ENTRY (fs->font_size_entry), text);
}

static void
size_changed (GtkEntry *entry, FontSelector *fs)
{
	char *text;
	double size;
	
	text = gtk_entry_get_text (entry);
	size = atof (text);
	if (size > 0 && size < 128){
		fs->size = size;
		reload_preview (fs);
	}
}

static void
fs_fill_font_size_list (FontSelector *fs)
{
	int i;
	static int point_sizes [] = {
		4, 8, 9, 10, 11, 12, 13, 14, 16, 18, 20, 22,
		24, 26, 28, 32, 36, 40, 48, 56, 64, 72, 0 };

	for (i = 0; point_sizes [i] != 0; i++){
		char buffer [10];
		char *array [1];
		
		sprintf (buffer, "%d", point_sizes [i]);
		array [0] = buffer;
		gtk_clist_append (GTK_CLIST (fs->font_size_list), array);
	}
	gtk_signal_connect (
		GTK_OBJECT (fs->font_size_list), "select_row",
		GTK_SIGNAL_FUNC(size_selected), fs);

	gtk_signal_connect (
		GTK_OBJECT (fs->font_size_entry), "changed",
		GTK_SIGNAL_FUNC (size_changed), fs);
}

static void
fs_init (FontSelector *fs)
{
	GtkWidget *toplevel;
	GtkWidget *old_parent;
	
	fs->gui = glade_xml_new (GNUMERIC_GLADEDIR "/font-sel.glade", NULL);
	if (!fs->gui){
		g_error ("Could not load font-sel.glade");
		return;
	}
	toplevel = glade_xml_get_widget (fs->gui, "toplevel-table");
	old_parent = gtk_widget_get_toplevel (toplevel);
	gtk_widget_reparent (toplevel, GTK_WIDGET (fs));
	gtk_widget_destroy (old_parent);
	gtk_widget_queue_resize (toplevel);

	fs->font_name_entry  = glade_xml_get_widget (fs->gui, "font-name-entry");
	fs->font_style_entry = glade_xml_get_widget (fs->gui, "font-style-entry");
	fs->font_size_entry  = glade_xml_get_widget (fs->gui, "font-size-entry");
	fs->font_name_list  = glade_xml_get_widget (fs->gui, "font-name-list");
	fs->font_style_list = glade_xml_get_widget (fs->gui, "font-style-list");
	fs->font_size_list  = glade_xml_get_widget (fs->gui, "font-size-list");
	fs->font_preview = glade_xml_get_widget (fs->gui, "preview-entry");
	fs_fill_font_style_list (fs);
	fs_fill_font_name_list (fs);
	fs_fill_font_size_list (fs);
}

static void
fs_destroy (GtkObject *object)
{
	FontSelector *fs = FONT_SELECTOR (object);

	if (fs->gnome_font)
		gtk_object_unref (GTK_OBJECT (fs->gnome_font));
	if (fs->display_font)
		gtk_object_unref (GTK_OBJECT (fs->display_font));		
	((GtkObjectClass *)fs_parent_class)->destroy (object);
}

static void
fs_class_init (GtkObjectClass *class)
{
	class->destroy = fs_destroy;

	fs_parent_class = gtk_type_class (gtk_hbox_get_type ());
}

GtkType
font_selector_get_type (void)
{
	static GtkType fs_type = 0;

	if (!fs_type){
		GtkTypeInfo fs_info = {
			"FontSelector",
			sizeof (FontSelector),
			sizeof (FontSelectorClass),
			(GtkClassInitFunc) fs_class_init,
			(GtkObjectInitFunc) fs_init,
			NULL, /* reserved 1 */
			NULL, /* reserved 2 */
			(GtkClassInitFunc) NULL
		};

		fs_type = gtk_type_unique (gtk_hbox_get_type (), &fs_info);
	}

	return fs_type;
}

GtkWidget *
font_selector_new (void)
{
	GtkWidget *w;

	w = gtk_type_new (FONT_SELECTOR_TYPE);
	return w;
}