Trivial reformatting
[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 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 #include <gtk/gtk.h>
33
34 #include <epan/packet.h>
35 #include <epan/prefs.h>
36
37 #ifdef _WIN32
38 #include <windows.h>
39 #include <tchar.h>
40 #include <wsutil/unicode-utils.h>
41 #endif
42
43 #include "../simple_dialog.h"
44
45 #include "gtk/main.h"
46 #include "gtk/recent.h"
47 #include "gtk/gtkglobals.h"
48 #include "gtk/font_utils.h"
49 #include "gtk/main_packet_list.h"
50 #include "gtk/main_proto_draw.h"
51 #include "gtk/follow_tcp.h"
52
53 #ifdef NEW_PACKET_LIST
54 #include "gtk/new_packet_list.h"
55 #endif
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
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 #ifdef NEW_PACKET_LIST
280     new_packet_list_set_font(new_r_font);
281 #else
282     packet_list_set_font(new_r_font);
283 #endif
284     set_ptree_font_all(new_r_font);
285     old_r_font = m_r_font;
286     old_b_font = m_b_font;
287     set_fonts(new_r_font, new_b_font);
288
289     /* Redraw the packet bytes windows. */
290     redraw_packet_bytes_all();
291
292     /* Redraw the "Follow TCP Stream" windows. */
293     follow_tcp_redraw_all();
294
295     /* We're no longer using the old fonts; unreference them. */
296     if (old_r_font != NULL)
297         pango_font_description_free(old_r_font);
298     if (old_b_font != NULL)
299         pango_font_description_free(old_b_font);
300     g_free(gui_font_name);
301
302     return FA_SUCCESS;
303 }
304
305
306 #ifdef _WIN32
307
308 #define NAME_BUFFER_LEN 32
309
310 static char appfontname[128] = "tahoma 8";
311
312 static void
313 set_app_font_gtk2(const char *fontname)
314 {
315     GtkSettings *settings;
316
317     if (fontname != NULL && *fontname == 0) return;
318
319     settings = gtk_settings_get_default();
320
321     if (fontname == NULL) {
322         g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
323     } else {
324         GtkWidget *w;
325         PangoFontDescription *pfd;
326         PangoContext *pc;
327         PangoFont *pfont;
328
329         w = gtk_label_new(NULL);
330         pfd = pango_font_description_from_string(fontname);
331         pc = gtk_widget_get_pango_context(w);
332         pfont = pango_context_load_font(pc, pfd);
333
334         if (pfont != NULL) {
335             g_strlcpy(appfontname, fontname, 128);
336             appfontname[127] = '\0';
337             g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
338         }
339
340         gtk_widget_destroy(w);
341         pango_font_description_free(pfd);
342     }
343 }
344
345 static char *default_windows_menu_fontspec_gtk2(void)
346 {
347     gchar *fontspec = NULL;
348     NONCLIENTMETRICS ncm;
349
350     memset(&ncm, 0, sizeof ncm);
351     ncm.cbSize = sizeof ncm;
352
353     if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0)) {
354         HDC screen = GetDC(0);
355         double y_scale = 72.0 / GetDeviceCaps(screen, LOGPIXELSY);
356         int point_size = (int) (ncm.lfMenuFont.lfHeight * y_scale);
357
358         if (point_size < 0) point_size = -point_size;
359         fontspec = g_strdup_printf("%s %d", ncm.lfMenuFont.lfFaceName,
360                                    point_size);
361         ReleaseDC(0, screen);
362     }
363
364     return fontspec;
365 }
366
367 static void try_to_get_windows_font_gtk2(void)
368 {
369     gchar *fontspec;
370
371     fontspec = default_windows_menu_fontspec_gtk2();
372
373     if (fontspec != NULL) {
374         int match = 0;
375         PangoFontDescription *pfd;
376         PangoFont *pfont;
377         PangoContext *pc;
378         GtkWidget *w;
379
380         pfd = pango_font_description_from_string(fontspec);
381
382         w = gtk_label_new(NULL);
383         pc = gtk_widget_get_pango_context(w);
384         pfont = pango_context_load_font(pc, pfd);
385         match = (pfont != NULL);
386
387         pango_font_description_free(pfd);
388         g_object_unref(G_OBJECT(pc));
389         gtk_widget_destroy(w);
390
391         if (match) set_app_font_gtk2(fontspec);
392         g_free(fontspec);
393     }
394 }
395 #endif /* _WIN32 */
396
397
398 void font_init(void)
399 {
400 #ifdef _WIN32
401   /* try to load the application font for GTK2 */
402   try_to_get_windows_font_gtk2();
403 #endif
404
405   /* Try to load the regular and boldface fixed-width fonts */
406   m_r_font = pango_font_description_from_string(prefs.gui_font_name);
407   m_b_font = pango_font_description_copy(m_r_font);
408   pango_font_description_set_weight(m_b_font, PANGO_WEIGHT_BOLD);
409   if (m_r_font == NULL || m_b_font == NULL) {
410     /* XXX - pop this up as a dialog box? no */
411     if (m_r_font == NULL) {
412         fprintf(stderr, "wireshark: Warning: font %s not found - defaulting to Monospace 9\n",
413                 prefs.gui_font_name);
414     } else {
415       pango_font_description_free(m_r_font);
416     }
417     if (m_b_font == NULL) {
418         fprintf(stderr, "wireshark: Warning: bold font %s not found - defaulting"
419                         " to Monospace 9\n", prefs.gui_font_name);
420     } else {
421       pango_font_description_free(m_b_font);
422     }
423     if ((m_r_font = pango_font_description_from_string("Monospace 9")) == NULL)
424     {
425       fprintf(stderr, "wireshark: Error: font Monospace 9 not found\n");
426       exit(1);
427     }
428     if ((m_b_font = pango_font_description_copy(m_r_font)) == NULL) {
429       fprintf(stderr, "wireshark: Error: font Monospace 9 bold not found\n");
430       exit(1);
431     }
432     g_free(prefs.gui_font_name);
433     pango_font_description_set_weight(m_b_font, PANGO_WEIGHT_BOLD);
434     prefs.gui_font_name = g_strdup("Monospace 9");
435   }
436
437   /* Call this for the side-effects that set_fonts() produces */
438   set_fonts(m_r_font, m_b_font);
439 }