The Battle for Wesnoth  1.19.0-dev
image.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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 
18 #include "gui/widgets/image.hpp"
19 
20 #include "picture.hpp" // We want the file in src/
21 
23 #include "gui/core/log.hpp"
25 #include "wml_exception.hpp"
26 #include "gettext.hpp"
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 
37 
38 image::image(const implementation::builder_image& builder)
39  : styled_widget(builder, type())
40 {
41 }
42 
44 {
46 
47  if(image_size.x == 0 || image_size.y == 0) {
48  DBG_GUI_L << LOG_HEADER << " empty image return default.";
49  return get_config_default_size();
50  }
51 
52  const point minimum = get_config_default_size();
53  const point maximum = get_config_maximum_size();
54 
55  point result {image_size.x, image_size.y};
56 
57  if(minimum.x > 0 && result.x < minimum.x) {
58  DBG_GUI_L << LOG_HEADER << " increase width to minimum.";
59  result.x = minimum.x;
60  } else if(maximum.x > 0 && result.x > maximum.x) {
61  DBG_GUI_L << LOG_HEADER << " decrease width to maximum.";
62  result.x = maximum.x;
63  }
64 
65  if(minimum.y > 0 && result.y < minimum.y) {
66  DBG_GUI_L << LOG_HEADER << " increase height to minimum.";
67  result.y = minimum.y;
68  } else if(maximum.y > 0 && result.y > maximum.y) {
69  DBG_GUI_L << LOG_HEADER << " decrease height to maximum.";
70  result.y = maximum.y;
71  }
72 
73  DBG_GUI_L << LOG_HEADER << " result " << result << ".";
74  return result;
75 }
76 
77 void image::set_active(const bool /*active*/)
78 {
79  /* DO NOTHING */
80 }
81 
82 bool image::get_active() const
83 {
84  return true;
85 }
86 
87 unsigned image::get_state() const
88 {
89  return ENABLED;
90 }
91 
93 {
94  return false;
95 }
96 
97 // }---------- DEFINITION ---------{
98 
101 {
102  DBG_GUI_P << "Parsing image " << id;
103 
104  load_resolutions<resolution>(cfg);
105 }
106 
108  : resolution_definition(cfg)
109 {
110  // Note the order should be the same as the enum state_t in image.hpp.
111  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_enabled", missing_mandatory_wml_tag("image_definition][resolution", "state_enabled")));
112 }
113 
114 // }---------- BUILDER -----------{
115 
116 namespace implementation
117 {
118 
119 builder_image::builder_image(const config& cfg) : builder_styled_widget(cfg)
120 {
121 }
122 
123 std::unique_ptr<widget> builder_image::build() const
124 {
125  auto widget = std::make_unique<image>(*this);
126 
127  DBG_GUI_G << "Window builder: placed image '" << id << "' with definition '"
128  << definition << "'.";
129 
130  return widget;
131 }
132 
133 } // namespace implementation
134 
135 // }------------ END --------------
136 
137 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: image.cpp:77
virtual bool get_active() const override
See styled_widget::get_active.
Definition: image.cpp:82
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: image.cpp:43
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: image.cpp:92
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: image.cpp:87
Base class for all visible items.
const t_string & get_label() const
point get_config_maximum_size() const
Gets the best size as defined in the config.
point get_config_default_size() const
Gets the default size as defined in the config.
Base class for all widgets.
Definition: widget.hpp:53
Generic locator abstracting the location of an image.
Definition: picture.hpp:63
Define the common log macros for the gui toolkit.
#define DBG_GUI_L
Definition: log.hpp:55
#define DBG_GUI_G
Definition: log.hpp:41
#define DBG_GUI_P
Definition: log.hpp:66
#define LOG_HEADER
Definition: image.cpp:29
Generic file dialog.
Functions to load and save images from/to disk.
point get_size(const locator &i_locator, bool skip_cache)
Returns the width and height of an image.
Definition: picture.cpp:813
Contains the implementation details for lexical_cast and shouldn't be used directly.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
resolution(const config &cfg)
Definition: image.cpp:107
image_definition(const config &cfg)
Definition: image.cpp:99
virtual std::unique_ptr< widget > build() const override
Definition: image.cpp:123
std::string definition
Parameters for the styled_widget.
Base class of a resolution, contains the common keys for a resolution.
std::vector< state_definition > state
Holds a 2D point.
Definition: point.hpp:25
std::string missing_mandatory_wml_tag(const std::string &section, const std::string &tag)
Returns a standard message for a missing wml child (tag).
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)