All pastes #43334 Raw Edit

Fonts listing

public text v1 · immutable
#43334 ·published 2006-02-27 10:42 UTC
rendered paste body
/* testing & playing with pango :-) */

#include <pango/pango.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <math.h>


/* signal handler(s) */
gboolean handle_delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
	return FALSE;
}


gboolean handle_destroy(GtkWidget *widget, gpointer data)
{
	gtk_main_quit();
}


int main(int argc, char *argv[])
{
	PangoContext *pg_context = NULL;
	PangoFontFamily **pg_families = NULL;
	gint num_families = 0;
	
	GtkWidget *window = NULL;
	GtkWidget **labels = NULL;
	GtkWidget *table = NULL;
	gint table_dim = 0;
	gint family_offset = 0;
	gint row = 0, col = 0;
	gboolean all_done = FALSE;

	PangoContext *pg_labels_context = NULL;
	PangoFontDescription *pg_labels_desc = NULL;
	const gchar *family_name = NULL;

	/* setup GTK environment */
	gtk_init(&argc, &argv);
	
	/* create a window & attach required signals */
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(window), "Pango example");
	gtk_window_set_default_size(GTK_WINDOW(window), 200, 100);
	
	/* attach signal handlers to the window */
	gtk_signal_connect(GTK_OBJECT(window), "delete-event", GTK_SIGNAL_FUNC(handle_delete_event), NULL);
	gtk_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(handle_destroy), NULL);

	/* get a pango context form the window */
	pg_context = gtk_widget_get_pango_context(window);

	/* get the font_family listing */
	pango_context_list_families(pg_context, &pg_families, &num_families);
	
	/* initialize the labels array */
	labels = (GtkWidget **)g_malloc(sizeof(GtkWidget *) * num_families);
	
	/* init a GtkTable instance to hold all the labels */
	table_dim = ceil(sqrt(num_families));
	table = gtk_table_new(table_dim, table_dim, TRUE);
		
	/* render each label with its own font */
	pg_labels_desc = pango_font_description_new();
	for(row = 0; row < table_dim; ++row)
	{
		for(col = 0; col < table_dim; ++col)
		{
			/* get the family name in a string & create a label for that name */
			family_name = pango_font_family_get_name(pg_families[family_offset]);
			labels[family_offset] = gtk_label_new(family_name);
			
			/* get the pango context for that label & modify its font */
			pg_labels_context = gtk_widget_get_pango_context(labels[family_offset]);
			pango_font_description_set_family(pg_labels_desc, family_name);
			gtk_widget_modify_font(labels[family_offset], pg_labels_desc);
			
			/* add the font to the gtk-table */
			gtk_table_attach(GTK_TABLE(table), labels[family_offset], col, col + 1, row, row + 1, GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 6, 6);
			family_offset ++;
			
			/* finish adding to gtk-table when all done */
			if(family_offset == (num_families - 1))
			{
				all_done = TRUE;		
				break;
			}
		}
		
		if(TRUE == all_done)
			break;
	}	
	
	/* pack the window & show it */;
	gtk_container_add(GTK_CONTAINER(window), table);
	
	gtk_widget_show_all(window);
	
	gtk_main();	
	return 0;
}