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>
35 #include <epan/prefs.h>
40 #include <wsutil/unicode-utils.h>
43 #include "../simple_dialog.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"
53 #ifdef NEW_PACKET_LIST
54 #include "gtk/new_packet_list.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);
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 #ifdef NEW_PACKET_LIST
280 new_packet_list_set_font(new_r_font);
282 packet_list_set_font(new_r_font);
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);
289 /* Redraw the packet bytes windows. */
290 redraw_packet_bytes_all();
292 /* Redraw the "Follow TCP Stream" windows. */
293 follow_tcp_redraw_all();
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);
308 #define NAME_BUFFER_LEN 32
310 static char appfontname[128] = "tahoma 8";
313 set_app_font_gtk2(const char *fontname)
315 GtkSettings *settings;
317 if (fontname != NULL && *fontname == 0) return;
319 settings = gtk_settings_get_default();
321 if (fontname == NULL) {
322 g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
325 PangoFontDescription *pfd;
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);
335 g_strlcpy(appfontname, fontname, 128);
336 appfontname[127] = '\0';
337 g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
340 gtk_widget_destroy(w);
341 pango_font_description_free(pfd);
345 static char *default_windows_menu_fontspec_gtk2(void)
347 gchar *fontspec = NULL;
348 NONCLIENTMETRICS ncm;
350 memset(&ncm, 0, sizeof ncm);
351 ncm.cbSize = sizeof ncm;
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);
358 if (point_size < 0) point_size = -point_size;
359 fontspec = g_strdup_printf("%s %d", ncm.lfMenuFont.lfFaceName,
361 ReleaseDC(0, screen);
367 static void try_to_get_windows_font_gtk2(void)
371 fontspec = default_windows_menu_fontspec_gtk2();
373 if (fontspec != NULL) {
375 PangoFontDescription *pfd;
380 pfd = pango_font_description_from_string(fontspec);
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);
387 pango_font_description_free(pfd);
388 g_object_unref(G_OBJECT(pc));
389 gtk_widget_destroy(w);
391 if (match) set_app_font_gtk2(fontspec);
401 /* try to load the application font for GTK2 */
402 try_to_get_windows_font_gtk2();
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);
415 pango_font_description_free(m_r_font);
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);
421 pango_font_description_free(m_b_font);
423 if ((m_r_font = pango_font_description_from_string("Monospace 9")) == NULL)
425 fprintf(stderr, "wireshark: Error: font Monospace 9 not found\n");
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");
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");
437 /* Call this for the side-effects that set_fonts() produces */
438 set_fonts(m_r_font, m_b_font);