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