offset fixes, admit to textline, proper updating of height/font at attr set,
[dia.git] / lib / diagtkfontsel.c
blob62644ddb50a7f22a45e5e9b4501d6d8f15bc08e1
1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * Massively updated for Pango by Owen Taylor, May 2000
5 * GtkFontSelection widget for Gtk+, by Damon Chaplin, May 1998.
6 * Based on the GnomeFontSelector widget, by Elliot Lee, but major changes.
7 * The GnomeFontSelector was derived from app/text_tool.c in the GIMP.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
27 * file for a list of people on the GTK+ Team. See the ChangeLog
28 * files for a list of changes. These files are distributed with
29 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 /* Copied into Dia to allow setting a PangoFT2 context for font listings.
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
39 #undef GTK_DISABLE_DEPRECATED /* GtkTypeInfo */
41 #include <gdk/gdk.h>
42 #include <gdk/gdkkeysyms.h>
44 #include "diagtkfontsel.h"
46 #include "gtk/gtkbutton.h"
47 #include "gtk/gtkcellrenderertext.h"
48 #include "gtk/gtkentry.h"
49 #include "gtk/gtkframe.h"
50 #include "gtk/gtkhbbox.h"
51 #include "gtk/gtkhbox.h"
52 #include "gtk/gtklabel.h"
53 #include "gtk/gtkliststore.h"
54 #include "gtk/gtkrc.h"
55 #include "gtk/gtksignal.h"
56 #include "gtk/gtkstock.h"
57 #include "gtk/gtktable.h"
58 #include "gtk/gtktreeselection.h"
59 #include "gtk/gtktreeview.h"
60 #include "gtk/gtkvbox.h"
61 #include "gtk/gtkscrolledwindow.h"
62 #include "intl.h"
64 /* We don't enable the font and style entries because they don't add
65 * much in terms of visible effect and have a weird effect on keynav.
66 * the Windows font selector has entries similarly positioned but they
67 * act in conjunction with the associated lists to form a single focus
68 * location.
70 #undef INCLUDE_FONT_ENTRIES
72 /* This is the default text shown in the preview entry, though the user
73 can set it. Remember that some fonts only have capital letters. */
74 #define PREVIEW_TEXT N_("abcdefghijk ABCDEFGHIJK")
76 /* This is the initial and maximum height of the preview entry (it expands
77 when large font sizes are selected). Initial height is also the minimum. */
78 #define INITIAL_PREVIEW_HEIGHT 44
79 #define MAX_PREVIEW_HEIGHT 300
81 /* These are the sizes of the font, style & size lists. */
82 #define FONT_LIST_HEIGHT 136
83 #define FONT_LIST_WIDTH 190
84 #define FONT_STYLE_LIST_WIDTH 170
85 #define FONT_SIZE_LIST_WIDTH 60
87 /* These are what we use as the standard font sizes, for the size list.
89 static const guint16 font_sizes[] = {
90 8, 9, 10, 11, 12, 13, 14, 16, 18, 20, 22, 24, 26, 28,
91 32, 36, 40, 48, 56, 64, 72
94 enum {
95 PROP_0,
96 PROP_FONT_NAME,
97 PROP_PREVIEW_TEXT
101 enum {
102 FAMILY_COLUMN,
103 FAMILY_NAME_COLUMN
106 enum {
107 FACE_COLUMN,
108 FACE_NAME_COLUMN
111 enum {
112 SIZE_COLUMN
115 static void dia_gtk_font_selection_class_init (DiaGtkFontSelectionClass *klass);
116 static void dia_gtk_font_selection_set_property (GObject *object,
117 guint prop_id,
118 const GValue *value,
119 GParamSpec *pspec);
120 static void dia_gtk_font_selection_get_property (GObject *object,
121 guint prop_id,
122 GValue *value,
123 GParamSpec *pspec);
124 static void dia_gtk_font_selection_init (DiaGtkFontSelection *fontsel);
125 static void dia_gtk_font_selection_finalize (GObject *object);
127 /* These are the callbacks & related functions. */
128 static void dia_gtk_font_selection_select_font (GtkTreeSelection *selection,
129 gpointer data);
130 static void dia_gtk_font_selection_show_available_fonts (DiaGtkFontSelection *fs);
132 static void dia_gtk_font_selection_show_available_styles (DiaGtkFontSelection *fs);
133 static void dia_gtk_font_selection_select_best_style (DiaGtkFontSelection *fs,
134 gboolean use_first);
135 static void dia_gtk_font_selection_select_style (GtkTreeSelection *selection,
136 gpointer data);
138 static void dia_gtk_font_selection_select_best_size (DiaGtkFontSelection *fs);
139 static void dia_gtk_font_selection_show_available_sizes (DiaGtkFontSelection *fs,
140 gboolean first_time);
141 static void dia_gtk_font_selection_size_activate (GtkWidget *w,
142 gpointer data);
143 static gboolean dia_gtk_font_selection_size_focus_out (GtkWidget *w,
144 GdkEventFocus *event,
145 gpointer data);
146 static void dia_gtk_font_selection_select_size (GtkTreeSelection *selection,
147 gpointer data);
149 static void dia_gtk_font_selection_scroll_on_map (GtkWidget *w,
150 gpointer data);
152 static void dia_gtk_font_selection_preview_changed (GtkWidget *entry,
153 DiaGtkFontSelection *fontsel);
155 /* Misc. utility functions. */
156 static void dia_gtk_font_selection_load_font (DiaGtkFontSelection *fs);
157 static void dia_gtk_font_selection_update_preview (DiaGtkFontSelection *fs);
159 /* FontSelectionDialog */
160 static void dia_gtk_font_selection_dialog_class_init (DiaGtkFontSelectionDialogClass *klass);
161 static void dia_gtk_font_selection_dialog_init (DiaGtkFontSelectionDialog *fontseldiag);
163 static gint dia_gtk_font_selection_dialog_on_configure (GtkWidget *widget,
164 GdkEventConfigure *event,
165 DiaGtkFontSelectionDialog *fsd);
167 static GtkWindowClass *font_selection_parent_class = NULL;
168 static GtkVBoxClass *font_selection_dialog_parent_class = NULL;
170 GtkType
171 dia_gtk_font_selection_get_type ()
173 static GtkType font_selection_type = 0;
175 if (!font_selection_type)
177 static const GtkTypeInfo fontsel_type_info =
179 "DiaGtkFontSelection",
180 sizeof (DiaGtkFontSelection),
181 sizeof (DiaGtkFontSelectionClass),
182 (GtkClassInitFunc) dia_gtk_font_selection_class_init,
183 (GtkObjectInitFunc) dia_gtk_font_selection_init,
184 /* reserved_1 */ NULL,
185 /* reserved_2 */ NULL,
186 (GtkClassInitFunc) NULL,
189 font_selection_type = gtk_type_unique (GTK_TYPE_VBOX,
190 &fontsel_type_info);
193 return font_selection_type;
196 static void
197 dia_gtk_font_selection_class_init (DiaGtkFontSelectionClass *klass)
199 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
201 font_selection_parent_class = gtk_type_class (GTK_TYPE_VBOX);
203 gobject_class->set_property = dia_gtk_font_selection_set_property;
204 gobject_class->get_property = dia_gtk_font_selection_get_property;
206 g_object_class_install_property (gobject_class,
207 PROP_FONT_NAME,
208 g_param_spec_string ("font_name",
209 _("Font name"),
210 _("The X string that represents this font."),
211 NULL,
212 G_PARAM_READWRITE));
213 g_object_class_install_property (gobject_class,
214 PROP_PREVIEW_TEXT,
215 g_param_spec_string ("preview_text",
216 _("Preview text"),
217 _("The text to display in order to demonstrate the selected font."),
218 PREVIEW_TEXT,
219 G_PARAM_READWRITE));
220 gobject_class->finalize = dia_gtk_font_selection_finalize;
223 static void
224 dia_gtk_font_selection_set_property (GObject *object,
225 guint prop_id,
226 const GValue *value,
227 GParamSpec *pspec)
229 DiaGtkFontSelection *fontsel;
231 fontsel = DIA_GTK_FONT_SELECTION (object);
233 switch (prop_id)
235 case PROP_FONT_NAME:
236 dia_gtk_font_selection_set_font_name (fontsel, g_value_get_string (value));
237 break;
238 case PROP_PREVIEW_TEXT:
239 dia_gtk_font_selection_set_preview_text (fontsel, g_value_get_string (value));
240 break;
241 default:
242 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
243 break;
247 static void dia_gtk_font_selection_get_property (GObject *object,
248 guint prop_id,
249 GValue *value,
250 GParamSpec *pspec)
252 DiaGtkFontSelection *fontsel;
254 fontsel = DIA_GTK_FONT_SELECTION (object);
256 switch (prop_id)
258 case PROP_FONT_NAME:
259 g_value_set_string (value, dia_gtk_font_selection_get_font_name (fontsel));
260 break;
261 case PROP_PREVIEW_TEXT:
262 g_value_set_string (value, dia_gtk_font_selection_get_preview_text (fontsel));
263 break;
264 default:
265 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
266 break;
271 static void
272 dia_gtk_font_selection_init (DiaGtkFontSelection *fontsel)
274 GtkWidget *scrolled_win;
275 GtkWidget *text_frame;
276 GtkWidget *text_box;
277 GtkWidget *table, *label;
278 GtkWidget *font_label, *style_label;
279 GtkListStore *model;
280 GtkTreeViewColumn *column;
281 GList *focus_chain = NULL;
283 gtk_widget_push_composite_child ();
285 fontsel->size = 12 * PANGO_SCALE;
287 /* Create the table of font, style & size. */
288 table = gtk_table_new (3, 3, FALSE);
289 gtk_widget_show (table);
290 gtk_table_set_col_spacings (GTK_TABLE (table), 8);
291 gtk_box_pack_start (GTK_BOX (fontsel), table, TRUE, TRUE, 0);
293 #ifdef INCLUDE_FONT_ENTRIES
294 fontsel->font_entry = gtk_entry_new ();
295 gtk_entry_set_editable (GTK_ENTRY (fontsel->font_entry), FALSE);
296 gtk_widget_set_usize (fontsel->font_entry, 20, -1);
297 gtk_widget_show (fontsel->font_entry);
298 gtk_table_attach (GTK_TABLE (table), fontsel->font_entry, 0, 1, 1, 2,
299 GTK_FILL, 0, 0, 0);
301 fontsel->font_style_entry = gtk_entry_new ();
302 gtk_entry_set_editable (GTK_ENTRY (fontsel->font_style_entry), FALSE);
303 gtk_widget_set_usize (fontsel->font_style_entry, 20, -1);
304 gtk_widget_show (fontsel->font_style_entry);
305 gtk_table_attach (GTK_TABLE (table), fontsel->font_style_entry, 1, 2, 1, 2,
306 GTK_FILL, 0, 0, 0);
307 #endif /* INCLUDE_FONT_ENTRIES */
309 fontsel->size_entry = gtk_entry_new ();
310 gtk_widget_set_usize (fontsel->size_entry, 20, -1);
311 gtk_widget_show (fontsel->size_entry);
312 gtk_table_attach (GTK_TABLE (table), fontsel->size_entry, 2, 3, 1, 2,
313 GTK_FILL, 0, 0, 0);
314 gtk_signal_connect (GTK_OBJECT (fontsel->size_entry), "activate",
315 (GtkSignalFunc) dia_gtk_font_selection_size_activate,
316 fontsel);
317 gtk_signal_connect_after (GTK_OBJECT (fontsel->size_entry), "focus_out_event",
318 (GtkSignalFunc) dia_gtk_font_selection_size_focus_out,
319 fontsel);
321 font_label = gtk_label_new_with_mnemonic (_("_Family:"));
322 gtk_misc_set_alignment (GTK_MISC (font_label), 0.0, 0.5);
323 gtk_widget_show (font_label);
324 gtk_table_attach (GTK_TABLE (table), font_label, 0, 1, 0, 1,
325 GTK_FILL, 0, 0, 0);
327 style_label = gtk_label_new_with_mnemonic (_("_Style:"));
328 gtk_misc_set_alignment (GTK_MISC (style_label), 0.0, 0.5);
329 gtk_widget_show (style_label);
330 gtk_table_attach (GTK_TABLE (table), style_label, 1, 2, 0, 1,
331 GTK_FILL, 0, 0, 0);
333 label = gtk_label_new_with_mnemonic (_("Si_ze:"));
334 gtk_label_set_mnemonic_widget (GTK_LABEL (label),
335 fontsel->size_entry);
336 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
337 gtk_widget_show (label);
338 gtk_table_attach (GTK_TABLE (table), label, 2, 3, 0, 1,
339 GTK_FILL, 0, 0, 0);
342 /* Create the lists */
344 model = gtk_list_store_new (2,
345 G_TYPE_OBJECT, /* FAMILY_COLUMN */
346 G_TYPE_STRING); /* FAMILY_NAME_COLUMN */
347 fontsel->family_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
348 g_object_unref (model);
350 column = gtk_tree_view_column_new_with_attributes ("Family",
351 gtk_cell_renderer_text_new (),
352 "text", FAMILY_NAME_COLUMN,
353 NULL);
354 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
355 gtk_tree_view_append_column (GTK_TREE_VIEW (fontsel->family_list), column);
357 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (fontsel->family_list), FALSE);
358 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->family_list)),
359 GTK_SELECTION_BROWSE);
361 gtk_label_set_mnemonic_widget (GTK_LABEL (font_label), fontsel->family_list);
363 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
364 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
365 gtk_widget_set_usize (scrolled_win, FONT_LIST_WIDTH, FONT_LIST_HEIGHT);
366 gtk_container_add (GTK_CONTAINER (scrolled_win), fontsel->family_list);
367 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
368 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
369 gtk_widget_show (fontsel->family_list);
370 gtk_widget_show (scrolled_win);
372 gtk_table_attach (GTK_TABLE (table), scrolled_win, 0, 1, 1, 3,
373 GTK_EXPAND | GTK_FILL,
374 GTK_EXPAND | GTK_FILL, 0, 0);
375 focus_chain = g_list_append (focus_chain, scrolled_win);
377 model = gtk_list_store_new (2,
378 G_TYPE_OBJECT, /* FACE_COLUMN */
379 G_TYPE_STRING); /* FACE_NAME_COLUMN */
380 fontsel->face_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
381 g_object_unref (model);
383 gtk_label_set_mnemonic_widget (GTK_LABEL (style_label), fontsel->face_list);
385 column = gtk_tree_view_column_new_with_attributes ("Face",
386 gtk_cell_renderer_text_new (),
387 "text", FACE_NAME_COLUMN,
388 NULL);
389 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
390 gtk_tree_view_append_column (GTK_TREE_VIEW (fontsel->face_list), column);
392 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (fontsel->face_list), FALSE);
393 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->face_list)),
394 GTK_SELECTION_BROWSE);
396 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
397 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
398 gtk_widget_set_usize (scrolled_win, FONT_STYLE_LIST_WIDTH, FONT_LIST_HEIGHT);
399 gtk_container_add (GTK_CONTAINER (scrolled_win), fontsel->face_list);
400 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
401 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
402 gtk_widget_show (fontsel->face_list);
403 gtk_widget_show (scrolled_win);
404 gtk_table_attach (GTK_TABLE (table), scrolled_win, 1, 2, 1, 3,
405 GTK_EXPAND | GTK_FILL,
406 GTK_EXPAND | GTK_FILL, 0, 0);
407 focus_chain = g_list_append (focus_chain, scrolled_win);
409 focus_chain = g_list_append (focus_chain, fontsel->size_entry);
411 model = gtk_list_store_new (1, G_TYPE_INT);
412 fontsel->size_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
413 g_object_unref (model);
415 column = gtk_tree_view_column_new_with_attributes ("Size",
416 gtk_cell_renderer_text_new (),
417 "text", SIZE_COLUMN,
418 NULL);
419 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
420 gtk_tree_view_append_column (GTK_TREE_VIEW (fontsel->size_list), column);
422 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (fontsel->size_list), FALSE);
423 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->size_list)),
424 GTK_SELECTION_BROWSE);
426 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
427 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
428 gtk_container_add (GTK_CONTAINER (scrolled_win), fontsel->size_list);
429 gtk_widget_set_usize (scrolled_win, -1, FONT_LIST_HEIGHT);
430 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
431 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
432 gtk_widget_show (fontsel->size_list);
433 gtk_widget_show (scrolled_win);
434 gtk_table_attach (GTK_TABLE (table), scrolled_win, 2, 3, 2, 3,
435 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
436 focus_chain = g_list_append (focus_chain, scrolled_win);
438 gtk_container_set_focus_chain (GTK_CONTAINER (table), focus_chain);
439 g_list_free (focus_chain);
441 /* Insert the fonts. */
442 dia_gtk_font_selection_show_available_fonts (fontsel);
444 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->family_list)), "changed",
445 G_CALLBACK (dia_gtk_font_selection_select_font), fontsel);
447 gtk_signal_connect_after (GTK_OBJECT (fontsel->family_list), "map",
448 GTK_SIGNAL_FUNC (dia_gtk_font_selection_scroll_on_map),
449 fontsel);
451 dia_gtk_font_selection_show_available_styles (fontsel);
453 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->face_list)), "changed",
454 G_CALLBACK (dia_gtk_font_selection_select_style), fontsel);
456 dia_gtk_font_selection_show_available_sizes (fontsel, TRUE);
458 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->size_list)), "changed",
459 G_CALLBACK (dia_gtk_font_selection_select_size), fontsel);
461 /* create the text entry widget */
462 label = gtk_label_new_with_mnemonic (_("_Preview:"));
463 gtk_widget_show (label);
465 text_frame = gtk_frame_new (NULL);
466 gtk_frame_set_label_widget (GTK_FRAME (text_frame), label);
468 gtk_widget_show (text_frame);
469 gtk_frame_set_shadow_type (GTK_FRAME (text_frame), GTK_SHADOW_ETCHED_IN);
470 gtk_box_pack_start (GTK_BOX (fontsel), text_frame,
471 FALSE, TRUE, 0);
473 /* This is just used to get a 4-pixel space around the preview entry. */
474 text_box = gtk_hbox_new (FALSE, 0);
475 gtk_widget_show (text_box);
476 gtk_container_add (GTK_CONTAINER (text_frame), text_box);
477 gtk_container_set_border_width (GTK_CONTAINER (text_box), 4);
479 fontsel->preview_entry = gtk_entry_new ();
480 gtk_label_set_mnemonic_widget (GTK_LABEL (label), fontsel->preview_entry);
482 gtk_widget_show (fontsel->preview_entry);
483 gtk_signal_connect (GTK_OBJECT (fontsel->preview_entry), "changed",
484 (GtkSignalFunc) dia_gtk_font_selection_preview_changed,
485 fontsel);
486 gtk_widget_set_usize (fontsel->preview_entry, -1, INITIAL_PREVIEW_HEIGHT);
487 gtk_box_pack_start (GTK_BOX (text_box), fontsel->preview_entry,
488 TRUE, TRUE, 0);
490 dia_gtk_font_selection_update_preview (fontsel);
492 gtk_widget_pop_composite_child();
495 GtkWidget *
496 dia_gtk_font_selection_new ()
498 DiaGtkFontSelection *fontsel;
500 fontsel = gtk_type_new (DIA_GTK_TYPE_FONT_SELECTION);
502 return GTK_WIDGET (fontsel);
505 static void
506 dia_gtk_font_selection_finalize (GObject *object)
508 DiaGtkFontSelection *fontsel;
510 g_return_if_fail (DIA_GTK_IS_FONT_SELECTION (object));
512 fontsel = DIA_GTK_FONT_SELECTION (object);
514 if (G_OBJECT_CLASS (font_selection_parent_class)->finalize)
515 (* G_OBJECT_CLASS (font_selection_parent_class)->finalize) (object);
518 static void
519 dia_gtk_font_selection_preview_changed (GtkWidget *entry,
520 DiaGtkFontSelection *fontsel)
522 g_object_notify (G_OBJECT (fontsel), "preview_text");
525 static void
526 scroll_to_selection (GtkTreeView *tree_view)
528 GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
529 GtkTreeModel *model;
530 GtkTreeIter iter;
532 if (gtk_tree_selection_get_selected (selection, &model, &iter))
534 GtkTreePath *path = gtk_tree_model_get_path (model, &iter);
535 gtk_tree_view_scroll_to_cell (tree_view, path, NULL, TRUE, 0.5, 0.5);
536 gtk_tree_path_free (path);
540 static void
541 set_cursor_to_iter (GtkTreeView *view,
542 GtkTreeIter *iter)
544 GtkTreeModel *model = gtk_tree_view_get_model (view);
545 GtkTreePath *path = gtk_tree_model_get_path (model, iter);
547 gtk_tree_view_set_cursor (view, path, 0, FALSE);
549 gtk_tree_path_free (path);
552 /* This is called when the list is mapped. Here we scroll to the current
553 font if necessary. */
554 static void
555 dia_gtk_font_selection_scroll_on_map (GtkWidget *widget,
556 gpointer data)
558 DiaGtkFontSelection *fontsel;
560 #ifdef FONTSEL_DEBUG
561 g_message ("In expose_list\n");
562 #endif
563 fontsel = DIA_GTK_FONT_SELECTION (data);
565 /* Try to scroll the font family list to the selected item */
566 scroll_to_selection (GTK_TREE_VIEW (fontsel->family_list));
568 /* Try to scroll the font family list to the selected item */
569 scroll_to_selection (GTK_TREE_VIEW (fontsel->face_list));
571 /* Try to scroll the font family list to the selected item */
572 scroll_to_selection (GTK_TREE_VIEW (fontsel->size_list));
575 /* This is called when a family is selected in the list. */
576 static void
577 dia_gtk_font_selection_select_font (GtkTreeSelection *selection,
578 gpointer data)
580 DiaGtkFontSelection *fontsel;
581 GtkTreeModel *model;
582 GtkTreeIter iter;
583 const gchar *family_name;
585 fontsel = DIA_GTK_FONT_SELECTION (data);
587 if (gtk_tree_selection_get_selected (selection, &model, &iter))
589 PangoFontFamily *family;
591 gtk_tree_model_get (model, &iter, FAMILY_COLUMN, &family, -1);
592 if (fontsel->family != family)
594 fontsel->family = family;
596 family_name = pango_font_family_get_name (fontsel->family);
597 #ifdef INCLUDE_FONT_ENTRIES
598 gtk_entry_set_text (GTK_ENTRY (fontsel->font_entry), family_name);
599 #endif
601 dia_gtk_font_selection_show_available_styles (fontsel);
602 dia_gtk_font_selection_select_best_style (fontsel, TRUE);
605 g_object_unref (family);
609 static int
610 cmp_families (const void *a, const void *b)
612 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
613 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
615 return g_utf8_collate (a_name, b_name);
618 static void
619 dia_gtk_font_selection_show_available_fonts (DiaGtkFontSelection *fontsel)
621 GtkListStore *model;
622 PangoFontFamily **families;
623 PangoFontFamily *match_family = NULL;
624 gint n_families, i;
625 GtkTreeIter match_row;
627 model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fontsel->family_list)));
629 if (fontsel->context == NULL)
630 fontsel->context = gtk_widget_get_pango_context (GTK_WIDGET (fontsel));
631 pango_context_list_families (fontsel->context,
632 &families, &n_families);
633 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
635 gtk_list_store_clear (model);
637 for (i=0; i<n_families; i++)
639 const gchar *name = pango_font_family_get_name (families[i]);
640 GtkTreeIter iter;
642 gtk_list_store_append (model, &iter);
643 gtk_list_store_set (model, &iter,
644 FAMILY_COLUMN, families[i],
645 FAMILY_NAME_COLUMN, name,
646 -1);
648 if (i == 0 || !g_ascii_strcasecmp (name, "sans"))
650 match_family = families[i];
651 match_row = iter;
655 fontsel->family = match_family;
656 if (match_family)
658 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->family_list), &match_row);
659 #ifdef INCLUDE_FONT_ENTRIES
660 gtk_entry_set_text (GTK_ENTRY (fontsel->font_entry),
661 pango_font_family_get_name (match_family));
662 #endif /* INCLUDE_FONT_ENTRIES */
665 g_free (families);
668 static int
669 compare_font_descriptions (const PangoFontDescription *a, const PangoFontDescription *b)
671 int val = strcmp (pango_font_description_get_family (a), pango_font_description_get_family (b));
672 if (val != 0)
673 return val;
675 if (pango_font_description_get_weight (a) != pango_font_description_get_weight (b))
676 return pango_font_description_get_weight (a) - pango_font_description_get_weight (b);
678 if (pango_font_description_get_style (a) != pango_font_description_get_style (b))
679 return pango_font_description_get_style (a) - pango_font_description_get_style (b);
681 if (pango_font_description_get_stretch (a) != pango_font_description_get_stretch (b))
682 return pango_font_description_get_stretch (a) - pango_font_description_get_stretch (b);
684 if (pango_font_description_get_variant (a) != pango_font_description_get_variant (b))
685 return pango_font_description_get_variant (a) - pango_font_description_get_variant (b);
687 return 0;
690 static int
691 faces_sort_func (const void *a, const void *b)
693 PangoFontDescription *desc_a = pango_font_face_describe (*(PangoFontFace **)a);
694 PangoFontDescription *desc_b = pango_font_face_describe (*(PangoFontFace **)b);
696 int ord = compare_font_descriptions (desc_a, desc_b);
698 pango_font_description_free (desc_a);
699 pango_font_description_free (desc_b);
701 return ord;
704 static gboolean
705 font_description_style_equal (const PangoFontDescription *a,
706 const PangoFontDescription *b)
708 return (pango_font_description_get_weight (a) == pango_font_description_get_weight (b) &&
709 pango_font_description_get_style (a) == pango_font_description_get_style (b) &&
710 pango_font_description_get_stretch (a) == pango_font_description_get_stretch (b) &&
711 pango_font_description_get_variant (a) == pango_font_description_get_variant (b));
714 /* This fills the font style list with all the possible style combinations
715 for the current font family. */
716 static void
717 dia_gtk_font_selection_show_available_styles (DiaGtkFontSelection *fontsel)
719 gint n_faces, i;
720 PangoFontFace **faces;
721 PangoFontDescription *old_desc;
722 GtkListStore *model;
723 GtkTreeIter match_row;
724 PangoFontFace *match_face = NULL;
726 model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fontsel->face_list)));
728 if (fontsel->face)
729 old_desc = pango_font_face_describe (fontsel->face);
730 else
731 old_desc= NULL;
733 pango_font_family_list_faces (fontsel->family, &faces, &n_faces);
734 qsort (faces, n_faces, sizeof (PangoFontFace *), faces_sort_func);
736 gtk_list_store_clear (model);
738 for (i=0; i < n_faces; i++)
740 GtkTreeIter iter;
741 const gchar *str = pango_font_face_get_face_name (faces[i]);
743 gtk_list_store_append (model, &iter);
744 gtk_list_store_set (model, &iter,
745 FACE_COLUMN, faces[i],
746 FACE_NAME_COLUMN, str,
747 -1);
749 if (i == 0)
751 match_row = iter;
752 match_face = faces[i];
754 else if (old_desc)
756 PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
758 if (font_description_style_equal (tmp_desc, old_desc))
760 match_row = iter;
761 match_face = faces[i];
764 pango_font_description_free (tmp_desc);
768 if (old_desc)
769 pango_font_description_free (old_desc);
771 fontsel->face = match_face;
772 if (match_face)
774 #ifdef INCLUDE_FONT_ENTRIES
775 const gchar *str = pango_font_face_get_face_name (fontsel->face);
777 gtk_entry_set_text (GTK_ENTRY (fontsel->font_style_entry), str);
778 #endif
779 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->face_list), &match_row);
782 g_free (faces);
785 /* This selects a style when the user selects a font. It just uses the first
786 available style at present. I was thinking of trying to maintain the
787 selected style, e.g. bold italic, when the user selects different fonts.
788 However, the interface is so easy to use now I'm not sure it's worth it.
789 Note: This will load a font. */
790 static void
791 dia_gtk_font_selection_select_best_style (DiaGtkFontSelection *fontsel,
792 gboolean use_first)
794 GtkTreeIter iter;
795 GtkTreeModel *model;
797 model = gtk_tree_view_get_model (GTK_TREE_VIEW (fontsel->face_list));
799 if (gtk_tree_model_get_iter_first (model, &iter))
801 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->face_list), &iter);
802 scroll_to_selection (GTK_TREE_VIEW (fontsel->face_list));
805 dia_gtk_font_selection_show_available_sizes (fontsel, FALSE);
806 dia_gtk_font_selection_select_best_size (fontsel);
810 /* This is called when a style is selected in the list. */
811 static void
812 dia_gtk_font_selection_select_style (GtkTreeSelection *selection,
813 gpointer data)
815 DiaGtkFontSelection *fontsel = DIA_GTK_FONT_SELECTION (data);
816 GtkTreeModel *model;
817 GtkTreeIter iter;
819 if (gtk_tree_selection_get_selected (selection, &model, &iter))
821 PangoFontFace *face;
823 gtk_tree_model_get (model, &iter, FACE_COLUMN, &face, -1);
824 fontsel->face = face;
826 g_object_unref (face);
829 dia_gtk_font_selection_show_available_sizes (fontsel, FALSE);
830 dia_gtk_font_selection_select_best_size (fontsel);
833 static void
834 dia_gtk_font_selection_show_available_sizes (DiaGtkFontSelection *fontsel,
835 gboolean first_time)
837 gint i;
838 GtkListStore *model;
839 GtkTreeSelection *selection;
840 gchar buffer[128];
841 gchar *p;
843 model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fontsel->size_list)));
844 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->size_list));
846 /* Insert the standard font sizes */
847 if (first_time)
849 gtk_list_store_clear (model);
851 for (i = 0; i < G_N_ELEMENTS (font_sizes); i++)
853 GtkTreeIter iter;
855 gtk_list_store_append (model, &iter);
856 gtk_list_store_set (model, &iter, SIZE_COLUMN, font_sizes[i], -1);
858 if (font_sizes[i] * PANGO_SCALE == fontsel->size)
859 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->size_list), &iter);
862 else
864 GtkTreeIter iter;
865 gboolean found = FALSE;
867 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter);
868 for (i = 0; i < G_N_ELEMENTS (font_sizes) && !found; i++)
870 if (font_sizes[i] * PANGO_SCALE == fontsel->size)
872 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->size_list), &iter);
873 found = TRUE;
876 gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &iter);
879 if (!found)
881 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->size_list));
882 gtk_tree_selection_unselect_all (selection);
886 /* Set the entry to the new size, rounding to 1 digit,
887 * trimming of trailing 0's and a trailing period
889 sprintf (buffer, "%.1f", fontsel->size / (1.0 * PANGO_SCALE));
890 if (strchr (buffer, '.'))
892 p = buffer + strlen (buffer) - 1;
893 while (*p == '0')
894 p--;
895 if (*p == '.')
896 p--;
897 p[1] = '\0';
900 /* Compare, to avoid moving the cursor unecessarily */
901 if (strcmp (gtk_entry_get_text (GTK_ENTRY (fontsel->size_entry)), buffer) != 0)
902 gtk_entry_set_text (GTK_ENTRY (fontsel->size_entry), buffer);
905 static void
906 dia_gtk_font_selection_select_best_size (DiaGtkFontSelection *fontsel)
908 dia_gtk_font_selection_load_font (fontsel);
911 static void
912 dia_gtk_font_selection_set_size (DiaGtkFontSelection *fontsel,
913 gint new_size)
915 if (fontsel->size != new_size)
917 fontsel->size = new_size;
919 dia_gtk_font_selection_show_available_sizes (fontsel, FALSE);
920 dia_gtk_font_selection_load_font (fontsel);
924 /* If the user hits return in the font size entry, we change to the new font
925 size. */
926 static void
927 dia_gtk_font_selection_size_activate (GtkWidget *w,
928 gpointer data)
930 DiaGtkFontSelection *fontsel;
931 gint new_size;
932 const gchar *text;
934 fontsel = DIA_GTK_FONT_SELECTION (data);
936 text = gtk_entry_get_text (GTK_ENTRY (fontsel->size_entry));
937 new_size = MAX (0.1, atof (text) * PANGO_SCALE + 0.5);
939 dia_gtk_font_selection_set_size (fontsel, new_size);
942 static gboolean
943 dia_gtk_font_selection_size_focus_out (GtkWidget *w,
944 GdkEventFocus *event,
945 gpointer data)
947 dia_gtk_font_selection_size_activate (w, data);
949 return TRUE;
952 /* This is called when a size is selected in the list. */
953 static void
954 dia_gtk_font_selection_select_size (GtkTreeSelection *selection,
955 gpointer data)
957 DiaGtkFontSelection *fontsel;
958 GtkTreeModel *model;
959 GtkTreeIter iter;
960 gint new_size;
962 fontsel = DIA_GTK_FONT_SELECTION (data);
964 if (gtk_tree_selection_get_selected (selection, &model, &iter))
966 gtk_tree_model_get (model, &iter, SIZE_COLUMN, &new_size, -1);
967 dia_gtk_font_selection_set_size (fontsel, new_size * PANGO_SCALE);
971 static void
972 dia_gtk_font_selection_load_font (DiaGtkFontSelection *fontsel)
974 dia_gtk_font_selection_update_preview (fontsel);
977 static PangoFontDescription *
978 dia_gtk_font_selection_get_font_description (DiaGtkFontSelection *fontsel)
980 PangoFontDescription *font_desc = pango_font_face_describe (fontsel->face);
981 pango_font_description_set_size (font_desc, fontsel->size);
983 return font_desc;
986 /* This sets the font in the preview entry to the selected font, and tries to
987 make sure that the preview entry is a reasonable size, i.e. so that the
988 text can be seen with a bit of space to spare. But it tries to avoid
989 resizing the entry every time the font changes.
990 This also used to shrink the preview if the font size was decreased, but
991 that made it awkward if the user wanted to resize the window themself. */
992 static void
993 dia_gtk_font_selection_update_preview (DiaGtkFontSelection *fontsel)
995 GtkRcStyle *rc_style;
996 gint new_height;
997 GtkRequisition old_requisition;
998 GtkWidget *preview_entry = fontsel->preview_entry;
999 const gchar *text;
1001 gtk_widget_get_child_requisition (preview_entry, &old_requisition);
1003 rc_style = gtk_rc_style_new ();
1004 rc_style->font_desc = dia_gtk_font_selection_get_font_description (fontsel);
1006 gtk_widget_modify_style (preview_entry, rc_style);
1007 gtk_rc_style_unref (rc_style);
1009 gtk_widget_size_request (preview_entry, NULL);
1011 /* We don't ever want to be over MAX_PREVIEW_HEIGHT pixels high. */
1012 new_height = CLAMP (preview_entry->requisition.height, INITIAL_PREVIEW_HEIGHT, MAX_PREVIEW_HEIGHT);
1014 if (new_height > old_requisition.height || new_height < old_requisition.height - 30)
1015 gtk_widget_set_usize (preview_entry, -1, new_height);
1017 /* This sets the preview text, if it hasn't been set already. */
1018 text = gtk_entry_get_text (GTK_ENTRY (preview_entry));
1019 if (strlen (text) == 0)
1020 gtk_entry_set_text (GTK_ENTRY (preview_entry), _(PREVIEW_TEXT));
1021 gtk_entry_set_position (GTK_ENTRY (preview_entry), 0);
1024 /*****************************************************************************
1025 * These functions are the main public interface for getting/setting the font.
1026 *****************************************************************************/
1029 gchar *
1030 dia_gtk_font_selection_get_font_name (DiaGtkFontSelection *fontsel)
1032 gchar *result;
1034 PangoFontDescription *font_desc = dia_gtk_font_selection_get_font_description (fontsel);
1035 result = pango_font_description_to_string (font_desc);
1036 pango_font_description_free (font_desc);
1038 return result;
1042 /* This sets the current font, selecting the appropriate list rows.
1043 First we check the fontname is valid and try to find the font family
1044 - i.e. the name in the main list. If we can't find that, then just return.
1045 Next we try to set each of the properties according to the fontname.
1046 Finally we select the font family & style in the lists. */
1047 gboolean
1048 dia_gtk_font_selection_set_font_name (DiaGtkFontSelection *fontsel,
1049 const gchar *fontname)
1051 PangoFontFamily *new_family = NULL;
1052 PangoFontFace *new_face = NULL;
1053 PangoFontFace *fallback_face = NULL;
1054 PangoFontDescription *new_desc;
1055 GtkTreeModel *model;
1056 GtkTreeIter iter;
1057 GtkTreeIter match_iter;
1058 gboolean valid;
1060 g_return_val_if_fail (DIA_GTK_IS_FONT_SELECTION (fontsel), FALSE);
1062 new_desc = pango_font_description_from_string (fontname);
1064 /* Check to make sure that this is in the list of allowed fonts */
1066 model = gtk_tree_view_get_model (GTK_TREE_VIEW (fontsel->family_list));
1067 for (valid = gtk_tree_model_get_iter_first (model, &iter);
1068 valid;
1069 valid = gtk_tree_model_iter_next (model, &iter))
1071 PangoFontFamily *family;
1073 gtk_tree_model_get (model, &iter, FAMILY_COLUMN, &family, -1);
1075 if (g_ascii_strcasecmp (pango_font_family_get_name (family),
1076 pango_font_description_get_family (new_desc)) == 0)
1077 new_family = family;
1079 g_object_unref (family);
1081 if (new_family)
1082 break;
1085 if (!new_family)
1086 return FALSE;
1088 fontsel->family = new_family;
1089 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->family_list), &iter);
1090 dia_gtk_font_selection_show_available_styles (fontsel);
1092 model = gtk_tree_view_get_model (GTK_TREE_VIEW (fontsel->face_list));
1093 for (valid = gtk_tree_model_get_iter_first (model, &iter);
1094 valid;
1095 valid = gtk_tree_model_iter_next (model, &iter))
1097 PangoFontFace *face;
1098 PangoFontDescription *tmp_desc;
1100 gtk_tree_model_get (model, &iter, FACE_COLUMN, &face, -1);
1101 tmp_desc = pango_font_face_describe (face);
1103 if (font_description_style_equal (tmp_desc, new_desc))
1104 new_face = face;
1106 if (!fallback_face)
1108 fallback_face = face;
1109 match_iter = iter;
1112 pango_font_description_free (tmp_desc);
1113 g_object_unref (face);
1115 if (new_face)
1117 match_iter = iter;
1118 break;
1122 if (!new_face)
1123 new_face = fallback_face;
1125 fontsel->face = new_face;
1126 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->face_list), &match_iter);
1128 dia_gtk_font_selection_set_size (fontsel, pango_font_description_get_size (new_desc));
1130 g_object_freeze_notify (G_OBJECT (fontsel));
1131 g_object_notify (G_OBJECT (fontsel), "font_name");
1132 g_object_notify (G_OBJECT (fontsel), "font");
1133 g_object_thaw_notify (G_OBJECT (fontsel));
1135 pango_font_description_free (new_desc);
1137 return TRUE;
1141 /* This returns the text in the preview entry. You should copy the returned
1142 text if you need it. */
1143 G_CONST_RETURN gchar*
1144 dia_gtk_font_selection_get_preview_text (DiaGtkFontSelection *fontsel)
1146 return gtk_entry_get_text (GTK_ENTRY (fontsel->preview_entry));
1150 /* This sets the text in the preview entry. */
1151 void
1152 dia_gtk_font_selection_set_preview_text (DiaGtkFontSelection *fontsel,
1153 const gchar *text)
1155 gtk_entry_set_text (GTK_ENTRY (fontsel->preview_entry), text);
1158 void
1159 dia_gtk_font_selection_set_context (DiaGtkFontSelection *fs,
1160 const PangoContext *context)
1162 fs->context = context;
1163 dia_gtk_font_selection_show_available_fonts (fs);
1166 /*****************************************************************************
1167 * DiaGtkFontSelectionDialog
1168 *****************************************************************************/
1170 GtkType
1171 dia_gtk_font_selection_dialog_get_type (void)
1173 static GtkType font_selection_dialog_type = 0;
1175 if (!font_selection_dialog_type)
1177 GtkTypeInfo fontsel_diag_info =
1179 "DiaGtkFontSelectionDialog",
1180 sizeof (DiaGtkFontSelectionDialog),
1181 sizeof (DiaGtkFontSelectionDialogClass),
1182 (GtkClassInitFunc) dia_gtk_font_selection_dialog_class_init,
1183 (GtkObjectInitFunc) dia_gtk_font_selection_dialog_init,
1184 /* reserved_1 */ NULL,
1185 /* reserved_2 */ NULL,
1186 (GtkClassInitFunc) NULL
1189 font_selection_dialog_type = gtk_type_unique (GTK_TYPE_DIALOG,
1190 &fontsel_diag_info);
1193 return font_selection_dialog_type;
1196 static void
1197 dia_gtk_font_selection_dialog_class_init (DiaGtkFontSelectionDialogClass *klass)
1199 GtkObjectClass *object_class;
1201 object_class = (GtkObjectClass*) klass;
1203 font_selection_dialog_parent_class = gtk_type_class (GTK_TYPE_DIALOG);
1206 static void
1207 dia_gtk_font_selection_dialog_init (DiaGtkFontSelectionDialog *fontseldiag)
1209 GtkDialog *dialog;
1211 gtk_widget_push_composite_child ();
1213 dialog = GTK_DIALOG (fontseldiag);
1215 fontseldiag->dialog_width = -1;
1216 fontseldiag->auto_resize = TRUE;
1218 gtk_widget_set_events (GTK_WIDGET (fontseldiag), GDK_STRUCTURE_MASK);
1219 gtk_signal_connect (GTK_OBJECT (fontseldiag), "configure_event",
1220 (GtkSignalFunc) dia_gtk_font_selection_dialog_on_configure,
1221 fontseldiag);
1223 gtk_container_set_border_width (GTK_CONTAINER (fontseldiag), 4);
1224 gtk_window_set_policy (GTK_WINDOW (fontseldiag), FALSE, TRUE, TRUE);
1226 fontseldiag->main_vbox = dialog->vbox;
1228 fontseldiag->fontsel = dia_gtk_font_selection_new ();
1229 gtk_container_set_border_width (GTK_CONTAINER (fontseldiag->fontsel), 4);
1230 gtk_widget_show (fontseldiag->fontsel);
1231 gtk_box_pack_start (GTK_BOX (fontseldiag->main_vbox),
1232 fontseldiag->fontsel, TRUE, TRUE, 0);
1234 /* Create the action area */
1235 fontseldiag->action_area = dialog->action_area;
1237 fontseldiag->cancel_button = gtk_dialog_add_button (dialog,
1238 GTK_STOCK_CANCEL,
1239 GTK_RESPONSE_CANCEL);
1241 fontseldiag->apply_button = gtk_dialog_add_button (dialog,
1242 GTK_STOCK_APPLY,
1243 GTK_RESPONSE_APPLY);
1244 gtk_widget_hide (fontseldiag->apply_button);
1246 fontseldiag->ok_button = gtk_dialog_add_button (dialog,
1247 GTK_STOCK_OK,
1248 GTK_RESPONSE_OK);
1249 gtk_widget_grab_default (fontseldiag->ok_button);
1251 gtk_window_set_title (GTK_WINDOW (fontseldiag),
1252 _("Font Selection"));
1254 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
1256 gtk_widget_pop_composite_child ();
1259 GtkWidget*
1260 dia_gtk_font_selection_dialog_new (const gchar *title)
1262 DiaGtkFontSelectionDialog *fontseldiag;
1264 fontseldiag = gtk_type_new (DIA_GTK_TYPE_FONT_SELECTION_DIALOG);
1266 if (title)
1267 gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
1269 return GTK_WIDGET (fontseldiag);
1272 gchar*
1273 dia_gtk_font_selection_dialog_get_font_name (DiaGtkFontSelectionDialog *fsd)
1275 return dia_gtk_font_selection_get_font_name (DIA_GTK_FONT_SELECTION (fsd->fontsel));
1278 gboolean
1279 dia_gtk_font_selection_dialog_set_font_name (DiaGtkFontSelectionDialog *fsd,
1280 const gchar *fontname)
1282 return dia_gtk_font_selection_set_font_name (DIA_GTK_FONT_SELECTION (fsd->fontsel), fontname);
1285 G_CONST_RETURN gchar*
1286 dia_gtk_font_selection_dialog_get_preview_text (DiaGtkFontSelectionDialog *fsd)
1288 return dia_gtk_font_selection_get_preview_text (DIA_GTK_FONT_SELECTION (fsd->fontsel));
1291 void
1292 dia_gtk_font_selection_dialog_set_preview_text (DiaGtkFontSelectionDialog *fsd,
1293 const gchar *text)
1295 dia_gtk_font_selection_set_preview_text (DIA_GTK_FONT_SELECTION (fsd->fontsel), text);
1298 void
1299 dia_gtk_font_selection_dialog_set_context (DiaGtkFontSelectionDialog *fsd,
1300 const PangoContext *context)
1302 dia_gtk_font_selection_set_context (DIA_GTK_FONT_SELECTION (fsd->fontsel),
1303 context);
1306 /* This turns auto-shrink off if the user resizes the width of the dialog.
1307 It also turns it back on again if the user resizes it back to its normal
1308 width. */
1309 static gint
1310 dia_gtk_font_selection_dialog_on_configure (GtkWidget *widget,
1311 GdkEventConfigure *event,
1312 DiaGtkFontSelectionDialog *fsd)
1314 /* This sets the initial width. */
1315 if (fsd->dialog_width == -1)
1316 fsd->dialog_width = event->width;
1317 else if (fsd->auto_resize && fsd->dialog_width != event->width)
1319 fsd->auto_resize = FALSE;
1320 gtk_window_set_policy (GTK_WINDOW (fsd), FALSE, TRUE, FALSE);
1322 else if (!fsd->auto_resize && fsd->dialog_width == event->width)
1324 fsd->auto_resize = TRUE;
1325 gtk_window_set_policy (GTK_WINDOW (fsd), FALSE, TRUE, TRUE);
1328 return FALSE;