widget-font-selector.c 16 KB
Newer Older
1
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 3 4 5
/*
 * A font selector widget.  This is a simplified version of the
 * GnomePrint font selector widget.
 *
6
 * Authors:
7
 *   Miguel de Icaza (miguel@gnu.org)
8
 *   Almer S. Tigelaar (almer@gnome.org)
9
 */
10

11 12 13 14 15 16 17 18
#include <gnumeric-config.h>
#include <gnumeric.h>
#include "widget-font-selector.h"

#include <value.h>
#include <mstyle.h>
#include <preview-grid.h>
#include <style-color.h>
19
#include <gui-util.h>
20 21
#include <mstyle.h>
#include <preview-grid.h>
22

23
#include <goffice/utils/go-font.h>
Jody Goldberg's avatar
Jody Goldberg committed
24
#include <gsf/gsf-impl-utils.h>
25
#include <gtk/gtkhbox.h>
26 27 28 29 30
#include <gtk/gtkscrolledwindow.h>
#include <gtk/gtktreeview.h>
#include <gtk/gtktreeselection.h>
#include <gtk/gtkliststore.h>
#include <gtk/gtkcellrenderertext.h>
31
#include <glade/glade.h>
32 33 34 35
#include <glib/gi18n.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
36 37 38 39 40 41 42 43

struct _FontSelector {
	GtkHBox box;
	GladeXML *gui;

	GtkWidget *font_name_entry;
	GtkWidget *font_style_entry;
	GtkWidget *font_size_entry;
44 45 46
	GtkTreeView *font_name_list;
	GtkTreeView *font_style_list;
	GtkTreeView *font_size_list;
47 48 49 50

	FooCanvas *font_preview_canvas;
	FooCanvasItem *font_preview_grid;

Morten Welinder's avatar
Morten Welinder committed
51
	GnmStyle     *mstyle;
52 53 54

	GSList       *family_names;
	GSList       *font_sizes;
55 56 57 58 59
};

typedef struct {
	GtkHBoxClass parent_class;

Morten Welinder's avatar
Morten Welinder committed
60
	void (* font_changed) (FontSelector *fs, GnmStyle *mstyle);
61
} FontSelectorClass;
62

63
/*
64 65 66 67
 * 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.
68
 */
69
#define FS(x) FONT_SELECTOR(x)
Arturo Espinosa's avatar
Arturo Espinosa committed
70

71 72 73 74 75
typedef FontSelector Fs;
typedef FontSelectorClass FsClass;

static GtkHBoxClass *fs_parent_class;

76 77 78 79 80 81
/* Signals we emit */
enum {
	FONT_CHANGED,
	LAST_SIGNAL
};

82
static guint fs_signals[LAST_SIGNAL] = { 0 };
83

84
static void
Morten Welinder's avatar
Morten Welinder committed
85
fs_modify_style (FontSelector *fs, GnmStyle *modification)
86
{
Morten Welinder's avatar
Morten Welinder committed
87
	GnmStyle *original = fs->mstyle;
88 89
	g_return_if_fail (modification != NULL);

90
	fs->mstyle = gnm_style_merge (original, modification);
91
	g_signal_emit (G_OBJECT (fs),
92
		fs_signals[FONT_CHANGED], 0, modification);
93 94 95
	foo_canvas_item_set (fs->font_preview_grid,
		"default-style",  fs->mstyle,
		NULL);
96 97
	gnm_style_unref (modification);
	gnm_style_unref (original);
98
}
Arturo Espinosa's avatar
Arturo Espinosa committed
99

100
/*
Morten Welinder's avatar
Morten Welinder committed
101
 * We cannot moveto a list element until it is mapped.
102
 */
103
/*static void
Morten Welinder's avatar
Morten Welinder committed
104
list_mapped (GtkWidget *widget, G_GNUC_UNUSED gpointer user_data)
105 106 107 108 109 110 111
{
	GtkCList * clist = GTK_CLIST (widget);
	int row = 0;
	if (clist->selection)
		row = GPOINTER_TO_UINT (clist->selection->data);
	if (!gtk_clist_row_is_visible (clist, row))
		gtk_clist_moveto (clist, row, 0, 0.5, 0.0);
112
}*/
113

114
static void
115
cb_list_adjust (GtkTreeView* view)
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
	GtkTreeModel *model;
	GtkTreeIter iter;
	GtkTreePath *path;
	GtkScrolledWindow* scroll;
	GdkRectangle rect;
	GtkAdjustment *adj;
	int pos, height, child_height;

	if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (view), &model, &iter)) {
		path = gtk_tree_model_get_path (model, &iter);
		scroll = GTK_SCROLLED_WINDOW (gtk_widget_get_parent (GTK_WIDGET (view)));
		height = GTK_WIDGET (view)->allocation.height;
		child_height = GTK_WIDGET (view)->requisition.height;
		if (height < child_height) {
			gtk_tree_view_get_cell_area (view, path, NULL, &rect);
			adj = gtk_scrolled_window_get_vadjustment (scroll);
			pos = gtk_adjustment_get_value (adj);
			if (rect.y < 0)
				pos += rect.y;
			else if (rect.y + rect.height > height)
				pos += rect.y + rect.height - height;
			gtk_adjustment_set_value (adj, pos);
			gtk_scrolled_window_set_vadjustment (scroll, adj);
		}
		gtk_tree_path_free (path);
	}
}
Arturo Espinosa's avatar
Arturo Espinosa committed
144

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
static void
list_init (GtkTreeView* view)
{
	GtkCellRenderer *renderer;
	GtkListStore *store;
	GtkTreeViewColumn *column;

	gtk_tree_view_set_headers_visible (view, FALSE);
	store = gtk_list_store_new (1, G_TYPE_STRING);
	gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
	renderer = gtk_cell_renderer_text_new ();
	column = gtk_tree_view_column_new_with_attributes (
								NULL,
								renderer, "text", 0, NULL);
	gtk_tree_view_column_set_expand (column, TRUE);
	gtk_tree_view_append_column (view, column);
	g_signal_connect (view, "realize", G_CALLBACK (cb_list_adjust), NULL);
}
Arturo Espinosa's avatar
Arturo Espinosa committed
163

164 165 166 167 168 169 170 171 172 173 174 175 176
static void
font_selected (GtkTreeSelection *selection,
	       FontSelector *fs)
{
	gchar *text;
	GnmStyle *change;
	GtkTreeModel *model;
	GtkTreeIter iter;

	gtk_tree_selection_get_selected (selection, &model, &iter);
	gtk_tree_model_get (model, &iter, 0, &text, -1);
	gtk_entry_set_text (GTK_ENTRY (fs->font_name_entry), text);

177 178
	change = gnm_style_new ();
	gnm_style_set_font_name (change, text);
179
	g_free (text);
180
	fs_modify_style (fs, change);
181
}
Arturo Espinosa's avatar
Arturo Espinosa committed
182

183 184 185
static void
fs_fill_font_name_list (FontSelector *fs)
{
186
	GSList *l;
187 188
	GtkListStore *store;
	GtkTreeIter iter;
189 190 191 192
	PangoContext *context;

#warning "FIXME: We need to do this when we realize the widget as we don't have a screen until then."
	context = gtk_widget_get_pango_context (GTK_WIDGET (fs));
Arturo Espinosa's avatar
Arturo Espinosa committed
193

194
	fs->family_names = go_fonts_list_families (context);
195 196
	list_init (fs->font_name_list);
	store = GTK_LIST_STORE (gtk_tree_view_get_model (fs->font_name_list));
197
	for (l = fs->family_names; l; l = l->next) {
198 199
		gtk_list_store_append (store, &iter);
		gtk_list_store_set (store, &iter, 0, l->data, -1);
200
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
201

202
	g_signal_connect (
203
		 G_OBJECT (gtk_tree_view_get_selection (fs->font_name_list)), "changed",
204
		 GTK_SIGNAL_FUNC (font_selected), fs);
205
}
Arturo Espinosa's avatar
Arturo Espinosa committed
206

207
static char const *styles[] = {
Arturo Espinosa's avatar
Arturo Espinosa committed
208 209 210 211 212
	 N_("Normal"),
	 N_("Bold"),
	 N_("Bold italic"),
	 N_("Italic"),
	 NULL
213
};
Arturo Espinosa's avatar
Arturo Espinosa committed
214

215
static void
216 217
style_selected (GtkTreeSelection *selection,
		FontSelector *fs)
218
{
219
	GnmStyle *change = gnm_style_new ();
220 221 222 223
	GtkTreeModel *model;
	GtkTreeIter iter;
	GtkTreePath *path;
	int row;
Arturo Espinosa's avatar
Arturo Espinosa committed
224

225 226 227 228 229 230 231
	gtk_tree_selection_get_selected (selection, &model, &iter);
	path = gtk_tree_model_get_path (model, &iter);
	row = *gtk_tree_path_get_indices (path);
	gtk_tree_path_free (path);

	switch (row) {
	case 0:
232 233
		gnm_style_set_font_bold (change, FALSE);
		gnm_style_set_font_italic (change, FALSE);
234 235
		break;
	case 1:
236 237
		gnm_style_set_font_bold (change, TRUE);
		gnm_style_set_font_italic (change, FALSE);
238 239 240
		break;

	case 2:
241 242
		gnm_style_set_font_bold (change, TRUE);
		gnm_style_set_font_italic (change, TRUE);
243 244
		break;
	case 3:
245 246
		gnm_style_set_font_bold (change, FALSE);
		gnm_style_set_font_italic (change, TRUE);
247 248 249 250 251
		break;
	}

	gtk_entry_set_text (GTK_ENTRY (fs->font_style_entry), _(styles[row]));
	fs_modify_style (fs, change);
252
}
Arturo Espinosa's avatar
Arturo Espinosa committed
253

254 255 256
static void
fs_fill_font_style_list (FontSelector *fs)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
257
	 int i;
258 259 260 261 262 263 264 265 266 267 268 269 270
	GtkListStore *store;
	GtkTreeIter iter;

	list_init (fs->font_style_list);
	store = GTK_LIST_STORE (gtk_tree_view_get_model (fs->font_style_list));
	for (i = 0; styles[i] != NULL; i++) {
		gtk_list_store_append (store, &iter);
		gtk_list_store_set (store, &iter, 0, _(styles[i]), -1);
	}
	g_signal_connect (
		G_OBJECT (gtk_tree_view_get_selection (fs->font_style_list)), "changed",
		GTK_SIGNAL_FUNC (style_selected), fs);
}
Arturo Espinosa's avatar
Arturo Espinosa committed
271

272 273 274 275
static void
select_row (GtkTreeView *list, int row)
{
	GtkTreePath *path;
Arturo Espinosa's avatar
Arturo Espinosa committed
276

277 278 279 280 281 282 283 284 285 286
	if (row < 0)
		gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (list));
	else {
		path = gtk_tree_path_new_from_indices (row, -1);

		gtk_tree_selection_select_path (gtk_tree_view_get_selection (list), path);
		if (GTK_WIDGET_REALIZED (list))
			cb_list_adjust (list);
		gtk_tree_path_free (path);
	}
287
}
Arturo Espinosa's avatar
Arturo Espinosa committed
288

289
static void
290 291
size_selected (GtkTreeSelection *selection,
	       FontSelector *fs)
292
{
293
	GnmStyle *change = gnm_style_new ();
Morten Welinder's avatar
Morten Welinder committed
294
	gchar *text;
295 296 297 298 299 300
	GtkTreeModel *model;
	GtkTreeIter iter;

	gtk_tree_selection_get_selected (selection, &model, &iter);
	gtk_tree_model_get (model, &iter, 0, &text, -1);
	gtk_entry_set_text (GTK_ENTRY (fs->font_size_entry), text);
301
	gnm_style_set_font_size (change, atof (text));
Morten Welinder's avatar
Morten Welinder committed
302
	g_free (text);
303
	fs_modify_style (fs, change);
304
}
Arturo Espinosa's avatar
Arturo Espinosa committed
305

306 307 308
static void
size_changed (GtkEntry *entry, FontSelector *fs)
{
309
	int i;
310 311 312 313 314 315
	char const *text = gtk_entry_get_text (entry);
	double size = atof (text);
	int psize = (int)(size * PANGO_SCALE + 0.5);
	GSList *l;

	if (size >= 1. && size < 128) {
316 317
		GnmStyle *change = gnm_style_new ();
		gnm_style_set_font_size (change, size);
318 319 320 321 322 323 324 325 326 327 328 329 330
		fs_modify_style (fs, change);
	}
	g_signal_handlers_block_by_func (
		gtk_tree_view_get_selection (fs->font_size_list),
		size_selected, fs);

	for (i = 0, l = fs->font_sizes; l; i++, l = l->next) {
		int this_psize = GPOINTER_TO_INT (l->data);
		if (this_psize == psize)
			break;
	}

	select_row (fs->font_size_list, l ? i : -1);
331 332 333
	g_signal_handlers_unblock_by_func (
				gtk_tree_view_get_selection (fs->font_size_list),
				size_selected, fs);
334
}
Arturo Espinosa's avatar
Arturo Espinosa committed
335

336 337 338
static void
fs_fill_font_size_list (FontSelector *fs)
{
339 340
	GtkListStore *store;
	GtkTreeIter iter;
341
	GSList *ptr;
342

343 344
	fs->font_sizes = go_fonts_list_sizes ();

345 346
	list_init (fs->font_size_list);
	store = GTK_LIST_STORE (gtk_tree_view_get_model (fs->font_size_list));
347 348 349
	for (ptr = fs->font_sizes ; ptr != NULL ; ptr = ptr->next) {
		int psize = GPOINTER_TO_INT (ptr->data);
		char *size_text = g_strdup_printf ("%g", psize / (double)PANGO_SCALE);
350
		gtk_list_store_append (store, &iter);
351 352
		gtk_list_store_set (store, &iter, 0, size_text, -1);
		g_free (size_text);
353
	}
354
	g_signal_connect (
355
		G_OBJECT (gtk_tree_view_get_selection (fs->font_size_list)), "changed",
356
		GTK_SIGNAL_FUNC (size_selected), fs);
357

358
	g_signal_connect (
Jody Goldberg's avatar
Jody Goldberg committed
359
		G_OBJECT (fs->font_size_entry), "changed",
360 361 362
		GTK_SIGNAL_FUNC (size_changed), fs);
}

363
static void
Morten Welinder's avatar
Morten Welinder committed
364
canvas_size_changed (G_GNUC_UNUSED GtkWidget *widget,
365
		     GtkAllocation *allocation, FontSelector *fs)
366
{
367 368 369 370 371 372 373
	int width  = allocation->width - 1;
	int height = allocation->height - 1;

	foo_canvas_item_set (fs->font_preview_grid,
                 "default-col-width",  width,
                 "default-row-height", height,
		 NULL);
374

Jody Goldberg's avatar
Jody Goldberg committed
375
	foo_canvas_set_scroll_region (fs->font_preview_canvas, 0, 0,
376
				      width, height);
377 378
}

379 380 381
static void
fs_init (FontSelector *fs)
{
382
	GtkWidget *w;
Morten Welinder's avatar
Morten Welinder committed
383

384
	fs->gui = gnm_glade_xml_new (NULL, "font-sel.glade", "toplevel-table", NULL);
385
	if (fs->gui == NULL)
386 387
                return;

388 389 390 391
	fs->mstyle = gnm_style_new_default ();
	gnm_style_set_align_v   (fs->mstyle, VALIGN_CENTER);
	gnm_style_set_align_h   (fs->mstyle, HALIGN_CENTER);
	gnm_style_set_font_size (fs->mstyle, 10);
392 393 394

	gtk_box_pack_start_defaults (GTK_BOX (fs),
		glade_xml_get_widget (fs->gui, "toplevel-table"));
395 396 397 398

	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");
399 400 401
	fs->font_name_list  = GTK_TREE_VIEW (glade_xml_get_widget (fs->gui, "font-name-list"));
	fs->font_style_list = GTK_TREE_VIEW (glade_xml_get_widget (fs->gui, "font-style-list"));
	fs->font_size_list  = GTK_TREE_VIEW (glade_xml_get_widget (fs->gui, "font-size-list"));
402

Jody Goldberg's avatar
Jody Goldberg committed
403 404 405 406
	w = foo_canvas_new ();
	fs->font_preview_canvas = FOO_CANVAS (w);
	foo_canvas_set_scroll_region (fs->font_preview_canvas, -1, -1, INT_MAX/2, INT_MAX/2);
	foo_canvas_scroll_to (fs->font_preview_canvas, 0, 0);
407
	gtk_widget_show_all (w);
Jody Goldberg's avatar
Jody Goldberg committed
408 409
	w = glade_xml_get_widget (fs->gui, "font-preview-frame");
	gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (fs->font_preview_canvas));
410
	fs->font_preview_grid = FOO_CANVAS_ITEM (foo_canvas_item_new (
Jody Goldberg's avatar
Jody Goldberg committed
411
		foo_canvas_root (fs->font_preview_canvas),
412
		preview_grid_get_type (),
413 414 415
		"render-gridlines",	FALSE,
		"default-value",	value_new_string ("AaBbCcDdEe12345"),
		"default-style",	fs->mstyle,
416
		NULL));
417

418 419
	g_signal_connect (G_OBJECT (fs->font_preview_canvas),
		"size-allocate",
420 421
		GTK_SIGNAL_FUNC (canvas_size_changed), fs);

422
	fs_fill_font_name_list (fs);
423
	fs_fill_font_style_list (fs);
424 425 426 427 428 429 430 431
	fs_fill_font_size_list (fs);
}

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

432
	if (fs->mstyle) {
433
		gnm_style_unref (fs->mstyle);
434 435 436
		fs->mstyle = NULL;
	}

437
	if (fs->gui) {
Jody Goldberg's avatar
Jody Goldberg committed
438
		g_object_unref (G_OBJECT (fs->gui));
439 440 441
		fs->gui = NULL;
	}

442 443 444 445 446 447 448 449 450 451 452
	if (fs->family_names) {
		g_slist_foreach (fs->family_names, (GFunc)g_free, NULL);
		g_slist_free (fs->family_names);
		fs->family_names = NULL;
	}

	if (fs->font_sizes) {
		g_slist_free (fs->font_sizes);
		fs->font_sizes = NULL;
	}

453 454 455 456
	((GtkObjectClass *)fs_parent_class)->destroy (object);
}

static void
457
fs_class_init (GObjectClass *klass)
458
{
459 460 461
	GtkObjectClass *gto_class = (GtkObjectClass *) klass;

	gto_class->destroy = fs_destroy;
462

463
	fs_parent_class = g_type_class_peek (gtk_hbox_get_type ());
464

465
	fs_signals[FONT_CHANGED] =
466
		g_signal_new (
467
			"font_changed",
468 469 470 471
			G_OBJECT_CLASS_TYPE (klass),
			G_SIGNAL_RUN_LAST,
			G_STRUCT_OFFSET (FontSelectorClass, font_changed),
			NULL, NULL,
472
			g_cclosure_marshal_VOID__POINTER,
473
			G_TYPE_NONE, 1, G_TYPE_POINTER);
474 475
}

Jody Goldberg's avatar
Jody Goldberg committed
476 477
GSF_CLASS (FontSelector, font_selector,
	   fs_class_init, fs_init, GTK_TYPE_HBOX)
478 479 480 481 482 483

GtkWidget *
font_selector_new (void)
{
	GtkWidget *w;

484
	w = g_object_new (FONT_SELECTOR_TYPE, NULL);
485 486
	return w;
}
Arturo Espinosa's avatar
Arturo Espinosa committed
487

488
void
Jody Goldberg's avatar
Jody Goldberg committed
489
font_selector_set_value (FontSelector *fs, GnmValue const *v)
490
{
Jody Goldberg's avatar
Jody Goldberg committed
491
	GnmValue *val;
492

493
	g_return_if_fail (IS_FONT_SELECTOR (fs));
494

495
	val = (v != NULL)
496
		? value_dup (v)
497 498 499 500
		: value_new_string ("AaBbCcDdEe12345");
	foo_canvas_item_set (fs->font_preview_grid,
		"default-value",  val,
		NULL);
501 502
}

Arturo Espinosa's avatar
Arturo Espinosa committed
503
void
504 505
font_selector_set_name (FontSelector *fs,
			const char *font_name)
Arturo Espinosa's avatar
Arturo Espinosa committed
506
{
507
	GSList *l;
Arturo Espinosa's avatar
Arturo Espinosa committed
508
	int row;
509

Arturo Espinosa's avatar
Arturo Espinosa committed
510
	g_return_if_fail (IS_FONT_SELECTOR (fs));
511
	g_return_if_fail (font_name != NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
512

513
	for (row = 0, l = fs->family_names; l; l = l->next, row++)
514
		if (g_ascii_strcasecmp (font_name, l->data) == 0)
Arturo Espinosa's avatar
Arturo Espinosa committed
515 516 517
			break;

	if (l != NULL)
518
		select_row (fs->font_name_list, row);
Arturo Espinosa's avatar
Arturo Espinosa committed
519

520 521 522 523 524 525 526 527
}

void
font_selector_set_style (FontSelector *fs,
			 gboolean is_bold,
			 gboolean is_italic)
{
	int n;
Morten Welinder's avatar
Morten Welinder committed
528
	GnmStyle *change;
529 530 531 532

	g_return_if_fail (IS_FONT_SELECTOR (fs));

	if (is_bold) {
Arturo Espinosa's avatar
Arturo Espinosa committed
533 534 535 536 537 538 539 540 541 542 543
		if (is_italic)
			n = 2;
		else
			n = 1;
	} else {
		if (is_italic)
			n = 3;
		else
			n = 0;
	}
	select_row (fs->font_style_list, n);
544

545 546 547
	change = gnm_style_new ();
	gnm_style_set_font_bold   (change, is_bold);
	gnm_style_set_font_italic (change, is_italic);
548
	fs_modify_style (fs, change);
549 550 551 552 553
}

void
font_selector_set_strike (FontSelector *fs, gboolean strikethrough)
{
Morten Welinder's avatar
Morten Welinder committed
554
	GnmStyle *change;
555

556 557
	g_return_if_fail (IS_FONT_SELECTOR (fs));

558 559
	change = gnm_style_new ();
	gnm_style_set_font_strike (change, strikethrough);
560
	fs_modify_style (fs, change);
561 562 563 564 565 566 567 568 569 570 571 572
}

void
font_selector_set_script (FontSelector *fs, GOFontScript script)
{
	GnmStyle *change;

	g_return_if_fail (IS_FONT_SELECTOR (fs));

	change = gnm_style_new ();
	gnm_style_set_font_script (change, script);
	fs_modify_style (fs, change);
573 574 575
}

void
576
font_selector_set_underline (FontSelector *fs, GnmUnderline underline)
577
{
Morten Welinder's avatar
Morten Welinder committed
578
	GnmStyle *change;
579

580 581
	g_return_if_fail (IS_FONT_SELECTOR (fs));

582 583
	change = gnm_style_new ();
	gnm_style_set_font_uline (change, underline);
584
	fs_modify_style (fs, change);
585 586 587
}

void
Morten Welinder's avatar
Morten Welinder committed
588
font_selector_set_color (FontSelector *fs, GnmColor *color)
589
{
Morten Welinder's avatar
Morten Welinder committed
590
	GnmStyle *change;
591

592 593
	g_return_if_fail (IS_FONT_SELECTOR (fs));

594 595
	change = gnm_style_new ();
	gnm_style_set_font_color (change, color);
596
	fs_modify_style (fs, change);
597 598 599 600 601 602
}

void
font_selector_set_points (FontSelector *fs,
			  double point_size)
{
603 604 605
	const char *old_text = gtk_entry_get_text (GTK_ENTRY (fs->font_size_entry));
	char *buffer = g_strdup_printf ("%g", point_size);
	if (strcmp (old_text, buffer) != 0)
Arturo Espinosa's avatar
Arturo Espinosa committed
606
		gtk_entry_set_text (GTK_ENTRY (fs->font_size_entry), buffer);
607
	g_free (buffer);
Arturo Espinosa's avatar
Arturo Espinosa committed
608
}
609

610 611 612 613 614 615 616
void
font_selector_set_from_pango (FontSelector *fs, PangoFontDescription const *desc)
{
	font_selector_set_name (fs, pango_font_description_get_family (desc));
	font_selector_set_style (fs,
		pango_font_description_get_weight (desc) >= PANGO_WEIGHT_BOLD,
		pango_font_description_get_style (desc) != PANGO_STYLE_NORMAL);
Jody Goldberg's avatar
Jody Goldberg committed
617 618 619 620 621 622 623 624
	font_selector_set_points (fs,
		pango_font_description_get_size (desc) / PANGO_SCALE);
}

void
font_selector_get_pango (FontSelector *fs, PangoFontDescription *desc)
{
	pango_font_description_set_family (desc,
625
		gnm_style_get_font_name (fs->mstyle));
Jody Goldberg's avatar
Jody Goldberg committed
626
	pango_font_description_set_weight (desc,
627
		gnm_style_get_font_bold (fs->mstyle) ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);
Jody Goldberg's avatar
Jody Goldberg committed
628
	pango_font_description_set_style (desc,
629
		gnm_style_get_font_italic (fs->mstyle) ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL);
Jody Goldberg's avatar
Jody Goldberg committed
630
	pango_font_description_set_size (desc,
631
		gnm_style_get_font_size (fs->mstyle) * PANGO_SCALE);
632 633
}

634 635 636 637 638 639 640 641 642 643
void
font_selector_editable_enters (FontSelector *fs, GtkWindow *dialog)
{
	gnumeric_editable_enters (dialog,
		GTK_WIDGET (fs->font_name_entry));
	gnumeric_editable_enters (dialog,
		GTK_WIDGET (fs->font_style_entry));
	gnumeric_editable_enters (dialog,
		GTK_WIDGET (fs->font_size_entry));
}