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.
39 #undef GTK_DISABLE_DEPRECATED /* GtkTypeInfo */
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"
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
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
115 static void dia_gtk_font_selection_class_init (DiaGtkFontSelectionClass
*klass
);
116 static void dia_gtk_font_selection_set_property (GObject
*object
,
120 static void dia_gtk_font_selection_get_property (GObject
*object
,
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
,
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
,
135 static void dia_gtk_font_selection_select_style (GtkTreeSelection
*selection
,
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
,
143 static gboolean
dia_gtk_font_selection_size_focus_out (GtkWidget
*w
,
144 GdkEventFocus
*event
,
146 static void dia_gtk_font_selection_select_size (GtkTreeSelection
*selection
,
149 static void dia_gtk_font_selection_scroll_on_map (GtkWidget
*w
,
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
;
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
,
193 return font_selection_type
;
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
,
208 g_param_spec_string ("font_name",
210 _("The X string that represents this font."),
213 g_object_class_install_property (gobject_class
,
215 g_param_spec_string ("preview_text",
217 _("The text to display in order to demonstrate the selected font."),
220 gobject_class
->finalize
= dia_gtk_font_selection_finalize
;
224 dia_gtk_font_selection_set_property (GObject
*object
,
229 DiaGtkFontSelection
*fontsel
;
231 fontsel
= DIA_GTK_FONT_SELECTION (object
);
236 dia_gtk_font_selection_set_font_name (fontsel
, g_value_get_string (value
));
238 case PROP_PREVIEW_TEXT
:
239 dia_gtk_font_selection_set_preview_text (fontsel
, g_value_get_string (value
));
242 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
247 static void dia_gtk_font_selection_get_property (GObject
*object
,
252 DiaGtkFontSelection
*fontsel
;
254 fontsel
= DIA_GTK_FONT_SELECTION (object
);
259 g_value_set_string (value
, dia_gtk_font_selection_get_font_name (fontsel
));
261 case PROP_PREVIEW_TEXT
:
262 g_value_set_string (value
, dia_gtk_font_selection_get_preview_text (fontsel
));
265 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
272 dia_gtk_font_selection_init (DiaGtkFontSelection
*fontsel
)
274 GtkWidget
*scrolled_win
;
275 GtkWidget
*text_frame
;
277 GtkWidget
*table
, *label
;
278 GtkWidget
*font_label
, *style_label
;
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,
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,
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,
314 gtk_signal_connect (GTK_OBJECT (fontsel
->size_entry
), "activate",
315 (GtkSignalFunc
) dia_gtk_font_selection_size_activate
,
317 gtk_signal_connect_after (GTK_OBJECT (fontsel
->size_entry
), "focus_out_event",
318 (GtkSignalFunc
) dia_gtk_font_selection_size_focus_out
,
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,
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,
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,
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
,
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
,
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 (),
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
),
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
,
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
,
486 gtk_widget_set_usize (fontsel
->preview_entry
, -1, INITIAL_PREVIEW_HEIGHT
);
487 gtk_box_pack_start (GTK_BOX (text_box
), fontsel
->preview_entry
,
490 dia_gtk_font_selection_update_preview (fontsel
);
492 gtk_widget_pop_composite_child();
496 dia_gtk_font_selection_new ()
498 DiaGtkFontSelection
*fontsel
;
500 fontsel
= gtk_type_new (DIA_GTK_TYPE_FONT_SELECTION
);
502 return GTK_WIDGET (fontsel
);
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
);
519 dia_gtk_font_selection_preview_changed (GtkWidget
*entry
,
520 DiaGtkFontSelection
*fontsel
)
522 g_object_notify (G_OBJECT (fontsel
), "preview_text");
526 scroll_to_selection (GtkTreeView
*tree_view
)
528 GtkTreeSelection
*selection
= gtk_tree_view_get_selection (tree_view
);
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
);
541 set_cursor_to_iter (GtkTreeView
*view
,
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. */
555 dia_gtk_font_selection_scroll_on_map (GtkWidget
*widget
,
558 DiaGtkFontSelection
*fontsel
;
561 g_message ("In expose_list\n");
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. */
577 dia_gtk_font_selection_select_font (GtkTreeSelection
*selection
,
580 DiaGtkFontSelection
*fontsel
;
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
);
601 dia_gtk_font_selection_show_available_styles (fontsel
);
602 dia_gtk_font_selection_select_best_style (fontsel
, TRUE
);
605 g_object_unref (family
);
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
);
619 dia_gtk_font_selection_show_available_fonts (DiaGtkFontSelection
*fontsel
)
622 PangoFontFamily
**families
;
623 PangoFontFamily
*match_family
= NULL
;
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
]);
642 gtk_list_store_append (model
, &iter
);
643 gtk_list_store_set (model
, &iter
,
644 FAMILY_COLUMN
, families
[i
],
645 FAMILY_NAME_COLUMN
, name
,
648 if (i
== 0 || !g_ascii_strcasecmp (name
, "sans"))
650 match_family
= families
[i
];
655 fontsel
->family
= 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 */
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
));
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
);
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
);
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. */
717 dia_gtk_font_selection_show_available_styles (DiaGtkFontSelection
*fontsel
)
720 PangoFontFace
**faces
;
721 PangoFontDescription
*old_desc
;
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
)));
729 old_desc
= pango_font_face_describe (fontsel
->face
);
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
++)
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
,
752 match_face
= faces
[i
];
756 PangoFontDescription
*tmp_desc
= pango_font_face_describe (faces
[i
]);
758 if (font_description_style_equal (tmp_desc
, old_desc
))
761 match_face
= faces
[i
];
764 pango_font_description_free (tmp_desc
);
769 pango_font_description_free (old_desc
);
771 fontsel
->face
= 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
);
779 set_cursor_to_iter (GTK_TREE_VIEW (fontsel
->face_list
), &match_row
);
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. */
791 dia_gtk_font_selection_select_best_style (DiaGtkFontSelection
*fontsel
,
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. */
812 dia_gtk_font_selection_select_style (GtkTreeSelection
*selection
,
815 DiaGtkFontSelection
*fontsel
= DIA_GTK_FONT_SELECTION (data
);
819 if (gtk_tree_selection_get_selected (selection
, &model
, &iter
))
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
);
834 dia_gtk_font_selection_show_available_sizes (DiaGtkFontSelection
*fontsel
,
839 GtkTreeSelection
*selection
;
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 */
849 gtk_list_store_clear (model
);
851 for (i
= 0; i
< G_N_ELEMENTS (font_sizes
); i
++)
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
);
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
);
876 gtk_tree_model_iter_next (GTK_TREE_MODEL (model
), &iter
);
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;
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
);
906 dia_gtk_font_selection_select_best_size (DiaGtkFontSelection
*fontsel
)
908 dia_gtk_font_selection_load_font (fontsel
);
912 dia_gtk_font_selection_set_size (DiaGtkFontSelection
*fontsel
,
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
927 dia_gtk_font_selection_size_activate (GtkWidget
*w
,
930 DiaGtkFontSelection
*fontsel
;
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
);
943 dia_gtk_font_selection_size_focus_out (GtkWidget
*w
,
944 GdkEventFocus
*event
,
947 dia_gtk_font_selection_size_activate (w
, data
);
952 /* This is called when a size is selected in the list. */
954 dia_gtk_font_selection_select_size (GtkTreeSelection
*selection
,
957 DiaGtkFontSelection
*fontsel
;
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
);
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
);
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. */
993 dia_gtk_font_selection_update_preview (DiaGtkFontSelection
*fontsel
)
995 GtkRcStyle
*rc_style
;
997 GtkRequisition old_requisition
;
998 GtkWidget
*preview_entry
= fontsel
->preview_entry
;
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 *****************************************************************************/
1030 dia_gtk_font_selection_get_font_name (DiaGtkFontSelection
*fontsel
)
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
);
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. */
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
;
1057 GtkTreeIter match_iter
;
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
);
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
);
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
);
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
))
1108 fallback_face
= face
;
1112 pango_font_description_free (tmp_desc
);
1113 g_object_unref (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
);
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. */
1152 dia_gtk_font_selection_set_preview_text (DiaGtkFontSelection
*fontsel
,
1155 gtk_entry_set_text (GTK_ENTRY (fontsel
->preview_entry
), text
);
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 *****************************************************************************/
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
;
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
);
1207 dia_gtk_font_selection_dialog_init (DiaGtkFontSelectionDialog
*fontseldiag
)
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
,
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
,
1239 GTK_RESPONSE_CANCEL
);
1241 fontseldiag
->apply_button
= gtk_dialog_add_button (dialog
,
1243 GTK_RESPONSE_APPLY
);
1244 gtk_widget_hide (fontseldiag
->apply_button
);
1246 fontseldiag
->ok_button
= gtk_dialog_add_button (dialog
,
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 ();
1260 dia_gtk_font_selection_dialog_new (const gchar
*title
)
1262 DiaGtkFontSelectionDialog
*fontseldiag
;
1264 fontseldiag
= gtk_type_new (DIA_GTK_TYPE_FONT_SELECTION_DIALOG
);
1267 gtk_window_set_title (GTK_WINDOW (fontseldiag
), title
);
1269 return GTK_WIDGET (fontseldiag
);
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
));
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
));
1292 dia_gtk_font_selection_dialog_set_preview_text (DiaGtkFontSelectionDialog
*fsd
,
1295 dia_gtk_font_selection_set_preview_text (DIA_GTK_FONT_SELECTION (fsd
->fontsel
), text
);
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
),
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
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
);