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_proto_draw.h"
50 #include "gtk/follow_tcp.h"
51 #include "gtk/new_packet_list.h"
54 static PangoFontDescription *m_r_font, *m_b_font;
57 /* Get the regular user font.
59 * @return the regular user font
61 PangoFontDescription *user_font_get_regular(void)
66 /* Get the bold user font.
68 * @return the bold user font
70 PangoFontDescription *user_font_get_bold(void)
76 set_fonts(PangoFontDescription *regular, PangoFontDescription *bold)
78 /* Yes, assert. The code that loads the font should check
79 * for NULL and provide its own error message. */
80 g_assert(m_r_font && m_b_font);
86 view_zoom_in_cb(GtkWidget *w _U_, gpointer d _U_)
88 gint save_gui_zoom_level;
90 save_gui_zoom_level = recent.gui_zoom_level;
91 recent.gui_zoom_level++;
92 switch (user_font_apply()) {
97 case FA_FONT_NOT_RESIZEABLE:
98 /* "font_apply()" popped up an alert box. */
99 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
102 case FA_FONT_NOT_AVAILABLE:
103 /* We assume this means that the specified size isn't available. */
104 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
105 "Your current font isn't available in the next larger size.\n");
106 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
112 view_zoom_out_cb(GtkWidget *w _U_, gpointer d _U_)
114 gint save_gui_zoom_level;
116 save_gui_zoom_level = recent.gui_zoom_level;
117 recent.gui_zoom_level--;
118 switch (user_font_apply()) {
123 case FA_FONT_NOT_RESIZEABLE:
124 /* "font_apply()" popped up an alert box. */
125 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
128 case FA_FONT_NOT_AVAILABLE:
129 /* We assume this means that the specified size isn't available. */
130 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
131 "Your current font isn't available in the next smaller size.\n");
132 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
138 view_zoom_100_cb(GtkWidget *w _U_, gpointer d _U_)
140 gint save_gui_zoom_level;
142 save_gui_zoom_level = recent.gui_zoom_level;
143 recent.gui_zoom_level = 0;
144 switch (user_font_apply()) {
149 case FA_FONT_NOT_RESIZEABLE:
150 /* "font_apply()" popped up an alert box. */
151 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
154 case FA_FONT_NOT_AVAILABLE:
155 /* We assume this means that the specified size isn't available.
156 XXX - this "shouldn't happen". */
157 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
158 "Your current font couldn't be reloaded at the size you selected.\n");
159 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
167 user_font_test(gchar *font_name)
169 PangoFontDescription *new_r_font, *new_b_font;
171 new_r_font = pango_font_description_from_string(font_name);
172 if (new_r_font == NULL) {
173 /* Oops, that font didn't work.
174 Tell the user, but don't tear down the font selection
175 dialog, so that they can try again. */
176 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
177 "The font you selected can't be loaded.");
182 new_b_font = pango_font_description_copy(new_r_font);
183 pango_font_description_set_weight(new_b_font, PANGO_WEIGHT_BOLD);
184 if (new_b_font == NULL) {
185 /* Oops, that font didn't work.
186 Tell the user, but don't tear down the font selection
187 dialog, so that they can try again. */
188 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
189 "The font you selected doesn't have a boldface version.");
191 pango_font_description_free(new_r_font);
200 /* Given a font name, construct the name of a version of that font with
201 the current zoom factor applied. */
203 font_zoom(char *gui_font_name)
208 long font_point_size_l;
210 if (recent.gui_zoom_level == 0) {
211 /* There is no zoom factor - just return the name, so that if
212 this is GTK+ 1.2[.x] and the font name isn't an XLFD font
213 name, we don't fail. */
214 return g_strdup(gui_font_name);
217 font_name_dup = g_strdup(gui_font_name);
219 /* find the start of the font_size string */
220 font_name_p = strrchr(font_name_dup, ' ');
224 /* calculate the new font size */
225 font_point_size_l = strtol(font_name_p, NULL, 10);
226 font_point_size_l += recent.gui_zoom_level;
228 /* build a new font name */
229 new_font_name = g_strdup_printf("%s %ld", font_name_dup, font_point_size_l);
231 g_free(font_name_dup);
233 return new_font_name;
237 user_font_apply(void) {
239 PangoFontDescription *new_r_font, *new_b_font;
240 PangoFontDescription *old_r_font = NULL, *old_b_font = NULL;
242 /* convert font name to reflect the zoom level */
243 gui_font_name = font_zoom(prefs.gui_font_name);
244 if (gui_font_name == NULL) {
246 * This means the font name isn't an XLFD font name.
247 * We just report that for now as a font not available in
250 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
251 "Your current font isn't available in any other sizes.\n");
252 return FA_FONT_NOT_RESIZEABLE;
255 /* load normal and bold font */
256 new_r_font = pango_font_description_from_string(gui_font_name);
257 new_b_font = pango_font_description_copy(new_r_font);
258 pango_font_description_set_weight(new_b_font, PANGO_WEIGHT_BOLD);
260 if (new_r_font == NULL || new_b_font == NULL) {
261 /* We're no longer using the new fonts; unreference them. */
262 if (new_r_font != NULL)
263 pango_font_description_free(new_r_font);
264 if (new_b_font != NULL)
265 pango_font_description_free(new_b_font);
266 g_free(gui_font_name);
268 /* We let our caller pop up a dialog box, as the error message
269 depends on the context (did they zoom in or out, or did they
270 do something else? */
271 return FA_FONT_NOT_AVAILABLE;
274 /* the font(s) seem to be ok */
275 new_packet_list_set_font(new_r_font);
276 set_ptree_font_all(new_r_font);
277 old_r_font = m_r_font;
278 old_b_font = m_b_font;
279 set_fonts(new_r_font, new_b_font);
281 /* Redraw the packet bytes windows. */
282 redraw_packet_bytes_all();
284 /* Redraw the "Follow TCP Stream" windows. */
285 follow_tcp_redraw_all();
287 /* We're no longer using the old fonts; unreference them. */
288 if (old_r_font != NULL)
289 pango_font_description_free(old_r_font);
290 if (old_b_font != NULL)
291 pango_font_description_free(old_b_font);
292 g_free(gui_font_name);
300 #define NAME_BUFFER_LEN 32
302 static char appfontname[128] = "tahoma 8";
305 set_app_font_gtk2(const char *fontname)
307 GtkSettings *settings;
309 if (fontname != NULL && *fontname == 0) return;
311 settings = gtk_settings_get_default();
313 if (fontname == NULL) {
314 g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
317 PangoFontDescription *pfd;
321 w = gtk_label_new(NULL);
322 pfd = pango_font_description_from_string(fontname);
323 pc = gtk_widget_get_pango_context(w);
324 pfont = pango_context_load_font(pc, pfd);
327 g_strlcpy(appfontname, fontname, 128);
328 appfontname[127] = '\0';
329 g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
332 gtk_widget_destroy(w);
333 pango_font_description_free(pfd);
337 static char *default_windows_menu_fontspec_gtk2(void)
339 gchar *fontspec = NULL;
340 NONCLIENTMETRICS ncm;
342 memset(&ncm, 0, sizeof ncm);
343 ncm.cbSize = sizeof ncm;
345 if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0)) {
346 HDC screen = GetDC(0);
347 double y_scale = 72.0 / GetDeviceCaps(screen, LOGPIXELSY);
348 int point_size = (int) (ncm.lfMenuFont.lfHeight * y_scale);
350 if (point_size < 0) point_size = -point_size;
351 fontspec = g_strdup_printf("%s %d", ncm.lfMenuFont.lfFaceName,
353 ReleaseDC(0, screen);
359 static void try_to_get_windows_font_gtk2(void)
363 fontspec = default_windows_menu_fontspec_gtk2();
365 if (fontspec != NULL) {
367 PangoFontDescription *pfd;
372 pfd = pango_font_description_from_string(fontspec);
374 w = gtk_label_new(NULL);
375 pc = gtk_widget_get_pango_context(w);
376 pfont = pango_context_load_font(pc, pfd);
377 match = (pfont != NULL);
379 pango_font_description_free(pfd);
380 g_object_unref(G_OBJECT(pc));
381 gtk_widget_destroy(w);
383 if (match) set_app_font_gtk2(fontspec);
393 /* try to load the application font for GTK2 */
394 try_to_get_windows_font_gtk2();
397 /* Try to load the regular and boldface fixed-width fonts */
398 m_r_font = pango_font_description_from_string(prefs.gui_font_name);
399 m_b_font = pango_font_description_copy(m_r_font);
400 pango_font_description_set_weight(m_b_font, PANGO_WEIGHT_BOLD);
401 if (m_r_font == NULL || m_b_font == NULL) {
402 /* XXX - pop this up as a dialog box? no */
403 if (m_r_font == NULL) {
404 fprintf(stderr, "wireshark: Warning: font %s not found - defaulting to Monospace 9\n",
405 prefs.gui_font_name);
407 pango_font_description_free(m_r_font);
409 if (m_b_font == NULL) {
410 fprintf(stderr, "wireshark: Warning: bold font %s not found - defaulting"
411 " to Monospace 9\n", prefs.gui_font_name);
413 pango_font_description_free(m_b_font);
415 if ((m_r_font = pango_font_description_from_string("Monospace 9")) == NULL)
417 fprintf(stderr, "wireshark: Error: font Monospace 9 not found\n");
420 if ((m_b_font = pango_font_description_copy(m_r_font)) == NULL) {
421 fprintf(stderr, "wireshark: Error: font Monospace 9 bold not found\n");
424 g_free(prefs.gui_font_name);
425 pango_font_description_set_weight(m_b_font, PANGO_WEIGHT_BOLD);
426 prefs.gui_font_name = g_strdup("Monospace 9");
429 /* Call this for the side-effects that set_fonts() produces */
430 set_fonts(m_r_font, m_b_font);