Freeciv-3.3: /fast/freeciv/nightly/main/client/gui-sdl3/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
23/* utility */
24#include "fc_dirent.h"
25#include "fcintl.h"
26#include "log.h"
27#include "mem.h"
28#include "string_vector.h"
29
30/* client/gui-sdl3 */
31#include "gui_main.h"
32#include "themespec.h"
33
34#include "themes_common.h"
35#include "themes_g.h"
36
37/*************************************************************************/
40 void gui_load_theme(const char *directory, const char *theme_name)
41{
42 char buf[strlen(directory) + strlen(DIR_SEPARATOR) + strlen(theme_name)
43 + strlen(DIR_SEPARATOR "theme") + 1];
44
45 if (active_theme != NULL) {
46 /* We don't support changing theme once it has been loaded */
47 return;
48 }
49
50 /* Free previous loaded theme, if any */
51 theme_free(active_theme);
52 active_theme = NULL;
53
54 fc_snprintf(buf, sizeof(buf), "%s" DIR_SEPARATOR "%s" DIR_SEPARATOR "theme",
55 directory, theme_name);
56
57 themespec_try_read(buf);
58 theme_load_sprites(active_theme);
59
60 update_font_from_theme(default_font_size(active_theme));
61}
62
63/*************************************************************************/
66 void gui_clear_theme(void)
67{
68 if (!load_theme(GUI_SDL_OPTION(default_theme_name))) {
69 /* TRANS: No full stop after the URL, could cause confusion. */
70 log_fatal(_("No Sdl3-client theme was found. For instructions on how to "
71 "get one, please visit %s"), HOMEPAGE_URL);
72
73 exit(EXIT_FAILURE);
74 }
75}
76
77/*************************************************************************/
83 char **get_gui_specific_themes_directories(int *count)
84{
85 const struct strvec *data_dirs = get_data_dirs();
86 char **directories = fc_malloc(strvec_size(data_dirs) * sizeof(char *));
87 int i = 0;
88
89 *count = strvec_size(data_dirs);
90 strvec_iterate(data_dirs, data_dir) {
91 char buf[strlen(data_dir) + strlen("/themes/gui-sdl2") + 1];
92
93 fc_snprintf(buf, sizeof(buf), "%s/themes/gui-sdl2", data_dir);
94
95 directories[i++] = fc_strdup(buf);
96 } strvec_iterate_end;
97
98 return directories;
99}
100
101/*************************************************************************/
107 char **get_usable_themes_in_directory(const char *directory, int *count)
108{
109 DIR *dir;
110 struct dirent *entry;
111 char **theme_names = fc_malloc(sizeof(char *) * 2);
112 /* Allocated memory size */
113 int t_size = 2;
114
115 *count = 0;
116
117 dir = fc_opendir(directory);
118 if (!dir) {
119 /* This isn't directory or we can't list it */
120 return theme_names;
121 }
122
123 while ((entry = readdir(dir))) {
124 char buf[strlen(directory) + strlen(entry->d_name) + 32];
125 struct stat stat_result;
126
127 fc_snprintf(buf, sizeof(buf),
128 "%s/%s/theme.themespec", directory, entry->d_name);
129
130 if (fc_stat(buf, &stat_result) != 0) {
131 /* File doesn't exist */
132 continue;
133 }
134
135 if (!S_ISREG(stat_result.st_mode)) {
136 /* Not a regular file */
137 continue;
138 }
139
140 /* Otherwise it's ok */
141
142 /* Increase array size if needed */
143 if (*count == t_size) {
144 theme_names = fc_realloc(theme_names, t_size * 2 * sizeof(char *));
145 t_size *= 2;
146 }
147
148 theme_names[*count] = fc_strdup(entry->d_name);
149 (*count)++;
150 }
151
152 closedir(dir);
153
154 return theme_names;
155}
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
fcintl.h
_
#define _(String)
Definition fcintl.h:67
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
gui_load_theme
void gui_load_theme(const char *directory, const char *theme_name)
Definition themes.c:45
update_font_from_theme
void update_font_from_theme(int theme_font_size)
Definition gui_main.c:1328
default_font_size
unsigned default_font_size(struct theme *act_theme)
Definition gui_main.c:1318
GUI_SDL_OPTION
#define GUI_SDL_OPTION(optname)
Definition gui_main.h:37
theme_free
void theme_free(struct theme *ftheme)
Definition themespec.c:317
theme_load_sprites
void theme_load_sprites(struct theme *t)
Definition themespec.c:990
themespec_try_read
void themespec_try_read(const char *theme_name)
Definition themespec.c:334
active_theme
struct theme * active_theme
Definition themespec.c:154
themespec.h
gui_main.h
log.h
log_fatal
#define log_fatal(message,...)
Definition log.h:100
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
get_data_dirs
const struct strvec * get_data_dirs(void)
Definition shared.c:886
DIR_SEPARATOR
#define DIR_SEPARATOR
Definition shared.h:127
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
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