gtkfontchooserwidget.c 45.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GTK - The GIMP Toolkit
 * Copyright (C) 2011 Alberto Ruiz <aruiz@gnome.org>
 *
 * 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
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23 24 25 26
 */

#include "config.h"

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

#include <atk/atk.h>

#include "gtkfontchooserwidget.h"
27
#include "gtkfontchooserwidgetprivate.h"
28 29 30 31

#include "gtkadjustment.h"
#include "gtkbuildable.h"
#include "gtkbox.h"
32 33
#include "gtkcellrenderertext.h"
#include "gtkentry.h"
Matthias Clasen's avatar
Matthias Clasen committed
34
#include "gtksearchentry.h"
35 36 37 38
#include "gtkgrid.h"
#include "gtkfontchooser.h"
#include "gtkfontchooserutils.h"
#include "gtkintl.h"
39 40
#include "gtklabel.h"
#include "gtkliststore.h"
41
#include "gtkstack.h"
42 43
#include "gtkprivate.h"
#include "gtkscale.h"
44
#include "gtkscrolledwindow.h"
45
#include "gtkspinbutton.h"
46 47 48
#include "gtktextview.h"
#include "gtktreeselection.h"
#include "gtktreeview.h"
49
#include "gtkwidget.h"
50
#include "gtksettings.h"
51
#include "gtkdialog.h"
52 53

/**
54
 * SECTION:gtkfontchooserwidget
55 56
 * @Short_description: A widget for selecting fonts
 * @Title: GtkFontChooserWidget
57
 * @See_also: #GtkFontChooserDialog
58
 *
Matthias Clasen's avatar
Matthias Clasen committed
59 60
 * The #GtkFontChooserWidget widget lists the available fonts,
 * styles and sizes, allowing the user to select a font. It is
61
 * used in the #GtkFontChooserDialog widget to provide a
Matthias Clasen's avatar
Matthias Clasen committed
62
 * dialog box for selecting fonts.
63 64
 *
 * To set the font which is initially selected, use
65
 * gtk_font_chooser_set_font() or gtk_font_chooser_set_font_desc().
66
 *
67 68
 * To get the selected font use gtk_font_chooser_get_font() or
 * gtk_font_chooser_get_font_desc().
69 70
 *
 * To change the text which is shown in the preview area, use
71
 * gtk_font_chooser_set_preview_text().
72
 *
73 74 75
 * # CSS nodes
 *
 * GtkFontChooserWidget has a single CSS node with name fontchooser.
76 77
 *
 * Since: 3.2
78 79 80 81 82 83 84
 */


struct _GtkFontChooserWidgetPrivate
{
  GtkWidget    *search_entry;
  GtkWidget    *family_face_list;
85
  GtkTreeViewColumn *family_face_column;
86
  GtkCellRenderer *family_face_cell;
87
  GtkWidget    *list_scrolled_window;
88
  GtkWidget    *list_stack;
89
  GtkTreeModel *model;
90 91 92 93 94 95 96 97 98
  GtkTreeModel *filter_model;

  GtkWidget       *preview;
  gchar           *preview_text;
  gboolean         show_preview_entry;

  GtkWidget *size_spin;
  GtkWidget *size_slider;

99 100
  PangoFontMap         *font_map;

101
  PangoFontDescription *font_desc;
102 103
  GtkTreeIter           font_iter;      /* invalid if font not available or pointer into model
                                           (not filter_model) to the row containing font */
104 105 106
  GtkFontFilterFunc filter_func;
  gpointer          filter_data;
  GDestroyNotify    filter_data_destroy;
107 108

  guint last_fontconfig_timestamp;
109 110 111 112 113 114 115 116 117 118 119 120
};

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

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

121
/* Keep in line with GtkTreeStore defined in gtkfontchooserwidget.ui */
122 123 124
enum {
  FAMILY_COLUMN,
  FACE_COLUMN,
125
  FONT_DESC_COLUMN,
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  PREVIEW_TITLE_COLUMN
};

static void gtk_font_chooser_widget_set_property         (GObject         *object,
                                                          guint            prop_id,
                                                          const GValue    *value,
                                                          GParamSpec      *pspec);
static void gtk_font_chooser_widget_get_property         (GObject         *object,
                                                          guint            prop_id,
                                                          GValue          *value,
                                                          GParamSpec      *pspec);
static void gtk_font_chooser_widget_finalize             (GObject         *object);

static void gtk_font_chooser_widget_screen_changed       (GtkWidget       *widget,
                                                          GdkScreen       *previous_screen);

142 143 144 145
static gboolean gtk_font_chooser_widget_find_font        (GtkFontChooserWidget *fontchooser,
                                                          const PangoFontDescription *font_desc,
                                                          GtkTreeIter          *iter);
static void     gtk_font_chooser_widget_ensure_selection (GtkFontChooserWidget *fontchooser);
146

147 148 149
static gchar   *gtk_font_chooser_widget_get_font         (GtkFontChooserWidget *fontchooser);
static void     gtk_font_chooser_widget_set_font         (GtkFontChooserWidget *fontchooser,
                                                          const gchar          *fontname);
150

151
static PangoFontDescription *gtk_font_chooser_widget_get_font_desc  (GtkFontChooserWidget *fontchooser);
152 153 154
static void                  gtk_font_chooser_widget_merge_font_desc(GtkFontChooserWidget       *fontchooser,
                                                                     const PangoFontDescription *font_desc,
                                                                     GtkTreeIter                *iter);
155
static void                  gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
156 157 158
                                                                     PangoFontDescription *font_desc);


159 160 161 162 163 164 165 166
static const gchar *gtk_font_chooser_widget_get_preview_text (GtkFontChooserWidget *fontchooser);
static void         gtk_font_chooser_widget_set_preview_text (GtkFontChooserWidget *fontchooser,
                                                              const gchar          *text);

static gboolean gtk_font_chooser_widget_get_show_preview_entry (GtkFontChooserWidget *fontchooser);
static void     gtk_font_chooser_widget_set_show_preview_entry (GtkFontChooserWidget *fontchooser,
                                                                gboolean              show_preview_entry);

167
static void     gtk_font_chooser_widget_set_cell_size          (GtkFontChooserWidget *fontchooser);
168 169
static void     gtk_font_chooser_widget_load_fonts             (GtkFontChooserWidget *fontchooser,
                                                                gboolean              force);
170 171 172 173 174 175 176 177 178
static gboolean visible_func                                   (GtkTreeModel *model,
								GtkTreeIter  *iter,
								gpointer      user_data);
static void     gtk_font_chooser_widget_cell_data_func         (GtkTreeViewColumn *column,
								GtkCellRenderer   *cell,
								GtkTreeModel      *tree_model,
								GtkTreeIter       *iter,
								gpointer           user_data);

179 180 181
static void selection_changed (GtkTreeSelection *selection,
                               GtkFontChooserWidget *fontchooser);

182 183 184
static void gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface);

G_DEFINE_TYPE_WITH_CODE (GtkFontChooserWidget, gtk_font_chooser_widget, GTK_TYPE_BOX,
185
                         G_ADD_PRIVATE (GtkFontChooserWidget)
186 187 188
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
                                                gtk_font_chooser_widget_iface_init))

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
typedef struct _GtkDelayedFontDescription GtkDelayedFontDescription;
struct _GtkDelayedFontDescription {
  PangoFontFace        *face;
  PangoFontDescription *desc;
  guint                 ref_count;
};

static GtkDelayedFontDescription *
gtk_delayed_font_description_new (PangoFontFace *face)
{
  GtkDelayedFontDescription *result;
  
  result = g_slice_new0 (GtkDelayedFontDescription);

  result->face = g_object_ref (face);
  result->desc = NULL;
  result->ref_count = 1;

  return result;
}

static GtkDelayedFontDescription *
gtk_delayed_font_description_ref (GtkDelayedFontDescription *desc)
{
  desc->ref_count++;

  return desc;
}

static void
gtk_delayed_font_description_unref (GtkDelayedFontDescription *desc)
{
  desc->ref_count--;

  if (desc->ref_count > 0)
    return;

  g_object_unref (desc->face);
  if (desc->desc)
    pango_font_description_free (desc->desc);

  g_slice_free (GtkDelayedFontDescription, desc);
}

static const PangoFontDescription *
gtk_delayed_font_description_get (GtkDelayedFontDescription *desc)
{
  if (desc->desc == NULL)
    desc->desc = pango_font_face_describe (desc->face);

  return desc->desc;
}

#define GTK_TYPE_DELAYED_FONT_DESCRIPTION (gtk_delayed_font_description_get_type ())
G_DEFINE_BOXED_TYPE (GtkDelayedFontDescription, gtk_delayed_font_description,
                     gtk_delayed_font_description_ref,
                     gtk_delayed_font_description_unref)
246 247 248 249 250 251 252 253 254 255
static void
gtk_font_chooser_widget_set_property (GObject         *object,
                                      guint            prop_id,
                                      const GValue    *value,
                                      GParamSpec      *pspec)
{
  GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);

  switch (prop_id)
    {
256
    case GTK_FONT_CHOOSER_PROP_FONT:
257
      gtk_font_chooser_widget_set_font (fontchooser, g_value_get_string (value));
258
      break;
259
    case GTK_FONT_CHOOSER_PROP_FONT_DESC:
260
      gtk_font_chooser_widget_take_font_desc (fontchooser, g_value_dup_boxed (value));
261
      break;
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
      gtk_font_chooser_widget_set_preview_text (fontchooser, g_value_get_string (value));
      break;
    case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
      gtk_font_chooser_widget_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_font_chooser_widget_get_property (GObject         *object,
                                      guint            prop_id,
                                      GValue          *value,
                                      GParamSpec      *pspec)
{
  GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);

  switch (prop_id)
    {
284
    case GTK_FONT_CHOOSER_PROP_FONT:
285
      g_value_take_string (value, gtk_font_chooser_widget_get_font (fontchooser));
286
      break;
287
    case GTK_FONT_CHOOSER_PROP_FONT_DESC:
288
      g_value_set_boxed (value, gtk_font_chooser_widget_get_font_desc (fontchooser));
289
      break;
290 291 292 293 294 295 296 297 298 299 300 301
    case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
      g_value_set_string (value, gtk_font_chooser_widget_get_preview_text (fontchooser));
      break;
    case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
      g_value_set_boolean (value, gtk_font_chooser_widget_get_show_preview_entry (fontchooser));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

302 303 304 305 306 307 308
static void
gtk_font_chooser_widget_refilter_font_list (GtkFontChooserWidget *fontchooser)
{
  gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (fontchooser->priv->filter_model));
  gtk_font_chooser_widget_ensure_selection (fontchooser);
}

309
static void
Matthias Clasen's avatar
Matthias Clasen committed
310
text_changed_cb (GtkEntry             *entry,
311 312
                 GtkFontChooserWidget *fc)
{
313
  gtk_font_chooser_widget_refilter_font_list (fc);
314 315
}

316 317 318 319
static void
stop_search_cb (GtkEntry             *entry,
                GtkFontChooserWidget *fc)
{
320 321 322 323 324 325 326 327 328 329 330 331 332 333
  if (gtk_entry_get_text (entry)[0] != 0)
    gtk_entry_set_text (entry, "");
  else
    {
      GtkWidget *dialog;
      GtkWidget *button = NULL;

      dialog = gtk_widget_get_ancestor (GTK_WIDGET (fc), GTK_TYPE_DIALOG);
      if (dialog)
        button = gtk_dialog_get_widget_for_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);

      if (button)
        gtk_widget_activate (button);
    }
334 335
}

336
static void
337
size_change_cb (GtkAdjustment *adjustment,
338 339
                gpointer       user_data)
{
340 341 342
  GtkFontChooserWidget *fontchooser = user_data;
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
  PangoFontDescription *font_desc;
343
  gdouble size = gtk_adjustment_get_value (adjustment);
344

345
  font_desc = pango_font_description_new ();
346
  if (pango_font_description_get_size_is_absolute (priv->font_desc))
347
    pango_font_description_set_absolute_size (font_desc, size * PANGO_SCALE);
348
  else
349
    pango_font_description_set_size (font_desc, size * PANGO_SCALE);
350

351
  gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
352 353
}

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
static gboolean
output_cb (GtkSpinButton *spin,
           gpointer       data)
{
  GtkAdjustment *adjustment;
  gchar *text;
  gdouble value;

  adjustment = gtk_spin_button_get_adjustment (spin);
  value = gtk_adjustment_get_value (adjustment);
  text = g_strdup_printf ("%2.4g", value);
  gtk_entry_set_text (GTK_ENTRY (spin), text);
  g_free (text);

  return TRUE;
}

371
static void
372
gtk_font_chooser_widget_update_marks (GtkFontChooserWidget *fontchooser)
373
{
374
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
375
  GtkAdjustment *adj, *spin_adj;
376 377 378
  const int *sizes;
  gint *font_sizes;
  gint i, n_sizes;
379
  gdouble value, spin_value;
380

381
  if (gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter))
382
    {
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
      PangoFontFace *face;

      gtk_tree_model_get (priv->model, &priv->font_iter,
                          FACE_COLUMN, &face,
                          -1);

      pango_font_face_list_sizes (face, &font_sizes, &n_sizes);

      /* It seems not many fonts actually have a sane set of sizes */
      for (i = 0; i < n_sizes; i++)
        font_sizes[i] = font_sizes[i] / PANGO_SCALE;

      g_object_unref (face);
    }
  else
    {
      font_sizes = NULL;
      n_sizes = 0;
401 402
    }

403 404 405 406 407
  if (n_sizes < 2)
    {
      static const gint fallback_sizes[] = {
        6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
      };
408

409 410 411 412 413 414 415 416 417
      sizes = fallback_sizes;
      n_sizes = G_N_ELEMENTS (fallback_sizes);
    }
  else
    {
      sizes = font_sizes;
    }

  gtk_scale_clear_marks (GTK_SCALE (priv->size_slider));
418

419 420 421 422 423 424 425 426 427 428
  adj        = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
  spin_adj   = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
  spin_value = gtk_adjustment_get_value (spin_adj);

  if (spin_value < sizes[0])
    value = (gdouble) sizes[0];
  else if (spin_value > sizes[n_sizes - 1])
    value = (gdouble)sizes[n_sizes - 1];
  else
    value = (gdouble)spin_value;
429

430 431 432
  /* ensure clamping doesn't callback into font resizing code */
  g_signal_handlers_block_by_func (adj, size_change_cb, fontchooser);
  gtk_adjustment_configure (adj,
433
                            value,
434 435 436 437 438 439 440 441 442 443 444 445 446
                            sizes[0],
                            sizes[n_sizes - 1],
                            gtk_adjustment_get_step_increment (adj),
                            gtk_adjustment_get_page_increment (adj),
                            gtk_adjustment_get_page_size (adj));
  g_signal_handlers_unblock_by_func (adj, size_change_cb, fontchooser);

  for (i = 0; i < n_sizes; i++)
    {
      gtk_scale_add_mark (GTK_SCALE (priv->size_slider),
                          sizes[i],
                          GTK_POS_BOTTOM, NULL);
    }
447

448
  g_free (font_sizes);
449 450 451 452 453 454 455 456
}

static void
row_activated_cb (GtkTreeView       *view,
                  GtkTreePath       *path,
                  GtkTreeViewColumn *column,
                  gpointer           user_data)
{
457
  GtkFontChooserWidget *fontchooser = user_data;
458 459
  gchar *fontname;

460 461
  fontname = gtk_font_chooser_widget_get_font (fontchooser);
  _gtk_font_chooser_font_activated (GTK_FONT_CHOOSER (fontchooser), fontname);
462 463 464 465 466 467 468
  g_free (fontname);
}

static void
cursor_changed_cb (GtkTreeView *treeview,
                   gpointer     user_data)
{
469
  GtkFontChooserWidget *fontchooser = user_data;
470
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
471
  GtkDelayedFontDescription *desc;
472
  GtkTreeIter filter_iter, iter;
Benjamin Otte's avatar
Benjamin Otte committed
473
  GtkTreePath *path = NULL;
474 475 476 477 478 479

  gtk_tree_view_get_cursor (treeview, &path, NULL);

  if (!path)
    return;

480
  if (!gtk_tree_model_get_iter (priv->filter_model, &filter_iter, path))
481 482 483 484 485 486 487
    {
      gtk_tree_path_free (path);
      return;
    }

  gtk_tree_path_free (path);

488 489 490
  gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
                                                    &iter,
                                                    &filter_iter);
491
  gtk_tree_model_get (priv->model, &iter,
492 493
                      FONT_DESC_COLUMN, &desc,
                      -1);
494

495 496 497 498 499
  gtk_font_chooser_widget_merge_font_desc (fontchooser,
                                           gtk_delayed_font_description_get (desc),
                                           &iter);

  gtk_delayed_font_description_unref (desc);
500 501 502
}

static gboolean
503 504 505
resize_by_scroll_cb (GtkWidget      *scrolled_window,
                     GdkEventScroll *event,
                     gpointer        user_data)
506
{
507 508
  GtkFontChooserWidget *fc = user_data;
  GtkFontChooserWidgetPrivate *priv = fc->priv;
509 510 511 512 513 514 515 516 517 518
  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));
519 520 521 522 523 524 525 526 527
  else if (event->direction == GDK_SCROLL_SMOOTH && event->delta_x != 0.0)
    gtk_adjustment_set_value (adj,
                              gtk_adjustment_get_value (adj) +
                              gtk_adjustment_get_step_increment (adj) * event->delta_x);
  else if (event->direction == GDK_SCROLL_SMOOTH && event->delta_y != 0.0)
    gtk_adjustment_set_value (adj,
                              gtk_adjustment_get_value (adj) -
                              gtk_adjustment_get_step_increment (adj) * event->delta_y);

528 529 530
  return TRUE;
}

531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
static void
gtk_font_chooser_widget_update_preview_attributes (GtkFontChooserWidget *fontchooser)
{
  GtkFontChooserWidgetPrivate *priv;
  PangoAttrList *attrs;

  priv = fontchooser->priv;

  attrs = pango_attr_list_new ();

  /* Prevent font fallback */
  pango_attr_list_insert (attrs, pango_attr_fallback_new (FALSE));

  /* Force current font */
  pango_attr_list_insert (attrs, pango_attr_font_desc_new (priv->font_desc));

  gtk_entry_set_attributes (GTK_ENTRY (priv->preview), attrs);
  pango_attr_list_unref (attrs);
}

551 552 553 554 555 556
static void
row_inserted_cb (GtkTreeModel *model,
                 GtkTreePath  *path,
                 GtkTreeIter  *iter,
                 gpointer      user_data)
{
557 558
  GtkFontChooserWidget *fontchooser = user_data;
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
559

560
  gtk_stack_set_visible_child_name (GTK_STACK (priv->list_stack), "list");
561 562 563 564 565 566 567
}

static void
row_deleted_cb  (GtkTreeModel *model,
                 GtkTreePath  *path,
                 gpointer      user_data)
{
568 569
  GtkFontChooserWidget *fontchooser = user_data;
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
570 571

  if (gtk_tree_model_iter_n_children (model, NULL) == 0)
572
    gtk_stack_set_visible_child_name (GTK_STACK (priv->list_stack), "empty");
573 574
}

575 576 577 578 579 580
static void
gtk_font_chooser_widget_class_init (GtkFontChooserWidgetClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

581
  g_type_ensure (GTK_TYPE_DELAYED_FONT_DESCRIPTION);
Matthias Clasen's avatar
Matthias Clasen committed
582
  g_type_ensure (G_TYPE_THEMED_ICON);
583

584 585 586 587 588 589 590 591
  widget_class->screen_changed = gtk_font_chooser_widget_screen_changed;

  gobject_class->finalize = gtk_font_chooser_widget_finalize;
  gobject_class->set_property = gtk_font_chooser_widget_set_property;
  gobject_class->get_property = gtk_font_chooser_widget_get_property;

  _gtk_font_chooser_install_properties (gobject_class);

592
  /* Bind class to template */
593
  gtk_widget_class_set_template_from_resource (widget_class,
594
					       "/org/gtk/libgtk/ui/gtkfontchooserwidget.ui");
595

596 597 598 599 600
  gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, search_entry);
  gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, family_face_list);
  gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, family_face_column);
  gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, family_face_cell);
  gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, list_scrolled_window);
601
  gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, list_stack);
602 603 604 605 606
  gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, model);
  gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, filter_model);
  gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, preview);
  gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, size_spin);
  gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, size_slider);
607 608

  gtk_widget_class_bind_template_callback (widget_class, text_changed_cb);
609
  gtk_widget_class_bind_template_callback (widget_class, stop_search_cb);
610 611 612 613 614 615 616 617
  gtk_widget_class_bind_template_callback (widget_class, cursor_changed_cb);
  gtk_widget_class_bind_template_callback (widget_class, row_activated_cb);
  gtk_widget_class_bind_template_callback (widget_class, gtk_font_chooser_widget_set_cell_size);
  gtk_widget_class_bind_template_callback (widget_class, resize_by_scroll_cb);
  gtk_widget_class_bind_template_callback (widget_class, row_deleted_cb);
  gtk_widget_class_bind_template_callback (widget_class, row_inserted_cb);
  gtk_widget_class_bind_template_callback (widget_class, row_deleted_cb);
  gtk_widget_class_bind_template_callback (widget_class, size_change_cb);
618
  gtk_widget_class_bind_template_callback (widget_class, output_cb);
619
  gtk_widget_class_bind_template_callback (widget_class, selection_changed);
620 621

  gtk_widget_class_set_css_name (widget_class, "fontchooser");
622 623
}

624 625 626
static void
gtk_font_chooser_widget_init (GtkFontChooserWidget *fontchooser)
{
627
  GtkFontChooserWidgetPrivate *priv;
628

629
  fontchooser->priv = gtk_font_chooser_widget_get_instance_private (fontchooser);
630 631
  priv = fontchooser->priv;

632 633
  gtk_widget_init_template (GTK_WIDGET (fontchooser));

634 635 636
  /* Default preview string  */
  priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
  priv->show_preview_entry = TRUE;
637
  priv->font_desc = pango_font_description_new ();
638 639

  /* Set default preview text */
640 641
  gtk_entry_set_text (GTK_ENTRY (priv->preview), priv->preview_text);

642
  gtk_font_chooser_widget_update_preview_attributes (fontchooser);
643

644
  gtk_widget_add_events (priv->preview, GDK_SCROLL_MASK);
645

646 647
  /* Set the upper values of the spin/scale with G_MAXINT / PANGO_SCALE */
  gtk_spin_button_set_range (GTK_SPIN_BUTTON (priv->size_spin),
648
			     1.0, (gdouble)(G_MAXINT / PANGO_SCALE));
649 650
  gtk_adjustment_set_upper (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
			    (gdouble)(G_MAXINT / PANGO_SCALE));
651

652 653 654
  /* Setup treeview/model auxilary functions */
  gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
                                          visible_func, (gpointer)priv, NULL);
655

656 657 658 659 660
  gtk_tree_view_column_set_cell_data_func (priv->family_face_column,
                                           priv->family_face_cell,
                                           gtk_font_chooser_widget_cell_data_func,
                                           fontchooser,
                                           NULL);
661

662
  /* Load data and set initial style-dependent parameters */
663
  gtk_font_chooser_widget_load_fonts (fontchooser, TRUE);
664
  gtk_font_chooser_widget_set_cell_size (fontchooser);
665
  gtk_font_chooser_widget_take_font_desc (fontchooser, NULL);
666 667 668 669 670 671 672
}

/**
 * gtk_font_chooser_widget_new:
 *
 * Creates a new #GtkFontChooserWidget.
 *
673
 * Returns: a new #GtkFontChooserWidget
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
 *
 * Since: 3.2
 */
GtkWidget *
gtk_font_chooser_widget_new (void)
{
  return g_object_new (GTK_TYPE_FONT_CHOOSER_WIDGET, NULL);
}

static int
cmp_families (const void *a,
              const void *b)
{
  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);
}

static void
694 695
gtk_font_chooser_widget_load_fonts (GtkFontChooserWidget *fontchooser,
                                    gboolean              force)
696 697
{
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
698
  GtkListStore *list_store;
699 700 701
  gint n_families, i;
  PangoFontFamily **families;
  gchar *family_and_face;
702
  guint fontconfig_timestamp;
703 704
  gboolean need_reload;
  PangoFontMap *font_map;
705 706 707 708 709 710 711 712 713

  g_object_get (gtk_widget_get_settings (GTK_WIDGET (fontchooser)),
                "gtk-fontconfig-timestamp", &fontconfig_timestamp,
                NULL);

  /* The fontconfig timestamp is only set on systems with fontconfig; every
   * other platform will set it to 0. For those systems, we fall back to
   * reloading the fonts every time.
   */
714 715
  need_reload = fontconfig_timestamp == 0 ||
                fontconfig_timestamp != priv->last_fontconfig_timestamp;
716 717

  priv->last_fontconfig_timestamp = fontconfig_timestamp;
718

719 720 721
  if (!need_reload && !force)
    return;

722 723
  list_store = GTK_LIST_STORE (priv->model);

724 725 726 727 728
  if (priv->font_map)
    font_map = priv->font_map;
  else
    font_map = pango_cairo_font_map_get_default ();
  pango_font_map_list_families (font_map, &families, &n_families);
729 730 731

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

732
  g_signal_handlers_block_by_func (priv->family_face_list, cursor_changed_cb, fontchooser);
733
  gtk_list_store_clear (list_store);
734
  g_signal_handlers_unblock_by_func (priv->family_face_list, cursor_changed_cb, fontchooser);
735 736 737 738 739 740 741 742 743 744 745 746 747

  /* Iterate over families and faces */
  for (i = 0; i < n_families; i++)
    {
      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);

      for (j = 0; j < n_faces; j++)
        {
748
          GtkDelayedFontDescription *desc;
749 750 751 752 753
          const gchar *face_name;

          face_name = pango_font_face_get_face_name (faces[j]);

          family_and_face = g_strconcat (fam_name, " ", face_name, NULL);
754
          desc = gtk_delayed_font_description_new (faces[j]);
755

756
          gtk_list_store_insert_with_values (list_store, &iter, -1,
757 758
                                             FAMILY_COLUMN, families[i],
                                             FACE_COLUMN, faces[j],
759
                                             FONT_DESC_COLUMN, desc,
760 761
                                             PREVIEW_TITLE_COLUMN, family_and_face,
                                             -1);
762 763

          g_free (family_and_face);
764
          gtk_delayed_font_description_unref (desc);
765 766 767 768 769 770
        }

      g_free (faces);
    }

  g_free (families);
771 772

  /* now make sure the font list looks right */
773
  if (!gtk_font_chooser_widget_find_font (fontchooser, priv->font_desc, &priv->font_iter))
774 775 776
    memset (&priv->font_iter, 0, sizeof (GtkTreeIter));

  gtk_font_chooser_widget_ensure_selection (fontchooser);
777 778 779 780 781 782 783
}

static gboolean
visible_func (GtkTreeModel *model,
              GtkTreeIter  *iter,
              gpointer      user_data)
{
784
  GtkFontChooserWidgetPrivate *priv = user_data;
785
  gboolean result = TRUE;
786 787
  const gchar *search_text;
  gchar **split_terms;
788
  gchar *font_name, *font_name_casefold;
789
  guint i;
790

791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
  if (priv->filter_func != NULL)
    {
      PangoFontFamily *family;
      PangoFontFace *face;

      gtk_tree_model_get (model, iter,
                          FAMILY_COLUMN, &family,
                          FACE_COLUMN, &face,
                          -1);

      result = priv->filter_func (family, face, priv->filter_data);

      g_object_unref (family);
      g_object_unref (face);
      
      if (!result)
        return FALSE;
    }

810
  /* If there's no filter string we show the item */
811
  search_text = gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
812 813 814 815 816 817 818 819 820 821 822
  if (strlen (search_text) == 0)
    return TRUE;

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

  if (font_name == NULL)
    return FALSE;

  split_terms = g_strsplit (search_text, " ", 0);
823
  font_name_casefold = g_utf8_casefold (font_name, -1);
824

825 826 827
  for (i = 0; split_terms[i] && result; i++)
    {
      gchar* term_casefold = g_utf8_casefold (split_terms[i], -1);
828

829 830
      if (!strstr (font_name_casefold, term_casefold))
        result = FALSE;
831

832 833
      g_free (term_casefold);
    }
834

835
  g_free (font_name_casefold);
836 837 838 839 840 841
  g_free (font_name);
  g_strfreev (split_terms);

  return result;
}

842 843 844 845 846 847 848 849 850 851 852 853 854
/* in pango units */
static int
gtk_font_chooser_widget_get_preview_text_height (GtkFontChooserWidget *fontchooser)
{
  GtkWidget *treeview = fontchooser->priv->family_face_list;
  double dpi, font_size;

  dpi = gdk_screen_get_resolution (gtk_widget_get_screen (treeview));
  gtk_style_context_get (gtk_widget_get_style_context (treeview),
                         gtk_widget_get_state_flags (treeview),
                         "font-size", &font_size,
                         NULL);

855
  return (dpi < 0.0 ? 96.0 : dpi) / 72.0 * PANGO_SCALE_X_LARGE * font_size * PANGO_SCALE;
856 857
}

858 859
static PangoAttrList *
gtk_font_chooser_widget_get_preview_attributes (GtkFontChooserWidget       *fontchooser,
860
                                                const PangoFontDescription *font_desc)
861 862 863 864 865 866
{
  PangoAttribute *attribute;
  PangoAttrList *attrs;

  attrs = pango_attr_list_new ();

867 868 869 870 871
  if (font_desc)
    {
      attribute = pango_attr_font_desc_new (font_desc);
      pango_attr_list_insert (attrs, attribute);
    }
872 873 874 875 876 877 878

  attribute = pango_attr_size_new_absolute (gtk_font_chooser_widget_get_preview_text_height (fontchooser));
  pango_attr_list_insert (attrs, attribute);

  return attrs;
}

879 880 881 882 883 884 885 886
static void
gtk_font_chooser_widget_cell_data_func (GtkTreeViewColumn *column,
                                        GtkCellRenderer   *cell,
                                        GtkTreeModel      *tree_model,
                                        GtkTreeIter       *iter,
                                        gpointer           user_data)
{
  GtkFontChooserWidget *fontchooser = user_data;
887
  GtkDelayedFontDescription *desc;
888
  PangoAttrList *attrs;
889
  char *preview_title;
890

891 892
  gtk_tree_model_get (tree_model, iter,
                      PREVIEW_TITLE_COLUMN, &preview_title,
893
                      FONT_DESC_COLUMN, &desc,
894 895
                      -1);

896 897
  attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser,
                                                          gtk_delayed_font_description_get (desc));
898 899

  g_object_set (cell,
900 901
                "xpad", 20,
                "ypad", 10,
902
                "attributes", attrs,
903
                "text", preview_title,
904 905
                NULL);

906
  gtk_delayed_font_description_unref (desc);
907
  pango_attr_list_unref (attrs);
908
  g_free (preview_title);
909 910
}

911 912 913 914 915 916 917 918 919
static void
gtk_font_chooser_widget_set_cell_size (GtkFontChooserWidget *fontchooser)
{
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
  PangoAttrList *attrs;
  GtkRequisition size;

  gtk_cell_renderer_set_fixed_size (priv->family_face_cell, -1, -1);

920 921
  attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser, NULL);

922
  g_object_set (priv->family_face_cell,
923 924
                "xpad", 20,
                "ypad", 10,
925
                "attributes", attrs,
926
                "text", "x",
927 928 929 930 931 932 933 934 935 936 937
                NULL);

  pango_attr_list_unref (attrs);

  gtk_cell_renderer_get_preferred_size (priv->family_face_cell,
                                        priv->family_face_list,
                                        &size,
                                        NULL);
  gtk_cell_renderer_set_fixed_size (priv->family_face_cell, size.width, size.height);
}

938 939 940 941 942 943
static void
gtk_font_chooser_widget_finalize (GObject *object)
{
  GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;

944 945
  if (priv->font_desc)
    pango_font_description_free (priv->font_desc);
946 947 948 949

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

950 951
  g_free (priv->preview_text);

952 953
  g_clear_object (&priv->font_map);

954 955 956
  G_OBJECT_CLASS (gtk_font_chooser_widget_parent_class)->finalize (object);
}

957 958 959 960 961 962 963
static gboolean
my_pango_font_family_equal (const char *familya,
                            const char *familyb)
{
  return g_ascii_strcasecmp (familya, familyb) == 0;
}

964 965 966 967
static gboolean
gtk_font_chooser_widget_find_font (GtkFontChooserWidget        *fontchooser,
                                   const PangoFontDescription  *font_desc,
                                   /* out arguments */
968
                                   GtkTreeIter                 *iter)
969 970
{
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
971
  gboolean valid;
972

973 974 975
  if (pango_font_description_get_family (font_desc) == NULL)
    return FALSE;

976
  for (valid = gtk_tree_model_get_iter_first (priv->model, iter);
977
       valid;
978
       valid = gtk_tree_model_iter_next (priv->model, iter))
979
    {
980 981
      GtkDelayedFontDescription *desc;
      PangoFontDescription *merged;
982 983
      PangoFontFamily *family;

984 985
      gtk_tree_model_get (priv->model, iter,
                          FAMILY_COLUMN, &family,
986
                          FONT_DESC_COLUMN, &desc,
987 988 989 990
                          -1);

      if (!my_pango_font_family_equal (pango_font_description_get_family (font_desc),
                                       pango_font_family_get_name (family)))
991 992 993 994 995
        {
          gtk_delayed_font_description_unref (desc);
          g_object_unref (family);
          continue;
        }
996

997
      merged = pango_font_description_copy_static (gtk_delayed_font_description_get (desc));
998

999 1000 1001 1002 1003 1004 1005 1006
      pango_font_description_merge_static (merged, font_desc, FALSE);
      if (pango_font_description_equal (merged, font_desc))
        {
          gtk_delayed_font_description_unref (desc);
          pango_font_description_free (merged);
          g_object_unref (family);
          break;
        }
1007

1008 1009 1010
      gtk_delayed_font_description_unref (desc);
      pango_font_description_free (merged);
      g_object_unref (family);
1011 1012
    }
  
1013
  return valid;
1014 1015
}

1016 1017 1018 1019 1020 1021
static void
fontconfig_changed (GtkFontChooserWidget *fontchooser)
{
  gtk_font_chooser_widget_load_fonts (fontchooser, TRUE);
}

1022 1023
static void
gtk_font_chooser_widget_screen_changed (GtkWidget *widget,
1024
                                        GdkScreen *previous_screen)
1025 1026
{
  GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (widget);
1027
  GtkSettings *settings;
1028

1029 1030
  if (GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->screen_changed)
    GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->screen_changed (widget, previous_screen);
1031

1032 1033 1034
  if (previous_screen)
    {
      settings = gtk_settings_get_for_screen (previous_screen);
1035
      g_signal_handlers_disconnect_by_func (settings, fontconfig_changed, widget);
1036 1037 1038
    }
  settings = gtk_widget_get_settings (widget);
  g_signal_connect_object (settings, "notify::gtk-fontconfig-timestamp",
1039
                           G_CALLBACK (fontconfig_changed), widget, G_CONNECT_SWAPPED);
1040

1041 1042 1043 1044 1045 1046
  if (previous_screen == NULL)
    previous_screen = gdk_screen_get_default ();

  if (previous_screen == gtk_widget_get_screen (widget))
    return;

1047
  gtk_font_chooser_widget_load_fonts (fontchooser, FALSE);
1048 1049 1050 1051 1052 1053
}

static PangoFontFamily *
gtk_font_chooser_widget_get_family (GtkFontChooser *chooser)
{
  GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
  PangoFontFamily *family;

  if (!gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter))
    return NULL;

  gtk_tree_model_get (priv->model, &priv->font_iter,
                      FAMILY_COLUMN, &family,
                      -1);
  g_object_unref (family);
1064

1065
  return family;
1066 1067 1068 1069 1070 1071
}

static PangoFontFace *
gtk_font_chooser_widget_get_face (GtkFontChooser *chooser)
{
  GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1072 1073
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
  PangoFontFace *face;
1074

1075 1076 1077 1078 1079 1080 1081 1082 1083
  if (!gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter))
    return NULL;

  gtk_tree_model_get (priv->model, &priv->font_iter,
                      FACE_COLUMN, &face,
                      -1);
  g_object_unref (face);

  return face;
1084 1085 1086 1087 1088 1089
}

static gint
gtk_font_chooser_widget_get_size (GtkFontChooser *chooser)
{
  GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1090 1091 1092 1093
  PangoFontDescription *desc = gtk_font_chooser_widget_get_font_desc (fontchooser);

  if (desc)
    return pango_font_description_get_size (desc);
1094

1095
  return -1;
1096 1097 1098
}

static gchar *
1099
gtk_font_chooser_widget_get_font (GtkFontChooserWidget *fontchooser)
1100
{
1101 1102 1103 1104 1105 1106
  PangoFontDescription *desc = gtk_font_chooser_widget_get_font_desc (fontchooser);

  if (desc)
    return pango_font_description_to_string (desc);

  return NULL;
1107
}
1108

1109
static PangoFontDescription *
1110
gtk_font_chooser_widget_get_font_desc (GtkFontChooserWidget *fontchooser)
1111
{
1112 1113 1114 1115 1116 1117 1118 1119
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
  GtkTreeSelection *selection;

  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->family_face_list));
  if (gtk_tree_selection_count_selected_rows (selection) > 0)
    return fontchooser->priv->font_desc;

  return NULL;
1120 1121
}

1122
static void
1123 1124
gtk_font_chooser_widget_set_font (GtkFontChooserWidget *fontchooser,
                                  const gchar          *fontname)
1125 1126 1127 1128
{
  PangoFontDescription *font_desc;

  font_desc = pango_font_description_from_string (fontname);
1129
  gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
1130 1131
}

1132 1133 1134 1135 1136 1137 1138 1139
static void
selection_changed (GtkTreeSelection     *selection,
                   GtkFontChooserWidget *fontchooser)
{
  g_object_notify (G_OBJECT (fontchooser), "font");
  g_object_notify (G_OBJECT (fontchooser), "font-desc");
}

1140
static void
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
gtk_font_chooser_widget_ensure_selection (GtkFontChooserWidget *fontchooser)
{
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
  GtkTreeSelection *selection;
  GtkTreeIter filter_iter;
  
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->family_face_list));

  if (gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter) &&
      gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
                                                        &filter_iter,
                                                        &priv->font_iter))
    {
1154 1155 1156
      GtkTreePath *path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter_model),
                                                   &filter_iter);

1157
      gtk_tree_selection_select_iter (selection, &filter_iter);
1158 1159 1160
      gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->family_face_list),
                                    path, NULL, FALSE, 0.0, 0.0);
      gtk_tree_path_free (path);
1161 1162 1163 1164 1165 1166 1167 1168
    }
  else
    {
      gtk_tree_selection_unselect_all (selection);
    }
}

static void
1169 1170 1171
gtk_font_chooser_widget_merge_font_desc (GtkFontChooserWidget       *fontchooser,
                                         const PangoFontDescription *font_desc,
                                         GtkTreeIter                *iter)
1172 1173
{
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1174
  PangoFontMask mask;
1175

1176 1177 1178 1179 1180 1181 1182 1183
  g_assert (font_desc != NULL);
  /* iter may be NULL if the font doesn't exist on the list */

  mask = pango_font_description_get_set_fields (font_desc);

  /* sucky test, because we can't restrict the comparison to 
   * only the parts that actually do get merged */
  if (pango_font_description_equal (font_desc, priv->font_desc))
1184
    return;
1185

1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
  pango_font_description_merge (priv->font_desc, font_desc, TRUE);
  
  if (mask & PANGO_FONT_MASK_SIZE)
    {
      double font_size = (double) pango_font_description_get_size (priv->font_desc) / PANGO_SCALE;
      /* XXX: This clamps, which can cause it to reloop into here, do we need
       * to block its signal handler? */
      gtk_range_set_value (GTK_RANGE (priv->size_slider), font_size);
      gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_spin), font_size);
    }
  if (mask & (PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_STYLE | PANGO_FONT_MASK_VARIANT |
              PANGO_FONT_MASK_WEIGHT | PANGO_FONT_MASK_STRETCH))
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
    {
      if (&priv->font_iter != iter)
        {
          if (iter == NULL)
            memset (&priv->font_iter, 0, sizeof (GtkTreeIter));
          else
            memcpy (&priv->font_iter, iter, sizeof (GtkTreeIter));
          
          gtk_font_chooser_widget_ensure_selection (fontchooser);
        }
1208 1209

      gtk_font_chooser_widget_update_marks (fontchooser);
1210
    }
1211

1212
  gtk_font_chooser_widget_update_preview_attributes (fontchooser);
1213 1214 1215

  g_object_notify (G_OBJECT (fontchooser), "font");
  g_object_notify (G_OBJECT (fontchooser), "font-desc");
1216 1217
}

1218
static void
1219 1220
gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
                                        PangoFontDescription *font_desc)
1221 1222
{
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1223 1224 1225 1226
  PangoFontMask mask;

  if (font_desc == NULL)
    font_desc = pango_font_description_from_string (GTK_FONT_CHOOSER_DEFAULT_FONT_NAME);
1227

1228 1229 1230
  mask = pango_font_description_get_set_fields (font_desc);
  if (mask & (PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_STYLE | PANGO_FONT_MASK_VARIANT |
              PANGO_FONT_MASK_WEIGHT | PANGO_FONT_MASK_STRETCH))
1231
    {
1232
      GtkTreeIter iter;
1233

1234 1235
      if (gtk_font_chooser_widget_find_font (fontchooser, font_desc, &iter))
        gtk_font_chooser_widget_merge_font_desc (fontchooser, font_desc, &iter);
1236
      else
1237
        gtk_font_chooser_widget_merge_font_desc (fontchooser, font_desc, NULL);
1238
    }
1239
  else
1240
    {
1241
      gtk_font_chooser_widget_merge_font_desc (fontchooser, font_desc, &priv->font_iter);
1242
    }
1243 1244

  pango_font_description_free (font_desc);
1245 1246 1247 1248 1249
}

static const gchar*
gtk_font_chooser_widget_get_preview_text (GtkFontChooserWidget *fontchooser)
{
1250
  return fontchooser->priv->preview_text;
1251 1252 1253 1254 1255 1256
}

static void
gtk_font_chooser_widget_set_preview_text (GtkFontChooserWidget *fontchooser,
                                          const gchar          *text)
{
1257
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1258

1259 1260
  g_free (priv->preview_text);
  priv->preview_text = g_strdup (text);
1261

1262
  gtk_entry_set_text (GTK_ENTRY (priv->preview), text);
1263 1264

  g_object_notify (G_OBJECT (fontchooser), "preview-text");
1265 1266 1267 1268

  /* XXX: There's no API to tell the treeview that a column has changed,
   * so we just */
  gtk_widget_queue_draw (priv->family_face_list);
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
}

static gboolean
gtk_font_chooser_widget_get_show_preview_entry (GtkFontChooserWidget *fontchooser)
{
  return fontchooser->priv->show_preview_entry;
}

static void
gtk_font_chooser_widget_set_show_preview_entry (GtkFontChooserWidget *fontchooser,
                                                gboolean              show_preview_entry)
{
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;

  if (priv->show_preview_entry != show_preview_entry)
    {
      fontchooser->priv->show_preview_entry = show_preview_entry;

      if (show_preview_entry)
        gtk_widget_show (fontchooser->priv->preview);
      else
        gtk_widget_hide (fontchooser->priv->preview);

      g_object_notify (G_OBJECT (fontchooser), "show-preview-entry");
    }
}

1296
static void
1297 1298
gtk_font_chooser_widget_set_font_map (GtkFontChooser *chooser,
                                      PangoFontMap   *fontmap)
1299
{
1300
  GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;

  if (g_set_object (&priv->font_map, fontmap))
    {
      PangoContext *context;

      if (!fontmap)
        fontmap = pango_cairo_font_map_get_default ();

      context = gtk_widget_get_pango_context (priv->family_face_list);
      pango_context_set_font_map (context, fontmap);

1313 1314 1315
      context = gtk_widget_get_pango_context (priv->preview);
      pango_context_set_font_map (context, fontmap);

1316 1317 1318 1319
      gtk_font_chooser_widget_load_fonts (fontchooser, TRUE);
    }
}

1320 1321 1322 1323 1324 1325 1326 1327 1328
static PangoFontMap *
gtk_font_chooser_widget_get_font_map (GtkFontChooser *chooser)
{
  GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;

  return priv->font_map;
}

1329 1330 1331 1332 1333 1334
static void
gtk_font_chooser_widget_set_filter_func (GtkFontChooser  *chooser,
                                         GtkFontFilterFunc filter,
                                         gpointer          data,
                                         GDestroyNotify    destroy)
{
1335
  GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1336 1337 1338 1339 1340 1341 1342 1343 1344
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;

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

  priv->filter_func = filter;
  priv->filter_data = data;
  priv->filter_data_destroy = destroy;

1345
  gtk_font_chooser_widget_refilter_font_list (fontchooser);
1346 1347 1348 1349 1350 1351 1352 1353 1354
}

static void
gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface)
{
  iface->get_font_family = gtk_font_chooser_widget_get_family;
  iface->get_font_face = gtk_font_chooser_widget_get_face;
  iface->get_font_size = gtk_font_chooser_widget_get_size;
  iface->set_filter_func = gtk_font_chooser_widget_set_filter_func;
1355 1356
  iface->set_font_map = gtk_font_chooser_widget_set_font_map;
  iface->get_font_map = gtk_font_chooser_widget_get_font_map;
1357
}
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368

gboolean
gtk_font_chooser_widget_handle_event (GtkWidget   *widget,
                                      GdkEventKey *key_event)
{
  GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (widget);
  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
  GdkEvent *event = (GdkEvent *)key_event;

  return gtk_search_entry_handle_event (GTK_SEARCH_ENTRY (priv->search_entry), event);
}