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