Freeciv-3.3: /fast/freeciv/nightly/main/client/gui-gtk-3.22/themes.c Source File

Freeciv-3.3

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.22 */
36#include "gui_main.h"
37
38#include "themes_g.h"
39
40 static GtkCssProvider *theme_provider = NULL;
41
42/*************************************************************************/
45 void gui_load_theme(const char *directory, const char *theme_name)
46{
47 GError *error = NULL;
48 char buf[strlen(directory) + strlen(theme_name) + 32];
49
50 if (theme_provider == NULL) {
51 theme_provider = gtk_css_provider_new();
52 gtk_style_context_add_provider_for_screen(
53 gtk_widget_get_screen(toplevel),
54 GTK_STYLE_PROVIDER(theme_provider),
55 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
56 }
57
58 /* Gtk theme is a directory containing gtk-3.0/gtk.css file */
59 fc_snprintf(buf, sizeof(buf), "%s/%s/gtk-3.0/gtk.css", directory,
60 theme_name);
61
62 gtk_css_provider_load_from_file(theme_provider, g_file_new_for_path(buf), &error);
63
64 if (error != NULL) {
65 g_warning("%s\n", error->message);
66 }
67}
68
69/*************************************************************************/
72 void gui_clear_theme(void)
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) {
81 theme_loaded = load_theme(GUI_GTK_DEFAULT_THEME_NAME);
82 if (theme_loaded) {
83 sz_strlcpy(GUI_GTK_OPTION(default_theme_name), GUI_GTK_DEFAULT_THEME_NAME);
84 }
85 }
86
87 /* Still no theme loaded -> load system default theme */
88 if (!theme_loaded && theme_provider != NULL) {
89 gtk_style_context_remove_provider_for_screen(
90 gtk_widget_get_screen(toplevel),
91 GTK_STYLE_PROVIDER(theme_provider));
92 theme_provider = NULL;
93 }
94}
95
96/*************************************************************************/
104 char **get_gui_specific_themes_directories(int *count)
105{
106 gchar *standard_dir;
107 char *home_dir;
108 const struct strvec *data_dirs = get_data_dirs();
109 char **directories = fc_malloc((2 + strvec_size(data_dirs))
110 * sizeof(char *));
111
112 *count = 0;
113
114 /* Freeciv-specific GTK3.22 themes directories */
115 strvec_iterate(data_dirs, dir_name) {
116 char buf[strlen(dir_name) + strlen("/themes/gtk3.22") + 1];
117
118 fc_snprintf(buf, sizeof(buf), "%s/themes/gtk3.22", dir_name);
119
120 directories[(*count)++] = fc_strdup(buf);
121 } strvec_iterate_end;
122
123 /* standard GTK+ themes directory */
124#ifdef CROSSER
125 standard_dir = "../share/themes";
126#else /* CROSSER */
127 standard_dir = "/usr/share/themes";
128#endif /* CROSSER */
129 directories[(*count)++] = fc_strdup(standard_dir);
130
131 /* user GTK+ themes directory (~/.themes) */
132 home_dir = user_home_dir();
133 if (home_dir) {
134 char buf[strlen(home_dir) + 16];
135
136 fc_snprintf(buf, sizeof(buf), "%s/.themes/", home_dir);
137 directories[(*count)++] = fc_strdup(buf);
138 }
139
140 return directories;
141}
142
143/*************************************************************************/
149 char **get_usable_themes_in_directory(const char *directory, int *count)
150{
151 DIR *dir;
152 struct dirent *entry;
153 char **theme_names = fc_malloc(sizeof(char *) * 2);
154 /* Allocated memory size */
155 int t_size = 2;
156
157
158 *count = 0;
159
160 dir = fc_opendir(directory);
161 if (!dir) {
162 /* This isn't directory or we can't list it */
163 return theme_names;
164 }
165
166 while ((entry = readdir(dir))) {
167 char buf[strlen(directory) + strlen(entry->d_name) + 32];
168 struct stat stat_result;
169
170 fc_snprintf(buf, sizeof(buf),
171 "%s/%s/gtk-3.0/gtk.css", directory, entry->d_name);
172
173 if (fc_stat(buf, &stat_result) != 0) {
174 /* File doesn't exist */
175 continue;
176 }
177
178 if (!S_ISREG(stat_result.st_mode)) {
179 /* Not a regular file */
180 continue;
181 }
182
183 /* Otherwise it's ok */
184
185 /* Increase array size if needed */
186 if (*count == t_size) {
187 theme_names = fc_realloc(theme_names, t_size * 2 * sizeof(char *));
188 t_size *= 2;
189 }
190
191 theme_names[*count] = fc_strdup(entry->d_name);
192 (*count)++;
193 }
194
195 closedir(dir);
196
197 return theme_names;
198}
incite_cost
char * incite_cost
Definition comments.c:74
fc_opendir
DIR * fc_opendir(const char *dir_to_open)
Definition fc_dirent.c:29
fc_dirent.h
toplevel
GtkWidget * toplevel
Definition gui_main.c:125
GUI_GTK_DEFAULT_THEME_NAME
#define GUI_GTK_DEFAULT_THEME_NAME
Definition gui_main.h:27
GUI_GTK_OPTION
#define GUI_GTK_OPTION(optname)
Definition gui_main.h:25
load_theme
static bool load_theme
Definition theme_dlg.c:30
get_gui_specific_themes_directories
char ** get_gui_specific_themes_directories(int *count)
Definition themes.c:104
gui_clear_theme
void gui_clear_theme(void)
Definition themes.c:72
theme_provider
static GtkCssProvider * theme_provider
Definition themes.c:40
gui_load_theme
void gui_load_theme(const char *directory, const char *theme_name)
Definition themes.c:45
gui_main.h
mem.h
fc_strdup
#define fc_strdup(str)
Definition mem.h:43
fc_realloc
#define fc_realloc(ptr, sz)
Definition mem.h:36
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
user_home_dir
char * user_home_dir(void)
Definition shared.c:634
get_data_dirs
const struct strvec * get_data_dirs(void)
Definition shared.c:893
strvec_size
size_t strvec_size(const struct strvec *psv)
Definition string_vector.c:344
string_vector.h
strvec_iterate
#define strvec_iterate(psv, str)
Definition string_vector.h:61
strvec_iterate_end
#define strvec_iterate_end
Definition string_vector.h:68
entry
Definition registry_ini.c:193
strvec
Definition string_vector.c:29
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
fc_stat
int fc_stat(const char *filename, struct stat *buf)
Definition support.c:574
sz_strlcpy
#define sz_strlcpy(dest, src)
Definition support.h:189
directories
struct theme_directory * directories
Definition themes_common.c:60
themes_common.h
themes_g.h
get_usable_themes_in_directory
get_usable_themes_in_directory
Definition themes_g.h:22
directory
const char * directory
Definition themes_g.h:23