Freeciv-3.1
Loading...
Searching...
No Matches
themes.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2005 The Freeciv Team
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13#ifdef HAVE_CONFIG_H
14#include <fc_config.h>
15#endif
16
17#ifdef FREECIV_HAVE_DIRENT_H
18#include <dirent.h>
19#endif
20
21#include <sys/stat.h>
22#include <sys/types.h>
23#include <unistd.h>
24
25#include <gtk/gtk.h>
26
27/* utility */
28#include "fc_dirent.h"
29#include "mem.h"
30#include "string_vector.h"
31
32/* client */
33#include "themes_common.h"
34
35/* gui-gtk-3.0 */
36#include "gui_main.h"
37
38#include "themes_g.h"
39
40/*************************************************************************/
43void gui_load_theme(const char *directory, const char *theme_name)
44{
45 static GtkCssProvider *fc_css_provider = NULL;
46 GError *error = NULL;
47 char buf[strlen(directory) + strlen(theme_name) + 32];
48
49 if (fc_css_provider == NULL) {
50 fc_css_provider = gtk_css_provider_new();
51 gtk_style_context_add_provider(gtk_widget_get_style_context(toplevel),
52 GTK_STYLE_PROVIDER(fc_css_provider),
53 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
54 }
55
56 /* Gtk theme is a directory containing gtk-3.0/gtk.css file */
57 fc_snprintf(buf, sizeof(buf), "%s/%s/gtk-3.0/gtk.css", directory,
58 theme_name);
59
60 gtk_css_provider_load_from_path(fc_css_provider, buf, &error);
61
62 if (error) {
63 g_warning("%s\n", error->message);
64 }
65
66 gtk_style_context_invalidate(gtk_widget_get_style_context(toplevel));
67}
68
69/*************************************************************************/
73{
74 bool theme_loaded;
75
76 /* Try to load user defined theme */
77 theme_loaded = load_theme(GUI_GTK_OPTION(default_theme_name));
78
79 /* No user defined theme loaded -> try to load Freeciv default theme */
80 if (!theme_loaded) {
82 if (theme_loaded) {
84 }
85 }
86
87 /* Still no theme loaded -> load system default theme */
88 if (!theme_loaded) {
89 static GtkCssProvider *default_provider = NULL;
90
91 if (default_provider == NULL) {
92 default_provider = gtk_css_provider_new();
93 }
94 gtk_style_context_add_provider_for_screen(
95 gtk_widget_get_screen(toplevel),
96 GTK_STYLE_PROVIDER(default_provider),
97 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
98 }
99}
100
101/*************************************************************************/
110{
111 gchar *standard_dir;
112 char *home_dir;
113 const struct strvec *data_dirs = get_data_dirs();
114 char **directories = fc_malloc((2 + strvec_size(data_dirs))
115 * sizeof(char *));
116
117 *count = 0;
118
119 /* Freeciv-specific GTK3 themes directories */
120 strvec_iterate(data_dirs, dir_name) {
121 char buf[strlen(dir_name) + strlen("/themes/gtk3") + 1];
122
123 fc_snprintf(buf, sizeof(buf), "%s/themes/gtk3", dir_name);
124
125 directories[(*count)++] = fc_strdup(buf);
127
128 /* standard GTK+ themes directory */
129#ifdef CROSSER
130 standard_dir = "../share/themes";
131#else /* CROSSER */
132 standard_dir = "/usr/share/themes";
133#endif /* CROSSER */
134 directories[(*count)++] = fc_strdup(standard_dir);
135
136 /* user GTK+ themes directory (~/.themes) */
137 home_dir = user_home_dir();
138 if (home_dir) {
139 char buf[strlen(home_dir) + 16];
140
141 fc_snprintf(buf, sizeof(buf), "%s/.themes/", home_dir);
142 directories[(*count)++] = fc_strdup(buf);
143 }
144
145 return directories;
146}
147
148/*************************************************************************/
154char **get_usable_themes_in_directory(const char *directory, int *count)
155{
156 DIR *dir;
157 struct dirent *entry;
158 char **theme_names = fc_malloc(sizeof(char *) * 2);
159 /* Allocated memory size */
160 int t_size = 2;
161
162
163 *count = 0;
164
165 dir = fc_opendir(directory);
166 if (!dir) {
167 /* This isn't directory or we can't list it */
168 return theme_names;
169 }
170
171 while ((entry = readdir(dir))) {
172 char buf[strlen(directory) + strlen(entry->d_name) + 32];
173 struct stat stat_result;
174
175 fc_snprintf(buf, sizeof(buf),
176 "%s/%s/gtk-3.0/gtk.css", directory, entry->d_name);
177
178 if (fc_stat(buf, &stat_result) != 0) {
179 /* File doesn't exist */
180 continue;
181 }
182
183 if (!S_ISREG(stat_result.st_mode)) {
184 /* Not a regular file */
185 continue;
186 }
187
188 /* Otherwise it's ok */
189
190 /* Increase array size if needed */
191 if (*count == t_size) {
192 theme_names = fc_realloc(theme_names, t_size * 2 * sizeof(char *));
193 t_size *= 2;
194 }
195
196 theme_names[*count] = fc_strdup(entry->d_name);
197 (*count)++;
198 }
199
200 closedir(dir);
201
202 return theme_names;
203}
DIR * fc_opendir(const char *dir_to_open)
Definition fc_dirent.c:29
GtkWidget * toplevel
Definition gui_main.c:124
#define GUI_GTK_DEFAULT_THEME_NAME
Definition gui_main.h:27
#define GUI_GTK_OPTION(optname)
Definition gui_main.h:25
static bool load_theme
Definition theme_dlg.c:30
char ** get_gui_specific_themes_directories(int *count)
Definition themes.c:109
void gui_clear_theme(void)
Definition themes.c:72
void gui_load_theme(const char *directory, const char *theme_name)
Definition themes.c:43
#define fc_strdup(str)
Definition mem.h:43
#define fc_realloc(ptr, sz)
Definition mem.h:36
#define fc_malloc(sz)
Definition mem.h:34
char * user_home_dir(void)
Definition shared.c:628
const struct strvec * get_data_dirs(void)
Definition shared.c:886
size_t strvec_size(const struct strvec *psv)
#define strvec_iterate(psv, str)
#define strvec_iterate_end
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
int fc_stat(const char *filename, struct stat *buf)
Definition support.c:575
#define sz_strlcpy(dest, src)
Definition support.h:167
struct theme_directory * directories
get_usable_themes_in_directory
Definition themes_g.h:22
const char * directory
Definition themes_g.h:23