Freeciv-3.3: /fast/freeciv/nightly/main/client/gui-gtk-5.0/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-5.0 */
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 char buf[strlen(directory) + strlen(theme_name) + 32];
48
49 if (theme_provider == NULL) {
50 theme_provider = gtk_css_provider_new();
51 gtk_style_context_add_provider_for_display(
52 gtk_widget_get_display(toplevel),
53 GTK_STYLE_PROVIDER(theme_provider),
54 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
55 }
56
57 /* Gtk theme is a directory containing gtk-4.0/gtk.css file */
58 fc_snprintf(buf, sizeof(buf), "%s/%s/gtk-4.0/gtk.css", directory,
59 theme_name);
60
61 gtk_css_provider_load_from_file(theme_provider, g_file_new_for_path(buf));
62}
63
64/*************************************************************************/
67 void gui_clear_theme(void)
68{
69 bool theme_loaded;
70
71 /* Try to load user defined theme */
72 theme_loaded = load_theme(GUI_GTK_OPTION(default_theme_name));
73
74 /* No user defined theme loaded -> try to load Freeciv default theme */
75 if (!theme_loaded) {
76 theme_loaded = load_theme(GUI_GTK_DEFAULT_THEME_NAME);
77 if (theme_loaded) {
78 sz_strlcpy(GUI_GTK_OPTION(default_theme_name), GUI_GTK_DEFAULT_THEME_NAME);
79 }
80 }
81
82 /* Still no theme loaded -> drop theme provider */
83 if (!theme_loaded && theme_provider != NULL) {
84 gtk_style_context_remove_provider_for_display(
85 gtk_widget_get_display(toplevel),
86 GTK_STYLE_PROVIDER(theme_provider));
87 theme_provider = NULL;
88 }
89}
90
91/*************************************************************************/
99 char **get_gui_specific_themes_directories(int *count)
100{
101 gchar *standard_dir;
102 char *home_dir;
103 const struct strvec *data_dirs = get_data_dirs();
104 char **directories = fc_malloc((2 + strvec_size(data_dirs))
105 * sizeof(char *));
106
107 *count = 0;
108
109 /* Freeciv-specific GTK4x themes directories */
110 strvec_iterate(data_dirs, dir_name) {
111 char buf[strlen(dir_name) + strlen("/themes/gtk4") + 1];
112
113 fc_snprintf(buf, sizeof(buf), "%s/themes/gtk4", dir_name);
114
115 directories[(*count)++] = fc_strdup(buf);
116 } strvec_iterate_end;
117
118 /* Standard GTK themes directory */
119#ifdef CROSSER
120 standard_dir = "../share/themes";
121#else /* CROSSER */
122 standard_dir = "/usr/share/themes";
123#endif /* CROSSER */
124 directories[(*count)++] = fc_strdup(standard_dir);
125
126 /* User GTK themes directory (~/.themes) */
127 home_dir = user_home_dir();
128 if (home_dir) {
129 char buf[strlen(home_dir) + 16];
130
131 fc_snprintf(buf, sizeof(buf), "%s/.themes/", home_dir);
132 directories[(*count)++] = fc_strdup(buf);
133 }
134
135 return directories;
136}
137
138/*************************************************************************/
144 char **get_usable_themes_in_directory(const char *directory, int *count)
145{
146 DIR *dir;
147 struct dirent *entry;
148 char **theme_names = fc_malloc(sizeof(char *) * 2);
149 /* Allocated memory size */
150 int t_size = 2;
151
152
153 *count = 0;
154
155 dir = fc_opendir(directory);
156 if (!dir) {
157 /* This isn't directory or we can't list it */
158 return theme_names;
159 }
160
161 while ((entry = readdir(dir))) {
162 char buf[strlen(directory) + strlen(entry->d_name) + 32];
163 struct stat stat_result;
164
165 fc_snprintf(buf, sizeof(buf),
166 "%s/%s/gtk-4.0/gtk.css", directory, entry->d_name);
167
168 if (fc_stat(buf, &stat_result) != 0) {
169 /* File doesn't exist */
170 continue;
171 }
172
173 if (!S_ISREG(stat_result.st_mode)) {
174 /* Not a regular file */
175 continue;
176 }
177
178 /* Otherwise it's ok */
179
180 /* Increase array size if needed */
181 if (*count == t_size) {
182 theme_names = fc_realloc(theme_names, t_size * 2 * sizeof(char *));
183 t_size *= 2;
184 }
185
186 theme_names[*count] = fc_strdup(entry->d_name);
187 (*count)++;
188 }
189
190 closedir(dir);
191
192 return theme_names;
193}
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