gtkfontchooser.c 41.6 KB
Newer Older
1
/* GTK - The GIMP Toolkit
2
 * Copyright (C) 2011 Alberto Ruiz <aruiz@gnome.org>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <stdlib.h>
#include <glib/gprintf.h>
#include <string.h>

#include <atk/atk.h>

28
#include "gtkfontchooser.h"
29 30 31 32 33 34 35 36
#include "gtkcellrenderertext.h"
#include "gtkentry.h"
#include "gtkframe.h"
#include "gtkhbbox.h"
#include "gtkhbox.h"
#include "gtklabel.h"
#include "gtkliststore.h"
#include "gtkstock.h"
37
#include "gtktextview.h"
38 39
#include "gtktreeselection.h"
#include "gtktreeview.h"
40
#include "gtkbox.h"
41 42 43 44 45
#include "gtkscrolledwindow.h"
#include "gtkintl.h"
#include "gtkaccessible.h"
#include "gtkbuildable.h"
#include "gtkprivate.h"
46 47
#include "gtkalignment.h"
#include "gtkscale.h"
48
#include "gtkbox.h"
49
#include "gtkspinbutton.h"
50
#include "gtknotebook.h"
51
#include "gtkwidget.h"
52
#include "gtkgrid.h"
53 54

/**
55
 * SECTION:gtkfontchooser
56
 * @Short_description: A widget for selecting fonts
57 58
 * @Title: GtkFontChooser
 * @See_also: #GtkFontChooserDialog
59
 *
60
 * The #GtkFontChooser widget lists the available fonts, styles and sizes,
61
 * allowing the user to select a font.
62
 * It is used in the #GtkFontChooserDialog widget to provide a dialog box for
63 64 65
 * selecting fonts.
 *
 * To set the font which is initially selected, use
66
 * gtk_font_chooser_set_font_name().
67
 *
68
 * To get the selected font use gtk_font_chooser_get_font_name().
69 70
 *
 * To change the text which is shown in the preview area, use
71
 * gtk_font_chooser_set_preview_text().
72 73
 *
 * Since: 3.2
74 75 76
 */


77
struct _GtkFontChooserPrivate
78
{
79 80
  GtkWidget    *search_entry;
  GtkWidget    *family_face_list;
81 82
  GtkWidget    *list_scrolled_window;
  GtkWidget    *empty_list;
83
  GtkWidget    *list_notebook;
Matthias Clasen's avatar
Matthias Clasen committed
84
  GtkListStore *model;
85
  GtkTreeModel *filter_model;
86

87 88 89 90 91 92 93
  GtkWidget       *preview;
  gchar           *preview_text;
  gboolean         show_preview_entry;

  GtkWidget *size_spin;
  GtkWidget *size_slider;

94 95 96
  gint             size;
  PangoFontFace   *face;
  PangoFontFamily *family;
97 98

  gulong           cursor_changed_handler;
99 100 101 102

  GtkFontFilterFunc filter_func;
  gpointer          filter_data;
  GDestroyNotify    filter_data_destroy;
103 104 105
};

#define DEFAULT_FONT_NAME "Sans 10"
106
#define MAX_FONT_SIZE 999
107

108
/* This is the initial fixed height and the top padding of the preview entry */
109
#define PREVIEW_HEIGHT 72
110
#define PREVIEW_TOP_PADDING 6
111 112

/* These are the sizes of the font, style & size lists. */
113 114 115 116
#define FONT_LIST_HEIGHT  136
#define FONT_LIST_WIDTH   190
#define FONT_STYLE_LIST_WIDTH 170
#define FONT_SIZE_LIST_WIDTH  60
117

118
#define ROW_FORMAT_STRING "<span weight=\"bold\" size=\"small\">%s</span>\n<span size=\"x-large\" font_desc=\"%s\">%s</span>"
119

Matthias Clasen's avatar
Matthias Clasen committed
120
#define NO_FONT_MATCHED_SEARCH "No fonts matched your search. You can revise your search and try again."
121

122 123
/* These are what we use as the standard font sizes, for the size list.
 */
124
static const gint font_sizes[] = {
125
  6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
126 127 128 129 130
};

enum {
   PROP_0,
   PROP_FONT_NAME,
131 132
   PROP_PREVIEW_TEXT,
   PROP_SHOW_PREVIEW_ENTRY
133 134 135 136 137 138
};


enum {
  FAMILY_COLUMN,
  FACE_COLUMN,
139
  PREVIEW_TEXT_COLUMN,
140
  PREVIEW_TITLE_COLUMN
141 142
};

143 144 145 146 147 148 149 150 151
static void  gtk_font_chooser_set_property       (GObject         *object,
                                                  guint            prop_id,
                                                  const GValue    *value,
                                                  GParamSpec      *pspec);
static void  gtk_font_chooser_get_property       (GObject         *object,
                                                  guint            prop_id,
                                                  GValue          *value,
                                                  GParamSpec      *pspec);
static void  gtk_font_chooser_finalize           (GObject         *object);
152
static void  gtk_font_chooser_dispose            (GObject         *object);
153

154 155
static void  gtk_font_chooser_screen_changed     (GtkWidget       *widget,
                                                  GdkScreen       *previous_screen);
156
static void  gtk_font_chooser_style_updated      (GtkWidget       *widget);
157

158
static void gtk_font_chooser_bootstrap_fontlist (GtkFontChooser *fontchooser);
159

160
G_DEFINE_TYPE (GtkFontChooser, gtk_font_chooser, GTK_TYPE_BOX)
161 162

static void
163
gtk_font_chooser_class_init (GtkFontChooserClass *klass)
164 165
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
166
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
167

168 169
  widget_class->screen_changed = gtk_font_chooser_screen_changed;
  widget_class->style_updated = gtk_font_chooser_style_updated;
170

171
  gobject_class->dispose = gtk_font_chooser_dispose;
172 173 174
  gobject_class->finalize = gtk_font_chooser_finalize;
  gobject_class->set_property = gtk_font_chooser_set_property;
  gobject_class->get_property = gtk_font_chooser_get_property;
175 176 177 178 179 180 181 182 183 184 185 186 187

  g_object_class_install_property (gobject_class,
                                   PROP_FONT_NAME,
                                   g_param_spec_string ("font-name",
                                                        P_("Font name"),
                                                        P_("The string that represents this font"),
                                                        DEFAULT_FONT_NAME,
                                                        GTK_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
                                   PROP_PREVIEW_TEXT,
                                   g_param_spec_string ("preview-text",
                                                        P_("Preview text"),
                                                        P_("The text to display in order to demonstrate the selected font"),
188
                                                        pango_language_get_sample_string (NULL),
189 190
                                                        GTK_PARAM_READWRITE));

191 192 193 194 195 196 197 198
  g_object_class_install_property (gobject_class,
                                   PROP_SHOW_PREVIEW_ENTRY,
                                   g_param_spec_boolean ("show-preview-entry",
                                                        P_("Show preview text entry"),
                                                        P_("Whether the preview text entry is shown or not"),
                                                        TRUE,
                                                        GTK_PARAM_READWRITE));

199
  g_type_class_add_private (klass, sizeof (GtkFontChooserPrivate));
200 201
}

Matthias Clasen's avatar
Matthias Clasen committed
202
static void
203
gtk_font_chooser_set_property (GObject         *object,
204 205 206
                               guint            prop_id,
                               const GValue    *value,
                               GParamSpec      *pspec)
207
{
208
  GtkFontChooser *fontchooser;
209

210
  fontchooser = GTK_FONT_CHOOSER (object);
211 212 213 214

  switch (prop_id)
    {
    case PROP_FONT_NAME:
215
      gtk_font_chooser_set_font_name (fontchooser, g_value_get_string (value));
216 217
      break;
    case PROP_PREVIEW_TEXT:
218
      gtk_font_chooser_set_preview_text (fontchooser, g_value_get_string (value));
219
      break;
220
    case PROP_SHOW_PREVIEW_ENTRY:
221
      gtk_font_chooser_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
222
      break;
223 224 225 226 227 228
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

229
static void
230
gtk_font_chooser_get_property (GObject         *object,
231 232 233
                               guint            prop_id,
                               GValue          *value,
                               GParamSpec      *pspec)
234
{
235
  GtkFontChooser *fontchooser;
236

237
  fontchooser = GTK_FONT_CHOOSER (object);
238 239 240 241

  switch (prop_id)
    {
    case PROP_FONT_NAME:
242
      g_value_take_string (value, gtk_font_chooser_get_font_name (fontchooser));
243 244
      break;
    case PROP_PREVIEW_TEXT:
245
      g_value_set_string (value, gtk_font_chooser_get_preview_text (fontchooser));
246
      break;
247
    case PROP_SHOW_PREVIEW_ENTRY:
248
      g_value_set_boolean (value, gtk_font_chooser_get_show_preview_entry (fontchooser));
249
      break;
250 251 252 253 254
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}
Matthias Clasen's avatar
Matthias Clasen committed
255

256
static void
Matthias Clasen's avatar
Matthias Clasen committed
257 258 259
text_changed_cb (GtkEntry       *entry,
                 GParamSpec     *pspec,
                 GtkFontChooser *fc)
260
{
Matthias Clasen's avatar
Matthias Clasen committed
261 262 263 264
  GtkFontChooserPrivate *priv = fc->priv;
  const gchar *text;

  text = gtk_entry_get_text (entry);
Matthias Clasen's avatar
Matthias Clasen committed
265

Matthias Clasen's avatar
Matthias Clasen committed
266
  if (text == NULL || text[0] == '\0')
267
    {
Matthias Clasen's avatar
Matthias Clasen committed
268 269 270 271 272 273 274 275
      GIcon *icon;

      icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
      g_object_set (G_OBJECT (priv->search_entry),
                    "secondary-icon-gicon", icon,
                    "secondary-icon-activatable", FALSE,
                    "secondary-icon-sensitive", FALSE,
                    NULL);
276
      g_object_unref (icon);
277
    }
Matthias Clasen's avatar
Matthias Clasen committed
278
  else
279
    {
Matthias Clasen's avatar
Matthias Clasen committed
280 281 282 283 284 285 286 287 288 289 290 291
      if (!gtk_entry_get_icon_activatable (GTK_ENTRY (priv->search_entry), GTK_ENTRY_ICON_SECONDARY))
        {
          GIcon *icon;

          icon = g_themed_icon_new_with_default_fallbacks ("edit-clear-symbolic");
          g_object_set (G_OBJECT (priv->search_entry),
                        "secondary-icon-gicon", icon,
                        "secondary-icon-activatable", TRUE,
                        "secondary-icon-sensitive", TRUE,
                        NULL);
          g_object_unref (icon);
        }
292
    }
293

294
  gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
295 296
}

297
static void
298 299 300 301 302
icon_press_cb (GtkEntry             *entry,
               GtkEntryIconPosition  pos,
               GdkEvent             *event,
               gpointer              user_data)
{
Matthias Clasen's avatar
Matthias Clasen committed
303
  gtk_entry_set_text (entry, "");
304 305
}

306
static void
307 308
slider_change_cb (GtkAdjustment *adjustment,
                  gpointer       user_data)
309
{
310 311
  GtkFontChooser        *fc    = (GtkFontChooser*)user_data;
  GtkFontChooserPrivate *priv  = fc->priv;
312 313 314 315 316 317 318
  GtkAdjustment         *spin_adj     = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON (priv->size_spin));
  gdouble                slider_value = gtk_adjustment_get_value (adjustment);
  gdouble                spin_value   = gtk_adjustment_get_value (spin_adj);

  if (slider_value != spin_value)
    gtk_adjustment_set_value (spin_adj,
                              gtk_adjustment_get_value (adjustment));
319 320
}

321
static void
322 323
spin_change_cb (GtkAdjustment *adjustment,
                gpointer       user_data)
324
{
325
  PangoFontDescription    *desc;
326
  GtkFontChooser          *fontchooser = (GtkFontChooser*)user_data;
327
  GtkFontChooserPrivate   *priv        = fontchooser->priv;
328
  GtkAdjustment           *slider_adj  = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
329 330

  gdouble size = gtk_adjustment_get_value (adjustment);
331 332 333 334 335
  priv->size = ((gint)size) * PANGO_SCALE;

  desc = pango_context_get_font_description (gtk_widget_get_pango_context (priv->preview));
  pango_font_description_set_size (desc, priv->size);
  gtk_widget_override_font (priv->preview, desc);
336

337
  g_object_notify (G_OBJECT (fontchooser), "font-name");
338 339 340 341 342 343

  /* If the new value is lower than the lower bound of the slider, we set
   * the slider adjustment to the lower bound value if it is not already set
   */
  if (size < gtk_adjustment_get_lower (slider_adj) &&
      gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_lower (slider_adj))
344
    gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_lower (slider_adj));
345 346 347 348 349 350

  /* If the new value is upper than the upper bound of the slider, we set
   * the slider adjustment to the upper bound value if it is not already set
   */
  else if (size > gtk_adjustment_get_upper (slider_adj) &&
           gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_upper (slider_adj))
351
    gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_upper (slider_adj));
352 353 354

  /* If the new value is not already set on the slider we set it */
  else if (size != gtk_adjustment_get_value (slider_adj))
355
    gtk_adjustment_set_value (slider_adj, size);
356

357
  gtk_widget_queue_draw (priv->preview);
358 359
}

360
static void
361
set_range_marks (GtkFontChooserPrivate *priv,
362 363 364
                 GtkWidget             *size_slider,
                 gint                  *sizes,
                 gint                   length)
365
{
366
  GtkAdjustment *adj;
367
  gint i;
368
  gdouble value;
369

370
  if (length < 2)
371 372
    {
      sizes = (gint*)font_sizes;
373
      length = G_N_ELEMENTS (font_sizes);
374
    }
Matthias Clasen's avatar
Matthias Clasen committed
375

376
  gtk_scale_clear_marks (GTK_SCALE (size_slider));
Matthias Clasen's avatar
Matthias Clasen committed
377

378
  adj = gtk_range_get_adjustment(GTK_RANGE (size_slider));
Matthias Clasen's avatar
Matthias Clasen committed
379

380 381 382 383 384
  gtk_adjustment_set_lower (adj, (gdouble) sizes[0]);
  gtk_adjustment_set_upper (adj, (gdouble) sizes[length-1]);

  value = gtk_adjustment_get_value (adj);
  if (value > (gdouble) sizes[length-1])
385
    gtk_adjustment_set_value (adj, (gdouble) sizes[length-1]);
386
  else if (value < (gdouble) sizes[0])
387
    gtk_adjustment_set_value (adj, (gdouble) sizes[0]);
Matthias Clasen's avatar
Matthias Clasen committed
388

389
  for (i = 0; i < length; i++)
390 391 392
    gtk_scale_add_mark (GTK_SCALE (size_slider),
                        (gdouble) sizes[i],
                        GTK_POS_BOTTOM, NULL);
393 394
}

395
static void
396 397
cursor_changed_cb (GtkTreeView *treeview,
                   gpointer     user_data)
398
{
399 400 401
  GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
  GtkFontChooserPrivate *priv = fontchooser->priv;

402
  PangoFontFamily      *family;
403 404
  PangoFontFace        *face;
  PangoFontDescription *desc;
Matthias Clasen's avatar
Matthias Clasen committed
405

406
  gint *sizes;
407
  gint  i, n_sizes;
408

409
  GtkTreeIter  iter;
410
  GtkTreePath *path = gtk_tree_path_new ();
Matthias Clasen's avatar
Matthias Clasen committed
411

412
  gtk_tree_view_get_cursor (treeview, &path, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
413

414 415 416
  if (!path)
    return;

417
  if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->filter_model), &iter, path))
418 419 420
    {
      gtk_tree_path_free (path);
      return;
Matthias Clasen's avatar
Matthias Clasen committed
421 422 423
    }


424
  gtk_tree_model_get (GTK_TREE_MODEL (priv->filter_model), &iter,
425
                      FACE_COLUMN, &face,
426
                      FAMILY_COLUMN, &family,
427
                      -1);
428

429
  gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
430

431 432
  gtk_tree_path_free (path);
  path = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
433

Matthias Clasen's avatar
Matthias Clasen committed
434
  if (face == NULL || family == NULL)
435
    {
Matthias Clasen's avatar
Matthias Clasen committed
436 437 438 439 440
      if (face)
        g_object_unref (face);
      if (family)
        g_object_unref (family);

441 442 443 444
      return;
    }

  desc = pango_font_face_describe (face);
445 446
  pango_font_description_set_size (desc, priv->size);
  gtk_widget_override_font (priv->preview, desc);
447

448 449
  pango_font_face_list_sizes (face, &sizes, &n_sizes);
  /* It seems not many fonts actually have a sane set of sizes */
450
  for (i = 0; i < n_sizes; i++)
451
    sizes[i] = sizes[i] / PANGO_SCALE;
Matthias Clasen's avatar
Matthias Clasen committed
452

453
  set_range_marks (priv, priv->size_slider, sizes, n_sizes);
454

455 456 457 458 459 460 461
  if (priv->family)
    g_object_unref (priv->family);
  priv->family = family;

  if (priv->face)
    g_object_unref (priv->face);
  priv->face = face;
462

Matthias Clasen's avatar
Matthias Clasen committed
463
  pango_font_description_free (desc);
464

465
  g_object_notify (G_OBJECT (fontchooser), "font-name");
466 467
}

468
static gboolean
469 470
zoom_preview_cb (GtkWidget      *scrolled_window,
                 GdkEventScroll *event,
471
                 gpointer        user_data)
472
{
473 474
  GtkFontChooser        *fc    = (GtkFontChooser*)user_data;
  GtkFontChooserPrivate *priv  = fc->priv;
475 476 477 478 479 480 481 482 483 484 485 486 487 488

  GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));

  if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
    gtk_adjustment_set_value (adj,
                              gtk_adjustment_get_value (adj) +
                              gtk_adjustment_get_step_increment (adj));
  else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
    gtk_adjustment_set_value (adj,
                              gtk_adjustment_get_value (adj) -
                              gtk_adjustment_get_step_increment (adj));
  return TRUE;
}

489 490 491 492 493 494 495 496
static void
row_inserted_cb (GtkTreeModel *model,
                 GtkTreePath  *path,
                 GtkTreeIter  *iter,
                 gpointer      user_data)
{
  GtkFontChooser        *fontchooser = (GtkFontChooser*)user_data;
  GtkFontChooserPrivate *priv        = fontchooser->priv;
Matthias Clasen's avatar
Matthias Clasen committed
497

498
  gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 0);
499 500 501 502 503 504 505 506 507 508 509
}

static void
row_deleted_cb  (GtkTreeModel *model,
                 GtkTreePath  *path,
                 gpointer      user_data)
{
  GtkFontChooser        *fontchooser = (GtkFontChooser*)user_data;
  GtkFontChooserPrivate *priv        = fontchooser->priv;

  if (gtk_tree_model_iter_n_children (model, NULL) == 0)
510
    gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 1);
511 512
}

513
static void
514
gtk_font_chooser_init (GtkFontChooser *fontchooser)
515
{
516
  GIcon                   *icon;
517
  GtkFontChooserPrivate   *priv;
518
  PangoFontDescription    *font_desc;
519
  GtkWidget               *scrolled_win;
520
  GtkWidget               *grid;
521

522 523 524
  fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
                                                   GTK_TYPE_FONT_CHOOSER,
                                                   GtkFontChooserPrivate);
525

526
  priv = fontchooser->priv;
527

528 529
  /* Default preview string  */
  priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
530
  priv->show_preview_entry = TRUE;
531

532
  /* Getting the default size */
533
  font_desc  = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)));
534 535 536 537
  priv->size = pango_font_description_get_size (font_desc);
  priv->face = NULL;
  priv->family = NULL;

538
  gtk_widget_push_composite_child ();
539

540
  /* Creating fundamental widgets for the private struct */
541 542
  priv->search_entry = gtk_entry_new ();
  priv->family_face_list = gtk_tree_view_new ();
543
  priv->preview = gtk_entry_new ();
544
  priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
545
                                                (gdouble) font_sizes[0],
546
                                                (gdouble) font_sizes[G_N_ELEMENTS (font_sizes) - 1],
547
                                                1.0);
548

549
  priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
550

551
  /** Bootstrapping widget layout **/
552
  gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
553

554
  /* Main font family/face view */
555 556
  priv->list_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  scrolled_win = priv->list_scrolled_window;
557 558
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
                                  GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
559 560
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
                                       GTK_SHADOW_ETCHED_IN);
561
  gtk_widget_set_size_request (scrolled_win, 400, 300);
562
  gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
563 564

  /* Text to display when list is empty */
565 566 567 568 569 570 571 572 573 574 575 576
  priv->empty_list = gtk_label_new (_(NO_FONT_MATCHED_SEARCH));
  gtk_widget_set_margin_top    (priv->empty_list, 12);
  gtk_widget_set_margin_left   (priv->empty_list, 12);
  gtk_widget_set_margin_right  (priv->empty_list, 12);
  gtk_widget_set_margin_bottom (priv->empty_list, 12);
  gtk_widget_set_halign (priv->empty_list, GTK_ALIGN_CENTER);
  gtk_widget_set_valign (priv->empty_list, GTK_ALIGN_START);

  priv->list_notebook = gtk_notebook_new ();
  gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv->list_notebook), FALSE);
  gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), scrolled_win, NULL);
  gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), priv->empty_list, NULL);
577

578 579
  /* Basic layout */
  grid = gtk_grid_new ();
Matthias Clasen's avatar
Matthias Clasen committed
580

Matthias Clasen's avatar
Matthias Clasen committed
581
  gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
Matthias Clasen's avatar
Matthias Clasen committed
582
  gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
583

Matthias Clasen's avatar
Matthias Clasen committed
584
  gtk_grid_attach (GTK_GRID (grid), priv->search_entry, 0, 0, 2, 1);
585
  gtk_grid_attach (GTK_GRID (grid), priv->list_notebook, 0, 1, 2, 1);
Matthias Clasen's avatar
Matthias Clasen committed
586
  gtk_grid_attach (GTK_GRID (grid), priv->preview,      0, 2, 2, 1);
Matthias Clasen's avatar
Matthias Clasen committed
587

Matthias Clasen's avatar
Matthias Clasen committed
588 589
  gtk_grid_attach (GTK_GRID (grid), priv->size_slider,  0, 3, 1, 1);
  gtk_grid_attach (GTK_GRID (grid), priv->size_spin,    1, 3, 1, 1);
590

591 592
  gtk_widget_set_hexpand  (GTK_WIDGET (scrolled_win),      TRUE);
  gtk_widget_set_vexpand  (GTK_WIDGET (scrolled_win),      TRUE);
593 594 595 596
  gtk_widget_set_hexpand  (GTK_WIDGET (priv->search_entry), TRUE);

  gtk_widget_set_hexpand  (GTK_WIDGET (priv->size_slider), TRUE);
  gtk_widget_set_hexpand  (GTK_WIDGET (priv->size_spin),   FALSE);
597 598

  gtk_box_pack_start (GTK_BOX (fontchooser), grid, TRUE, TRUE, 0);
599

600
  /* Setting the adjustment values for the size slider */
601 602 603 604 605
  gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
                            (gdouble)(priv->size / PANGO_SCALE));
  gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
                            (gdouble)(priv->size / PANGO_SCALE));

606 607
  gtk_widget_show_all (GTK_WIDGET (fontchooser));
  gtk_widget_hide     (GTK_WIDGET (fontchooser));
608

609
  /* Treeview column and model bootstrapping */
610
  gtk_font_chooser_bootstrap_fontlist (fontchooser);
611

612
  /* Set default preview text */
613 614
  gtk_entry_set_text (GTK_ENTRY (priv->preview),
                      pango_language_get_sample_string (NULL));
615

616
  /* Set search icon and place holder text */
617
  icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
Matthias Clasen's avatar
Matthias Clasen committed
618 619 620 621 622
  g_object_set (G_OBJECT (priv->search_entry),
                "secondary-icon-gicon", icon,
                "secondary-icon-activatable", FALSE,
                "secondary-icon-sensitive", FALSE,
                NULL);
623 624
  g_object_unref (icon);

625
  gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
626

627
  /** Callback connections **/
Matthias Clasen's avatar
Matthias Clasen committed
628 629 630
  g_signal_connect (priv->search_entry, "notify::text",
                    G_CALLBACK (text_changed_cb), fontchooser);
  g_signal_connect (priv->search_entry,
631
                    "icon-press", G_CALLBACK (icon_press_cb), NULL);
632

Matthias Clasen's avatar
Matthias Clasen committed
633
  g_signal_connect (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
634
                    "value-changed", G_CALLBACK (slider_change_cb), fontchooser);
Matthias Clasen's avatar
Matthias Clasen committed
635
  g_signal_connect (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
636
                    "value-changed", G_CALLBACK (spin_change_cb), fontchooser);
637

638 639 640
  priv->cursor_changed_handler =
      g_signal_connect (priv->family_face_list, "cursor-changed",
                        G_CALLBACK (cursor_changed_cb), fontchooser);
641 642

  /* Zoom on preview scroll*/
Matthias Clasen's avatar
Matthias Clasen committed
643 644
  g_signal_connect (priv->preview, "scroll-event",
                    G_CALLBACK (zoom_preview_cb), fontchooser);
645

Matthias Clasen's avatar
Matthias Clasen committed
646
  g_signal_connect (priv->size_slider, "scroll-event",
647
                    G_CALLBACK (zoom_preview_cb), fontchooser);
648

649
  set_range_marks (priv, priv->size_slider, (gint*)font_sizes, G_N_ELEMENTS (font_sizes));
Matthias Clasen's avatar
Matthias Clasen committed
650

651
  /* Font list empty hides the scrolledwindow */
652
  g_signal_connect (G_OBJECT (priv->filter_model), "row-deleted",
653
                    G_CALLBACK (row_deleted_cb), fontchooser);
654
  g_signal_connect (G_OBJECT (priv->filter_model), "row-inserted",
655
                    G_CALLBACK (row_inserted_cb), fontchooser);
656

657
  /* Set default focus */
Matthias Clasen's avatar
Matthias Clasen committed
658
  gtk_widget_pop_composite_child ();
659 660 661
}

/**
662
 * gtk_font_chooser_new:
663
 *
664
 * Creates a new #GtkFontChooser.
665
 *
666
 * Return value: a new #GtkFontChooser
667 668
 *
 * Since: 3.2
669 670
 */
GtkWidget *
671
gtk_font_chooser_new (void)
672
{
673
  GtkFontChooser *fontchooser;
674

675
  fontchooser = g_object_new (GTK_TYPE_FONT_CHOOSER, NULL);
676

677
  return GTK_WIDGET (fontchooser);
678 679
}

680
static int
Matthias Clasen's avatar
Matthias Clasen committed
681
cmp_families (const void *a,
682
              const void *b)
683 684 685 686 687 688 689
{
  const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
  const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);

  return g_utf8_collate (a_name, b_name);
}

Matthias Clasen's avatar
Matthias Clasen committed
690
static void
691 692 693
populate_list (GtkFontChooser *fontchooser,
               GtkTreeView    *treeview,
               GtkListStore   *model)
694
{
695
  GtkFontChooserPrivate *priv = fontchooser->priv;
696 697
  GtkStyleContext      *style_context;
  PangoFontDescription *default_font;
698
  PangoFontDescription *selected_font;
699

700
  gint match;
701
  GtkTreeIter   match_row;
702
  GtkTreePath  *path;
703

Matthias Clasen's avatar
Matthias Clasen committed
704
  gint n_families, i;
705
  PangoFontFamily **families;
706 707 708 709 710
  GString     *tmp;
  GString     *family_and_face;

  if (!gtk_widget_has_screen (GTK_WIDGET (fontchooser)))
    return;
711

712 713
  tmp = g_string_new (NULL);
  family_and_face = g_string_new (NULL);
714

715 716 717 718 719 720
  pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
                               &families,
                               &n_families);

  qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);

721
  /* Get theme font */
722
  style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
723 724 725
  default_font = (PangoFontDescription*) gtk_style_context_get_font (style_context,
                                                                     GTK_STATE_NORMAL);

726 727 728 729 730 731 732 733
  if (priv->face)
    selected_font = pango_font_face_describe (priv->face);
  else
    selected_font = NULL;

  gtk_list_store_clear (model);

  match = 0;
734

735
  /* Iterate over families and faces */
736
  for (i = 0; i < n_families; i++)
737
    {
738 739 740 741 742 743
      GtkTreeIter     iter;
      PangoFontFace **faces;
      int             j, n_faces;
      const gchar    *fam_name = pango_font_family_get_name (families[i]);

      pango_font_family_list_faces (families[i], &faces, &n_faces);
Matthias Clasen's avatar
Matthias Clasen committed
744

745
      for (j = 0; j < n_faces; j++)
746
        {
747 748 749
          PangoFontDescription *pango_desc;
          const gchar *face_name;
          gchar *font_desc;
Matthias Clasen's avatar
Matthias Clasen committed
750

751 752 753
          if (priv->filter_func != NULL &&
              !priv->filter_func (families[i], faces[j], priv->filter_data))
            continue;
Matthias Clasen's avatar
Matthias Clasen committed
754

755 756 757 758 759
          pango_desc = pango_font_face_describe (faces[j]);
          face_name = pango_font_face_get_face_name (faces[j]);
          font_desc = pango_font_description_to_string (pango_desc);

          g_string_printf (family_and_face, "%s %s", fam_name, face_name);
760
          g_string_printf (tmp, ROW_FORMAT_STRING,
761 762 763
                           family_and_face->str,
                           font_desc,
                           fontchooser->priv->preview_text);
764 765 766 767 768

          gtk_list_store_append (model, &iter);
          gtk_list_store_set (model, &iter,
                              FAMILY_COLUMN, families[i],
                              FACE_COLUMN, faces[j],
769 770
                              PREVIEW_TITLE_COLUMN, family_and_face->str,
                              PREVIEW_TEXT_COLUMN, tmp->str,
771 772
                              -1);

773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
          /* Select the current font,
           * the default font/face from the theme,
           * or the first font
           */
          if (match < 3 &&
              selected_font != NULL &&
              pango_font_description_equal (selected_font, pango_desc))
            {
              match_row = iter;
              match = 3;
            }
          if (match < 2 &&
              strcmp (fam_name, pango_font_description_get_family (default_font)) == 0)
            {
              match_row = iter;
              match = 2;
            }
          if (match < 1)
791 792
            {
              match_row = iter;
793
              match = 1;
794
            }
795

796
          pango_font_description_free (pango_desc);
797
          g_free (font_desc);
798
        }
799 800

      g_free (faces);
801 802
    }

803
  path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
804
  if (path)
805 806
    {
      gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
807
      gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
808
      gtk_tree_path_free (path);
809
    }
810

811 812 813
  if (selected_font)
    pango_font_description_free (selected_font);

814
  g_string_free (family_and_face, TRUE);
815
  g_string_free (tmp, TRUE);
816
  g_free (families);
817 818
}

819
static gboolean
820 821 822
visible_func (GtkTreeModel *model,
              GtkTreeIter  *iter,
              gpointer      user_data)
823
{
824
  gboolean result = TRUE;
825
  GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)user_data;
826 827 828

  const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
  gchar       *font_name;
829 830 831 832 833 834 835
  gchar       *term;
  gchar      **split_terms;
  gint         n_terms = 0;

  /* If there's no filter string we show the item */
  if (strlen (search_text) == 0)
    return TRUE;
836 837

  gtk_tree_model_get (model, iter,
838
                      PREVIEW_TITLE_COLUMN, &font_name,
839 840
                      -1);

841
  if (font_name == NULL)
Matthias Clasen's avatar
Matthias Clasen committed
842
    return FALSE;
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862

  split_terms = g_strsplit (search_text, " ", 0);
  term = split_terms[0];

  while (term && result)
  {
    gchar* font_name_casefold = g_utf8_casefold (font_name, -1);
    gchar* term_casefold = g_utf8_casefold (term, -1);

    if (g_strrstr (font_name_casefold, term_casefold))
      result = result && TRUE;
    else
      result = FALSE;

    n_terms++;
    term = split_terms[n_terms];

    g_free (term_casefold);
    g_free (font_name_casefold);
  }
863 864

  g_free (font_name);
865 866
  g_strfreev (split_terms);

867
  return result;
868 869
}

870
static void
Matthias Clasen's avatar
Matthias Clasen committed
871
gtk_font_chooser_bootstrap_fontlist (GtkFontChooser *fontchooser)
872
{
873 874
  GtkFontChooserPrivate *priv = fontchooser->priv;
  GtkTreeView       *treeview = GTK_TREE_VIEW (priv->family_face_list);
875
  GtkCellRenderer   *cell;
876 877
  GtkTreeViewColumn *col;

878 879 880 881 882
  priv->model = gtk_list_store_new (4,
                                    PANGO_TYPE_FONT_FAMILY,
                                    PANGO_TYPE_FONT_FACE,
                                    G_TYPE_STRING,
                                    G_TYPE_STRING);
883

884 885
  priv->filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->model), NULL);
  g_object_unref (priv->model);
886

887 888
  gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
                                          visible_func, (gpointer)priv, NULL);
889

890 891
  gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (priv->filter_model));
  g_object_unref (priv->filter_model);
892

893 894
  gtk_tree_view_set_rules_hint      (treeview, TRUE);
  gtk_tree_view_set_headers_visible (treeview, FALSE);
895

896
  cell = gtk_cell_renderer_text_new ();
897
  col = gtk_tree_view_column_new_with_attributes ("Family",
898
                                                  cell,
899
                                                  "markup", PREVIEW_TEXT_COLUMN,
900
                                                  NULL);
Matthias Clasen's avatar
Matthias Clasen committed
901

902
  g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
903

904 905
  gtk_tree_view_append_column (treeview, col);

906
  populate_list (fontchooser, treeview, priv->model);
907 908
}

909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
static void
gtk_font_chooser_dispose (GObject *object)
{
  GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (object);
  GtkFontChooserPrivate *priv = fontchooser->priv;

  if (priv->cursor_changed_handler != 0)
    {
      g_signal_handler_disconnect (priv->family_face_list,
                                   priv->cursor_changed_handler);
      priv->cursor_changed_handler = 0;
    }

  G_OBJECT_CLASS (gtk_font_chooser_parent_class)->dispose (object);
}
924

925
static void
926
gtk_font_chooser_finalize (GObject *object)
927
{
928
  GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (object);
929
  GtkFontChooserPrivate *priv = fontchooser->priv;
930

931 932 933 934 935 936 937 938
  if (priv->family)
    g_object_unref (priv->family);

  if (priv->face)
    g_object_unref (priv->face);

  if (priv->filter_data_destroy)
    priv->filter_data_destroy (priv->filter_data);
939

940
  G_OBJECT_CLASS (gtk_font_chooser_parent_class)->finalize (object);
941 942 943
}

static void
944
gtk_font_chooser_screen_changed (GtkWidget *widget,
945
                                 GdkScreen *previous_screen)
946
{
947
  GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);