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