1 /* X-Chat
2  * Copyright (C) 1998 Peter Zelezny.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 
23 #include "fe-gtk.h"
24 #include "../common/cfgfiles.h"
25 #include "../common/hexchat.h"
26 #include "../common/fe.h"
27 #include "resources.h"
28 
29 #include <gio/gio.h>
30 #include <gdk-pixbuf/gdk-pixbuf.h>
31 
32 GdkPixbuf *pix_ulist_voice;
33 GdkPixbuf *pix_ulist_halfop;
34 GdkPixbuf *pix_ulist_op;
35 GdkPixbuf *pix_ulist_owner;
36 GdkPixbuf *pix_ulist_founder;
37 GdkPixbuf *pix_ulist_netop;
38 
39 GdkPixbuf *pix_tray_normal;
40 GdkPixbuf *pix_tray_fileoffer;
41 GdkPixbuf *pix_tray_highlight;
42 GdkPixbuf *pix_tray_message;
43 
44 GdkPixbuf *pix_tree_channel;
45 GdkPixbuf *pix_tree_dialog;
46 GdkPixbuf *pix_tree_server;
47 GdkPixbuf *pix_tree_util;
48 
49 GdkPixbuf *pix_book;
50 GdkPixbuf *pix_hexchat;
51 
52 static GdkPixmap *
pixmap_load_from_file_real(char * file)53 pixmap_load_from_file_real (char *file)
54 {
55 	GdkPixbuf *img;
56 	GdkPixmap *pixmap;
57 
58 	img = gdk_pixbuf_new_from_file (file, 0);
59 	if (!img)
60 		return NULL;
61 	gdk_pixbuf_render_pixmap_and_mask (img, &pixmap, NULL, 128);
62 	g_object_unref (img);
63 
64 	return pixmap;
65 }
66 
67 GdkPixmap *
pixmap_load_from_file(char * filename)68 pixmap_load_from_file (char *filename)
69 {
70 	char buf[256];
71 	GdkPixmap *pix;
72 
73 	if (filename[0] == '\0')
74 		return NULL;
75 
76 	pix = pixmap_load_from_file_real (filename);
77 	if (pix == NULL)
78 	{
79 		strcpy (buf, "Cannot open:\n\n");
80 		strncpy (buf + 14, filename, sizeof (buf) - 14);
81 		buf[sizeof (buf) - 1] = 0;
82 		fe_message (buf, FE_MSG_ERROR);
83 	}
84 
85 	return pix;
86 }
87 
88 /* load custom icons from <config>/icons, don't mess in system folders */
89 static GdkPixbuf *
load_pixmap(const char * filename)90 load_pixmap (const char *filename)
91 {
92 	GdkPixbuf *pixbuf, *scaledpixbuf;
93 	const char *scale;
94 	int iscale;
95 
96 	gchar *path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "icons" G_DIR_SEPARATOR_S "%s.png", get_xdir (), filename);
97 	pixbuf = gdk_pixbuf_new_from_file (path, 0);
98 	g_free (path);
99 
100 	if (!pixbuf)
101 	{
102 		path = g_strdup_printf ("/icons/%s.png", filename);
103 		pixbuf = gdk_pixbuf_new_from_resource (path, NULL);
104 		g_free (path);
105 	}
106 
107 	// Hack to avoid unbearably tiny icons on HiDPI screens.
108 	scale = g_getenv ("GDK_SCALE");
109 	if (scale)
110 	{
111 		iscale = atoi (scale);
112 		if (iscale > 0)
113 		{
114 			scaledpixbuf = gdk_pixbuf_scale_simple (pixbuf, gdk_pixbuf_get_width (pixbuf) * iscale,
115 				gdk_pixbuf_get_height (pixbuf) * iscale, GDK_INTERP_BILINEAR);
116 
117 			if (scaledpixbuf)
118 			{
119 				g_object_unref (pixbuf);
120 				pixbuf = scaledpixbuf;
121 			}
122 		}
123 	}
124 
125 	g_warn_if_fail (pixbuf != NULL);
126 
127 	return pixbuf;
128 }
129 
130 void
pixmaps_init(void)131 pixmaps_init (void)
132 {
133 	hexchat_register_resource();
134 
135 	pix_ulist_voice = load_pixmap ("ulist_voice");
136 	pix_ulist_halfop = load_pixmap ("ulist_halfop");
137 	pix_ulist_op = load_pixmap ("ulist_op");
138 	pix_ulist_owner = load_pixmap ("ulist_owner");
139 	pix_ulist_founder = load_pixmap ("ulist_founder");
140 	pix_ulist_netop = load_pixmap ("ulist_netop");
141 
142 	pix_tray_normal = load_pixmap ("tray_normal");
143 	pix_tray_fileoffer = load_pixmap ("tray_fileoffer");
144 	pix_tray_highlight = load_pixmap ("tray_highlight");
145 	pix_tray_message = load_pixmap ("tray_message");
146 
147 	pix_tree_channel = load_pixmap ("tree_channel");
148 	pix_tree_dialog = load_pixmap ("tree_dialog");
149 	pix_tree_server = load_pixmap ("tree_server");
150 	pix_tree_util = load_pixmap ("tree_util");
151 
152 	/* non-replaceable book pixmap */
153 	pix_book = gdk_pixbuf_new_from_resource ("/icons/book.png", NULL);
154 
155 	/* used in About window, tray icon and WindowManager icon. */
156 	pix_hexchat = load_pixmap ("hexchat");
157 }
158