Rewrote some prohibited APIs in gtk/ (sprintf, strcpy, strcat).
[obnox/wireshark/wip.git] / gtk / font_utils.c
1 /* font_utils.c
2  * Utilities to use for font manipulation
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <gtk/gtk.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33
34 #include <epan/packet.h>
35 #include <epan/ws_strsplit.h>
36
37 #ifdef _WIN32
38 #include <windows.h>
39 #include <tchar.h>
40 #include <epan/unicode-utils.h>
41 #endif
42
43 #include "main.h"
44 #include "recent.h"
45 #include <epan/prefs.h>
46
47 #include "gtkglobals.h"
48
49 #include "compat_macros.h"
50 #include "font_utils.h"
51 #include "simple_dialog.h"
52
53 #include "packet_list.h"
54 #include "proto_draw.h"
55 #include "follow_tcp.h"
56
57
58
59 #if GTK_MAJOR_VERSION < 2
60 guint        m_font_height, m_font_width;
61 #endif
62 FONT_TYPE *m_r_font, *m_b_font;
63
64
65 /* Get the regular user font.
66  *
67  * @return the regular user font
68  */
69 FONT_TYPE *user_font_get_regular(void)
70 {
71     return m_r_font;
72 }
73
74 /* Get the bold user font.
75  *
76  * @return the bold user font
77  */
78 FONT_TYPE *user_font_get_bold(void)
79 {
80     return m_b_font;
81 }
82
83 #if GTK_MAJOR_VERSION < 2
84 /* Get the regular user font height.
85  *
86  * @return the regular user font height
87  */
88 guint user_font_get_regular_height(void)
89 {
90     return m_font_height;
91 }
92
93 /* Get the regular user font width.
94  *
95  * @return the regular user font width
96  */
97 guint user_font_get_regular_width(void)
98 {
99     return m_font_width;
100 }
101 #endif
102
103
104 static void
105 set_fonts(FONT_TYPE *regular, FONT_TYPE *bold)
106 {
107         /* Yes, assert. The code that loads the font should check
108          * for NULL and provide its own error message. */
109         g_assert(m_r_font && m_b_font);
110         m_r_font = regular;
111         m_b_font = bold;
112
113 #if GTK_MAJOR_VERSION < 2
114         m_font_height = m_r_font->ascent + m_r_font->descent;
115         m_font_width = gdk_string_width(m_r_font, "0");
116 #endif
117 }
118
119 void
120 view_zoom_in_cb(GtkWidget *w _U_, gpointer d _U_)
121 {
122     gint save_gui_zoom_level;
123
124     save_gui_zoom_level = recent.gui_zoom_level;
125     recent.gui_zoom_level++;
126     switch (user_font_apply()) {
127
128     case FA_SUCCESS:
129         break;
130
131     case FA_FONT_NOT_RESIZEABLE:
132         /* "font_apply()" popped up an alert box. */
133         recent.gui_zoom_level = save_gui_zoom_level;    /* undo zoom */
134         break;
135
136     case FA_FONT_NOT_AVAILABLE:
137         /* We assume this means that the specified size isn't available. */
138         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
139             "Your current font isn't available in the next larger size.\n");
140         recent.gui_zoom_level = save_gui_zoom_level;    /* undo zoom */
141         break;
142     }
143 }
144
145 void
146 view_zoom_out_cb(GtkWidget *w _U_, gpointer d _U_)
147 {
148     gint save_gui_zoom_level;
149
150     save_gui_zoom_level = recent.gui_zoom_level;
151     recent.gui_zoom_level--;
152     switch (user_font_apply()) {
153
154     case FA_SUCCESS:
155         break;
156
157     case FA_FONT_NOT_RESIZEABLE:
158         /* "font_apply()" popped up an alert box. */
159         recent.gui_zoom_level = save_gui_zoom_level;    /* undo zoom */
160         break;
161
162     case FA_FONT_NOT_AVAILABLE:
163         /* We assume this means that the specified size isn't available. */
164         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
165             "Your current font isn't available in the next smaller size.\n");
166         recent.gui_zoom_level = save_gui_zoom_level;    /* undo zoom */
167         break;
168     }
169 }
170
171 void
172 view_zoom_100_cb(GtkWidget *w _U_, gpointer d _U_)
173 {
174     gint save_gui_zoom_level;
175
176     save_gui_zoom_level = recent.gui_zoom_level;
177     recent.gui_zoom_level = 0;
178     switch (user_font_apply()) {
179
180     case FA_SUCCESS:
181         break;
182
183     case FA_FONT_NOT_RESIZEABLE:
184         /* "font_apply()" popped up an alert box. */
185         recent.gui_zoom_level = save_gui_zoom_level;    /* undo zoom */
186         break;
187
188     case FA_FONT_NOT_AVAILABLE:
189         /* We assume this means that the specified size isn't available.
190            XXX - this "shouldn't happen". */
191         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
192             "Your current font couldn't be reloaded at the size you selected.\n");
193         recent.gui_zoom_level = save_gui_zoom_level;    /* undo zoom */
194         break;
195     }
196 }
197
198
199 #if GTK_MAJOR_VERSION < 2
200 /* Given a font name, construct the name of the next heavier version of
201    that font. */
202
203 #define XLFD_WEIGHT     3       /* index of the "weight" field */
204
205 /* Map from a given weight to the appropriate weight for the "bold"
206    version of a font.
207    XXX - the XLFD says these strings shouldn't be used for font matching;
208    can we get the weight, as a number, from GDK, and ask GDK to find us
209    a font just like the given font, but with the appropriate higher
210    weight? */
211 static const struct {
212         char    *light;
213         char    *heavier;
214 } weight_map[] = {
215         { "ultralight", "light" },
216         { "extralight", "semilight" },
217         { "light",      "medium" },
218         { "semilight",  "semibold" },
219         { "medium",     "bold" },
220         { "normal",     "bold" },
221         { "semibold",   "extrabold" },
222         { "bold",       "ultrabold" }
223 };
224 #define N_WEIGHTS       (sizeof weight_map / sizeof weight_map[0])
225
226 /* Try to convert a font name to it's bold version.
227  *
228  * @param the font to convert
229  * @return the bold font
230  */
231 static char *
232 user_font_boldify(const char *font_name)
233 {
234         char *bold_font_name;
235         gchar **xlfd_tokens;
236         unsigned int i;
237
238         /* Is this an XLFD font?  If it begins with "-", yes, otherwise no. */
239         if (font_name[0] == '-') {
240                 xlfd_tokens = g_strsplit(font_name, "-", XLFD_WEIGHT+1);
241
242                 /*
243                  * Make sure we *have* a weight (this might not be a valid
244                  * XLFD font name).
245                  */
246                 for (i = 0; i < XLFD_WEIGHT+1; i++) {
247                         if (xlfd_tokens[i] == NULL) {
248                                 /*
249                                  * We don't, so treat this as a non-XLFD
250                                  * font name.
251                                  */
252                                 goto not_xlfd;
253                         }
254                 }
255                 for (i = 0; i < N_WEIGHTS; i++) {
256                         if (strcmp(xlfd_tokens[XLFD_WEIGHT],
257                             weight_map[i].light) == 0) {
258                                 g_free(xlfd_tokens[XLFD_WEIGHT]);
259                                 xlfd_tokens[XLFD_WEIGHT] =
260                                     g_strdup(weight_map[i].heavier);
261                                 break;
262                         }
263                 }
264                 bold_font_name = g_strjoinv("-", xlfd_tokens);
265                 g_strfreev(xlfd_tokens);
266                 return bold_font_name;
267         }
268
269 not_xlfd:
270         /*
271          * This isn't an XLFD font name; just append "bold" to the name
272          * of the font.
273          */
274         bold_font_name = g_strconcat(font_name, "bold", NULL);
275         return bold_font_name;
276 }
277 #endif
278
279
280 gboolean
281 user_font_test(gchar *font_name)
282 {
283 #if GTK_MAJOR_VERSION < 2
284         gchar   *bold_font_name;
285 #endif
286         FONT_TYPE *new_r_font, *new_b_font;
287
288 #if GTK_MAJOR_VERSION < 2
289         /* Get the name that the boldface version of that font would have. */
290         bold_font_name = user_font_boldify(font_name);
291
292         /* Now load those fonts, just to make sure we can. */
293         new_r_font = gdk_font_load(font_name);
294 #else
295         new_r_font = pango_font_description_from_string(font_name);
296 #endif
297         if (new_r_font == NULL) {
298                 /* Oops, that font didn't work.
299                    Tell the user, but don't tear down the font selection
300                    dialog, so that they can try again. */
301                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
302                    "The font you selected can't be loaded.");
303
304 #if GTK_MAJOR_VERSION < 2
305                 g_free(bold_font_name);
306 #endif
307                 return FALSE;
308         }
309
310 #if GTK_MAJOR_VERSION < 2
311         new_b_font = gdk_font_load(bold_font_name);
312 #else
313         new_b_font = pango_font_description_copy(new_r_font);
314         pango_font_description_set_weight(new_b_font, PANGO_WEIGHT_BOLD);
315 #endif
316         if (new_b_font == NULL) {
317                 /* Oops, that font didn't work.
318                    Tell the user, but don't tear down the font selection
319                    dialog, so that they can try again. */
320                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
321                    "The font you selected doesn't have a boldface version.");
322
323 #if GTK_MAJOR_VERSION < 2
324                 g_free(bold_font_name);
325                 gdk_font_unref(new_r_font);
326 #else
327                 pango_font_description_free(new_r_font);
328 #endif
329                 return FALSE;
330         }
331
332         return TRUE;
333 }
334
335
336
337 /* Given a font name, construct the name of a version of that font with
338    the current zoom factor applied. */
339 static char *
340 font_zoom(char *gui_font_name)
341 {
342     char *new_font_name;
343     char *font_name_dup;
344     char *font_name_p;
345     long font_point_size_l;
346 #if GTK_MAJOR_VERSION < 2
347     int minus_chars;
348     char *font_foundry;
349     char *font_family;
350     char *font_weight;
351     char *font_slant;
352     char *font_set_width;
353     char *font_add_style;
354     char *font_pixel_size;
355     char *font_point_size;
356     char *font_res_x;
357     char *font_res_y;
358     char *font_spacing;
359     char *font_aver_width;
360     char *font_charset_reg;
361     char *font_charset_encoding;
362 #endif
363
364     if (recent.gui_zoom_level == 0) {
365         /* There is no zoom factor - just return the name, so that if
366            this is GTK+ 1.2[.x] and the font name isn't an XLFD font
367            name, we don't fail. */
368         return g_strdup(gui_font_name);
369     }
370
371     font_name_dup = g_strdup(gui_font_name);
372     font_name_p = font_name_dup;
373
374 #if GTK_MAJOR_VERSION >= 2
375     /* find the start of the font_size string */
376     font_name_p = strrchr(font_name_dup, ' ');
377     *font_name_p = '\0';
378     font_name_p++;
379
380     /* calculate the new font size */
381     font_point_size_l = strtol(font_name_p, NULL, 10);
382     font_point_size_l += recent.gui_zoom_level;
383
384     /* build a new font name */
385     new_font_name = g_strdup_printf("%s %ld", font_name_dup, font_point_size_l);
386 #else
387     minus_chars = 0;
388     /* replace all '-' chars by NUL and count them */
389     while ((font_name_p = strchr(font_name_p, '-')) != NULL) {
390         *font_name_p = '\0';
391         font_name_p++;
392         minus_chars++;
393     }
394
395     if (minus_chars != 14) {
396         /*
397          * Not a valid XLFD font name.
398          * XXX - can we try scaling it by looking for a size at the end
399          * and tweaking that?  Unfortunately, some fonts have numbers
400          * at the end that aren't, as far as I know, sizes, e.g. "nil2".
401          */
402         return NULL;
403     }
404
405     /* first element (font name registry) empty */
406     font_name_p = font_name_dup;
407     font_name_p += strlen(font_name_p);
408     font_name_p++;
409
410     /* get pointers to all font name elements */
411     font_foundry = font_name_p;
412     font_name_p += strlen(font_name_p);
413     font_name_p++;
414
415     font_family = font_name_p;
416     font_name_p += strlen(font_name_p);
417     font_name_p++;
418
419     font_weight = font_name_p;
420     font_name_p += strlen(font_name_p);
421     font_name_p++;
422
423     font_slant = font_name_p;
424     font_name_p += strlen(font_name_p);
425     font_name_p++;
426
427     font_set_width = font_name_p;
428     font_name_p += strlen(font_name_p);
429     font_name_p++;
430
431     font_add_style = font_name_p;
432     font_name_p += strlen(font_name_p);
433     font_name_p++;
434
435     font_pixel_size = font_name_p;
436     font_name_p += strlen(font_name_p);
437     font_name_p++;
438
439     font_point_size = font_name_p;
440     font_name_p += strlen(font_name_p);
441     font_name_p++;
442
443     font_res_x = font_name_p;
444     font_name_p += strlen(font_name_p);
445     font_name_p++;
446
447     font_res_y = font_name_p;
448     font_name_p += strlen(font_name_p);
449     font_name_p++;
450
451     font_spacing = font_name_p;
452     font_name_p += strlen(font_name_p);
453     font_name_p++;
454
455     font_aver_width = font_name_p;
456     font_name_p += strlen(font_name_p);
457     font_name_p++;
458
459     font_charset_reg = font_name_p;
460     font_name_p += strlen(font_name_p);
461     font_name_p++;
462
463     font_charset_encoding = font_name_p;
464     font_name_p += strlen(font_name_p);
465     font_name_p++;
466
467     /* calculate the new font size */
468     font_point_size_l = strtol(font_point_size, NULL, 10);
469     font_point_size_l += recent.gui_zoom_level*10;
470     if (font_point_size_l <= 0)
471         font_point_size_l = 10;
472
473     /* build a new font name */
474     new_font_name = g_strdup_printf("-%s-%s-%s-%s-%s-%s-%s-%ld-%s-%s-%s-%s-%s-%s",
475         font_foundry, font_family, font_weight, font_slant, font_set_width,
476         font_add_style, font_pixel_size, font_point_size_l, font_res_x,
477         font_res_y, font_spacing, font_aver_width, font_charset_reg,
478         font_charset_encoding);
479 #endif
480
481     g_free(font_name_dup);
482
483     return new_font_name;
484 }
485
486 fa_ret_t
487 user_font_apply(void) {
488     char *gui_font_name;
489 #if GTK_MAJOR_VERSION < 2
490     char *bold_font_name;
491 #endif
492     FONT_TYPE *new_r_font, *new_b_font;
493     FONT_TYPE *old_r_font = NULL, *old_b_font = NULL;
494
495     /* convert font name to reflect the zoom level */
496     gui_font_name = font_zoom(prefs.PREFS_GUI_FONT_NAME);
497     if (gui_font_name == NULL) {
498         /*
499          * This means the font name isn't an XLFD font name.
500          * We just report that for now as a font not available in
501          * multiple sizes.
502          */
503         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
504             "Your current font isn't available in any other sizes.\n");
505         return FA_FONT_NOT_RESIZEABLE;
506     }
507
508     /* load normal and bold font */
509 #if GTK_MAJOR_VERSION < 2
510     new_r_font = gdk_font_load(gui_font_name);
511     bold_font_name = user_font_boldify(gui_font_name);
512     new_b_font = gdk_font_load(bold_font_name);
513 #else
514     new_r_font = pango_font_description_from_string(gui_font_name);
515     new_b_font = pango_font_description_copy(new_r_font);
516     pango_font_description_set_weight(new_b_font, PANGO_WEIGHT_BOLD);
517 #endif
518
519     if (new_r_font == NULL || new_b_font == NULL) {
520         /* We're no longer using the new fonts; unreference them. */
521 #if GTK_MAJOR_VERSION < 2
522         if (new_r_font != NULL)
523             gdk_font_unref(new_r_font);
524         if (new_b_font != NULL)
525             gdk_font_unref(new_b_font);
526 #else
527         if (new_r_font != NULL)
528             pango_font_description_free(new_r_font);
529         if (new_b_font != NULL)
530             pango_font_description_free(new_b_font);
531 #endif
532         g_free(gui_font_name);
533
534         /* We let our caller pop up a dialog box, as the error message
535            depends on the context (did they zoom in or out, or did they
536            do something else? */
537         return FA_FONT_NOT_AVAILABLE;
538     }
539
540     /* the font(s) seem to be ok */
541     packet_list_set_font(new_r_font);
542     set_ptree_font_all(new_r_font);
543     old_r_font = m_r_font;
544     old_b_font = m_b_font;
545     set_fonts(new_r_font, new_b_font);
546 #if GTK_MAJOR_VERSION < 2
547     g_free(bold_font_name);
548 #endif
549
550     /* Redraw the hex dump windows. */
551     redraw_hex_dump_all();
552
553     /* Redraw the "Follow TCP Stream" windows. */
554     follow_tcp_redraw_all();
555
556     /* We're no longer using the old fonts; unreference them. */
557 #if GTK_MAJOR_VERSION < 2
558     if (old_r_font != NULL)
559         gdk_font_unref(old_r_font);
560     if (old_b_font != NULL)
561         gdk_font_unref(old_b_font);
562 #else
563     if (old_r_font != NULL)
564         pango_font_description_free(old_r_font);
565     if (old_b_font != NULL)
566         pango_font_description_free(old_b_font);
567 #endif
568     g_free(gui_font_name);
569
570     return FA_SUCCESS;
571 }
572
573
574 #ifdef _WIN32
575
576 #define NAME_BUFFER_LEN 32
577
578 #if GTK_MAJOR_VERSION < 2
579
580
581 /* The setting of the MS default font for system stuff (menus, dialogs, ...),
582  * coming from: Allin Cottrell, http://www.ecn.wfu.edu/~cottrell/gtk_win32,
583  * Thank you very much for this! */
584 static int get_windows_font_gtk1(char *fontspec, int fontspec_len)
585 {
586     HDC h_dc;
587     HGDIOBJ h_font;
588     TEXTMETRIC tm;
589     TCHAR name[NAME_BUFFER_LEN];
590     int len, pix_height;
591
592     h_dc = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
593     if (h_dc == NULL) return 1;
594     h_font = GetStockObject(DEFAULT_GUI_FONT);
595     if (h_font == NULL || !SelectObject(h_dc, h_font)) {
596         DeleteDC(h_dc);
597         return 1;
598     }
599     len = GetTextFace(h_dc, NAME_BUFFER_LEN, name);
600     if (len <= 0) {
601         DeleteDC(h_dc);
602         return 1;
603     }
604     if (!GetTextMetrics(h_dc, &tm)) {
605         DeleteDC(h_dc);
606         return 1;
607     }
608     pix_height = tm.tmHeight;
609     DeleteDC(h_dc);
610     g_snprintf(fontspec, fontspec_len, "-*-%s-*-*-*-*-%i-*-*-*-p-*-iso8859-1",
611             utf_16to8(name), pix_height);
612     return 0;
613 }
614
615 void app_font_gtk1_init(GtkWidget *top_level_w)
616 {
617     GtkStyle *style;
618     char winfont[80];
619
620     style = gtk_widget_get_style(top_level_w);
621     if (get_windows_font_gtk1(winfont, sizeof(winfont)) == 0)
622         style->font = gdk_font_load(winfont);
623     if (style->font) gtk_widget_set_style(top_level_w, style);
624 }
625
626
627 #else /* GTK_MAJOR_VERSION */
628 static char appfontname[128] = "tahoma 8";
629
630 static void
631 set_app_font_gtk2(const char *fontname)
632 {
633     GtkSettings *settings;
634
635     if (fontname != NULL && *fontname == 0) return;
636
637     settings = gtk_settings_get_default();
638
639     if (fontname == NULL) {
640         g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
641     } else {
642         GtkWidget *w;
643         PangoFontDescription *pfd;
644         PangoContext *pc;
645         PangoFont *pfont;
646
647         w = gtk_label_new(NULL);
648         pfd = pango_font_description_from_string(fontname);
649         pc = gtk_widget_get_pango_context(w);
650         pfont = pango_context_load_font(pc, pfd);
651
652         if (pfont != NULL) {
653             strncpy(appfontname, fontname, 128);
654             appfontname[127] = '\0';
655             g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
656         }
657
658         gtk_widget_destroy(w);
659         pango_font_description_free(pfd);
660     }
661 }
662
663 static char *default_windows_menu_fontspec_gtk2(void)
664 {
665     gchar *fontspec = NULL;
666     NONCLIENTMETRICS ncm;
667
668     memset(&ncm, 0, sizeof ncm);
669     ncm.cbSize = sizeof ncm;
670
671     if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0)) {
672         HDC screen = GetDC(0);
673         double y_scale = 72.0 / GetDeviceCaps(screen, LOGPIXELSY);
674         int point_size = (int) (ncm.lfMenuFont.lfHeight * y_scale);
675
676         if (point_size < 0) point_size = -point_size;
677         fontspec = g_strdup_printf("%s %d", ncm.lfMenuFont.lfFaceName,
678                                    point_size);
679         ReleaseDC(0, screen);
680     }
681
682     return fontspec;
683 }
684
685 static void try_to_get_windows_font_gtk2(void)
686 {
687     gchar *fontspec;
688
689     fontspec = default_windows_menu_fontspec_gtk2();
690
691     if (fontspec != NULL) {
692         int match = 0;
693         PangoFontDescription *pfd;
694         PangoFont *pfont;
695         PangoContext *pc;
696         GtkWidget *w;
697
698         pfd = pango_font_description_from_string(fontspec);
699
700         w = gtk_label_new(NULL);
701         pc = gtk_widget_get_pango_context(w);
702         pfont = pango_context_load_font(pc, pfd);
703         match = (pfont != NULL);
704
705         pango_font_description_free(pfd);
706         g_object_unref(G_OBJECT(pc));
707         gtk_widget_destroy(w);
708
709         if (match) set_app_font_gtk2(fontspec);
710         g_free(fontspec);
711     }
712 }
713 #endif /* GTK_MAJOR_VERSION */
714
715 #endif /* _WIN32 */
716
717
718 void font_init(void)
719 {
720 #if GTK_MAJOR_VERSION < 2
721   gchar *bold_font_name;
722 #endif
723
724 #ifdef _WIN32
725 #if GTK_MAJOR_VERSION >= 2
726   /* try to load the application font for GTK2 */
727   try_to_get_windows_font_gtk2();
728 #endif
729 #endif
730
731   /* Try to load the regular and boldface fixed-width fonts */
732 #if GTK_MAJOR_VERSION < 2
733   bold_font_name = user_font_boldify(prefs.gui_font_name1);
734   m_r_font = gdk_font_load(prefs.gui_font_name1);
735   m_b_font = gdk_font_load(bold_font_name);
736   if (m_r_font == NULL || m_b_font == NULL) {
737     /* XXX - pop this up as a dialog box? no */
738     if (m_r_font == NULL) {
739         fprintf(stderr, "wireshark: Warning: font %s not found - defaulting to 6x13 and 6x13bold\n",
740                 prefs.gui_font_name1);
741     } else {
742       gdk_font_unref(m_r_font);
743     }
744     if (m_b_font == NULL) {
745         fprintf(stderr, "wireshark: Warning: font %s not found - defaulting to 6x13 and 6x13bold\n",
746                 bold_font_name);
747     } else {
748       gdk_font_unref(m_b_font);
749     }
750     g_free(bold_font_name);
751     if ((m_r_font = gdk_font_load("6x13")) == NULL) {
752       fprintf(stderr, "wireshark: Error: font 6x13 not found\n");
753       exit(1);
754     }
755     if ((m_b_font = gdk_font_load("6x13bold")) == NULL) {
756       fprintf(stderr, "wireshark: Error: font 6x13bold not found\n");
757       exit(1);
758     }
759     g_free(prefs.gui_font_name1);
760     prefs.gui_font_name1 = g_strdup("6x13");
761   }
762 #else /* GTK_MAJOR_VERSION */
763   m_r_font = pango_font_description_from_string(prefs.gui_font_name2);
764   m_b_font = pango_font_description_copy(m_r_font);
765   pango_font_description_set_weight(m_b_font, PANGO_WEIGHT_BOLD);
766   if (m_r_font == NULL || m_b_font == NULL) {
767     /* XXX - pop this up as a dialog box? no */
768     if (m_r_font == NULL) {
769         fprintf(stderr, "wireshark: Warning: font %s not found - defaulting to Monospace 9\n",
770                 prefs.gui_font_name2);
771     } else {
772       pango_font_description_free(m_r_font);
773     }
774     if (m_b_font == NULL) {
775         fprintf(stderr, "wireshark: Warning: bold font %s not found - defaulting"
776                         " to Monospace 9\n", prefs.gui_font_name2);
777     } else {
778       pango_font_description_free(m_b_font);
779     }
780     if ((m_r_font = pango_font_description_from_string("Monospace 9")) == NULL)
781     {
782       fprintf(stderr, "wireshark: Error: font Monospace 9 not found\n");
783       exit(1);
784     }
785     if ((m_b_font = pango_font_description_copy(m_r_font)) == NULL) {
786       fprintf(stderr, "wireshark: Error: font Monospace 9 bold not found\n");
787       exit(1);
788     }
789     g_free(prefs.gui_font_name2);
790     pango_font_description_set_weight(m_b_font, PANGO_WEIGHT_BOLD);
791     prefs.gui_font_name2 = g_strdup("Monospace 9");
792   }
793 #endif /* GTK_MAJOR_VERSION */
794
795   /* Call this for the side-effects that set_fonts() produces */
796   set_fonts(m_r_font, m_b_font);
797 }