1 /*
2  * Copyright (C) 2018-2021 Alexandros Theodotou <alex at zrythm dot org>
3  *
4  * This file is part of Zrythm
5  *
6  * Zrythm is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Zrythm is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with Zrythm.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "utils/resources.h"
21 
22 /**
23  * Note: MUST be free'd
24  */
25 static char *
get_icon_type_str(IconType icon_type)26 get_icon_type_str (IconType icon_type)
27 {
28   switch (icon_type)
29     {
30       /* FIXME why strdup and not const strings? */
31     case ICON_TYPE_ZRYTHM:
32       return g_strdup ("zrythm");
33     case ICON_TYPE_GNOME_BUILDER:
34       return g_strdup_printf ("gnome-builder");
35     case ICON_TYPE_BREEZE:
36       return g_strdup_printf ("breeze-icons");
37     case ICON_TYPE_FORK_AWESOME:
38       return g_strdup_printf ("fork-awesome");
39     }
40   g_warn_if_reached ();
41   return NULL;
42 }
43 
44 GtkWidget *
resources_get_icon(IconType icon_type,const char * filename)45 resources_get_icon (IconType     icon_type,
46                     const char * filename)
47 {
48   char * icon_dir = get_icon_type_str (icon_type);
49   char * path =
50     g_strdup_printf (
51       "%s/%s/%s", RESOURCES_ICON_PATH, icon_dir,
52       filename);
53   g_free (icon_dir);
54   GtkWidget * icon =
55     gtk_image_new_from_resource (path);
56   gtk_widget_set_visible (icon, 1);
57   g_free (path);
58   return icon;
59 }
60 
61 void
resources_set_image_icon(GtkImage * img,IconType icon_type,const char * filename)62 resources_set_image_icon (
63   GtkImage *   img,
64   IconType     icon_type,
65   const char * filename)
66 {
67   char * icon_dir = get_icon_type_str (icon_type);
68   char * path =
69     g_strdup_printf (
70       "%s/%s/%s", RESOURCES_ICON_PATH, icon_dir,
71       filename);
72   g_free (icon_dir);
73   gtk_image_set_from_resource (img, path);
74   g_free (path);
75 }
76 
77 /**
78  * Sets class template from resource.
79  *
80  * Filename is part after .../ui/
81  */
82 void
resources_set_class_template(GtkWidgetClass * klass,const char * filename)83 resources_set_class_template (
84   GtkWidgetClass * klass,
85   const char *     filename)
86 {
87   char path[500];
88   sprintf (
89     path, "%s/%s", RESOURCES_TEMPLATE_PATH,
90     filename);
91   gtk_widget_class_set_template_from_resource (
92     klass, path);
93 }
94 
95 void
resources_add_icon_to_button(GtkButton * btn,IconType icon_type,const char * path)96 resources_add_icon_to_button (
97   GtkButton *  btn,
98   IconType     icon_type,
99   const char * path)
100 {
101   GtkWidget * icon =
102     resources_get_icon (icon_type, path);
103   gtk_container_add (GTK_CONTAINER (btn), icon);
104 }
105 
106 /**
107  * Returns the bytes of the specified OpenGL shader
108  * in RESOURCES_GL_SHADERS_PATH.
109  *
110  * Caller must free the bytes with g_bytes_unref ().
111  *
112  * @return bytes or NULL if error.
113  */
114 GBytes *
resources_get_gl_shader_data(const char * path)115 resources_get_gl_shader_data (
116   const char * path)
117 {
118   GError * err = NULL;
119   char * str =
120     g_strdup_printf (
121       "%s/%s", RESOURCES_GL_SHADERS_PATH, path);
122   GBytes * data =
123     g_resources_lookup_data (str, 0, &err);
124 
125   if (err)
126     {
127       g_critical (
128         "Failed to load gl shader data at <%s>: %s",
129         path, err->message);
130     }
131 
132   return data;
133 }
134