gucharmap-mini-fontsel.c 15.4 KB
Newer Older
1
/*
Christian Persch's avatar
Christian Persch committed
2
 * Copyright © 2004 Noah Levitt
3
 * Copyright © 2008 Christian Persch
4 5 6
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
Christian Persch's avatar
Christian Persch committed
7
 * Free Software Foundation; either version 3 of the License, or (at your
8 9 10 11 12 13 14 15 16
 * option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
18 19
 */

Christian Persch's avatar
Christian Persch committed
20
#include <config.h>
21

22 23
#include <string.h>

24
#include <glib/gi18n-lib.h>
25 26 27
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
Noah Levitt's avatar
Noah Levitt committed
28
#include "gucharmap-mini-fontsel.h"
29 30

#define I_(string) g_intern_static_string (string)
31

32 33 34
enum
{
  MIN_FONT_SIZE = 5,
35
  MAX_FONT_SIZE = 400,
36 37
};

38 39
enum
{
40 41
  PROP_0,
  PROP_FONT_DESC
42 43
};

44
enum
45
{
46 47 48
  COL_FAMILIY
};

49 50 51
static void gucharmap_mini_font_selection_class_init (GucharmapMiniFontSelectionClass *klass);
static void gucharmap_mini_font_selection_init       (GucharmapMiniFontSelection *fontsel);
static void gucharmap_mini_font_selection_finalize   (GObject *object);
52

53
G_DEFINE_TYPE (GucharmapMiniFontSelection, gucharmap_mini_font_selection, GTK_TYPE_HBOX)
54 55

static void
56
fill_font_families_combo (GucharmapMiniFontSelection *fontsel)
57 58
{
  PangoFontFamily **families;
59
  int n_families, i;
60

61 62 63 64 65
  pango_context_list_families (
          gtk_widget_get_pango_context (GTK_WIDGET (fontsel)),
          &families, &n_families);

  for (i = 0;  i < n_families;  i++)
66
    {
67 68 69 70 71 72 73 74
      PangoFontFamily *family = families[i];
      GtkTreeIter iter;

      gtk_list_store_insert_with_values (fontsel->family_store,
                                         &iter,
                                         -1,
                                         COL_FAMILIY, pango_font_family_get_name (family),
                                         -1);
75
    }
76

Noah Levitt's avatar
Noah Levitt committed
77
  g_free (families);
78 79 80 81 82

  /* Now turn on sorting in the combo box */
  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (fontsel->family_store),
                                        COL_FAMILIY,
                                        GTK_SORT_ASCENDING);
83
}
84 85

static void
86
update_font_family_combo (GucharmapMiniFontSelection *fontsel)
87 88 89 90 91 92 93
{
  GtkTreeModel *model = GTK_TREE_MODEL (fontsel->family_store);
  GtkTreeIter iter;
  const char *font_family;
  gboolean found = FALSE;

  font_family = pango_font_description_get_family (fontsel->font_desc);
94 95
  if (!font_family || !font_family[0]) {
    gtk_combo_box_set_active (GTK_COMBO_BOX (fontsel->family), -1);
96
    return;
97
  }
98 99 100 101 102 103 104 105 106 107 108 109 110 111

  if (!gtk_tree_model_get_iter_first (model, &iter))
    return;

  do {
    char *family;

    gtk_tree_model_get (model, &iter, COL_FAMILIY, &family, -1);
    found = family && strcmp (family, font_family) == 0;
    g_free (family);
  } while (!found && gtk_tree_model_iter_next (model, &iter));

  if (found) {
    gtk_combo_box_set_active_iter (GTK_COMBO_BOX (fontsel->family), &iter);
112 113
  } else {
    gtk_combo_box_set_active (GTK_COMBO_BOX (fontsel->family), -1);
114
  }
115 116
}

117
static void
118 119
family_combo_changed (GtkComboBox *combo,
                      GucharmapMiniFontSelection *fontsel)
120
{
121 122
  GtkTreeIter iter;
  char *family;
123

124 125
  if (!gtk_combo_box_get_active_iter (combo, &iter))
    return;
126

127 128 129 130 131
  gtk_tree_model_get (GTK_TREE_MODEL (fontsel->family_store),
                      &iter,
                      COL_FAMILIY, &family,
                      -1);
  if (!family)
132 133
    return;

134
  pango_font_description_set_family (fontsel->font_desc, family);
135
  g_free (family);
136 137

  g_object_notify (G_OBJECT (fontsel), "font-desc");
138 139
}

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
static gboolean
match_function (GtkEntryCompletion *completion,
                const gchar *key,
                GtkTreeIter *iter,
                gpointer user_data)
{
  GucharmapMiniFontSelection *fontsel = GUCHARMAP_MINI_FONT_SELECTION (user_data);
  char *family, *family_fold;
  gchar **sub_match, **p;
  gboolean all_matches = TRUE;

  gtk_tree_model_get (GTK_TREE_MODEL (fontsel->family_store),
                      iter,
                      COL_FAMILIY, &family,
                      -1);
  family_fold = g_utf8_casefold (family, -1);

  /* Match if all space separated substrings are part of family string */
  sub_match = g_strsplit (key, " ", -1);
  p = sub_match;
  while (*p) {
      gchar *match_fold = g_utf8_casefold (*p++, -1);
      if (!g_strstr_len (family_fold, -1, match_fold))
          all_matches = FALSE;
      g_free (match_fold);
      if (!all_matches)
          break;
  }

  g_free (family);
  g_free (family_fold);
  g_strfreev (sub_match);

  return all_matches;
}

static gboolean
completion_match_selected(GtkEntryCompletion *widget,
                          GtkTreeModel       *model,
                          GtkTreeIter        *iter,
                          GucharmapMiniFontSelection *fontsel)
{
  char *family;

  gtk_tree_model_get (GTK_TREE_MODEL (fontsel->family_store),
                      iter,
                      COL_FAMILIY, &family,
                      -1);
  pango_font_description_set_family (fontsel->font_desc, family);
  g_free (family);

  g_object_notify (G_OBJECT (fontsel), "font-desc");

  return FALSE;
}


197 198 199 200
/* returns font size in points */
static int
get_font_size (GucharmapMiniFontSelection *fontsel)
{
201
  return PANGO_PIXELS (pango_font_description_get_size (fontsel->font_desc));
202
}
203

Noah Levitt's avatar
Noah Levitt committed
204
/* size is in points */
205
static void
206 207
set_font_size (GucharmapMiniFontSelection *fontsel, 
               int size)
208
{
209 210
  size = CLAMP (size, MIN_FONT_SIZE, MAX_FONT_SIZE);
  pango_font_description_set_size (fontsel->font_desc, PANGO_SCALE * size);
211

212 213 214 215
  gtk_adjustment_set_value (GTK_ADJUSTMENT (fontsel->size_adj), size);

  g_object_notify (G_OBJECT (fontsel), "font-desc");
}
216

217
static void 
218 219
font_size_changed (GtkAdjustment *adjustment,
                   GucharmapMiniFontSelection *fontsel)
220
{
221
  int new_size;
222

223 224 225 226
  new_size = gtk_adjustment_get_value (adjustment);
  if (new_size != get_font_size (fontsel))
    set_font_size (fontsel, new_size);
}
227

Noah Levitt's avatar
Noah Levitt committed
228
static void
229
gucharmap_mini_font_selection_finalize (GObject *object)
Noah Levitt's avatar
Noah Levitt committed
230
{
Noah Levitt's avatar
Noah Levitt committed
231
  GucharmapMiniFontSelection *fontsel = GUCHARMAP_MINI_FONT_SELECTION (object);
Noah Levitt's avatar
Noah Levitt committed
232
  pango_font_description_free (fontsel->font_desc);
233 234

  G_OBJECT_CLASS (gucharmap_mini_font_selection_parent_class)->finalize (object);
Noah Levitt's avatar
Noah Levitt committed
235 236
}

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
static void
gucharmap_mini_font_selection_set_property (GObject *object,
                                            guint prop_id,
                                            const GValue *value,
                                            GParamSpec *pspec)
{
  GucharmapMiniFontSelection *mini_fontsel = GUCHARMAP_MINI_FONT_SELECTION (object);

  switch (prop_id) {
    case PROP_FONT_DESC:
      gucharmap_mini_font_selection_set_font_desc (mini_fontsel, g_value_get_boxed (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}
Noah Levitt's avatar
Noah Levitt committed
254

Noah Levitt's avatar
Noah Levitt committed
255
static void
256 257 258 259
gucharmap_mini_font_selection_get_property (GObject *object,
                                            guint prop_id,
                                            GValue *value,
                                            GParamSpec *pspec)
Noah Levitt's avatar
Noah Levitt committed
260
{
261 262 263 264 265 266 267 268 269 270
  GucharmapMiniFontSelection*mini_fontsel = GUCHARMAP_MINI_FONT_SELECTION (object);

  switch (prop_id) {
    case PROP_FONT_DESC:
      g_value_set_boxed (value, gucharmap_mini_font_selection_get_font_desc (mini_fontsel));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
271 272
}

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
static void
gucharmap_mini_font_selection_class_init (GucharmapMiniFontSelectionClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = gucharmap_mini_font_selection_finalize;
  gobject_class->get_property = gucharmap_mini_font_selection_get_property;
  gobject_class->set_property = gucharmap_mini_font_selection_set_property;

  g_object_class_install_property
    (gobject_class,
     PROP_FONT_DESC,
     g_param_spec_boxed ("font-desc", NULL, NULL,
                         PANGO_TYPE_FONT_DESCRIPTION,
                         G_PARAM_READWRITE |
                         G_PARAM_STATIC_NAME |
                         G_PARAM_STATIC_NICK |
                         G_PARAM_STATIC_BLURB));
}
292

293
static void
Noah Levitt's avatar
Noah Levitt committed
294 295
bold_toggled (GtkToggleButton *toggle,
              GucharmapMiniFontSelection *fontsel)
296
{
Noah Levitt's avatar
Noah Levitt committed
297 298 299 300 301
  if (gtk_toggle_button_get_active (toggle))
    pango_font_description_set_weight (fontsel->font_desc, PANGO_WEIGHT_BOLD);
  else
    pango_font_description_set_weight (fontsel->font_desc, PANGO_WEIGHT_NORMAL);

302
  g_object_notify (G_OBJECT (fontsel), "font-desc");
303 304
}

Noah Levitt's avatar
Noah Levitt committed
305 306 307
static void
italic_toggled (GtkToggleButton *toggle,
                GucharmapMiniFontSelection *fontsel)
308
{
Noah Levitt's avatar
Noah Levitt committed
309 310 311 312 313
  if (gtk_toggle_button_get_active (toggle))
    pango_font_description_set_style (fontsel->font_desc, PANGO_STYLE_ITALIC);
  else
    pango_font_description_set_style (fontsel->font_desc, PANGO_STYLE_NORMAL);

314
  g_object_notify (G_OBJECT (fontsel), "font-desc");
315 316
}

Noah Levitt's avatar
Noah Levitt committed
317
static void
Noah Levitt's avatar
Noah Levitt committed
318
gucharmap_mini_font_selection_init (GucharmapMiniFontSelection *fontsel)
319
{
Christian Persch's avatar
Christian Persch committed
320
  GtkStyle *style;
321
  AtkObject *accessib;
Noah Levitt's avatar
Noah Levitt committed
322 323

  gtk_widget_ensure_style (GTK_WIDGET (fontsel));
Christian Persch's avatar
Christian Persch committed
324 325
  style = gtk_widget_get_style (GTK_WIDGET (fontsel));
  fontsel->font_desc = pango_font_description_copy (style->font_desc);
326
  fontsel->default_size = -1;
Noah Levitt's avatar
Noah Levitt committed
327

328 329
  fontsel->size_adj = gtk_adjustment_new (MIN_FONT_SIZE, 
                                          MIN_FONT_SIZE, MAX_FONT_SIZE, 1, 8, 0);
330 331 332 333

  accessib = gtk_widget_get_accessible (GTK_WIDGET (fontsel));
  atk_object_set_name (accessib, _("Font"));

334
  gtk_box_set_spacing (GTK_BOX (fontsel), 6);
335

336
  fontsel->family_store = gtk_list_store_new (1, G_TYPE_STRING);
337

338 339 340 341 342 343 344 345 346 347 348
  fontsel->family = gtk_combo_box_new_with_model_and_entry (GTK_TREE_MODEL (fontsel->family_store));
  gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX(fontsel->family), COL_FAMILIY);
  fontsel->completion = gtk_entry_completion_new();
  gtk_entry_completion_set_text_column (fontsel->completion, COL_FAMILIY);
  gtk_entry_completion_set_model (fontsel->completion, GTK_TREE_MODEL (fontsel->family_store));
  gtk_entry_completion_set_match_func(fontsel->completion, match_function, fontsel, NULL);
  g_signal_connect (G_OBJECT (fontsel->completion), "match-selected",
                    G_CALLBACK (completion_match_selected), fontsel);

  gtk_entry_set_completion (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (fontsel->family))),
                            fontsel->completion);
349
  gtk_widget_show (fontsel->family);
350 351 352
  accessib = gtk_widget_get_accessible (fontsel->family);
  atk_object_set_name (accessib, _("Font Family"));

Noah Levitt's avatar
Noah Levitt committed
353 354
  fontsel->bold = gtk_toggle_button_new_with_mnemonic (GTK_STOCK_BOLD);
  gtk_button_set_use_stock (GTK_BUTTON (fontsel->bold), TRUE);
Noah Levitt's avatar
Noah Levitt committed
355 356 357 358
  gtk_widget_show (fontsel->bold);
  g_signal_connect (fontsel->bold, "toggled",
                    G_CALLBACK (bold_toggled), fontsel);

Noah Levitt's avatar
Noah Levitt committed
359 360
  fontsel->italic = gtk_toggle_button_new_with_mnemonic (GTK_STOCK_ITALIC);
  gtk_button_set_use_stock (GTK_BUTTON (fontsel->italic), TRUE);
Noah Levitt's avatar
Noah Levitt committed
361 362 363
  gtk_widget_show (fontsel->italic);
  g_signal_connect (fontsel->italic, "toggled",
                    G_CALLBACK (italic_toggled), fontsel);
364 365 366

  fontsel->size = gtk_spin_button_new (GTK_ADJUSTMENT (fontsel->size_adj),
                                       0, 0);
367
  gtk_widget_show (fontsel->size);
368 369
  accessib = gtk_widget_get_accessible (fontsel->size);
  atk_object_set_name (accessib, _("Font Size"));
Noah Levitt's avatar
Noah Levitt committed
370
  g_signal_connect (fontsel->size_adj, "value-changed",
371
                    G_CALLBACK (font_size_changed), fontsel);
372

373
  fill_font_families_combo (fontsel);
374 375

  gtk_combo_box_set_active (GTK_COMBO_BOX (fontsel->family), -1);
376
  g_signal_connect (fontsel->family, "changed",
377
                    G_CALLBACK (family_combo_changed), fontsel);
378 379

  gtk_box_pack_start (GTK_BOX (fontsel), fontsel->family, FALSE, FALSE, 0);
Noah Levitt's avatar
Noah Levitt committed
380 381
  gtk_box_pack_start (GTK_BOX (fontsel), fontsel->bold, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (fontsel), fontsel->italic, FALSE, FALSE, 0);
382 383
  gtk_box_pack_start (GTK_BOX (fontsel), fontsel->size, FALSE, FALSE, 0);

384 385 386 387
  gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (fontsel->family), FALSE);
  gtk_button_set_focus_on_click (GTK_BUTTON (fontsel->bold), FALSE);
  gtk_button_set_focus_on_click (GTK_BUTTON (fontsel->italic), FALSE);

388
  gtk_container_set_border_width (GTK_CONTAINER (fontsel), 6);
389 390

  gtk_widget_show_all (GTK_WIDGET (fontsel));
391 392 393
}

GtkWidget *
394
gucharmap_mini_font_selection_new (void)
395
{
Noah Levitt's avatar
Noah Levitt committed
396 397
  return GTK_WIDGET (g_object_new (gucharmap_mini_font_selection_get_type (), 
                                   NULL));
398 399
}

400 401 402
void
gucharmap_mini_font_selection_set_font_desc (GucharmapMiniFontSelection *fontsel,
                                             PangoFontDescription *font_desc)
403
{
404 405 406
  GObject *object = G_OBJECT (fontsel);
  PangoFontDescription *new_font_desc;
  const char *new_font_family;
407

408 409
  g_return_if_fail (GUCHARMAP_IS_MINI_FONT_SELECTION (fontsel));
  g_return_if_fail (font_desc != NULL);
410

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
  g_object_freeze_notify (object);

  new_font_desc = pango_font_description_copy (font_desc);
  new_font_family = pango_font_description_get_family (new_font_desc);
  if (!new_font_family) {
    pango_font_description_set_family (new_font_desc, "Sans");
    new_font_family = pango_font_description_get_family (new_font_desc);
  }

  if ((!fontsel->font_desc ||
       strcmp (pango_font_description_get_family (fontsel->font_desc), new_font_family) != 0) &&
      pango_font_description_get_size (new_font_desc) > 0)
    fontsel->default_size = pango_font_description_get_size (new_font_desc) / PANGO_SCALE;

  if (fontsel->font_desc)
    pango_font_description_free (fontsel->font_desc);
427
  
428
  fontsel->font_desc = new_font_desc;
429
  
430
  update_font_family_combo (fontsel);
431
    
Noah Levitt's avatar
Noah Levitt committed
432 433 434 435
  /* treat oblique and italic both as italic */
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fontsel->italic), pango_font_description_get_style (fontsel->font_desc) == PANGO_STYLE_ITALIC || pango_font_description_get_style (fontsel->font_desc) == PANGO_STYLE_OBLIQUE);

  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fontsel->bold), pango_font_description_get_weight (fontsel->font_desc) > PANGO_WEIGHT_NORMAL);
436

437 438 439
  gtk_adjustment_set_value (
          GTK_ADJUSTMENT (fontsel->size_adj), 
          pango_font_description_get_size (fontsel->font_desc) / PANGO_SCALE);
440

441
  g_object_notify (G_OBJECT (fontsel), "font-desc");
442

443
  g_object_thaw_notify (object);
444 445
}

446 447
PangoFontDescription *
gucharmap_mini_font_selection_get_font_desc (GucharmapMiniFontSelection *fontsel)
448
{
449
  g_return_val_if_fail (GUCHARMAP_IS_MINI_FONT_SELECTION (fontsel), NULL);
450

451
  return fontsel->font_desc;
452 453 454
}

void
455 456
gucharmap_mini_font_selection_change_font_size (GucharmapMiniFontSelection *fontsel,
                                                float factor)
457
{
458
  int size, new_size;
459

460 461 462 463
  g_return_if_fail (factor > 0.0f);

  size = get_font_size (fontsel);
  new_size = (float) size * factor;
464 465 466 467 468 469 470

  if (factor > 1.0f)
    new_size = MAX (new_size, size + 1);
  else if (factor < 1.0f)
    new_size = MIN (new_size, size - 1);

  set_font_size (fontsel, new_size);
Noah Levitt's avatar
Noah Levitt committed
471
}
472

Noah Levitt's avatar
Noah Levitt committed
473 474 475
void
gucharmap_mini_font_selection_reset_font_size (GucharmapMiniFontSelection *fontsel)
{
Christian Persch's avatar
Christian Persch committed
476
  if (fontsel->default_size > 0) {
477
    set_font_size (fontsel, fontsel->default_size);
Christian Persch's avatar
Christian Persch committed
478 479 480 481 482 483
  } else {
    GtkStyle *style;

    style = gtk_widget_get_style (GTK_WIDGET (fontsel));
    set_font_size (fontsel, pango_font_description_get_size (style->font_desc) * 2.0f / PANGO_SCALE);
  }
Noah Levitt's avatar
Noah Levitt committed
484
}