The Battle for Wesnoth  1.17.23+dev
progress_bar.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2023
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 
19 
20 #include "gui/core/log.hpp"
22 #include "gui/widgets/settings.hpp"
23 #include "wml_exception.hpp"
24 #include "gettext.hpp"
25 
26 #include <functional>
27 
28 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
29 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
30 
31 namespace gui2
32 {
33 
34 // ------------ WIDGET -----------{
35 
36 REGISTER_WIDGET(progress_bar)
37 
38 progress_bar::progress_bar(const implementation::builder_progress_bar& builder)
39  : styled_widget(builder, type())
40  , percentage_(static_cast<unsigned>(-1))
41 {
42  // Force canvas update
43  set_percentage(0);
44 }
45 
46 void progress_bar::set_active(const bool /*active*/)
47 {
48  /* DO NOTHING */
49 }
50 
52 {
53  return true;
54 }
55 
56 unsigned progress_bar::get_state() const
57 {
58  return ENABLED;
59 }
60 
61 void progress_bar::set_percentage(unsigned percentage)
62 {
63  percentage = std::min<unsigned>(percentage, 100);
64 
65  if(percentage_ != percentage) {
66  percentage_ = percentage;
67 
68  for(auto & c : get_canvases())
69  {
70  c.set_variable("percentage", wfl::variant(percentage));
71  }
72 
73  queue_redraw();
74  }
75 }
76 
78 {
79  return false;
80 }
81 
82 // }---------- DEFINITION ---------{
83 
86 {
87  DBG_GUI_P << "Parsing progress bar " << id;
88 
89  load_resolutions<resolution>(cfg);
90 }
91 
94 {
95  // Note the order should be the same as the enum state_t in progress_bar.hpp.
96  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_enabled", _("Missing required state for progress bar")));
97 }
98 
99 // }---------- BUILDER -----------{
100 
101 namespace implementation
102 {
103 
104 builder_progress_bar::builder_progress_bar(const config& cfg)
105  : builder_styled_widget(cfg)
106 {
107 }
108 
109 std::unique_ptr<widget> builder_progress_bar::build() const
110 {
111  auto widget = std::make_unique<progress_bar>(*this);
112 
113  DBG_GUI_G << "Window builder: placed progress bar '" << id
114  << "' with definition '" << definition << "'.";
115 
116  return widget;
117 }
118 
119 } // namespace implementation
120 
121 // }------------ END --------------
122 
123 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
This object shows the progress of a certain action, or the value state of a certain item.
unsigned percentage_
The percentage done.
virtual unsigned get_state() const override
See styled_widget::get_state.
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
void set_percentage(unsigned percentage)
virtual bool get_active() const override
See styled_widget::get_active.
virtual void set_active(const bool active) override
See styled_widget::set_active.
Base class for all visible items.
std::vector< canvas > & get_canvases()
Base class for all widgets.
Definition: widget.hpp:54
void queue_redraw()
Indicates that this widget should be redrawn.
Definition: widget.cpp:456
static std::string _(const char *str)
Definition: gettext.hpp:93
Define the common log macros for the gui toolkit.
#define DBG_GUI_G
Definition: log.hpp:41
#define DBG_GUI_P
Definition: log.hpp:66
Generic file dialog.
Contains the implementation details for lexical_cast and shouldn't be used directly.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
This file contains the settings handling of the widget library.
virtual std::unique_ptr< widget > build() const override
std::string definition
Parameters for the styled_widget.
progress_bar_definition(const config &cfg)
Base class of a resolution, contains the common keys for a resolution.
std::vector< state_definition > state
mock_char c
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define VALIDATE_WML_CHILD(cfg, key, message)