The Battle for Wesnoth  1.19.0-dev
gui.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
18 #include "gui/gui.hpp"
19 
20 #include "config_cache.hpp"
21 #include "filesystem.hpp"
22 #include "gettext.hpp"
23 #include "gui/core/log.hpp"
25 #include "gui/widgets/settings.hpp"
26 #include "preferences/general.hpp"
27 #include "serialization/parser.hpp"
30 #include "wml_exception.hpp"
31 
32 namespace gui2
33 {
34 static bool initialized = false;
35 
36 void init()
37 {
38  if(initialized) {
39  return;
40  }
41 
42  LOG_GUI_G << "Initializing UI subststem.";
43 
44  // Save current screen size.
46 
47  //
48  // Read and validate the WML files.
49  //
50  config cfg;
51  try {
53 
54  preproc_map preproc(game_config::config_cache::instance().get_preproc_map());
55  filesystem::scoped_istream stream = preprocess_file(filesystem::get_wml_location("gui/_main.cfg"), &preproc);
56 
57  read(cfg, *stream, &validator);
58  } catch(const config::error& e) {
59  ERR_GUI_P << e.what();
60  ERR_GUI_P << "Setting: could not read file 'data/gui/_main.cfg'.";
61  } catch(const abstract_validator::error& e) {
62  ERR_GUI_P << "Setting: could not read file 'data/schema/gui.cfg'.";
63  ERR_GUI_P << e.message;
64  }
65 
66  //
67  // Parse GUI definitions.
68  //
69  const std::string& current_theme = preferences::gui_theme();
70 
71  for(const config& g : cfg.child_range("gui")) {
72  const std::string id = g["id"];
73 
74  auto iter = guis.emplace(id, gui_definition(g)).first;
75 
76  if(id == "default") {
77  default_gui = iter;
78  }
79 
80  if(!current_theme.empty() && id == current_theme) {
81  current_gui = iter;
82  }
83  }
84 
85  VALIDATE(default_gui != guis.end(), _("No default gui defined."));
86 
87  if(current_theme.empty()) {
89  } else if(current_gui == guis.end()) {
90  ERR_GUI_P << "Missing [gui] definition for '" << current_theme << "'";
92  }
93 
94  current_gui->second.activate();
95 
96  initialized = true;
97 }
98 
99 } // namespace gui2
double g
Definition: astarsearch.cpp:63
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
child_itors child_range(config_key_type key)
Definition: config.cpp:273
static config_cache & instance()
Get reference to the singleton object.
A GUI theme definition.
Realization of serialization/validator.hpp abstract validator.
Declarations for File-IO.
static std::string _(const char *str)
Definition: gettext.hpp:93
Define the common log macros for the gui toolkit.
#define LOG_GUI_G
Definition: log.hpp:42
#define ERR_GUI_P
Definition: log.hpp:69
std::string get_wml_location(const std::string &filename, const std::string &current_dir)
Returns a complete path to the actual WML file or directory or an empty string if the file isn't pres...
std::unique_ptr< std::istream > scoped_istream
Definition: filesystem.hpp:50
void update_screen_size_variables()
Update the size of the screen variables in settings.
Definition: settings.cpp:57
Generic file dialog.
void init()
Initializes the GUI subsystems.
Definition: gui.cpp:36
gui_theme_map_t guis
Map of all known GUIs.
gui_theme_map_t::iterator current_gui
Iterator pointing to the current GUI.
gui_theme_map_t::iterator default_gui
Iterator pointing to the default GUI.
static bool initialized
Definition: gui.cpp:34
std::string gui_theme()
Definition: general.cpp:545
filesystem::scoped_istream preprocess_file(const std::string &fname, preproc_map *defines)
Function to use the WML preprocessor on a file.
std::map< std::string, struct preproc_define > preproc_map
One of the realizations of serialization/validator.hpp abstract validator.
void read(config &cfg, std::istream &in, abstract_validator *validator)
Definition: parser.cpp:627
This file contains the settings handling of the widget library.
Used to manage with not initialized validators Supposed to be thrown from the constructor.
Definition: validator.hpp:97
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
#define e