2 * Utilities to use for font manipulation
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
34 #include <epan/packet.h>
39 #include <epan/unicode-utils.h>
44 #include <epan/prefs.h>
46 #include "gtkglobals.h"
48 #include "compat_macros.h"
49 #include "font_utils.h"
50 #include "simple_dialog.h"
52 #include "packet_list.h"
53 #include "proto_draw.h"
54 #include "follow_tcp.h"
58 PangoFontDescription *m_r_font, *m_b_font;
61 /* Get the regular user font.
63 * @return the regular user font
65 PangoFontDescription *user_font_get_regular(void)
70 /* Get the bold user font.
72 * @return the bold user font
74 PangoFontDescription *user_font_get_bold(void)
80 set_fonts(PangoFontDescription *regular, PangoFontDescription *bold)
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);
90 view_zoom_in_cb(GtkWidget *w _U_, gpointer d _U_)
92 gint save_gui_zoom_level;
94 save_gui_zoom_level = recent.gui_zoom_level;
95 recent.gui_zoom_level++;
96 switch (user_font_apply()) {
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 */
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 */
116 view_zoom_out_cb(GtkWidget *w _U_, gpointer d _U_)
118 gint save_gui_zoom_level;
120 save_gui_zoom_level = recent.gui_zoom_level;
121 recent.gui_zoom_level--;
122 switch (user_font_apply()) {
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 */
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 */
142 view_zoom_100_cb(GtkWidget *w _U_, gpointer d _U_)
144 gint save_gui_zoom_level;
146 save_gui_zoom_level = recent.gui_zoom_level;
147 recent.gui_zoom_level = 0;
148 switch (user_font_apply()) {
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 */
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 */
171 user_font_test(gchar *font_name)
173 PangoFontDescription *new_r_font, *new_b_font;
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.");
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.");
195 pango_font_description_free(new_r_font);
204 /* Given a font name, construct the name of a version of that font with
205 the current zoom factor applied. */
207 font_zoom(char *gui_font_name)
212 long font_point_size_l;
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);
221 font_name_dup = g_strdup(gui_font_name);
222 font_name_p = font_name_dup;
224 /* find the start of the font_size string */
225 font_name_p = strrchr(font_name_dup, ' ');
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;
233 /* build a new font name */
234 new_font_name = g_strdup_printf("%s %ld", font_name_dup, font_point_size_l);
236 g_free(font_name_dup);
238 return new_font_name;
242 user_font_apply(void) {
244 PangoFontDescription *new_r_font, *new_b_font;
245 PangoFontDescription *old_r_font = NULL, *old_b_font = NULL;
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) {
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
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;
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);
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);
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;
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);
286 /* Redraw the hex dump windows. */
287 redraw_hex_dump_all();
289 /* Redraw the "Follow TCP Stream" windows. */
290 follow_tcp_redraw_all();
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);
305 #define NAME_BUFFER_LEN 32
307 static char appfontname[128] = "tahoma 8";
310 set_app_font_gtk2(const char *fontname)
312 GtkSettings *settings;
314 if (fontname != NULL && *fontname == 0) return;
316 settings = gtk_settings_get_default();
318 if (fontname == NULL) {
319 g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
322 PangoFontDescription *pfd;
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);
332 g_strlcpy(appfontname, fontname, 128);
333 appfontname[127] = '\0';
334 g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
337 gtk_widget_destroy(w);
338 pango_font_description_free(pfd);
342 static char *default_windows_menu_fontspec_gtk2(void)
344 gchar *fontspec = NULL;
345 NONCLIENTMETRICS ncm;
347 memset(&ncm, 0, sizeof ncm);
348 ncm.cbSize = sizeof ncm;
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);
355 if (point_size < 0) point_size = -point_size;
356 fontspec = g_strdup_printf("%s %d", ncm.lfMenuFont.lfFaceName,
358 ReleaseDC(0, screen);
364 static void try_to_get_windows_font_gtk2(void)
368 fontspec = default_windows_menu_fontspec_gtk2();
370 if (fontspec != NULL) {
372 PangoFontDescription *pfd;
377 pfd = pango_font_description_from_string(fontspec);
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);
384 pango_font_description_free(pfd);
385 g_object_unref(G_OBJECT(pc));
386 gtk_widget_destroy(w);
388 if (match) set_app_font_gtk2(fontspec);
398 /* try to load the application font for GTK2 */
399 try_to_get_windows_font_gtk2();
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);
412 pango_font_description_free(m_r_font);
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);
418 pango_font_description_free(m_b_font);
420 if ((m_r_font = pango_font_description_from_string("Monospace 9")) == NULL)
422 fprintf(stderr, "wireshark: Error: font Monospace 9 not found\n");
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");
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");
434 /* Call this for the side-effects that set_fonts() produces */
435 set_fonts(m_r_font, m_b_font);