~alexlauni/ubuntu/lucid/gnome-control-center/fix-533888

« back to all changes in this revision

Viewing changes to capplets/font/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2007-06-13 12:30:08 UTC
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20070613123008-nmcon6qb309hkjek
Tags: upstream-2.19.3
ImportĀ upstreamĀ versionĀ 2.19.3

Show diffs side-by-side

added added

removed removed

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