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