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 "font_utils.h"
49 #include "simple_dialog.h"
51 #include "packet_list.h"
52 #include "proto_draw.h"
53 #include "follow_tcp.h"
57 PangoFontDescription *m_r_font, *m_b_font;
60 /* Get the regular user font.
62 * @return the regular user font
64 PangoFontDescription *user_font_get_regular(void)
69 /* Get the bold user font.
71 * @return the bold user font
73 PangoFontDescription *user_font_get_bold(void)
79 set_fonts(PangoFontDescription *regular, PangoFontDescription *bold)
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);
89 view_zoom_in_cb(GtkWidget *w _U_, gpointer d _U_)
91 gint save_gui_zoom_level;
93 save_gui_zoom_level = recent.gui_zoom_level;
94 recent.gui_zoom_level++;
95 switch (user_font_apply()) {
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 */
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 */
115 view_zoom_out_cb(GtkWidget *w _U_, gpointer d _U_)
117 gint save_gui_zoom_level;
119 save_gui_zoom_level = recent.gui_zoom_level;
120 recent.gui_zoom_level--;
121 switch (user_font_apply()) {
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 */
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 */
141 view_zoom_100_cb(GtkWidget *w _U_, gpointer d _U_)
143 gint save_gui_zoom_level;
145 save_gui_zoom_level = recent.gui_zoom_level;
146 recent.gui_zoom_level = 0;
147 switch (user_font_apply()) {
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 */
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 */
170 user_font_test(gchar *font_name)
172 PangoFontDescription *new_r_font, *new_b_font;
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.");
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.");
194 pango_font_description_free(new_r_font);
203 /* Given a font name, construct the name of a version of that font with
204 the current zoom factor applied. */
206 font_zoom(char *gui_font_name)
211 long font_point_size_l;
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);
220 font_name_dup = g_strdup(gui_font_name);
221 font_name_p = font_name_dup;
223 /* find the start of the font_size string */
224 font_name_p = strrchr(font_name_dup, ' ');
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;
232 /* build a new font name */
233 new_font_name = g_strdup_printf("%s %ld", font_name_dup, font_point_size_l);
235 g_free(font_name_dup);
237 return new_font_name;
241 user_font_apply(void) {
243 PangoFontDescription *new_r_font, *new_b_font;
244 PangoFontDescription *old_r_font = NULL, *old_b_font = NULL;
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) {
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
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;
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);
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);
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;
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);
285 /* Redraw the hex dump windows. */
286 redraw_hex_dump_all();
288 /* Redraw the "Follow TCP Stream" windows. */
289 follow_tcp_redraw_all();
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);
304 #define NAME_BUFFER_LEN 32
306 static char appfontname[128] = "tahoma 8";
309 set_app_font_gtk2(const char *fontname)
311 GtkSettings *settings;
313 if (fontname != NULL && *fontname == 0) return;
315 settings = gtk_settings_get_default();
317 if (fontname == NULL) {
318 g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
321 PangoFontDescription *pfd;
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);
331 g_strlcpy(appfontname, fontname, 128);
332 appfontname[127] = '\0';
333 g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
336 gtk_widget_destroy(w);
337 pango_font_description_free(pfd);
341 static char *default_windows_menu_fontspec_gtk2(void)
343 gchar *fontspec = NULL;
344 NONCLIENTMETRICS ncm;
346 memset(&ncm, 0, sizeof ncm);
347 ncm.cbSize = sizeof ncm;
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);
354 if (point_size < 0) point_size = -point_size;
355 fontspec = g_strdup_printf("%s %d", ncm.lfMenuFont.lfFaceName,
357 ReleaseDC(0, screen);
363 static void try_to_get_windows_font_gtk2(void)
367 fontspec = default_windows_menu_fontspec_gtk2();
369 if (fontspec != NULL) {
371 PangoFontDescription *pfd;
376 pfd = pango_font_description_from_string(fontspec);
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);
383 pango_font_description_free(pfd);
384 g_object_unref(G_OBJECT(pc));
385 gtk_widget_destroy(w);
387 if (match) set_app_font_gtk2(fontspec);
397 /* try to load the application font for GTK2 */
398 try_to_get_windows_font_gtk2();
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);
411 pango_font_description_free(m_r_font);
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);
417 pango_font_description_free(m_b_font);
419 if ((m_r_font = pango_font_description_from_string("Monospace 9")) == NULL)
421 fprintf(stderr, "wireshark: Error: font Monospace 9 not found\n");
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");
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");
433 /* Call this for the side-effects that set_fonts() produces */
434 set_fonts(m_r_font, m_b_font);