The Battle for Wesnoth  1.17.0-dev
progress_bar.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2021
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 
24 #include <functional>
25 
26 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
27 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
28 
29 namespace gui2
30 {
31 
32 // ------------ WIDGET -----------{
33 
34 REGISTER_WIDGET(progress_bar)
35 
36 progress_bar::progress_bar(const implementation::builder_progress_bar& builder)
37  : styled_widget(builder, type())
38  , percentage_(static_cast<unsigned>(-1))
39 {
40  // Force canvas update
41  set_percentage(0);
42 }
43 
44 void progress_bar::set_active(const bool /*active*/)
45 {
46  /* DO NOTHING */
47 }
48 
50 {
51  return true;
52 }
53 
54 unsigned progress_bar::get_state() const
55 {
56  return ENABLED;
57 }
58 
59 void progress_bar::set_percentage(unsigned percentage)
60 {
61  percentage = std::min<unsigned>(percentage, 100);
62 
63  if(percentage_ != percentage) {
64  percentage_ = percentage;
65 
66  for(auto & c : get_canvases())
67  {
68  c.set_variable("percentage", wfl::variant(percentage));
69  }
70 
71  set_is_dirty(true);
72  }
73 }
74 
76 {
77  return false;
78 }
79 
80 // }---------- DEFINITION ---------{
81 
84 {
85  DBG_GUI_P << "Parsing progress bar " << id << '\n';
86 
87  load_resolutions<resolution>(cfg);
88 }
89 
92 {
93  // Note the order should be the same as the enum state_t in progress_bar.hpp.
94  state.emplace_back(cfg.child("state_enabled"));
95 }
96 
97 // }---------- BUILDER -----------{
98 
99 namespace implementation
100 {
101 
102 builder_progress_bar::builder_progress_bar(const config& cfg)
103  : builder_styled_widget(cfg)
104 {
105 }
106 
108 {
109  progress_bar* widget = new progress_bar(*this);
110 
111  DBG_GUI_G << "Window builder: placed progress bar '" << id
112  << "' with definition '" << definition << "'.\n";
113 
114  return widget;
115 }
116 
117 } // namespace implementation
118 
119 // }------------ END --------------
120 
121 } // namespace gui2
Define the common log macros for the gui toolkit.
Base class of a resolution, contains the common keys for a resolution.
#define DBG_GUI_P
Definition: log.hpp:66
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:402
std::vector< state_definition > state
void set_percentage(unsigned percentage)
Base class for all widgets.
Definition: widget.hpp:49
This object shows the progress of a certain action, or the value state of a certain item...
virtual bool get_active() const override
See styled_widget::get_active.
Generic file dialog.
Definition: field-fwd.hpp:23
virtual widget * build() const override
std::string definition
Parameters for the styled_widget.
This file contains the settings handling of the widget library.
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:466
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
std::vector< canvas > & get_canvases()
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
virtual unsigned get_state() const override
See styled_widget::get_state.
Base class for all visible items.
virtual void set_active(const bool active) override
See styled_widget::set_active.
unsigned percentage_
The percentage done.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
mock_char c
#define DBG_GUI_G
Definition: log.hpp:41
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
progress_bar_definition(const config &cfg)