The Battle for Wesnoth  1.15.2+dev
progress_bar.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 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 
18 
19 #include "gui/core/log.hpp"
21 #include "gui/widgets/settings.hpp"
22 
23 #include "utils/functional.hpp"
24 
25 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
26 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
27 
28 namespace gui2
29 {
30 
31 // ------------ WIDGET -----------{
32 
33 REGISTER_WIDGET(progress_bar)
34 
35 progress_bar::progress_bar(const implementation::builder_progress_bar& builder)
36  : styled_widget(builder, type())
37  , percentage_(static_cast<unsigned>(-1))
38 {
39  // Force canvas update
40  set_percentage(0);
41 }
42 
43 void progress_bar::set_active(const bool /*active*/)
44 {
45  /* DO NOTHING */
46 }
47 
49 {
50  return true;
51 }
52 
53 unsigned progress_bar::get_state() const
54 {
55  return ENABLED;
56 }
57 
58 void progress_bar::set_percentage(unsigned percentage)
59 {
60  percentage = std::min<unsigned>(percentage, 100);
61 
62  if(percentage_ != percentage) {
63  percentage_ = percentage;
64 
65  for(auto & c : get_canvases())
66  {
67  c.set_variable("percentage", wfl::variant(percentage));
68  }
69 
70  set_is_dirty(true);
71  }
72 }
73 
75 {
76  return false;
77 }
78 
79 // }---------- DEFINITION ---------{
80 
83 {
84  DBG_GUI_P << "Parsing progress bar " << id << '\n';
85 
86  load_resolutions<resolution>(cfg);
87 }
88 
89 /*WIKI
90  * @page = GUIWidgetDefinitionWML
91  * @order = 1_progress_bar
92  *
93  * == Progress bar ==
94  *
95  * @macro = progress_bar_description
96  *
97  * The definition of a progress bar. This object shows the progress of a certain
98  * action, or the value state of a certain item.
99  *
100  * The following states exist:
101  * * state_enabled, the progress bar is enabled.
102  * @begin{parent}{name="gui/"}
103  * @begin{tag}{name="progress_bar_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
104  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
105  * @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
106  * @end{tag}{name="state_enabled"}
107  * @end{tag}{name="resolution"}
108  * @end{tag}{name="progress_bar_definition"}
109  * @end{parent}{name="gui/"}
110  */
112  : resolution_definition(cfg)
113 {
114  // Note the order should be the same as the enum state_t in progress_bar.hpp.
115  state.emplace_back(cfg.child("state_enabled"));
116 }
117 
118 // }---------- BUILDER -----------{
119 
120 /*WIKI_MACRO
121  * @begin{macro}{progress_bar_description}
122  * A progress bar shows the progress of a certain object.
123  * @end{macro}
124  */
125 
126 /*WIKI
127  * @page = GUIWidgetInstanceWML
128  * @order = 2_progress_bar
129  *
130  * == Image ==
131  *
132  * @macro = progress_bar_description
133  *
134  * A progress bar has no extra fields.
135  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
136  * @begin{tag}{name="progress_bar"}{min=0}{max=-1}{super="generic/widget_instance"}
137  * @end{tag}{name="progress_bar"}
138  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
139  */
140 
141 namespace implementation
142 {
143 
144 builder_progress_bar::builder_progress_bar(const config& cfg)
145  : builder_styled_widget(cfg)
146 {
147 }
148 
150 {
151  progress_bar* widget = new progress_bar(*this);
152 
153  DBG_GUI_G << "Window builder: placed progress bar '" << id
154  << "' with definition '" << definition << "'.\n";
155 
156  return widget;
157 }
158 
159 } // namespace implementation
160 
161 // }------------ END --------------
162 
163 } // 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:68
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:420
std::vector< state_definition > state
void set_percentage(unsigned percentage)
Base class for all widgets.
Definition: widget.hpp:47
virtual bool get_active() const override
See styled_widget::get_active.
Generic file dialog.
Definition: field-fwd.hpp:22
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:463
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:68
mock_char c
#define DBG_GUI_G
Definition: log.hpp:40
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
progress_bar_definition(const config &cfg)