The Battle for Wesnoth  1.15.12+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 <functional>
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 
91 {
92  // Note the order should be the same as the enum state_t in progress_bar.hpp.
93  state.emplace_back(cfg.child("state_enabled"));
94 }
95 
96 // }---------- BUILDER -----------{
97 
98 namespace implementation
99 {
100 
101 builder_progress_bar::builder_progress_bar(const config& cfg)
102  : builder_styled_widget(cfg)
103 {
104 }
105 
107 {
108  progress_bar* widget = new progress_bar(*this);
109 
110  DBG_GUI_G << "Window builder: placed progress bar '" << id
111  << "' with definition '" << definition << "'.\n";
112 
113  return widget;
114 }
115 
116 } // namespace implementation
117 
118 // }------------ END --------------
119 
120 } // 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:65
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:414
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:22
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:465
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:59
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)