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