b3b4db7cf4ac8169842848cf3f702b18227f113b
[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_tcp.h"
55
56
57
58 PangoFontDescription *m_r_font, *m_b_font;
59
60
61 /* Get the regular user font.
62  *
63  * @return the regular user font
64  */
65 PangoFontDescription *user_font_get_regular(void)
66 {
67     return m_r_font;
68 }
69
70 /* Get the bold user font.
71  *
72  * @return the bold user font
73  */
74 PangoFontDescription *user_font_get_bold(void)
75 {
76     return m_b_font;
77 }
78
79 static void
80 set_fonts(PangoFontDescription *regular, PangoFontDescription *bold)
81 {
82         /* Yes, assert. The code that loads the font should check
83          * for NULL and provide its own error message. */
84         g_assert(m_r_font && m_b_font);
85         m_r_font = regular;
86         m_b_font = bold;
87 }
88
89 void
90 view_zoom_in_cb(GtkWidget *w _U_, gpointer d _U_)
91 {
92     gint save_gui_zoom_level;
93
94     save_gui_zoom_level = recent.gui_zoom_level;
95     recent.gui_zoom_level++;
96     switch (user_font_apply()) {
97
98     case FA_SUCCESS:
99         break;
100
101     case FA_FONT_NOT_RESIZEABLE:
102         /* "font_apply()" popped up an alert box. */
103         recent.gui_zoom_level = save_gui_zoom_level;    /* undo zoom */
104         break;
105
106     case FA_FONT_NOT_AVAILABLE:
107         /* We assume this means that the specified size isn't available. */
108         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
109             "Your current font isn't available in the next larger size.\n");
110         recent.gui_zoom_level = save_gui_zoom_level;    /* undo zoom */
111         break;
112     }
113 }
114
115 void
116 view_zoom_out_cb(GtkWidget *w _U_, gpointer d _U_)
117 {
118     gint save_gui_zoom_level;
119
120     save_gui_zoom_level = recent.gui_zoom_level;
121     recent.gui_zoom_level--;
122     switch (user_font_apply()) {
123
124     case FA_SUCCESS:
125         break;
126
127     case FA_FONT_NOT_RESIZEABLE:
128         /* "font_apply()" popped up an alert box. */
129         recent.gui_zoom_level = save_gui_zoom_level;    /* undo zoom */
130         break;
131
132     case FA_FONT_NOT_AVAILABLE:
133         /* We assume this means that the specified size isn't available. */
134         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
135             "Your current font isn't available in the next smaller size.\n");
136         recent.gui_zoom_level = save_gui_zoom_level;    /* undo zoom */
137         break;
138     }
139 }
140
141 void
142 view_zoom_100_cb(GtkWidget *w _U_, gpointer d _U_)
143 {
144     gint save_gui_zoom_level;
145
146     save_gui_zoom_level = recent.gui_zoom_level;
147     recent.gui_zoom_level = 0;
148     switch (user_font_apply()) {
149
150     case FA_SUCCESS:
151         break;
152
153     case FA_FONT_NOT_RESIZEABLE:
154         /* "font_apply()" popped up an alert box. */
155         recent.gui_zoom_level = save_gui_zoom_level;    /* undo zoom */
156         break;
157
158     case FA_FONT_NOT_AVAILABLE:
159         /* We assume this means that the specified size isn't available.
160            XXX - this "shouldn't happen". */
161         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
162             "Your current font couldn't be reloaded at the size you selected.\n");
163         recent.gui_zoom_level = save_gui_zoom_level;    /* undo zoom */
164         break;
165     }
166 }
167
168
169
170 gboolean
171 user_font_test(gchar *font_name)
172 {
173         PangoFontDescription *new_r_font, *new_b_font;
174
175         new_r_font = pango_font_description_from_string(font_name);
176         if (new_r_font == NULL) {
177                 /* Oops, that font didn't work.
178                    Tell the user, but don't tear down the font selection
179                    dialog, so that they can try again. */
180                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
181                    "The font you selected can't be loaded.");
182
183                 return FALSE;
184         }
185
186         new_b_font = pango_font_description_copy(new_r_font);
187         pango_font_description_set_weight(new_b_font, PANGO_WEIGHT_BOLD);
188         if (new_b_font == NULL) {
189                 /* Oops, that font didn't work.
190                    Tell the user, but don't tear down the font selection
191                    dialog, so that they can try again. */
192                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
193                    "The font you selected doesn't have a boldface version.");
194
195                 pango_font_description_free(new_r_font);
196                 return FALSE;
197         }
198
199         return TRUE;
200 }
201
202
203
204 /* Given a font name, construct the name of a version of that font with
205    the current zoom factor applied. */
206 static char *
207 font_zoom(char *gui_font_name)
208 {
209     char *new_font_name;
210     char *font_name_dup;
211     char *font_name_p;
212     long font_point_size_l;
213
214     if (recent.gui_zoom_level == 0) {
215         /* There is no zoom factor - just return the name, so that if
216            this is GTK+ 1.2[.x] and the font name isn't an XLFD font
217            name, we don't fail. */
218         return g_strdup(gui_font_name);
219     }
220
221     font_name_dup = g_strdup(gui_font_name);
222     font_name_p = font_name_dup;
223
224     /* find the start of the font_size string */
225     font_name_p = strrchr(font_name_dup, ' ');
226     *font_name_p = '\0';
227     font_name_p++;
228
229     /* calculate the new font size */
230     font_point_size_l = strtol(font_name_p, NULL, 10);
231     font_point_size_l += recent.gui_zoom_level;
232
233     /* build a new font name */
234     new_font_name = g_strdup_printf("%s %ld", font_name_dup, font_point_size_l);
235
236     g_free(font_name_dup);
237
238     return new_font_name;
239 }
240
241 fa_ret_t
242 user_font_apply(void) {
243     char *gui_font_name;
244     PangoFontDescription *new_r_font, *new_b_font;
245     PangoFontDescription *old_r_font = NULL, *old_b_font = NULL;
246
247     /* convert font name to reflect the zoom level */
248     gui_font_name = font_zoom(prefs.gui_font_name);
249     if (gui_font_name == NULL) {
250         /*
251          * This means the font name isn't an XLFD font name.
252          * We just report that for now as a font not available in
253          * multiple sizes.
254          */
255         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
256             "Your current font isn't available in any other sizes.\n");
257         return FA_FONT_NOT_RESIZEABLE;
258     }
259
260     /* load normal and bold font */
261     new_r_font = pango_font_description_from_string(gui_font_name);
262     new_b_font = pango_font_description_copy(new_r_font);
263     pango_font_description_set_weight(new_b_font, PANGO_WEIGHT_BOLD);
264
265     if (new_r_font == NULL || new_b_font == NULL) {
266         /* We're no longer using the new fonts; unreference them. */
267         if (new_r_font != NULL)
268             pango_font_description_free(new_r_font);
269         if (new_b_font != NULL)
270             pango_font_description_free(new_b_font);
271         g_free(gui_font_name);
272
273         /* We let our caller pop up a dialog box, as the error message
274            depends on the context (did they zoom in or out, or did they
275            do something else? */
276         return FA_FONT_NOT_AVAILABLE;
277     }
278
279     /* the font(s) seem to be ok */
280     packet_list_set_font(new_r_font);
281     set_ptree_font_all(new_r_font);
282     old_r_font = m_r_font;
283     old_b_font = m_b_font;
284     set_fonts(new_r_font, new_b_font);
285
286     /* Redraw the hex dump windows. */
287     redraw_hex_dump_all();
288
289     /* Redraw the "Follow TCP Stream" windows. */
290     follow_tcp_redraw_all();
291
292     /* We're no longer using the old fonts; unreference them. */
293     if (old_r_font != NULL)
294         pango_font_description_free(old_r_font);
295     if (old_b_font != NULL)
296         pango_font_description_free(old_b_font);
297     g_free(gui_font_name);
298
299     return FA_SUCCESS;
300 }
301
302
303 #ifdef _WIN32
304
305 #define NAME_BUFFER_LEN 32
306
307 static char appfontname[128] = "tahoma 8";
308
309 static void
310 set_app_font_gtk2(const char *fontname)
311 {
312     GtkSettings *settings;
313
314     if (fontname != NULL && *fontname == 0) return;
315
316     settings = gtk_settings_get_default();
317
318     if (fontname == NULL) {
319         g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
320     } else {
321         GtkWidget *w;
322         PangoFontDescription *pfd;
323         PangoContext *pc;
324         PangoFont *pfont;
325
326         w = gtk_label_new(NULL);
327         pfd = pango_font_description_from_string(fontname);
328         pc = gtk_widget_get_pango_context(w);
329         pfont = pango_context_load_font(pc, pfd);
330
331         if (pfont != NULL) {
332             g_strlcpy(appfontname, fontname, 128);
333             appfontname[127] = '\0';
334             g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
335         }
336
337         gtk_widget_destroy(w);
338         pango_font_description_free(pfd);
339     }
340 }
341
342 static char *default_windows_menu_fontspec_gtk2(void)
343 {
344     gchar *fontspec = NULL;
345     NONCLIENTMETRICS ncm;
346
347     memset(&ncm, 0, sizeof ncm);
348     ncm.cbSize = sizeof ncm;
349
350     if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0)) {
351         HDC screen = GetDC(0);
352         double y_scale = 72.0 / GetDeviceCaps(screen, LOGPIXELSY);
353         int point_size = (int) (ncm.lfMenuFont.lfHeight * y_scale);
354
355         if (point_size < 0) point_size = -point_size;
356         fontspec = g_strdup_printf("%s %d", ncm.lfMenuFont.lfFaceName,
357                                    point_size);
358         ReleaseDC(0, screen);
359     }
360
361     return fontspec;
362 }
363
364 static void try_to_get_windows_font_gtk2(void)
365 {
366     gchar *fontspec;
367
368     fontspec = default_windows_menu_fontspec_gtk2();
369
370     if (fontspec != NULL) {
371         int match = 0;
372         PangoFontDescription *pfd;
373         PangoFont *pfont;
374         PangoContext *pc;
375         GtkWidget *w;
376
377         pfd = pango_font_description_from_string(fontspec);
378
379         w = gtk_label_new(NULL);
380         pc = gtk_widget_get_pango_context(w);
381         pfont = pango_context_load_font(pc, pfd);
382         match = (pfont != NULL);
383
384         pango_font_description_free(pfd);
385         g_object_unref(G_OBJECT(pc));
386         gtk_widget_destroy(w);
387
388         if (match) set_app_font_gtk2(fontspec);
389         g_free(fontspec);
390     }
391 }
392 #endif /* _WIN32 */
393
394
395 void font_init(void)
396 {
397 #ifdef _WIN32
398   /* try to load the application font for GTK2 */
399   try_to_get_windows_font_gtk2();
400 #endif
401
402   /* Try to load the regular and boldface fixed-width fonts */
403   m_r_font = pango_font_description_from_string(prefs.gui_font_name);
404   m_b_font = pango_font_description_copy(m_r_font);
405   pango_font_description_set_weight(m_b_font, PANGO_WEIGHT_BOLD);
406   if (m_r_font == NULL || m_b_font == NULL) {
407     /* XXX - pop this up as a dialog box? no */
408     if (m_r_font == NULL) {
409         fprintf(stderr, "wireshark: Warning: font %s not found - defaulting to Monospace 9\n",
410                 prefs.gui_font_name);
411     } else {
412       pango_font_description_free(m_r_font);
413     }
414     if (m_b_font == NULL) {
415         fprintf(stderr, "wireshark: Warning: bold font %s not found - defaulting"
416                         " to Monospace 9\n", prefs.gui_font_name);
417     } else {
418       pango_font_description_free(m_b_font);
419     }
420     if ((m_r_font = pango_font_description_from_string("Monospace 9")) == NULL)
421     {
422       fprintf(stderr, "wireshark: Error: font Monospace 9 not found\n");
423       exit(1);
424     }
425     if ((m_b_font = pango_font_description_copy(m_r_font)) == NULL) {
426       fprintf(stderr, "wireshark: Error: font Monospace 9 bold not found\n");
427       exit(1);
428     }
429     g_free(prefs.gui_font_name);
430     pango_font_description_set_weight(m_b_font, PANGO_WEIGHT_BOLD);
431     prefs.gui_font_name = g_strdup("Monospace 9");
432   }
433
434   /* Call this for the side-effects that set_fonts() produces */
435   set_fonts(m_r_font, m_b_font);
436 }