~kroq-gar78/ubuntu/precise/gnome-control-center/fix-885947

« back to all changes in this revision

Viewing changes to capplets/appearance/appearance-font.c

  • Committer: Bazaar Package Importer
  • Author(s): Rodrigo Moya
  • Date: 2011-05-17 10:47:27 UTC
  • mfrom: (0.1.11 experimental) (1.1.45 upstream)
  • Revision ID: james.westby@ubuntu.com-20110517104727-lqel6m8vhfw5jby1
Tags: 1:3.0.1.1-1ubuntu1
* Rebase on Debian, remaining Ubuntu changes:
* debian/control:
  - Build-Depend on hardening-wrapper, dpkg-dev and dh-autoreconf
  - Add dependency on ubuntu-system-service
  - Remove dependency on gnome-icon-theme-symbolic
  - Move dependency on apg, gnome-icon-theme-symbolic and accountsservice to
    be a Recommends: until we get them in main
* debian/rules:
  - Use autoreconf
  - Add binary-post-install rule for gnome-control-center-data
  - Run dh-autoreconf
* debian/gnome-control-center.dirs:
* debian/gnome-control-center.links:
  - Add a link to the control center shell for indicators
* debian/patches/00_disable-nm.patch:
  - Temporary patch to disable building with NetworkManager until we get
    the new one in the archive
* debian/patches/01_git_remove_gettext_calls.patch:
  - Remove calls to AM_GNU_GETTEXT, IT_PROG_INTLTOOL should be enough
* debian/patches/01_git_kill_warning.patch:
  - Kill warning
* debian/patches/50_ubuntu_systemwide_prefs.patch:
  - Ubuntu specific proxy preferences
* debian/patches/51_ubuntu_system_keyboard.patch:
  - Implement the global keyboard spec at https://wiki.ubuntu.com/DefaultKeyboardSettings

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2007 The GNOME Foundation
3
 
 * Written by Jonathan Blandford <jrb@gnome.org>
4
 
 *            Jens Granseuer <jensgr@gmx.net>
5
 
 * All Rights Reserved
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; either version 2 of the License, or
10
 
 * (at your option) any later version.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public License along
18
 
 * with this program; if not, write to the Free Software Foundation, Inc.,
19
 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
 
 */
21
 
 
22
 
#include "appearance.h"
23
 
 
24
 
#include <string.h>
25
 
#include <stdarg.h>
26
 
#include <math.h>
27
 
 
28
 
#ifdef HAVE_XFT2
29
 
#include <gdk/gdkx.h>
30
 
#include <X11/Xft/Xft.h>
31
 
#endif /* HAVE_XFT2 */
32
 
#include <glib/gi18n.h>
33
 
 
34
 
#include "capplet-util.h"
35
 
#include "gconf-property-editor.h"
36
 
 
37
 
#define GTK_FONT_KEY           "/desktop/gnome/interface/font_name"
38
 
#define DESKTOP_FONT_KEY       "/apps/nautilus/preferences/desktop_font"
39
 
 
40
 
#define METACITY_DIR "/apps/metacity/general"
41
 
#define WINDOW_TITLE_FONT_KEY METACITY_DIR "/titlebar_font"
42
 
#define WINDOW_TITLE_USES_SYSTEM_KEY METACITY_DIR "/titlebar_uses_system_font"
43
 
#define MONOSPACE_FONT_KEY "/desktop/gnome/interface/monospace_font_name"
44
 
#define DOCUMENT_FONT_KEY "/desktop/gnome/interface/document_font_name"
45
 
 
46
 
#ifdef HAVE_XFT2
47
 
#define FONT_RENDER_DIR "/desktop/gnome/font_rendering"
48
 
#define FONT_ANTIALIASING_KEY FONT_RENDER_DIR "/antialiasing"
49
 
#define FONT_HINTING_KEY      FONT_RENDER_DIR "/hinting"
50
 
#define FONT_RGBA_ORDER_KEY   FONT_RENDER_DIR "/rgba_order"
51
 
#define FONT_DPI_KEY          FONT_RENDER_DIR "/dpi"
52
 
 
53
 
/* X servers sometimes lie about the screen's physical dimensions, so we cannot
54
 
 * compute an accurate DPI value.  When this happens, the user gets fonts that
55
 
 * are too huge or too tiny.  So, we see what the server returns:  if it reports
56
 
 * something outside of the range [DPI_LOW_REASONABLE_VALUE,
57
 
 * DPI_HIGH_REASONABLE_VALUE], then we assume that it is lying and we use
58
 
 * DPI_FALLBACK instead.
59
 
 *
60
 
 * See get_dpi_from_gconf_or_server() below, and also
61
 
 * https://bugzilla.novell.com/show_bug.cgi?id=217790
62
 
 */
63
 
#define DPI_FALLBACK 96
64
 
#define DPI_LOW_REASONABLE_VALUE 50
65
 
#define DPI_HIGH_REASONABLE_VALUE 500
66
 
#endif /* HAVE_XFT2 */
67
 
 
68
 
static gboolean in_change = FALSE;
69
 
static gchar *old_font = NULL;
70
 
 
71
 
#define MAX_FONT_POINT_WITHOUT_WARNING 32
72
 
#define MAX_FONT_SIZE_WITHOUT_WARNING MAX_FONT_POINT_WITHOUT_WARNING*1024
73
 
 
74
 
#ifdef HAVE_XFT2
75
 
 
76
 
/*
77
 
 * Code for displaying previews of font rendering with various Xft options
78
 
 */
79
 
 
80
 
static void
81
 
sample_size_request (GtkWidget      *darea,
82
 
                     GtkRequisition *requisition)
83
 
{
84
 
  GdkPixbuf *pixbuf = g_object_get_data (G_OBJECT (darea), "sample-pixbuf");
85
 
 
86
 
  requisition->width = gdk_pixbuf_get_width (pixbuf) + 2;
87
 
  requisition->height = gdk_pixbuf_get_height (pixbuf) + 2;
88
 
}
89
 
 
90
 
static void
91
 
sample_expose (GtkWidget      *darea,
92
 
               GdkEventExpose *expose)
93
 
{
94
 
  GtkAllocation allocation;
95
 
  GdkPixbuf *pixbuf = g_object_get_data (G_OBJECT (darea), "sample-pixbuf");
96
 
  GdkWindow *window = gtk_widget_get_window (darea);
97
 
  GtkStyle  *style = gtk_widget_get_style (darea);
98
 
  int width = gdk_pixbuf_get_width (pixbuf);
99
 
  int height = gdk_pixbuf_get_height (pixbuf);
100
 
  int x;
101
 
  int y;
102
 
 
103
 
  gtk_widget_get_allocation (darea, &allocation);
104
 
  x = (allocation.width - width) / 2;
105
 
  y = (allocation.height - height) / 2;
106
 
  gdk_draw_rectangle (window, style->white_gc, TRUE,
107
 
                      0, 0,
108
 
                      allocation.width, allocation.height);
109
 
  gdk_draw_rectangle (window, style->black_gc, FALSE,
110
 
                      0, 0,
111
 
                      allocation.width - 1, allocation.height - 1);
112
 
 
113
 
  gdk_draw_pixbuf (window, NULL, pixbuf, 0, 0, x, y, width, height,
114
 
                   GDK_RGB_DITHER_NORMAL, 0, 0);
115
 
}
116
 
 
117
 
typedef enum {
118
 
  ANTIALIAS_NONE,
119
 
  ANTIALIAS_GRAYSCALE,
120
 
  ANTIALIAS_RGBA
121
 
} Antialiasing;
122
 
 
123
 
static GConfEnumStringPair antialias_enums[] = {
124
 
  { ANTIALIAS_NONE,      "none" },
125
 
  { ANTIALIAS_GRAYSCALE, "grayscale" },
126
 
  { ANTIALIAS_RGBA,      "rgba" },
127
 
  { -1,                  NULL }
128
 
};
129
 
 
130
 
typedef enum {
131
 
  HINT_NONE,
132
 
  HINT_SLIGHT,
133
 
  HINT_MEDIUM,
134
 
  HINT_FULL
135
 
} Hinting;
136
 
 
137
 
static GConfEnumStringPair hint_enums[] = {
138
 
  { HINT_NONE,   "none" },
139
 
  { HINT_SLIGHT, "slight" },
140
 
  { HINT_MEDIUM, "medium" },
141
 
  { HINT_FULL,   "full" },
142
 
  { -1,          NULL }
143
 
};
144
 
 
145
 
typedef enum {
146
 
  RGBA_RGB,
147
 
  RGBA_BGR,
148
 
  RGBA_VRGB,
149
 
  RGBA_VBGR
150
 
} RgbaOrder;
151
 
 
152
 
static GConfEnumStringPair rgba_order_enums[] = {
153
 
  { RGBA_RGB,  "rgb" },
154
 
  { RGBA_BGR,  "bgr" },
155
 
  { RGBA_VRGB, "vrgb" },
156
 
  { RGBA_VBGR, "vbgr" },
157
 
  { -1,         NULL }
158
 
};
159
 
 
160
 
static XftFont *
161
 
open_pattern (FcPattern   *pattern,
162
 
              Antialiasing antialiasing,
163
 
              Hinting      hinting)
164
 
{
165
 
#ifdef FC_HINT_STYLE
166
 
  static const int hintstyles[] = {
167
 
     FC_HINT_NONE, FC_HINT_SLIGHT, FC_HINT_MEDIUM, FC_HINT_FULL
168
 
  };
169
 
#endif /* FC_HINT_STYLE */
170
 
 
171
 
  FcPattern *res_pattern;
172
 
  FcResult result;
173
 
  XftFont *font;
174
 
 
175
 
  Display *xdisplay = gdk_x11_get_default_xdisplay ();
176
 
  int screen = gdk_x11_get_default_screen ();
177
 
 
178
 
  res_pattern = XftFontMatch (xdisplay, screen, pattern, &result);
179
 
  if (res_pattern == NULL)
180
 
    return NULL;
181
 
 
182
 
  FcPatternDel (res_pattern, FC_HINTING);
183
 
  FcPatternAddBool (res_pattern, FC_HINTING, hinting != HINT_NONE);
184
 
 
185
 
#ifdef FC_HINT_STYLE
186
 
  FcPatternDel (res_pattern, FC_HINT_STYLE);
187
 
  FcPatternAddInteger (res_pattern, FC_HINT_STYLE, hintstyles[hinting]);
188
 
#endif /* FC_HINT_STYLE */
189
 
 
190
 
  FcPatternDel (res_pattern, FC_ANTIALIAS);
191
 
  FcPatternAddBool (res_pattern, FC_ANTIALIAS, antialiasing != ANTIALIAS_NONE);
192
 
 
193
 
  FcPatternDel (res_pattern, FC_RGBA);
194
 
  FcPatternAddInteger (res_pattern, FC_RGBA,
195
 
                       antialiasing == ANTIALIAS_RGBA ? FC_RGBA_RGB : FC_RGBA_NONE);
196
 
 
197
 
  FcPatternDel (res_pattern, FC_DPI);
198
 
  FcPatternAddInteger (res_pattern, FC_DPI, 96);
199
 
 
200
 
  font = XftFontOpenPattern (xdisplay, res_pattern);
201
 
  if (!font)
202
 
    FcPatternDestroy (res_pattern);
203
 
 
204
 
  return font;
205
 
}
206
 
 
207
 
static void
208
 
setup_font_sample (GtkWidget   *darea,
209
 
                   Antialiasing antialiasing,
210
 
                   Hinting      hinting)
211
 
{
212
 
  const char *string1 = "abcfgop AO ";
213
 
  const char *string2 = "abcfgop";
214
 
 
215
 
  XftColor black, white;
216
 
  XRenderColor rendcolor;
217
 
 
218
 
  Display *xdisplay = gdk_x11_get_default_xdisplay ();
219
 
 
220
 
  GdkColormap *colormap = gdk_rgb_get_colormap ();
221
 
  Colormap xcolormap = GDK_COLORMAP_XCOLORMAP (colormap);
222
 
 
223
 
  GdkVisual *visual = gdk_colormap_get_visual (colormap);
224
 
  Visual *xvisual = GDK_VISUAL_XVISUAL (visual);
225
 
 
226
 
  FcPattern *pattern;
227
 
  XftFont *font1, *font2;
228
 
  XGlyphInfo extents1 = { 0 };
229
 
  XGlyphInfo extents2 = { 0 };
230
 
  GdkPixmap *pixmap;
231
 
  XftDraw *draw;
232
 
  GdkPixbuf *tmp_pixbuf, *pixbuf;
233
 
 
234
 
  int width, height;
235
 
  int ascent, descent;
236
 
 
237
 
  pattern = FcPatternBuild (NULL,
238
 
                            FC_FAMILY, FcTypeString, "Serif",
239
 
                            FC_SLANT, FcTypeInteger, FC_SLANT_ROMAN,
240
 
                            FC_SIZE, FcTypeDouble, 18.,
241
 
                            NULL);
242
 
  font1 = open_pattern (pattern, antialiasing, hinting);
243
 
  FcPatternDestroy (pattern);
244
 
 
245
 
  pattern = FcPatternBuild (NULL,
246
 
                            FC_FAMILY, FcTypeString, "Serif",
247
 
                            FC_SLANT, FcTypeInteger, FC_SLANT_ITALIC,
248
 
                            FC_SIZE, FcTypeDouble, 20.,
249
 
                            NULL);
250
 
  font2 = open_pattern (pattern, antialiasing, hinting);
251
 
  FcPatternDestroy (pattern);
252
 
 
253
 
  ascent = 0;
254
 
  descent = 0;
255
 
  if (font1) {
256
 
    XftTextExtentsUtf8 (xdisplay, font1, (unsigned char *) string1,
257
 
                        strlen (string1), &extents1);
258
 
    ascent = MAX (ascent, font1->ascent);
259
 
    descent = MAX (descent, font1->descent);
260
 
  }
261
 
 
262
 
  if (font2) {
263
 
    XftTextExtentsUtf8 (xdisplay, font2, (unsigned char *) string2,
264
 
                        strlen (string2), &extents2);
265
 
    ascent = MAX (ascent, font2->ascent);
266
 
    descent = MAX (descent, font2->descent);
267
 
  }
268
 
 
269
 
  width = extents1.xOff + extents2.xOff + 4;
270
 
  height = ascent + descent + 2;
271
 
 
272
 
  pixmap = gdk_pixmap_new (NULL, width, height, visual->depth);
273
 
 
274
 
  draw = XftDrawCreate (xdisplay, GDK_DRAWABLE_XID (pixmap), xvisual, xcolormap);
275
 
 
276
 
  rendcolor.red = 0;
277
 
  rendcolor.green = 0;
278
 
  rendcolor.blue = 0;
279
 
  rendcolor.alpha = 0xffff;
280
 
  XftColorAllocValue (xdisplay, xvisual, xcolormap, &rendcolor, &black);
281
 
 
282
 
  rendcolor.red = 0xffff;
283
 
  rendcolor.green = 0xffff;
284
 
  rendcolor.blue = 0xffff;
285
 
  rendcolor.alpha = 0xffff;
286
 
  XftColorAllocValue (xdisplay, xvisual, xcolormap, &rendcolor, &white);
287
 
  XftDrawRect (draw, &white, 0, 0, width, height);
288
 
  if (font1)
289
 
    XftDrawStringUtf8 (draw, &black, font1,
290
 
                       2, 2 + ascent,
291
 
                       (unsigned char *) string1, strlen (string1));
292
 
  if (font2)
293
 
    XftDrawStringUtf8 (draw, &black, font2,
294
 
                       2 + extents1.xOff, 2 + ascent,
295
 
                       (unsigned char *) string2, strlen (string2));
296
 
 
297
 
  XftDrawDestroy (draw);
298
 
 
299
 
  if (font1)
300
 
    XftFontClose (xdisplay, font1);
301
 
  if (font2)
302
 
    XftFontClose (xdisplay, font2);
303
 
 
304
 
  tmp_pixbuf = gdk_pixbuf_get_from_drawable (NULL, pixmap, colormap, 0, 0, 0, 0, width, height);
305
 
  pixbuf = gdk_pixbuf_scale_simple (tmp_pixbuf, 1 * width, 1 * height, GDK_INTERP_TILES);
306
 
 
307
 
  g_object_unref (pixmap);
308
 
  g_object_unref (tmp_pixbuf);
309
 
 
310
 
  g_object_set_data_full (G_OBJECT (darea), "sample-pixbuf",
311
 
                          pixbuf, (GDestroyNotify) g_object_unref);
312
 
 
313
 
  g_signal_connect (darea, "size_request", G_CALLBACK (sample_size_request), NULL);
314
 
  g_signal_connect (darea, "expose_event", G_CALLBACK (sample_expose), NULL);
315
 
}
316
 
 
317
 
/*
318
 
 * Code implementing a group of radio buttons with different Xft option combinations.
319
 
 * If one of the buttons is matched by the GConf key, we pick it. Otherwise we
320
 
 * show the group as inconsistent.
321
 
 */
322
 
static void
323
 
font_render_get_gconf (GConfClient  *client,
324
 
                       Antialiasing *antialiasing,
325
 
                       Hinting      *hinting)
326
 
{
327
 
  gchar *antialias_str = gconf_client_get_string (client, FONT_ANTIALIASING_KEY, NULL);
328
 
  gchar *hint_str = gconf_client_get_string (client, FONT_HINTING_KEY, NULL);
329
 
  gint val;
330
 
 
331
 
  val = ANTIALIAS_GRAYSCALE;
332
 
  if (antialias_str) {
333
 
    gconf_string_to_enum (antialias_enums, antialias_str, &val);
334
 
    g_free (antialias_str);
335
 
  }
336
 
  *antialiasing = val;
337
 
 
338
 
  val = HINT_FULL;
339
 
  if (hint_str) {
340
 
    gconf_string_to_enum (hint_enums, hint_str, &val);
341
 
    g_free (hint_str);
342
 
  }
343
 
  *hinting = val;
344
 
}
345
 
 
346
 
typedef struct {
347
 
  Antialiasing antialiasing;
348
 
  Hinting hinting;
349
 
  GtkToggleButton *radio;
350
 
} FontPair;
351
 
 
352
 
static GSList *font_pairs = NULL;
353
 
 
354
 
static void
355
 
font_render_load (GConfClient *client)
356
 
{
357
 
  Antialiasing antialiasing;
358
 
  Hinting hinting;
359
 
  gboolean inconsistent = TRUE;
360
 
  GSList *tmp_list;
361
 
 
362
 
  font_render_get_gconf (client, &antialiasing, &hinting);
363
 
 
364
 
  in_change = TRUE;
365
 
 
366
 
  for (tmp_list = font_pairs; tmp_list; tmp_list = tmp_list->next) {
367
 
    FontPair *pair = tmp_list->data;
368
 
 
369
 
    if (antialiasing == pair->antialiasing && hinting == pair->hinting) {
370
 
      gtk_toggle_button_set_active (pair->radio, TRUE);
371
 
      inconsistent = FALSE;
372
 
      break;
373
 
    }
374
 
  }
375
 
 
376
 
  for (tmp_list = font_pairs; tmp_list; tmp_list = tmp_list->next) {
377
 
    FontPair *pair = tmp_list->data;
378
 
 
379
 
    gtk_toggle_button_set_inconsistent (pair->radio, inconsistent);
380
 
  }
381
 
 
382
 
  in_change = FALSE;
383
 
}
384
 
 
385
 
static void
386
 
font_render_changed (GConfClient *client,
387
 
                     guint        cnxn_id,
388
 
                     GConfEntry  *entry,
389
 
                     gpointer     user_data)
390
 
{
391
 
  font_render_load (client);
392
 
}
393
 
 
394
 
static void
395
 
font_radio_toggled (GtkToggleButton *toggle_button,
396
 
                    FontPair        *pair)
397
 
{
398
 
  if (!in_change) {
399
 
    GConfClient *client = gconf_client_get_default ();
400
 
 
401
 
    gconf_client_set_string (client, FONT_ANTIALIASING_KEY,
402
 
                             gconf_enum_to_string (antialias_enums, pair->antialiasing),
403
 
                             NULL);
404
 
    gconf_client_set_string (client, FONT_HINTING_KEY,
405
 
                             gconf_enum_to_string (hint_enums, pair->hinting),
406
 
                             NULL);
407
 
 
408
 
    /* Restore back to the previous state until we get notification */
409
 
    font_render_load (client);
410
 
    g_object_unref (client);
411
 
  }
412
 
}
413
 
 
414
 
static void
415
 
setup_font_pair (GtkWidget    *radio,
416
 
                 GtkWidget    *darea,
417
 
                 Antialiasing  antialiasing,
418
 
                 Hinting       hinting)
419
 
{
420
 
  FontPair *pair = g_new (FontPair, 1);
421
 
 
422
 
  pair->antialiasing = antialiasing;
423
 
  pair->hinting = hinting;
424
 
  pair->radio = GTK_TOGGLE_BUTTON (radio);
425
 
 
426
 
  setup_font_sample (darea, antialiasing, hinting);
427
 
  font_pairs = g_slist_prepend (font_pairs, pair);
428
 
 
429
 
  g_signal_connect (radio, "toggled",
430
 
                    G_CALLBACK (font_radio_toggled), pair);
431
 
}
432
 
#endif /* HAVE_XFT2 */
433
 
 
434
 
static void
435
 
metacity_titlebar_load_sensitivity (AppearanceData *data)
436
 
{
437
 
  gtk_widget_set_sensitive (appearance_capplet_get_widget (data, "window_title_font"),
438
 
                            !gconf_client_get_bool (data->client,
439
 
                                                    WINDOW_TITLE_USES_SYSTEM_KEY,
440
 
                                                    NULL));
441
 
}
442
 
 
443
 
static void
444
 
metacity_changed (GConfClient *client,
445
 
                  guint        cnxn_id,
446
 
                  GConfEntry  *entry,
447
 
                  gpointer     user_data)
448
 
{
449
 
  metacity_titlebar_load_sensitivity (user_data);
450
 
}
451
 
 
452
 
/* returns 0 if the font is safe, otherwise returns the size in points. */
453
 
static gint
454
 
font_dangerous (const char *font)
455
 
{
456
 
  PangoFontDescription *pfd;
457
 
  gboolean retval = 0;
458
 
 
459
 
  pfd = pango_font_description_from_string (font);
460
 
  if (pfd == NULL)
461
 
    /* an invalid font was passed in.  This isn't our problem. */
462
 
    return 0;
463
 
 
464
 
  if ((pango_font_description_get_set_fields (pfd) & PANGO_FONT_MASK_SIZE) &&
465
 
      (pango_font_description_get_size (pfd) >= MAX_FONT_SIZE_WITHOUT_WARNING)) {
466
 
    retval = pango_font_description_get_size (pfd)/1024;
467
 
  }
468
 
  pango_font_description_free (pfd);
469
 
 
470
 
  return retval;
471
 
}
472
 
 
473
 
static GConfValue *
474
 
application_font_to_gconf (GConfPropertyEditor *peditor,
475
 
                           GConfValue          *value)
476
 
{
477
 
  GConfValue *new_value;
478
 
  const char *new_font;
479
 
  GtkWidget *font_button;
480
 
  gint danger_level;
481
 
 
482
 
  font_button = GTK_WIDGET (gconf_property_editor_get_ui_control (peditor));
483
 
  g_return_val_if_fail (font_button != NULL, NULL);
484
 
 
485
 
  new_value = gconf_value_new (GCONF_VALUE_STRING);
486
 
  new_font = gconf_value_get_string (value);
487
 
  if (font_dangerous (old_font)) {
488
 
    /* If we're already too large, we don't warn again. */
489
 
    gconf_value_set_string (new_value, new_font);
490
 
    return new_value;
491
 
  }
492
 
 
493
 
  danger_level = font_dangerous (new_font);
494
 
  if (danger_level) {
495
 
    GtkWidget *warning_dialog, *apply_button;
496
 
    const gchar *warning_label;
497
 
    gchar *warning_label2;
498
 
 
499
 
    warning_label = _("Font may be too large");
500
 
 
501
 
    if (danger_level > MAX_FONT_POINT_WITHOUT_WARNING) {
502
 
      warning_label2 = g_strdup_printf (ngettext (
503
 
                        "The font selected is %d point large, "
504
 
                        "and may make it difficult to effectively "
505
 
                        "use the computer.  It is recommended that "
506
 
                        "you select a size smaller than %d.",
507
 
                        "The font selected is %d points large, "
508
 
                        "and may make it difficult to effectively "
509
 
                        "use the computer. It is recommended that "
510
 
                        "you select a size smaller than %d.",
511
 
                        danger_level),
512
 
                        danger_level,
513
 
                        MAX_FONT_POINT_WITHOUT_WARNING);
514
 
    } else {
515
 
      warning_label2 = g_strdup_printf (ngettext (
516
 
                        "The font selected is %d point large, "
517
 
                        "and may make it difficult to effectively "
518
 
                        "use the computer.  It is recommended that "
519
 
                        "you select a smaller sized font.",
520
 
                        "The font selected is %d points large, "
521
 
                        "and may make it difficult to effectively "
522
 
                        "use the computer. It is recommended that "
523
 
                        "you select a smaller sized font.",
524
 
                        danger_level),
525
 
                        danger_level);
526
 
    }
527
 
 
528
 
    warning_dialog = gtk_message_dialog_new (NULL,
529
 
                                             GTK_DIALOG_MODAL,
530
 
                                             GTK_MESSAGE_WARNING,
531
 
                                             GTK_BUTTONS_NONE,
532
 
                                             "%s",
533
 
                                             warning_label);
534
 
 
535
 
    gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (warning_dialog),
536
 
                                              "%s", warning_label2);
537
 
 
538
 
    gtk_dialog_add_button (GTK_DIALOG (warning_dialog),
539
 
                           _("Use previous font"), GTK_RESPONSE_CLOSE);
540
 
 
541
 
    apply_button = gtk_button_new_with_label (_("Use selected font"));
542
 
 
543
 
    gtk_button_set_image (GTK_BUTTON (apply_button), gtk_image_new_from_stock (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON));
544
 
    gtk_dialog_add_action_widget (GTK_DIALOG (warning_dialog), apply_button, GTK_RESPONSE_APPLY);
545
 
    gtk_widget_set_can_default (apply_button, TRUE);
546
 
    gtk_widget_show (apply_button);
547
 
 
548
 
    gtk_dialog_set_default_response (GTK_DIALOG (warning_dialog), GTK_RESPONSE_CLOSE);
549
 
 
550
 
    g_free (warning_label2);
551
 
 
552
 
    if (gtk_dialog_run (GTK_DIALOG (warning_dialog)) == GTK_RESPONSE_APPLY) {
553
 
      gconf_value_set_string (new_value, new_font);
554
 
    } else {
555
 
      gconf_value_set_string (new_value, old_font);
556
 
      gtk_font_button_set_font_name (GTK_FONT_BUTTON (font_button), old_font);
557
 
    }
558
 
 
559
 
    gtk_widget_destroy (warning_dialog);
560
 
  } else {
561
 
    gconf_value_set_string (new_value, new_font);
562
 
  }
563
 
 
564
 
  return new_value;
565
 
}
566
 
 
567
 
static void
568
 
application_font_changed (GtkWidget *font_button)
569
 
{
570
 
  const gchar *font;
571
 
 
572
 
  font = gtk_font_button_get_font_name (GTK_FONT_BUTTON (font_button));
573
 
  g_free (old_font);
574
 
  old_font = g_strdup (font);
575
 
}
576
 
 
577
 
#ifdef HAVE_XFT2
578
 
/*
579
 
 * EnumGroup - a group of radio buttons tied to a string enumeration
580
 
 *             value. We add this here because the gconf peditor
581
 
 *             equivalent of this is both painful to use (you have
582
 
 *             to supply functions to convert from enums to indices)
583
 
 *             and conceptually broken (the order of radio buttons
584
 
 *             in a group when using Glade is not predictable.
585
 
 */
586
 
typedef struct
587
 
{
588
 
  GConfClient *client;
589
 
  GSList *items;
590
 
  gchar *gconf_key;
591
 
  GConfEnumStringPair *enums;
592
 
  int default_value;
593
 
} EnumGroup;
594
 
 
595
 
typedef struct
596
 
{
597
 
  EnumGroup *group;
598
 
  GtkToggleButton *widget;
599
 
  int value;
600
 
} EnumItem;
601
 
 
602
 
static void
603
 
enum_group_load (EnumGroup *group)
604
 
{
605
 
  gchar *str = gconf_client_get_string (group->client, group->gconf_key, NULL);
606
 
  gint val = group->default_value;
607
 
  GSList *tmp_list;
608
 
 
609
 
  if (str)
610
 
    gconf_string_to_enum (group->enums, str, &val);
611
 
 
612
 
  g_free (str);
613
 
 
614
 
  in_change = TRUE;
615
 
 
616
 
  for (tmp_list = group->items; tmp_list; tmp_list = tmp_list->next) {
617
 
    EnumItem *item = tmp_list->data;
618
 
 
619
 
    if (val == item->value)
620
 
      gtk_toggle_button_set_active (item->widget, TRUE);
621
 
  }
622
 
 
623
 
  in_change = FALSE;
624
 
}
625
 
 
626
 
static void
627
 
enum_group_changed (GConfClient *client,
628
 
                    guint        cnxn_id,
629
 
                    GConfEntry  *entry,
630
 
                    gpointer     user_data)
631
 
{
632
 
  enum_group_load (user_data);
633
 
}
634
 
 
635
 
static void
636
 
enum_item_toggled (GtkToggleButton *toggle_button,
637
 
                   EnumItem        *item)
638
 
{
639
 
  EnumGroup *group = item->group;
640
 
 
641
 
  if (!in_change) {
642
 
    gconf_client_set_string (group->client, group->gconf_key,
643
 
                             gconf_enum_to_string (group->enums, item->value),
644
 
                             NULL);
645
 
  }
646
 
 
647
 
  /* Restore back to the previous state until we get notification */
648
 
  enum_group_load (group);
649
 
}
650
 
 
651
 
static EnumGroup *
652
 
enum_group_create (const gchar         *gconf_key,
653
 
                   GConfEnumStringPair *enums,
654
 
                   int                  default_value,
655
 
                   GtkWidget           *first_widget,
656
 
                   ...)
657
 
{
658
 
  EnumGroup *group;
659
 
  GtkWidget *widget;
660
 
  va_list args;
661
 
 
662
 
  group = g_new (EnumGroup, 1);
663
 
 
664
 
  group->client = gconf_client_get_default ();
665
 
  group->gconf_key = g_strdup (gconf_key);
666
 
  group->enums = enums;
667
 
  group->default_value = default_value;
668
 
  group->items = NULL;
669
 
 
670
 
  va_start (args, first_widget);
671
 
 
672
 
  widget = first_widget;
673
 
  while (widget) {
674
 
    EnumItem *item;
675
 
 
676
 
    item = g_new (EnumItem, 1);
677
 
    item->group = group;
678
 
    item->widget = GTK_TOGGLE_BUTTON (widget);
679
 
    item->value = va_arg (args, int);
680
 
 
681
 
    g_signal_connect (item->widget, "toggled",
682
 
                      G_CALLBACK (enum_item_toggled), item);
683
 
 
684
 
    group->items = g_slist_prepend (group->items, item);
685
 
 
686
 
    widget = va_arg (args, GtkWidget *);
687
 
  }
688
 
 
689
 
  va_end (args);
690
 
 
691
 
  enum_group_load (group);
692
 
 
693
 
  gconf_client_notify_add (group->client, gconf_key,
694
 
                           enum_group_changed,
695
 
                           group, NULL, NULL);
696
 
 
697
 
  return group;
698
 
}
699
 
 
700
 
static void
701
 
enum_group_destroy (EnumGroup *group)
702
 
{
703
 
  g_object_unref (group->client);
704
 
  g_free (group->gconf_key);
705
 
 
706
 
  g_slist_foreach (group->items, (GFunc) g_free, NULL);
707
 
  g_slist_free (group->items);
708
 
 
709
 
  g_free (group);
710
 
}
711
 
 
712
 
static double
713
 
dpi_from_pixels_and_mm (int pixels, int mm)
714
 
{
715
 
  double dpi;
716
 
 
717
 
  if (mm >= 1)
718
 
    dpi = pixels / (mm / 25.4);
719
 
  else
720
 
    dpi = 0;
721
 
 
722
 
  return dpi;
723
 
}
724
 
 
725
 
static double
726
 
get_dpi_from_x_server (void)
727
 
{
728
 
  GdkScreen *screen;
729
 
  double dpi;
730
 
 
731
 
  screen = gdk_screen_get_default ();
732
 
  if (screen) {
733
 
    double width_dpi, height_dpi;
734
 
 
735
 
    width_dpi = dpi_from_pixels_and_mm (gdk_screen_get_width (screen),
736
 
                                        gdk_screen_get_width_mm (screen));
737
 
    height_dpi = dpi_from_pixels_and_mm (gdk_screen_get_height (screen),
738
 
                                         gdk_screen_get_height_mm (screen));
739
 
 
740
 
    if (width_dpi < DPI_LOW_REASONABLE_VALUE || width_dpi > DPI_HIGH_REASONABLE_VALUE ||
741
 
        height_dpi < DPI_LOW_REASONABLE_VALUE || height_dpi > DPI_HIGH_REASONABLE_VALUE)
742
 
      dpi = DPI_FALLBACK;
743
 
    else
744
 
      dpi = (width_dpi + height_dpi) / 2.0;
745
 
  } else {
746
 
    /* Huh!?  No screen? */
747
 
    dpi = DPI_FALLBACK;
748
 
  }
749
 
 
750
 
  return dpi;
751
 
}
752
 
 
753
 
/*
754
 
 * The font rendering details dialog
755
 
 */
756
 
static void
757
 
dpi_load (GConfClient   *client,
758
 
          GtkSpinButton *spinner)
759
 
{
760
 
  GConfValue *value;
761
 
  gdouble dpi;
762
 
 
763
 
  value = gconf_client_get_without_default (client, FONT_DPI_KEY, NULL);
764
 
 
765
 
  if (value) {
766
 
    dpi = gconf_value_get_float (value);
767
 
    gconf_value_free (value);
768
 
  } else
769
 
    dpi = get_dpi_from_x_server ();
770
 
 
771
 
  if (dpi < DPI_LOW_REASONABLE_VALUE)
772
 
    dpi = DPI_LOW_REASONABLE_VALUE;
773
 
 
774
 
  in_change = TRUE;
775
 
  gtk_spin_button_set_value (spinner, dpi);
776
 
  in_change = FALSE;
777
 
}
778
 
 
779
 
static void
780
 
dpi_changed (GConfClient *client,
781
 
             guint        cnxn_id,
782
 
             GConfEntry  *entry,
783
 
             gpointer     user_data)
784
 
{
785
 
  dpi_load (client, user_data);
786
 
}
787
 
 
788
 
static void
789
 
dpi_value_changed (GtkSpinButton *spinner,
790
 
                   GConfClient   *client)
791
 
{
792
 
  /* Like any time when using a spin button with GConf, there is
793
 
   * a race condition here. When we change, we send the new
794
 
   * value to GConf, then restore to the old value until
795
 
   * we get a response to emulate the proper model/view behavior.
796
 
   *
797
 
   * If the user changes the value faster than responses are
798
 
   * received from GConf, this may cause mildly strange effects.
799
 
   */
800
 
  if (!in_change) {
801
 
    gdouble new_dpi = gtk_spin_button_get_value (spinner);
802
 
 
803
 
    gconf_client_set_float (client, FONT_DPI_KEY, new_dpi, NULL);
804
 
 
805
 
    dpi_load (client, spinner);
806
 
  }
807
 
}
808
 
 
809
 
static void
810
 
cb_details_response (GtkDialog *dialog, gint response_id)
811
 
{
812
 
  if (response_id == GTK_RESPONSE_HELP) {
813
 
    capplet_help (GTK_WINDOW (dialog),
814
 
                  "goscustdesk-38");
815
 
  } else
816
 
    gtk_widget_hide (GTK_WIDGET (dialog));
817
 
}
818
 
 
819
 
static void
820
 
cb_show_details (GtkWidget *button,
821
 
                 AppearanceData *data)
822
 
{
823
 
  if (!data->font_details) {
824
 
    GtkAdjustment *adjustment;
825
 
    GtkWidget *widget;
826
 
    EnumGroup *group;
827
 
 
828
 
    data->font_details = appearance_capplet_get_widget (data, "render_details");
829
 
 
830
 
    gtk_window_set_transient_for (GTK_WINDOW (data->font_details),
831
 
                                  GTK_WINDOW (appearance_capplet_get_widget (data, "appearance_window")));
832
 
 
833
 
    widget = appearance_capplet_get_widget (data, "dpi_spinner");
834
 
 
835
 
    /* pick a sensible maximum dpi */
836
 
    adjustment = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (widget));
837
 
    gtk_adjustment_set_lower (adjustment, DPI_LOW_REASONABLE_VALUE);
838
 
    gtk_adjustment_set_upper (adjustment, DPI_HIGH_REASONABLE_VALUE);
839
 
    gtk_adjustment_set_step_increment (adjustment, 1);
840
 
 
841
 
    dpi_load (data->client, GTK_SPIN_BUTTON (widget));
842
 
    g_signal_connect (widget, "value_changed",
843
 
                      G_CALLBACK (dpi_value_changed), data->client);
844
 
 
845
 
    gconf_client_notify_add (data->client, FONT_DPI_KEY,
846
 
                             dpi_changed, widget, NULL, NULL);
847
 
 
848
 
    setup_font_sample (appearance_capplet_get_widget (data, "antialias_none_sample"),      ANTIALIAS_NONE,      HINT_FULL);
849
 
    setup_font_sample (appearance_capplet_get_widget (data, "antialias_grayscale_sample"), ANTIALIAS_GRAYSCALE, HINT_FULL);
850
 
    setup_font_sample (appearance_capplet_get_widget (data, "antialias_subpixel_sample"),  ANTIALIAS_RGBA,      HINT_FULL);
851
 
 
852
 
    group = enum_group_create (
853
 
        FONT_ANTIALIASING_KEY, antialias_enums, ANTIALIAS_GRAYSCALE,
854
 
        appearance_capplet_get_widget (data, "antialias_none_radio"),      ANTIALIAS_NONE,
855
 
        appearance_capplet_get_widget (data, "antialias_grayscale_radio"), ANTIALIAS_GRAYSCALE,
856
 
        appearance_capplet_get_widget (data, "antialias_subpixel_radio"),  ANTIALIAS_RGBA,
857
 
        NULL);
858
 
    data->font_groups = g_slist_prepend (data->font_groups, group);
859
 
 
860
 
    setup_font_sample (appearance_capplet_get_widget (data, "hint_none_sample"),   ANTIALIAS_GRAYSCALE, HINT_NONE);
861
 
    setup_font_sample (appearance_capplet_get_widget (data, "hint_slight_sample"), ANTIALIAS_GRAYSCALE, HINT_SLIGHT);
862
 
    setup_font_sample (appearance_capplet_get_widget (data, "hint_medium_sample"), ANTIALIAS_GRAYSCALE, HINT_MEDIUM);
863
 
    setup_font_sample (appearance_capplet_get_widget (data, "hint_full_sample"),   ANTIALIAS_GRAYSCALE, HINT_FULL);
864
 
 
865
 
    group = enum_group_create (FONT_HINTING_KEY, hint_enums, HINT_FULL,
866
 
                               appearance_capplet_get_widget (data, "hint_none_radio"),   HINT_NONE,
867
 
                               appearance_capplet_get_widget (data, "hint_slight_radio"), HINT_SLIGHT,
868
 
                               appearance_capplet_get_widget (data, "hint_medium_radio"), HINT_MEDIUM,
869
 
                               appearance_capplet_get_widget (data, "hint_full_radio"),   HINT_FULL,
870
 
                               NULL);
871
 
    data->font_groups = g_slist_prepend (data->font_groups, group);
872
 
 
873
 
    gtk_image_set_from_file (GTK_IMAGE (appearance_capplet_get_widget (data, "subpixel_rgb_image")),
874
 
                             GNOMECC_PIXMAP_DIR "/subpixel-rgb.png");
875
 
    gtk_image_set_from_file (GTK_IMAGE (appearance_capplet_get_widget (data, "subpixel_bgr_image")),
876
 
                             GNOMECC_PIXMAP_DIR "/subpixel-bgr.png");
877
 
    gtk_image_set_from_file (GTK_IMAGE (appearance_capplet_get_widget (data, "subpixel_vrgb_image")),
878
 
                             GNOMECC_PIXMAP_DIR "/subpixel-vrgb.png");
879
 
    gtk_image_set_from_file (GTK_IMAGE (appearance_capplet_get_widget (data, "subpixel_vbgr_image")),
880
 
                             GNOMECC_PIXMAP_DIR "/subpixel-vbgr.png");
881
 
 
882
 
    group = enum_group_create (FONT_RGBA_ORDER_KEY, rgba_order_enums, RGBA_RGB,
883
 
                               appearance_capplet_get_widget (data, "subpixel_rgb_radio"),  RGBA_RGB,
884
 
                               appearance_capplet_get_widget (data, "subpixel_bgr_radio"),  RGBA_BGR,
885
 
                               appearance_capplet_get_widget (data, "subpixel_vrgb_radio"), RGBA_VRGB,
886
 
                               appearance_capplet_get_widget (data, "subpixel_vbgr_radio"), RGBA_VBGR,
887
 
                               NULL);
888
 
    data->font_groups = g_slist_prepend (data->font_groups, group);
889
 
 
890
 
    g_signal_connect (G_OBJECT (data->font_details),
891
 
                      "response",
892
 
                      G_CALLBACK (cb_details_response), NULL);
893
 
    g_signal_connect (G_OBJECT (data->font_details),
894
 
                      "delete_event",
895
 
                      G_CALLBACK (gtk_true), NULL);
896
 
  }
897
 
 
898
 
  gtk_window_present (GTK_WINDOW (data->font_details));
899
 
}
900
 
#endif /* HAVE_XFT2 */
901
 
 
902
 
void
903
 
font_init (AppearanceData *data)
904
 
{
905
 
  GObject *peditor;
906
 
  GtkWidget *widget;
907
 
 
908
 
  data->font_details = NULL;
909
 
  data->font_groups = NULL;
910
 
 
911
 
  gconf_client_add_dir (data->client, "/desktop/gnome/interface",
912
 
                        GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
913
 
  gconf_client_add_dir (data->client, "/apps/nautilus/preferences",
914
 
                        GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
915
 
  gconf_client_add_dir (data->client, METACITY_DIR,
916
 
                        GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
917
 
#ifdef HAVE_XFT2
918
 
  gconf_client_add_dir (data->client, FONT_RENDER_DIR,
919
 
                        GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
920
 
#endif  /* HAVE_XFT2 */
921
 
 
922
 
  widget = appearance_capplet_get_widget (data, "application_font");
923
 
  peditor = gconf_peditor_new_font (NULL, GTK_FONT_KEY,
924
 
                                    widget,
925
 
                                    "conv-from-widget-cb", application_font_to_gconf,
926
 
                                    NULL);
927
 
  g_signal_connect_swapped (peditor, "value-changed",
928
 
                            G_CALLBACK (application_font_changed),
929
 
                            widget);
930
 
  application_font_changed (widget);
931
 
 
932
 
  peditor = gconf_peditor_new_font (NULL, DOCUMENT_FONT_KEY,
933
 
                                    appearance_capplet_get_widget (data, "document_font"),
934
 
                                    NULL);
935
 
 
936
 
  peditor = gconf_peditor_new_font (NULL, DESKTOP_FONT_KEY,
937
 
                                    appearance_capplet_get_widget (data, "desktop_font"),
938
 
                                    NULL);
939
 
 
940
 
  peditor = gconf_peditor_new_font (NULL, WINDOW_TITLE_FONT_KEY,
941
 
                                    appearance_capplet_get_widget (data, "window_title_font"),
942
 
                                    NULL);
943
 
 
944
 
  peditor = gconf_peditor_new_font (NULL, MONOSPACE_FONT_KEY,
945
 
                                    appearance_capplet_get_widget (data, "monospace_font"),
946
 
                                    NULL);
947
 
 
948
 
  gconf_client_notify_add (data->client, WINDOW_TITLE_USES_SYSTEM_KEY,
949
 
                           metacity_changed,
950
 
                           data, NULL, NULL);
951
 
 
952
 
  metacity_titlebar_load_sensitivity (data);
953
 
 
954
 
#ifdef HAVE_XFT2
955
 
  setup_font_pair (appearance_capplet_get_widget (data, "monochrome_radio"),
956
 
                   appearance_capplet_get_widget (data, "monochrome_sample"),
957
 
                   ANTIALIAS_NONE, HINT_FULL);
958
 
  setup_font_pair (appearance_capplet_get_widget (data, "best_shapes_radio"),
959
 
                   appearance_capplet_get_widget (data, "best_shapes_sample"),
960
 
                   ANTIALIAS_GRAYSCALE, HINT_MEDIUM);
961
 
  setup_font_pair (appearance_capplet_get_widget (data, "best_contrast_radio"),
962
 
                   appearance_capplet_get_widget (data, "best_contrast_sample"),
963
 
                   ANTIALIAS_GRAYSCALE, HINT_FULL);
964
 
  setup_font_pair (appearance_capplet_get_widget (data, "subpixel_radio"),
965
 
                   appearance_capplet_get_widget (data, "subpixel_sample"),
966
 
                   ANTIALIAS_RGBA, HINT_FULL);
967
 
 
968
 
  font_render_load (data->client);
969
 
 
970
 
  gconf_client_notify_add (data->client, FONT_RENDER_DIR,
971
 
                           font_render_changed,
972
 
                           data->client, NULL, NULL);
973
 
 
974
 
  g_signal_connect (appearance_capplet_get_widget (data, "details_button"),
975
 
                    "clicked", G_CALLBACK (cb_show_details), data);
976
 
#else /* !HAVE_XFT2 */
977
 
  gtk_widget_hide (appearance_capplet_get_widget (data, "font_render_frame"));
978
 
#endif /* HAVE_XFT2 */
979
 
}
980
 
 
981
 
void
982
 
font_shutdown (AppearanceData *data)
983
 
{
984
 
  g_slist_foreach (data->font_groups, (GFunc) enum_group_destroy, NULL);
985
 
  g_slist_free (data->font_groups);
986
 
  g_slist_foreach (font_pairs, (GFunc) g_free, NULL);
987
 
  g_slist_free (font_pairs);
988
 
  g_free (old_font);
989
 
}